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

Revision history [back]

click to hide/show revision 1
initial version

Turns out it is fairly simple:

In your constructor:

// Create the timer  
timer_ = this->create_wall_timer(  
   timer_duration,
   std::bind(&MyClass::timer_callback, this));  

// Instantly stop the timer  
timer_->cancel();

And then define e.g. a service callback to start/stop the timer:

if (start_) {
  timer_->reset();  // starts the timer
} else {
  timer_->cancel();  // stops the timer
}

It's OK to call cancel() on an already stopped timer.