Build Error using ros::Time to publish velocity messages for 3 seconds
Hi, I'm trying to publish velocity messages for 3 seconds. Whenever i build the cpp file, i get the error below.
Errors
<< pub_sub2:make /home/ros-industrial/catkin_ws/logs/pub_sub2/build.make.009.log
/home/ros-industrial/catkin_ws/src/pub_sub2/pubvel.cpp: In function ‘int main(int, char**)’:
/home/ros-industrial/catkin_ws/src/pub_sub2/pubvel.cpp:38:37: error: no match for ‘operator+’ (operand types are ‘ros::Duration’ and ‘ros::Time’)
ros::Time endTime = MessageTime + ros::Time::now();
^
I'm guessing it's a problem with the operator (+) but i don't understand why
Here's the c++ code below
// This program publishes randomly-generated velocity
// messages for turtlesim.
#include <ros/ros.h>
#include <geometry_msgs/Twist.h> // For geometry_msgs::Twist
#include <stdlib.h> // For rand() and RAND_MAX
int main(int argc, char **argv) {
ros::init(argc, argv, "publish_velocity");
ros::NodeHandle nh; // handles node
ros::Publisher pub = nh.advertise<geometry_msgs::Twist>(
"turtle1/cmd_vel", 1000); //links publisher to the node
while(ros::ok()) {
geometry_msgs::Twist msg; //Object msgs
msg.linear.x = 0.5;
msg.angular.z = 0;
msg.linear.z = 0.5;
// Time tracking
ros::Time beginTime = ros::Time::now();
ros::Duration MessageTime = ros::Duration(3);
ros::Time endTime = MessageTime + beginTime;
while(ros::Time::now() < endTime){
pub.publish(msg);
ROS_INFO_STREAM("Sending velocity command:"
<< " linear=" << msg.linear.x
<< " angular=" << msg.angular.z
<<"linearZ="<<msg.linear.z);
ros::Duration(0.3).sleep();
}
}
}
It might be that you have to reverse the order of the operands: a
timepoint + a duration => other time point
, butduration + timepoint => ?
. See also wiki/roscpp/Overview/Time.