Ask Your Question
1

How should I create a timer in ros2?

asked 2020-09-28 12:50:31 -0600

updated 2020-09-30 11:56:24 -0600

I am looking for the create_timer function in ros2 but I only find the rclcpp::Node::create_wall_timer.

From my understanding there should be some abstract create_timer function (instead of wall timer) to work optionally with the simulated time. Where is this function?

If that function is not there in ros2, what would be the solution to have some abstract timer that adapts to the wall or to the simulated time?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2020-09-29 13:41:50 -0600

sloretz gravatar image

There is an rclcpp::create_timer() free function

The trick to a timer that uses simulated time is to pass a clock that uses simulated time to the rclcpp::create_timer() function. The default clock on a node is a ROS clock - meaning it will use simulated time if simulated time is active.

You might also be interested in

edit flag offensive delete link more

Comments

Correct. Thank you. Does anyone know why create_timer is a free function? and Node::create_wall_timer is a member function?

Pablo Iñigo Blasco gravatar image Pablo Iñigo Blasco  ( 2020-09-30 13:02:54 -0600 )edit

The free function has an advantage in that the same code can be used with both LifecycleNode and Node. There's some talk of making all functions free functions in https://github.com/ros2/rclcpp/issues...

sloretz gravatar image sloretz  ( 2020-09-30 13:16:43 -0600 )edit
0

answered 2020-10-01 11:29:46 -0600

updated 2020-10-01 14:05:05 -0600

Below it is shown some example using a lambda function callback:

auto ros_clock = rclcpp::Clock::make_shared();
auto timer_ = rclcpp::create_timer(nh_, ros_clock, 0.5s,
   [=]()
   {
       do_actions();
   });
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-09-28 12:50:31 -0600

Seen: 2,451 times

Last updated: Oct 01 '20