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

Timer fires all at once

asked 2015-04-06 15:18:46 -0500

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?

edit retag flag offensive close merge delete

Comments

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 the oneshot flag to see if the timer is truly initialized by that piece of code.

alextoind gravatar image alextoind  ( 2015-04-06 15:36:49 -0500 )edit

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!

RafBerkvens gravatar image RafBerkvens  ( 2015-04-06 16:11:53 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2015-04-06 16:10:17 -0500

It seems that I simply have been reading the signs wrong. As I said, the Timer callback did a publish, and I looked at the published message to establish the time it was send. However, the time of that message was not updated before it was published. I now update the time of the message with the event's time, solving my issue.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-04-06 15:18:46 -0500

Seen: 368 times

Last updated: Apr 06 '15