ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

publisher from tutorial doesn't react to simulation time of 1s

asked 2023-07-04 03:16:57 -0500

MaxGyver gravatar image

I followed this tutorial (ROS/Tutorials/WritingPublisherSubscriber(python) - ROS Wiki) and everything works fine for real/wall time.

But then I tested the same code with simulation time (in separate windows/terminals):

$ roscore

$ rosparam set use_sim_time true

$ ./talker.py
[INFO] [1688140481.155550, 0.000000]: hello world 0.0

$ ./listener.py

$ rostopic pub /clock rosgraph_msgs/Clock [1,0] --use-rostime
publishing and latching message. Press ctrl-C to terminate

The "talker" didn't react to the updated ROS time. Then I cancelled (Ctrl-c) and I tried:

$ rostopic pub /clock rosgraph_msgs/Clock [2,0] --use-rostime
publishing and latching message. Press ctrl-C to terminate

Then the talker printed two messages for time = 2.0 seconds:

[INFO] [1688140488.717273, 2.000000]: hello world 2.0
[INFO] [1688140488.719683, 2.000000]: hello world 2.0

This is not what I expected.

  1. Why doesn't the talker react to the new simulation time of 1 second?
  2. Why does the the talker see two messages, when the simulation time is updated to 2 seconds?

ROS environment variables:

ROS_VERSION=1
ROS_DISTRO=noetic
ROS_ETC_DIR=/opt/ros/noetic/etc/ros
ROSLISP_PACKAGE_DIRECTORIES=
ROS_MASTER_URI=http://localhost:11311
ROS_PYTHON_VERSION=3
ROS_ROOT=/opt/ros/noetic/share/ros
ROS_PACKAGE_PATH=/opt/ros/noetic/share
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-07-04 17:57:34 -0500

hank880907 gravatar image

updated 2023-07-05 01:42:56 -0500

It has something to do with the implementation of rospy.timer.sleep

When you start up the program, it will print out

[INFO] [1688140488.717273, 2.000000]: hello world 0.0

And it will be blocked on

rate.sleep()

More specifically It will wait in this while loop until it gets non-zero time value.

When you set the sim time to 1, It will break out from the above-mentioned while loop and enters the second while loop which will wait for the required time to pass. (In this case 0.1 second.)

When you set the sim time to 2, It will break out from the second while loop, and printing out

[INFO] [1688140488.719683, 2.000000]: hello world 2.0

After that, it will try to sleep again. Since the time has proceeded 1 second, which is greater than the period that it is supposed to sleep. In this function, It tries to compute the duration that it is suppose to sleep. However, the remaining waiting time will result in a negative value which will make the sleep function to return immediately, and prints out the second

[INFO] [1688140488.719683, 2.000000]: hello world 2.0

Hope this explains the issue you are experiencing.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2023-07-04 02:55:16 -0500

Seen: 31 times

Last updated: Jul 05 '23