You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As discussed here: #1540. By design, and in the words of @wjwwood , by default it is NOT possible to "wait on asynchronous things within a ROS callback safely", see #1533
The issue (or more accurately behavior by design) is well know.
However with the current implementation of simulation time which listen to the /clock topic, this behavior breaks the symmetry between the usage of real time (system time internally accessed without the need of subscribing to a topic, and hence always updated) and simulation time (time received on the /clock topic and hence frozen in a callback).
I believe the symmetry should be maintain in order to not draw false conclusions about the behavior of a code developed in simulation VS real time, and thus independently from good/bad code practice.
Implementation considerations
Maybe insulation the /clock subscriber callback on a specific thread ?
Assigning to one of the rclcpp maintainers to decide what to do with this feature request; whether to put it on the backlog, help-wanted, or to close it out. Once we have figured out the status, feel free to unassign yourself.
by default it is NOT possible to "wait on asynchronous things within a ROS callback safely"
isn't that supposed to be synchronous? if im not mistaken.
These are @wjwwood words, but I believe asynchronous is the right term for anything going through topics.
this means that clock topic is being published certain frequency, right? but it still gets stuck on subscription callback on topic /clock?
Exactly, when use_sim_time is true, it gets stuck on the subscription callback which is not executed while any other callback is served. Hence a node clock is not having the same update behavior from the user perspective when use_sim_time is true versus use_sim_time is false (where getting an updated system time is immediate and completely from thread mechanisms).
Executing this sample code with use_sim_time is true + something publising on the /clock topic VS use_sim_time is false should highlight the issue:
While I will say that the difference between use_sim_time = True and use_sim_time = False is unfortunate, I will also say that the code you are using as an example is an anti-pattern. In particular, it is a bad idea to do long sleeps from within callbacks. That's because in the single-threaded executor case (which is the default), you are preventing any other callbacks from running in your process. There are various ways around this (using the MultiThreadedExecutor is one), but in general it is best to do as little work (and no sleeping) as possible inside of the callbacks.
In this case, I would probably rewrite this to use a single-shot timer for the initial 2 second sleep, and then reset the timer period after the first call to 100ms to do the sleeps in between.
I definitely agree that the example is an anti-pattern, but I believe the symmetry of behaviors between use_sim_time = True and use_sim_time = False should be independent from the good / bad coding practice.
The example given is extreme, but nonetheless, without knowing the internal mechanics of how the simulated clock is implemented, it is hard for a naive user to get that the clock that he's using has a behavior that depends that much on the availability of the node thread when use_sim_time = True, whereas when use_sim_time = False it is not
Activity
clalancette commentedon Feb 18, 2021
Assigning to one of the rclcpp maintainers to decide what to do with this feature request; whether to put it on the backlog, help-wanted, or to close it out. Once we have figured out the status, feel free to unassign yourself.
fujitatomoya commentedon Feb 19, 2021
@doisyg could you elaborate a little bit?
isn't that supposed to be
synchronous? if im not mistaken.this means that clock topic is being published certain frequency, right? but it still gets stuck on subscription callback on topic
/clock?doisyg commentedon Feb 19, 2021
These are @wjwwood words, but I believe asynchronous is the right term for anything going through topics.
Exactly, when
use_sim_timeistrue, it gets stuck on the subscription callback which is not executed while any other callback is served. Hence a node clock is not having the same update behavior from the user perspective whenuse_sim_timeistrueversususe_sim_timeisfalse(where getting an updated system time is immediate and completely from thread mechanisms).Executing this sample code with
use_sim_timeistrue+ something publising on the /clock topic VSuse_sim_timeisfalseshould highlight the issue:clalancette commentedon Feb 19, 2021
While I will say that the difference between
use_sim_time = Trueanduse_sim_time = Falseis unfortunate, I will also say that the code you are using as an example is an anti-pattern. In particular, it is a bad idea to do long sleeps from within callbacks. That's because in the single-threaded executor case (which is the default), you are preventing any other callbacks from running in your process. There are various ways around this (using theMultiThreadedExecutoris one), but in general it is best to do as little work (and no sleeping) as possible inside of the callbacks.In this case, I would probably rewrite this to use a single-shot timer for the initial 2 second sleep, and then reset the timer period after the first call to 100ms to do the sleeps in between.
doisyg commentedon Feb 19, 2021
I definitely agree that the example is an anti-pattern, but I believe the symmetry of behaviors between
use_sim_time = Trueanduse_sim_time = Falseshould be independent from the good / bad coding practice.The example given is extreme, but nonetheless, without knowing the internal mechanics of how the simulated clock is implemented, it is hard for a naive user to get that the clock that he's using has a behavior that depends that much on the availability of the node thread when
use_sim_time = True, whereas whenuse_sim_time = Falseit is notfujitatomoya commentedon Feb 20, 2021
thanks for sharing thoughts 👍
I really do not know this is anti-pattern or not, but just care about application perspective.
this makes sense to me, application does not want to adjust the sequence based on this parameter. (it can do, but does not want to, i guess)
would you care to share one actual use case, if you have? i am interested in it.
doisyg commentedon Feb 20, 2021
Any accurate usage of the node clock in a callback. I can think of time-stamping sensor data for instance
doisyg commentedon Feb 25, 2021
PR here: #1556
fujitatomoya commentedon Mar 29, 2021
close this, since #1556 has been merged.
fujitatomoya commentedon Apr 1, 2021
i believe that rclpy has the same problem like this.