holding a twist message on cmd_vel node
Hi again,
I have come up with some code that allows for a turtlebot 2, to go in a straight path for 5 seconds. My problem is that it sends the velocity command for a split fraction of time, Im not sure what it is. I am looking for a way to hold a published command on a topic for a period of time. What is the easiest way to hold a command on a topic for a certain period of time? I guess I'm looking for some command that is like; ros::topic.hold(time);
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
#include <time.h>
#include <iostream>
using namespace std;
int main(int argc, char** argv){
ros::init(argc, argv, "gs_now");
ros::NodeHandle nh;
ros::Publisher pub = nh.advertise<geometry_msgs::Twist>(
"/cmd_vel_mux/input/navi", 1);
ros::Rate rate(1);
geometry_msgs::Twist vel;
vel.angular.z = 0.0;
vel.linear.x = 0.1;
clock_t t, l;
t = clock();
while(ros::ok()){
l = clock() - t;
if( ((double)l / ((double)CLOCKS_PER_SEC)) == 1.000){
pub.publish(vel);
ROS_INFO_STREAM("Sending random velocity command:"
<< " linear=" << vel.linear.x
<< " angular=" << vel.angular.z
<< " at time =" << ((double)l / ((double)CLOCKS_PER_SEC)));
}
else if(((double)l / ((double)CLOCKS_PER_SEC)) == 2.000){
pub.publish(vel);
ROS_INFO_STREAM("Sending random velocity command:"
<< " linear=" << vel.linear.x
<< " angular=" << vel.angular.z
<< " at time =" << ((double)l / ((double)CLOCKS_PER_SEC)));
}
else if(((double)l / ((double)CLOCKS_PER_SEC)) == 3.000){
pub.publish(vel);
ROS_INFO_STREAM("Sending random velocity command:"
<< " linear=" << vel.linear.x
<< " angular=" << vel.angular.z
<< " at time =" << ((double)l / ((double)CLOCKS_PER_SEC)));
}
else if(((double)l / ((double)CLOCKS_PER_SEC)) == 4.000){
pub.publish(vel);
ROS_INFO_STREAM("Sending random velocity command:"
<< " linear=" << vel.linear.x
<< " angular=" << vel.angular.z
<< " at time =" << ((double)l / ((double)CLOCKS_PER_SEC)));
}
else if(((double)l / ((double)CLOCKS_PER_SEC)) == 5.000){
ros::shutdown();
}
//rate.sleep();
}
}
Thank you.