Ask Your Question
0

How to publish sim time at desired frequency

asked 2019-03-03 00:04:35 -0600

yk6 gravatar image

Hi, I want to run the simulated time at frequency of 1Hz by publishing to the /clock topic and set rosparam /use_sim_time to be true/1.

However my following code produce simulated clock at a frequency of few kHz.

If use the commented part, the simulated clock will start at 0 and stops there.

If rosparam /use_sim_time is not set or set to false, the commented part will work fine. I am guessing that this clock topic is treated as a usual topic published and the rospy.Rate refers back and use the wall time to time the Rate object?

rospy.init_node('tmp')
pub = rospy.Publisher('/clock', Clock, queue_size=1)
epoch = rospy.Time(1546300800, 0)
rate = rospy.Rate(1)
clock = rospy.Time()
clock.set(1546300800,0)
rate = rospy.Rate(1)


while not rospy.is_shutdown():
    rospy.loginfo('')
    epoch += rospy.Duration(1)
    pub.publish(epoch)
    # rate.sleep()
    # rospy.sleep(1)
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2019-03-04 04:39:24 -0600

yk6 gravatar image

Use python's time module to control the frequency

by adding import time and add time.sleep(1.0) in the while loop

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2019-03-02 23:57:59 -0600

Seen: 379 times

Last updated: Mar 04 '19