ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
2

ROS2 Sim time for callbacks

asked 2020-06-10 15:36:24 -0500

fiji3119 gravatar image

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".

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-06-11 00:33:34 -0500

KenYN gravatar image

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.

edit flag offensive delete link more

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.

fiji3119 gravatar image fiji3119  ( 2020-06-11 10:01:52 -0500 )edit

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/...

tfoote gravatar image tfoote  ( 2020-06-11 12:06:50 -0500 )edit
1

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.

fiji3119 gravatar image fiji3119  ( 2020-06-11 14:32:35 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2020-06-10 15:36:24 -0500

Seen: 2,606 times

Last updated: Jun 11 '20