ros2 / rclcpp

rclcpp (ROS Client Library for C++)
Apache License 2.0
514 stars 410 forks source link

'/clock' Topic cannot change each loop step time from simulation time #2533

Open fengzhongsu opened 1 month ago

fengzhongsu commented 1 month ago

Bug report

Required Info:

Steps to reproduce issue

In my node, i have publish topic '/clock' and set use_sim_time:=true. Although these settings change the results of this->now(), there is no implementation in which the frequency of program loops is independent of the simulation period.

Expected behavior

In ros1-Melodic, the same setting, will realize this result: 20240517-224428 The first echo result is use_sim_time = false, and The second echo result is use_sim_time = true

Actual behavior

In ros2-foxy, humble also have these issue, like this: 20240517-230053 when i set publish topic 'clock' and set use_sim_time:=true, the this->get_clock()->now() or this->now() can get the sim time, but the topic pub frequenct same not change by these settings.

Barry-Xu-2018 commented 1 month ago

How do you create timer to publish messages ?
create_wall_timer or create_timer Simulate time doesn't affect create_wall_timer.

fengzhongsu commented 1 month ago

您好,我已受到您的来信

fengzhongsu commented 1 month ago

How do you create timer to publish messages ? create_wall_timer or create_timer Simulate time doesn't affect create_wall_timer.

Hi, Barry. In simulation, i used rclcpp::Rate loop_rate(ctrl_freq); And In a while(rclcpp::ok()) { rclcpp::spin_some(mynode); //my program loop_rate.sleep(); }

Barry-Xu-2018 commented 1 month ago

Please note that rclcpp::Rate use RCL_SYSTEM_TIME in default. Do you change it ?

https://github.com/ros2/rclcpp/blob/348e22a48641bf2622f81e562ad3cb7bf910a183/rclcpp/include/rclcpp/rate.hpp#L133-L146

fengzhongsu commented 1 month ago

Please note that rclcpp::Rate use RCL_SYSTEM_TIME in default. Do you change it ?

https://github.com/ros2/rclcpp/blob/348e22a48641bf2622f81e562ad3cb7bf910a183/rclcpp/include/rclcpp/rate.hpp#L133-L146

I have been set use_sim_time=true https://design.ros2.org/articles/clock_and_time.html

fujitatomoya commented 1 month ago

I have been set use_sim_time=true https://design.ros2.org/articles/clock_and_time.html

use_sim_time is set on the node, but Rate object. with your example, as @Barry-Xu-2018 mentions, Rate is instantiated with system time as default, not simulation time. I think you need to set RCL_SYSTEM_TIME for Rate, the it can get the time from simulation clock.

this->get_clock()->now() or this->now() can get the sim time,

assuming that this is the Node. because the use_sim_time is set to true, clock belongs to node is now set to simulation clock. so that it can get simulation clock.

Barry-Xu-2018 commented 1 month ago

As fujitatomoya said,you should modify code on creating Rate.

rclcpp::Rate loop_rate(ctrl_freq, std::make_shared(RCL_ROS_TIME));

fengzhongsu commented 1 month ago

As fujitatomoya said,you should modify code on creating Rate.

rclcpp::Rate loop_rate(ctrl_freq, std::make_shared(RCL_ROS_TIME)); I have tried add this code. But have some error in IDE, my ros2 version is foxy error: no matching function for call to ‘make_shared(rcl_clock_type_t)’ 268 | rclcpp::Rate loop_rate(ctrl_freq, std::make_shared(RCL_ROS_TIME));

fujitatomoya commented 1 month ago

I have tried add this code. But have some error in IDE, my ros2 version is foxy

Foxy is already E.O.L, can you use humble or later instead?

Barry-Xu-2018 commented 1 month ago

For Foxy, you should create a Clock with RCL_ROS_TIME

https://github.com/ros2/rclcpp/blob/b0c25d5f22237d42e2cedad05dbb2e5cc31a3cf4/rclcpp/include/rclcpp/clock.hpp#L53-L66

Create a new "Rate" like with above Clock.

https://github.com/ros2/rclcpp/blob/b0c25d5f22237d42e2cedad05dbb2e5cc31a3cf4/rclcpp/include/rclcpp/rate.hpp#L115

BTW, foxy is EOL. So it's best to upgrade to the Humble or Iron version.