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(){
vel_pub_ = ph_.advertise<geometry_msgs::Twist>(
"/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.