Skip to content
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

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

Open
fengzhongsu opened this issue May 17, 2024 · 10 comments
Open

Comments

@fengzhongsu
Copy link

fengzhongsu commented May 17, 2024

Bug report

Required Info:

  • Operating System:
  • Installation type:
  • Version or commit hash:
  • DDS implementation:
  • Client library (if applicable):

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
Copy link
Collaborator

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

@fengzhongsu
Copy link
Author

fengzhongsu commented May 20, 2024 via email

@fengzhongsu
Copy link
Author

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
Copy link
Collaborator

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

class Rate : public RateBase
{
public:
RCLCPP_SMART_PTR_DEFINITIONS(Rate)
RCLCPP_PUBLIC
explicit Rate(
const double rate,
Clock::SharedPtr clock = std::make_shared<Clock>(RCL_SYSTEM_TIME));
RCLCPP_PUBLIC
explicit Rate(
const Duration & period,
Clock::SharedPtr clock = std::make_shared<Clock>(RCL_SYSTEM_TIME));

@fengzhongsu
Copy link
Author

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

class Rate : public RateBase
{
public:
RCLCPP_SMART_PTR_DEFINITIONS(Rate)
RCLCPP_PUBLIC
explicit Rate(
const double rate,
Clock::SharedPtr clock = std::make_shared<Clock>(RCL_SYSTEM_TIME));
RCLCPP_PUBLIC
explicit Rate(
const Duration & period,
Clock::SharedPtr clock = std::make_shared<Clock>(RCL_SYSTEM_TIME));

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

@fujitatomoya
Copy link
Collaborator

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
Copy link
Collaborator

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

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

@fengzhongsu
Copy link
Author

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
Copy link
Collaborator

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
Copy link
Collaborator

For Foxy, you should create a Clock with RCL_ROS_TIME

class Clock
{
public:
RCLCPP_SMART_PTR_DEFINITIONS(Clock)
/// Default c'tor
/**
* Initializes the clock instance with the given clock_type.
*
* \param clock_type type of the clock.
* \throws anything rclcpp::exceptions::throw_from_rcl_error can throw.
*/
RCLCPP_PUBLIC
explicit Clock(rcl_clock_type_t clock_type = RCL_SYSTEM_TIME);

Create a new "Rate" like with above Clock.

using Rate = GenericRate<std::chrono::system_clock>;

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants