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

I checked the underlying source code to figure out what hasPending() checks. Here is what bool TimerManager<T, D, E>::hasPending(int32_t handle) returns:

return info->next_expected <= T::now() || info->waiting_callbacks != 0;

It looks like hasPending() will only return true when a timer event is past due or just about to tick. I assume the code below will print true, CB!, then false.

#include "ros/ros.h"

void callback(const ros::TimerEvent &event)
{
 ROS_INFO("CB!");
}

int main(int argc, char **argv)
{
 ros::init(argc, argv, "timer_test");
 ros::NodeHandle nh;
 ros::Timer timer = nh.createTimer(ros::Duration(1.0), callback, true);
 ros::Duration(2).sleep(); // wait for longer than the timer period
 ROS_INFO(timer.hasPending() ? "true" : "false");
 ros::spinOnce();
 ROS_INFO(timer.hasPending() ? "true" : "false");

}

I hope this helps.