Timer fires all at once
I'm on Ubuntu 14.04 with ROS Indigo. I'm trying to repeat a published message for a few times, with some delay in between. It seemed that a ros::Timer
was the tool for the job. However, I'm getting unexpected results. Instead of the timer calling the function once every ros::Duration(0.1)
, the repetitions are called instantly.
Here's some pseudo code to illustrate the program, I haven't the checked this code:
class MyClass{
private:
void myFunction();
void myTimerCallback(const ros::TimerEvent& event)
ros::NodeHandle nh_;
ros::Timer *timer_;
...
}
void MyClass::myFunction(){
...
timer_ = new ros::Timer(nh_.createTimer(
ros::Duration(0.1),
&MyClass::myTimerCallback,
this));
...
}
void MyClass::myTimerCallback(const ros::TimerEvent& event){
// do stuff
}
int main(int argc, char **argv){
ros::init(argc, argv, "pseudo_timer");
MyClass my_class;
ros::spin();
}
What I expect is that myTimerCallback
is called once every 0.1 seconds, until I call timer_->stop()
. However, what I see is that myTimerCallback
is called the number of times I want it to be called in less than 0.001 seconds. In other words, there's no timing whatsoever.
Any ideas on what might be going wrong here?
Where do you initialize the timer? Is it (for example) in the constructor or somewhere else? You have put it in a generic
MyClass::myFunction()
method, but where do you call this one? You could try theoneshot
flag to see if the timer is truly initialized by that piece of code.Hi aletoind. The timer is initialized in a subscriber method. I think my design can be improved, but it does the trick for now. I've answered my own question, because I found where I did something wrong. Thanks for thinking with me!