热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

实现周期控制的两种方案

sleep方案思路:循环中执行任务,执行完任务后,计算该周期剩余的时间,通过sleep休眠该时间例如,ro

sleep方案

思路:循环中执行任务,执行完任务后,计算该周期剩余的时间,通过sleep休眠该时间

例如,ros中的Rate就是实现的这种方案,文章末尾附上Rate的源码

ros::Rate r(10);//10HZ
while(ros::ok())
{// 其他任务r.sleep(); //使用Rate对象调整休眠时间,考虑循环中其他任务占用的时间,确保让整个循环的频率是 10hz
}


定时器方案

思路:定义一个定时器,使其到达下一周期时自动触发要执行的任务


Note:
无论哪一种方案,执行任务的时间都必须小于控制周期,否则将毫无意义,因此在写周期控制时,首先要测试任务的执行时间








在这里插入图片描述

00001 /*********************************************************************
00002 *
00003 * Software License Agreement (BSD License)
00004 *
00005 * Copyright (c) 2009, Willow Garage, Inc.
00006 * All rights reserved.
00007 *
00008 * Redistribution and use in source and binary forms, with or without
00009 * modification, are permitted provided that the following conditions
00010 * are met:
00011 *
00012 * * Redistributions of source code must retain the above copyright
00013 * notice, this list of conditions and the following disclaimer.
00014 * * Redistributions in binary form must reproduce the above
00015 * copyright notice, this list of conditions and the following
00016 * disclaimer in the documentation and/or other materials provided
00017 * with the distribution.
00018 * * Neither the name of the Willow Garage nor the names of its
00019 * contributors may be used to endorse or promote products derived
00020 * from this software without specific prior written permission.
00021 *
00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 * POSSIBILITY OF SUCH DAMAGE.
00034 *
00035 * Author: Eitan Marder-Eppstein
00036 *********************************************************************/

00037 #ifndef ROSLIB_RATE_H
00038 #define ROSLIB_RATE_H
00039
00040 #include "ros/time.h"
00041
00042 namespace ros
00043 {
00048 class Rate
00049 {
00050 public:
00055 Rate(double frequency);
00056
00061 bool sleep();
00062
00066 void reset();
00067
00072 Duration cycleTime();
00073
00077 Duration expectedCycleTime() { return expected_cycle_time_; }
00078
00079 private:
00080 Time start_;
00081 Duration expected_cycle_time_, actual_cycle_time_;
00082 };
00083
00088 class WallRate
00089 {
00090 public:
00095 WallRate(double frequency);
00096
00101 bool sleep();
00102
00106 void reset();
00107
00112 WallDuration cycleTime();
00113
00117 WallDuration expectedCycleTime() { return expected_cycle_time_; }
00118
00119 private:
00120 WallTime start_;
00121 WallDuration expected_cycle_time_, actual_cycle_time_;
00122 };
00123
00124 }
00125
00126 #endif

在这里插入图片描述

00001 /*********************************************************************
00002 *
00003 * Software License Agreement (BSD License)
00004 *
00005 * Copyright (c) 2009, Willow Garage, Inc.
00006 * All rights reserved.
00007 *
00008 * Redistribution and use in source and binary forms, with or without
00009 * modification, are permitted provided that the following conditions
00010 * are met:
00011 *
00012 * * Redistributions of source code must retain the above copyright
00013 * notice, this list of conditions and the following disclaimer.
00014 * * Redistributions in binary form must reproduce the above
00015 * copyright notice, this list of conditions and the following
00016 * disclaimer in the documentation and/or other materials provided
00017 * with the distribution.
00018 * * Neither the name of the Willow Garage nor the names of its
00019 * contributors may be used to endorse or promote products derived
00020 * from this software without specific prior written permission.
00021 *
00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 * POSSIBILITY OF SUCH DAMAGE.
00034 *
00035 * Author: Eitan Marder-Eppstein
00036 *********************************************************************/

