Build Error using ros::Time to publish velocity messages for 3 seconds

asked 2018-08-09 12:47:43 -0600

nonso1234 gravatar image

updated 2018-10-25 09:34:36 -0600

Evgeny gravatar image

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();

    }


  }
}
edit retag flag offensive close merge delete

Comments

1

It might be that you have to reverse the order of the operands: a timepoint + a duration => other time point, but duration + timepoint => ?. See also wiki/roscpp/Overview/Time.

gvdhoorn gravatar image gvdhoorn  ( 2018-08-10 02:46:34 -0600 )edit