• ROS时间概念总结:ros::Time、ros::Duration、定时器ros::Timer&ros::Rate


    0. 写于最前面

    希望大家收藏:

    本文持续更新地址:https://haoqchen.site/2018/11/08/ROS-time/

    本文总结了一些ROS中常用到的时间相关的一些类、定时器、概念等。

    作者会长期更新自己学到的一些知识,有什么错误希望大家能够一起探讨,一起进步。喜欢的话点个赞呗。

    左侧专栏还在更新其他ROS实用技巧哦,关注一波?

    1. 概述

    roslib给用户提供了ros::Time and ros::Duration两个类来描述时刻以及时间间隔两个概念,其中Duration可以是负数。Time和Duration拥有一样的成员:
    int32 sec
    int32 nsec

    一般情况下,这里的时间都是跟平台(即系统)的时间相关联的,但ROS提供了一种模拟时钟即ROS时钟时间,当进行了相应设置时,这里的时间就是ROS时钟时间。

    2. 时间与时间间隔的基本使用

    2.1 获得当前时间

    ros::Time begin = ros::Time::now();
    注:如果使用的是模拟时间,now在/clock话题接收到第一个消息之前都会返回0


    2.2 定义类对象

    ros::Time::Time(uint32_t _sec, uint32_t _nsec)
    ros::Time::Time(double t)

    ros::Duration::Duration(uint32_t _sec, uint32_t _nsec)
    ros::Duration::Duration(double t)

    _sec是秒,_nsec是纳秒
    故ros::Time a_little_after_the_beginning(0, 1000000);等价于ros::Time a_little_after_the_beginning(0.001);

    2.3 相互转化、比较

    Time继承自
    template<class T, class D>
    class ros::TimeBase< T, D >

    Duration继承自
    template<class T>
    class ros::DurationBase< T >

    两个基类都实现了>、<、!=等比较符。并且两者都实现了
    uint64_t     toNSec () const
    double     toSec () const
    我们可以通过这两个函数来进行Time和Duration的转化

    2.4 运算符

    两个基类都重载了+、-、+=、-=运算符:

    1 hour + 1 hour = 2 hours (duration + duration = duration)

    2 hours - 1 hour = 1 hour (duration - duration = duration)

    Today + 1 day = tomorrow (time + duration = time)

    Today - tomorrow = -1 day (time - time = duration)

    Today + tomorrow = error (time + time is undefined)

    3. 延时与循环

    bool ros::Duration::sleep()
    ros::Duration(0.5).sleep(); // sleep for half a second
    

    4. 定时器

    ros::Timer

    首先需要说明的是,ROS并不是实时系统,所以定时器并不能确保精确定时。精确的执行时间以及理论上应该执行的时间可以在回调函数的ros::TimerEvent结构中得到。

    ros::Timer ros::NodeHandle::createTimer(ros::Duration period, <callback>, bool oneshot = false);
    ros::Timer timer = n.createTimer(ros::Duration(0.1), timerCallback);//定时0.1s
    void timerCallback(const ros::TimerEvent& e);

    其中oneshot是定义是否只定时一次,默认连续定时。这里也不一定要回调函数,也可以传函数对象等,这里不细述。

    其中TimerEvent结构体定义如下:

    struct TimerEvent
    {
      Time last_expected;                     ///< In a perfect world, this is when the last callback should have happened
      Time last_real;                         ///< When the last callback actually happened
    
      Time current_expected;                  ///< In a perfect world, this is when the current callback should be happening
      Time current_real;                      ///< This is when the current callback was actually called (Time::now() as of the beginning of the callback)
    
      struct
      {
        WallDuration last_duration;           ///< How long the last callback ran for, always in wall-clock time
      } profile;
    };

    ros::Rate

    ros::Rate r(10); // 10 hz
    while (ros::ok())
    {
    //... do some work ...
        bool met = r.sleep();
    }

    它的功能就是先设定一个频率,然后通过睡眠度过一个循环中剩下的时间,来达到该设定频率。如果能够达到该设定频率则返回true,不能则返回false。

    计时的起点是上一次睡眠的时间、构造函数被调用、或者调用void ros::Rate::reset()函数重置时间。

    因为没有TimerEvent,所以相对于Timer而言,Rate的精确度会有所下降。

    参考

    http://wiki.ros.org/roscpp/Overview/Time

    http://wiki.ros.org/roscpp_tutorials/Tutorials/Timers

    http://docs.ros.org/diamondback/api/rostime/html/classros_1_1Rate.html

  • 相关阅读:
    flask框架+上传文件接口实战【软件测试培训】【多测师_王sir】
    读取Excel中的视频文件地址+requests库下载后存入本地文件夹【软件测试培训】【多测师_王sir】
    UI和接口自动化中的设计模式:单例模式【软件测试培训】【多测师_王sir】
    Python+BeautifulReport生成完美的接口自动化测试报告【多测师_王sir】
    Linux命令中查找以.log结尾文件中不包含某个特定字符串这行的内容【多测师_王sir】
    查询多条数据
    django登录装饰接口封装
    django使用redis作为session缓存
    tinymce配置
    django重写authcenticate方法兼容用户、邮箱、密码认证登录
  • 原文地址:https://www.cnblogs.com/HaoQChen/p/11048603.html
Copyright © 2020-2023  润新知