ROS中的时间

ros::Time t1 = ros::Time::now();
double t_cur = t1.toSec();
printf("The time is: %16f\n",t_cur);


void GridMap::cloudCallback(const sensor_msgs::PointCloud2ConstPtr &img)
{
  double t_cur = img->header.stamp.toSec();
  printf("cloudCallback_time: %16f\n",t_cur);
}

在代码中可以用来打高精度时间戳

#include

    auto t1 = std::chrono::high_resolution_clock::now();
    auto t2 = std::chrono::high_resolution_clock::now();
    auto elased_time = std::chrono::duration_cast<std::chrono::nanoseconds>(t2-t1).count()/1.0e9;
    std::cout<<"Consumed Time(s): "<< elased_time <<std::endl;

三、c++中sleep, usleep, ros中的ros::Duration

sleep(0.5) = sleep(0)

usleep(0.5*1000000)
ros::Duration(0.5).sleep()

四、ros::Rate

 ros::Rate loop_rate(10);
 while(!have_odom){
    loop_rate.sleep();
    ros::spinOnce();
 }

五、ros::WallTime::now()解释

官方解释:Wall-clock time, or wall time, is the human perception of the passage of time from the start to the completion of a task. In the context of a task being performed on a computer, wall-clock time is a measure of the real time that elapses from start to end, including time that passes due to programmed (artificial) delays or waiting for resources to become available. In other words, it is the difference between the time at which a task finishes and the time at which the task started.

挂钟时间,或称挂钟时间,是人类对一项任务从开始到完成的时间流逝的感知。在计算机上执行任务的过程中,挂钟时间是对从开始到结束所经过的实时时间的度量,包括由于编程(人为)延迟或等待资源可用而经过的时间。换句话说,它是任务完成时间和任务开始时间之间的差异。

谈及时间,有一个很重要的概念是ROS Time和Wall Time,两者是有区别的。它们两个的接口完全一样,数据类型也一样,但是ROS Time表示的是ROS网络中的时间。

ROS网络中的时间是指,如果当时在非仿真环境里运行,那它就是当前的时间。但是假设去回放当时的情况,那就需要把当时的时间录下来。以控制为例,很多的数据处理需要知道当时某一个时刻发生了什么。Wall Time可以理解为墙上时间,墙上挂着的时间没有人改变的了,永远在往前走;ROS Time可以被人为修改,你可以暂停它,可以加速,可以减速,但是Wall Time不可以。

在开启一个Node之前,当把use_sim_time设置为true时,这个节点会从clock Topic获得时间。所以操作这个clock的发布者,可以实现一个让Node中得到ROS Time暂停、加速、减速的效果。同时下面这些方面都是跟Node透明的,所以非常适合离线的调试方式。当把ROSbag记下来以后重新play出来时,加两个横杠,–clock,它就会发布出这个消息。

Wall Time(挂钟时间)

&#x5982;&#x679C;&#x4F60;&#x60F3;&#x83B7;&#x5F97;&#x5B9E;&#x9645;&#x7684;&#x6302;&#x949F;&#x65F6;&#x95F4;&#x5373;&#x4F7F;&#x8FD0;&#x884C;&#x5728;&#x6A21;&#x62DF;&#x3002;
roslib&#x63D0;&#x4F9B;&#x6240;&#x6709;&#x65F6;&#x95F4;&#x7248;&#x672C;&#x6784;&#x5EFA;&#xFF1A;ros::WallTime, ros::WallDuration, &#x548C;ros::WallRate
&#x4E5F;&#x5206;&#x522B;&#x6709;&#x76F8;&#x540C;&#x7684;&#x63A5;&#x53E3;&#xFF1A;ros::Time, ros::Duration, and ros::Rate

播放rosbag时,若参数/use_sim_time 为true,则此时

ros::WallTime::now()&#x4E3A;&#x5F53;&#x524D;&#x7684;&#x771F;&#x5B9E;&#x65F6;&#x95F4;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x5899;&#x4E0A;&#x7684;&#x6302;&#x949F;&#x65F6;&#x95F4;&#xFF0C;&#x4E00;&#x76F4;&#x5728;&#x8D70;&#x3002;
ros::Time::now()&#x4E3A;rosbag&#x5F53;&#x65F6;&#x7684;&#x65F6;&#x95F4;&#xFF0C;&#x662F;&#x7531;bag&#x4E2D;/clock&#x83B7;&#x53D6;&#x7684;&#x3002;&#x662F;&#x4EFF;&#x771F;&#x65F6;&#x95F4;&#x3002;

参考:
[1]https://blog.csdn.net/sru_alo/article/details/102893536
[2]https://www.ncnynl.com/archives/201702/1301.html
[3]https://blog.csdn.net/heroacool/article/details/106210623

Original: https://blog.csdn.net/weixin_45736684/article/details/114229509
Author: zxw610
Title: ROS中的时间

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/754411/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球