00037 #include <ros/rate.h>
00038
00039 namespace ros
00040 {
00041
00042 Rate::Rate(double frequency)
00043 : start_(Time::now())
00044 , expected_cycle_time_(1.0 / frequency)
00045 , actual_cycle_time_(0.0)
00046 {}
00047
00048 bool Rate::sleep()
00049 {
00050 Time expected_end &#61; start_ &#43; expected_cycle_time_;
00051
00052 Time actual_end &#61; Time::now();
00053
00054 // detect backward jumps in time
00055 if (actual_end < start_)
00056 {
00057 expected_end &#61; actual_end &#43; expected_cycle_time_;
00058 }
00059
00060 //calculate the time we&#39;ll sleep for
00061 Duration sleep_time &#61; expected_end - actual_end;
00062
00063 //set the actual amount of time the loop took in case the user wants to know
00064 actual_cycle_time_ &#61; actual_end - start_;
00065
00066 //make sure to reset our start time
00067 start_ &#61; expected_end;
00068
00069 //if we&#39;ve taken too much time we won&#39;t sleep
00070 if(sleep_time <&#61; Duration(0.0))
00071 {
00072 // if we&#39;ve jumped forward in time, or the loop has taken more than a full extra
00073 // cycle, reset our cycle
00074 if (actual_end > expected_end &#43; expected_cycle_time_)
00075 {
00076 start_ &#61; actual_end;
00077 }
00078 return true;
00079 }
00080
00081 return sleep_time.sleep();
00082 }
00083
00084 void Rate::reset()
00085 {
00086 start_ &#61; Time::now();
00087 }
00088
00089 Duration Rate::cycleTime()
00090 {
00091 return actual_cycle_time_;
00092 }
00093
00094 WallRate::WallRate(double frequency)
00095 : start_(WallTime::now())
00096 , expected_cycle_time_(1.0 / frequency)
00097 , actual_cycle_time_(0.0)
00098 {}
00099
00100 bool WallRate::sleep()
00101 {
00102 WallTime expected_end &#61; start_ &#43; expected_cycle_time_;
00103
00104 WallTime actual_end &#61; WallTime::now();
00105
00106 // detect backward jumps in time
00107 if (actual_end < start_)
00108 {
00109 expected_end &#61; actual_end &#43; expected_cycle_time_;
00110 }
00111
00112 //calculate the time we&#39;ll sleep for
00113 WallDuration sleep_time &#61; expected_end - actual_end;
00114
00115 //set the actual amount of time the loop took in case the user wants to know
00116 actual_cycle_time_ &#61; actual_end - start_;
00117
00118 //make sure to reset our start time
00119 start_ &#61; expected_end;
00120
00121 //if we&#39;ve taken too much time we won&#39;t sleep
00122 if(sleep_time <&#61; WallDuration(0.0))
00123 {
00124 // if we&#39;ve jumped forward in time, or the loop has taken more than a full extra
00125 // cycle, reset our cycle
00126 if (actual_end > expected_end &#43; expected_cycle_time_)
00127 {
00128 start_ &#61; actual_end;
00129 }
00130 return true;
00131 }
00132
00133 return sleep_time.sleep();
00134 }
00135
00136 void WallRate::reset()
00137 {
00138 start_ &#61; WallTime::now();
00139 }
00140
00141 WallDuration WallRate::cycleTime()
00142 {
00143 return actual_cycle_time_;
00144 }
00145
00146 }


推荐阅读
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • Android日历提醒软件开源项目分享及使用教程
    本文介绍了一款名为Android日历提醒软件的开源项目,作者分享了该项目的代码和使用教程,并提供了GitHub项目地址。文章详细介绍了该软件的主界面风格、日程信息的分类查看功能,以及添加日程提醒和查看详情的界面。同时,作者还提醒了读者在使用过程中可能遇到的Android6.0权限问题,并提供了解决方法。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 学习SLAM的女生,很酷
    本文介绍了学习SLAM的女生的故事,她们选择SLAM作为研究方向,面临各种学习挑战,但坚持不懈,最终获得成功。文章鼓励未来想走科研道路的女生勇敢追求自己的梦想,同时提到了一位正在英国攻读硕士学位的女生与SLAM结缘的经历。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 本文介绍了如何使用Express App提供静态文件,同时提到了一些不需要使用的文件,如package.json和/.ssh/known_hosts,并解释了为什么app.get('*')无法捕获所有请求以及为什么app.use(express.static(__dirname))可能会提供不需要的文件。 ... [详细]
  • Postgresql备份和恢复的方法及命令行操作步骤
    本文介绍了使用Postgresql进行备份和恢复的方法及命令行操作步骤。通过使用pg_dump命令进行备份,pg_restore命令进行恢复,并设置-h localhost选项,可以完成数据的备份和恢复操作。此外,本文还提供了参考链接以获取更多详细信息。 ... [详细]
  • 十大经典排序算法动图演示+Python实现
    本文介绍了十大经典排序算法的原理、演示和Python实现。排序算法分为内部排序和外部排序,常见的内部排序算法有插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。文章还解释了时间复杂度和稳定性的概念,并提供了相关的名词解释。 ... [详细]
  • 手把手教你使用GraphPad Prism和Excel绘制回归分析结果的森林图
    本文介绍了使用GraphPad Prism和Excel绘制回归分析结果的森林图的方法。通过展示森林图,可以更加直观地将回归分析结果可视化。GraphPad Prism是一款专门为医学专业人士设计的绘图软件,同时也兼顾统计分析的功能,操作便捷,可以帮助科研人员轻松绘制出高质量的专业图形。文章以一篇发表在JACC杂志上的研究为例,利用其中的多因素回归分析结果来绘制森林图。通过本文的指导,读者可以学会如何使用GraphPad Prism和Excel绘制回归分析结果的森林图。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
author-avatar
最低调的鹌鹑
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有