[ROS Q&A] 148 – ROS CPP Time and Duration arithmetics build error

[ROS Q&A] 148 - ROS CPP Time and Duration arithmetics build error

Written by Marco Arruda

11/08/2018

In this video we are going to explain about a problem trying to build a ROS C++ node using ros::Time and ros::Duration.

It is because the order of the terms in sum operation matters. You can check in the video it is simple to solve but it can be a trap if you do not pay attention to that during the development.

Step 1. Create a project in ROS Development Studio(ROSDS)

ROSDS helps you follow our tutorial in a fast pace without dealing without setting up an environment locally. If you haven’t had an account yet, you can create a free account here. Let’s call the project ros time error.

Step 2. Create a package

Let’s start by creating a ROS package for our code

cd ~/catkin_ws/src
catkin_create_pkg mypkg roscpp geometry_msgs 

Then we call the source file mypkg_node.cpp and put it under the mypkg/src folder with the content from the question.

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

    }


  }
}

To compile the code, we need to at first uncomment the add_executable and target_link_libraries in the CMakeLists.txt. After that, we can compile it with

cd ~/catkin_ws
catkin_make

However, some error related to ROS time occurs. After checking the documentation ROS C++ Time and Duration, we found out that the order is not quite right. The following part should be changed.

...
    ros::Time endTime = beginTime +  MessageTime; 
...

If we compile again, the problem is solved.

Want to learn more?

If you are interested in using ROS with C++, please check our ROS Basics in 5 days(C++) course for more information.

 

Edit by: Tony Huang

RELATED LINKS

The original question on ROS_Answers
ROS Development Studio (ROSDS)
Robot Ignite Academy
ROS C++ Time and Duration


We love feedback!

Did you like this video? Do you have questions about what is explained? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS topics, please let us know on the comments area and we will do a video about it.

Topics: ROS C++
Masterclass 2023 batch2 blog banner

Check Out These Related Posts

129. ros2ai

129. ros2ai

I would like to dedicate this episode to all the ROS Developers who believe that ChatGPT or...

read more

0 Comments

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This