ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

How to publish sim time at desired frequency

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

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?

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

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

1 Answer

Sort by ยป oldest newest most voted

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

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



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

Seen: 747 times

Last updated: Mar 04 '19