ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How can I post the time that has passed during a process?

asked 2013-10-02 05:02:12 -0600

Pedro_85 gravatar image

updated 2014-01-28 17:18:07 -0600

ngrennan gravatar image

I would like to know how much time has gone by while performing a certain process. As an example I have used the Publisher/Subscriber tutorial code from ros.org with some minor modifications. Basically, I want to know the time that has passed since the beginning of the code until the end of it. I understand why this code doesn't work but I posted it anyway so you can get the idea of what I want.

#include "ros/ros.h"
#include "std_msgs/String.h"
#include "geometry_msgs/Vector3.h"

#include <sstream>

geometry_msgs::Vector3 valorFloat;
int valor = 1;
double start_time, finish_time, delta_t;

int main(int argc, char **argv)
{
  double tiempo_actual; 
  ros::init(argc, argv, "talker");
  ros::NodeHandle n;

  ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
  ros::Publisher mensaje = n.advertise<geometry_msgs::Vector3>("/ValorFloat",50);
  ros::Rate loop_rate(10);

  int count = 0;
  while (ros::ok())
  {
    current_time=(double)ros::Time::now().toSec();
    start_time = current_time; //Save current time as t1
    std_msgs::String msg;
    std::stringstream ss;
    ss << "hello world " << count;
    msg.data = ss.str();
    valorFloat.x=valor;
    valorFloat.y=valor+1;
    valorFloat.z=valor+2;
    chatter_pub.publish(msg);
    mensaje.publish(valorFloat);
    ros::spinOnce();
    loop_rate.sleep();
    ++count;
    finish_time=current_time; //Save current time as t2
    delta_t=finish_time-start_time;
    printf("Delta t: %f \n",delta_t);
  }


  return 0;
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2013-10-02 05:30:37 -0600

updated 2013-10-02 06:48:47 -0600

I believe the - operator is overloaded for ros::Time and returns a ros::Duration, so you should be able to do something like:

ros::Time start_time = ros::Time::now();
//do some work
ros::Duration delta_t = ros::Time::now() - start_time;
double delta_t_sec = delta_t.toSec();
edit flag offensive delete link more

Comments

Thanks! one detail though I had to change delta_t.to_sec() to delta_t.toSec()

Pedro_85 gravatar image Pedro_85  ( 2013-10-02 06:23:08 -0600 )edit

Oops, I was thinking in python. I'll edit my answer.

Dan Lazewatsky gravatar image Dan Lazewatsky  ( 2013-10-02 06:48:26 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2013-10-02 05:02:12 -0600

Seen: 1,272 times

Last updated: Oct 02 '13