Robotics StackExchange | Archived questions

ROS2 Sim time for callbacks

Hello everyone, I have a test node class with the constructor as follows:

NodeTest::NodeTest(const rclcpp::NodeOptions& options) : Node("node_test", options)
{
     this->set_parameter(rclcpp::Parameter("use_sim_time", true));
     timer_ = this->create_wall_timer(std::chrono::milliseconds(1), std::bind(&NodeTest::doWork, this));
}

I intend to use simulation time. Is there anyway to have callback(i.e doWork in this case) execute on expiring simulation time instead of system time? Basically, I am trying to run doWork()at a specific frequency that is based on sim_time, so that I can also run simulation in "slow motion".

Asked by fiji3119 on 2020-06-10 15:36:24 UTC

Comments

Answers

You can do:

rclcpp::Clock ros_clock(RCL_ROS_TIME);
rclcpp::create_timer(this, ros_clock, std::chrono::milliseconds(1), std::bind(&NodeTest::doWork, this));

But that's from Eloquent onwards, although it might be in a Dashing patch release too.

Asked by KenYN on 2020-06-11 00:33:34 UTC

Comments

@KenYN when I print ros_clock.now().seconds() and ros_clock.now().nanoseconds(), it shows system time i.e epoch but not sim_time (i.e gazebo's time). However, when I print node's time this->now().seconds(), it shows sim_time from gazebo. Is this correct? None of these, RCL_SYSTEM_TIME, RCL_ROS_TIME and RCL_STEADY_TIME shows simulation time.

Asked by fiji3119 on 2020-06-11 10:01:52 UTC

You should get the clock from the node to get the simulated time source connected, not construct a bare clock. use the get_clock() method on the Node.

http://docs.ros2.org/foxy/api/rclcpp/classrclcpp_1_1Node.html#aec0332887ae90caed727e5025cb62195

Asked by tfoote on 2020-06-11 12:06:50 UTC

Yes, that worked, thank you!. Also, regarding above example by @KenYN, I triedrclpp::create_timer(this, this->get_clock(), std::chrono::milliseconds(1), std::bind(&NodeTest::doWork, this)); but the doWork never gets called. Is the duration, std::chrono::milliseconds(1), going to be in simulation time ( use_sim_time=true)? The "/clock" topic is populated by gazebo in my example.

Asked by fiji3119 on 2020-06-11 14:32:35 UTC