-
Notifications
You must be signed in to change notification settings - Fork 167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ros2topic] fix pub rate reduced issue #141
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a good solution because it will use 100% of a CPU core waiting for the time to be expired. If you set the publish rate to 1 Hz, it will spend too much time just checking the time.
I would not merge this as-is.
Please use a |
I tried to use create_timer, the time_callback will also cost extra time exhaust. -r 1000 -p 1000, real time is about 1.280s. I have no idea to fix the issue in pub command. Since it just call APIs to publish messages. Compare with ROS, it used rospy.Rate.sleep(). ref to timer.py. There is no rate loss. Could ROS2 rclpy be possible to support timer with Rate.sleep() function? |
Can you share that code somehow to review it? |
Here is code: https://github.com/yechun1/ros2cli/blob/fix_ros2topic_pub_issue2/ros2topic/ros2topic/verb/pub.py#L130 $ ros2 topic pub /image sensor_msgs/Image -r 1000 -p 1000 interval = 1.280345 interval = 1.302808 |
04767a3
to
96a3d86
Compare
Compared with ROS1 which works very well, so I referenced to the code rospy.Rate.sleep(), since there is no rclpy.Rate.sleep() on ros2 currently, I implemented the code inside the pub.py, updated the patch, please help review, thanks. |
5c4808c
to
4938813
Compare
There are two separate issues here:
The second problem can be address - as suggested and recommended above - by using a timer. This works as long as the duration of the The solution using a timer will look like this:
Please update this PR accordingly to avoid the manual calculation of the time to sleep. The first problem will need to be addressed on a lower level. You can easily reproducing the problem with my code snippet by changing the |
I have tried timer but the result is not good: Here is sample code Below is result print every 0.001s * 1000. All the data has been added up on each 1ms cycle.
Below is result that disabled pub(msg) code, timer_call_time cost much more time.
Should we report a bug on timer with 1ms 1000hz test? Or adopt manual calculation that adopt the sleep mechanism from ROS topic pub code? |
Can you clarify what you mean with this. Why is it "not good"? What does "exhaust time" mean? On my system a callback triggered by a timer is on average about 0.2ms "late". For a rate of 1000 Hz that adds a significant delay. The current implementation of timers doesn't produce a "fixed cycle time" but accumulates these delays over time which for 1000Hz is pretty significant. I created ros2/rcl#289 to address this in the timer directly. Therefore I would suggest to update this patch to use a timer which can then also be used to validate the future fix of the timer itself. I don't think implementing custom timing logic in this package as a temporary workaround is a good idea. |
4938813
to
2a20391
Compare
time.sleep will add the time the publish call takes to each cycle. Use a timer to avoid pub rate loss. Signed-off-by: Chris Ye <chris.ye@intel.com>
understand, updated the code, please help review. |
Verified the timer patch based on (ros2/rcl#289]): before change (timer without ros2/rcl#289):
$ ros2 topic hz /image (ref to #133)
# after change: (timer based on [ros2/rcl#289])
ros2 topic hz /image
This is a significant improvement, the timer call delay issue should be fixed. |
@yechun1 Thank you for iterating with me on the patch. |
…e event (ros2#141) * Add 'handle_once' property to EventHandler This is useful for implementing "one shot" events. The property can be set via the constructor or setter. If 'handle_once' is set to True, then the EventHandler unregisters itself from the context after the first time it is handled. All subclasses of EventHandler pass it kwargs so they can use the new property. Tests included. * Add a common interface for describing an EventHandler A common pattern for describing event handlers has been moved to the parent class. Subclasses should implement the 'handler_description' and 'matcher_description' properties rather than implementing a describe method.
Signed-off-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>
when rate is 1000, time.sleep() in main loop will cost about 150ms every 1s, use time comparison instead of time.sleep(), save sleep cycle time lossing
Signed-off-by: Chris Ye chris.ye@intel.com