## ROS2 Node create_wall_timer

Typically: "How do I... ", "How can I... " questions
avena_robotics
Posts: 16
Joined: 11 Aug 2020, 12:47

### ROS2 Node create_wall_timer

Hello,

we use ROS2 to publish simple string to the topic. We created ROS2 timer using this->create_wall_timer inside Coppelia plugin to publish new message every 100 ms.

But this timer is not triggered at all. We have made a workaround by creating separate thread in which message is published. But it does not look like a good solution.

Is it a problem with how ROS2 timers works or we are doing something wrong?

For the base code we are using code from ROS2 tutorial https://index.ros.org/doc/ros2/Tutorial ... ubscriber/

avena_robotics
Posts: 16
Joined: 11 Aug 2020, 12:47

### Re: ROS2 Node create_wall_timer

Hello,

we have managed to solve our problem with ROS2 Timers.

You have to spin your node so that rclcpp::TimerBase can update its state (we assumed that).
To do this in plugin, you have to spin node inside onInstancePass method (_timer has type of rclcpp::TimerBase::SharedPtr):

Code: Select all

void Plugin::onInstancePass(const sim::InstancePassFlags &flags, bool first)
{
rclcpp::spin_some(this->get_node_base_interface());
}

If you want to stop timer on simulation stop:

Code: Select all

void Plugin::onSimulationAboutToEnd()
{
_timer->cancel();
}

To start it again without creating new instance of the timer:

Code: Select all

void Plugin::onSimulationAboutToStart()
{
_timer->reset();
}

If someone has better approach, please write about it ;)

Cheers.

fferri
Posts: 735
Joined: 09 Sep 2013, 19:28

### Re: ROS2 Node create_wall_timer

avena_robotics wrote:
15 Dec 2020, 16:06
we use ROS2 to publish simple string to the topic. We created ROS2 timer using this->create_wall_timer inside Coppelia plugin to publish new message every 100 ms.
Why a ROS timer? was it not possible with the regular API and simulation scripts to call a function every X milliseconds?
avena_robotics wrote:
15 Dec 2020, 16:06
But this timer is not triggered at all. We have made a workaround by creating separate thread in which message is published. But it does not look like a good solution.

Is it a problem with how ROS2 timers works or we are doing something wrong?

For the base code we are using code from ROS2 tutorial https://index.ros.org/doc/ros2/Tutorial ... ubscriber/
Depends on how you created the timer. Show complete code if possible, or the relevant parts of it.
avena_robotics wrote:
16 Dec 2020, 11:53
You have to spin your node so that rclcpp::TimerBase can update its state (we assumed that).
To do this in plugin, you have to spin node inside onInstancePass method (_timer has type of rclcpp::TimerBase::SharedPtr):

Code: Select all

void Plugin::onInstancePass(const sim::InstancePassFlags &flags, bool first)
{
rclcpp::spin_some(this->get_node_base_interface());
}

What's this->get_node_base_interface()?
Doesn't the plugin (assuming you started of the CoppeliaSim ROS2 plugin) allready call spin_some? What is your change about?
avena_robotics wrote:
16 Dec 2020, 11:53
If someone has better approach, please write about it ;)
Hard to tell, without understanding your use-case :-)

Cheers