problems with ros loop iteration

Hi, I am having problems with the amount of time this loop iterates.

 #include <ros/ros.h>
#include <geometry_msgs/Twist.h>

class GoStraight{
public:
GoStraight();

private:
void callback(const ros::TimerEvent&);
ros::NodeHandle ph_, nh_;
ros::Publisher vel_pub_;
geometry_msgs::Twist vel;
ros::Timer timer_;

};

GoStraight::GoStraight(){
"/cmd_vel_mux/input/navi", 1);
vel.angular.z = 0.0;
vel.linear.x = .2;
timer_ = nh_.createTimer(ros::Duration(3.0), &GoStraight::callback );
}

void callback(const ros::TimerEvent& event){

vel_pub_.publish(vel);

ROS_INFO_STREAM("Sending random velocity command:"
<< " linear=" << vel.linear.x
<< " angular=" << vel.angular.z);

}

int main(int argc, char** argv){

ros::init(argc, argv, "g_straight");
GoStraight g_straight;
ros::spin();

}

this is the output that I get:
[ INFO] [1403283564.623071038]: Sending random velocity command: linear=0.2 angular=0 at time =0.07
[ INFO] [1403283564.723195177]: Sending random velocity command: linear=0.2 angular=0 at time =0.07
[ INFO] [1403283564.823198861]: Sending random velocity command: linear=0.2 angular=0 at time =0.07
[ INFO] [140328


3564.923198642]: Sending random velocity command: linear=0.2 angular=0 at time =0.07 [ INFO] [1403283565.023199258]: Sending random velocity command: linear=0.2 angular=0 at time =0.08 [ INFO] [1403283565.123149431]: Sending random velocity command: linear=0.2 angular=0 at time =0.08 [ INFO] [1403283565.223199416]: Sending random velocity command: linear=0.2 angular=0 at time =0.08 [ INFO] [1403283565.323155826]: Sending random velocity command: linear=0.2 angular=0 at time =0.08 [ INFO] [1403283565.423198781]: Sending random velocity command: linear=0.2 angular=0 at time =0.08 [ INFO] [1403283565.523200109]: Sending random velocity command: linear=0.2 angular=0 at time =0.08 [ INFO] [1403283565.623197323]: Sending random velocity command: linear=0.2 angular=0 at time =0.08

I would like an output that goes like this:
[ INFO] [1403283565.523200109]: Sending random velocity command: linear=0.2 angular=0 at time =0.0
[ INFO] [1403283565.623197323]: Sending random velocity command: linear=0.2 angular=0 at time =1.0
[ INFO] [1403283565.523200109]: Sending random velocity command: linear=0.2 angular=0 at time =2.0
[ INFO] [1403283565.623197323]: Sending random velocity command: linear=0.2 angular=0 at time =3.0
[ INFO] [1403283565.523200109]: Sending random velocity command: linear=0.2 angular=0 at time =4.0
[ INFO] [1403283565.623197323]: Sending random velocity command: linear=0.0 angular=0 at time =5.0


I am having a hard time understand how the rate.sleep() affects the speed that the ROS loop iterates and help is greatly appreciated.

edit retag close merge delete

Sort by » oldest newest most voted

If you want a random velocity command being sent every second only, then set your ros::Rate rate(10) to rate(1) meaning it will be at 1hz and not 10hz. So it with happen 1 time every second, not ten like you were seeing before

more

oops I forgot to change the ROS INFO text but its not a random velocity. Either way I tried that, where it only loops at 1 hz, but then it still proceeds with a similar output except it iterates slower.

( 2014-06-20 15:21:51 -0500 )edit

Is an update happening around every 3 seconds? If so, that means your timer is set to fire every 3 seconds. Set this value to say 0.1 instead of 3, and you should get a message every 1/10th of a second (10hz). Hope this helped.

( 2014-08-05 08:13:10 -0500 )edit

I figured this out. Thank you though.

( 2014-08-05 09:16:56 -0500 )edit

What was the issue?

( 2014-08-05 09:50:58 -0500 )edit
1

I didn't use a call back function anymore and I just used a regular while(ros::ok()) to have the loop iterate at the rate that I wanted. I also implemented ros::time to control the rate that the actual messages were published to the topic.

( 2014-08-05 10:19:03 -0500 )edit

It looks like your loop is turing over at approximately 10 Hz which is the rate you set. rate.sleep() will sleep for the duration which will fill in the remainder of your cycle. I'm not familiar with the clock() methods I recommend that you use the ROS time objects which work in seconds natively not ticks. It's documented at http://wiki.ros.org/roscpp/Overview/Time as well as Rate and sleeping.

more

I am trying to modify my code so that the timer created passes to callback in the format as: ros::Timer timer = n.createTimer(ros::Duration(0.1), timerCallback); The only problem is I am not sure where to declare my callback. If i Declare it outside main, I get a bunch of errors about scope.

( 2014-06-20 15:24:06 -0500 )edit

I have changed my code to use those timer examples. I had a few problems though. My code is not compiling because I am getting errors from the callback. I'm not sure if I am doing this right. I just want the twist message to get published for 3 seconds. If you could help me with the format plz.

( 2014-06-24 11:40:29 -0500 )edit