热门标签 | 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 }


推荐阅读
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文介绍了Windows操作系统的版本及其特点,包括Windows 7系统的6个版本:Starter、Home Basic、Home Premium、Professional、Enterprise、Ultimate。Windows操作系统是微软公司研发的一套操作系统,具有人机操作性优异、支持的应用软件较多、对硬件支持良好等优点。Windows 7 Starter是功能最少的版本,缺乏Aero特效功能,没有64位支持,最初设计不能同时运行三个以上应用程序。 ... [详细]
  • 本文介绍了OpenStack的逻辑概念以及其构成简介,包括了软件开源项目、基础设施资源管理平台、三大核心组件等内容。同时还介绍了Horizon(UI模块)等相关信息。 ... [详细]
  • IB 物理真题解析:比潜热、理想气体的应用
    本文是对2017年IB物理试卷paper 2中一道涉及比潜热、理想气体和功率的大题进行解析。题目涉及液氧蒸发成氧气的过程,讲解了液氧和氧气分子的结构以及蒸发后分子之间的作用力变化。同时,文章也给出了解题技巧,建议根据得分点的数量来合理分配答题时间。最后,文章提供了答案解析,标注了每个得分点的位置。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • 本文介绍了解决二叉树层序创建问题的方法。通过使用队列结构体和二叉树结构体,实现了入队和出队操作,并提供了判断队列是否为空的函数。详细介绍了解决该问题的步骤和流程。 ... [详细]
  • 微软评估和规划(MAP)的工具包介绍及应用实验手册
    本文介绍了微软评估和规划(MAP)的工具包,该工具包是一个无代理工具,旨在简化和精简通过网络范围内的自动发现和评估IT基础设施在多个方案规划进程。工具包支持库存和使用用于SQL Server和Windows Server迁移评估,以及评估服务器的信息最广泛使用微软的技术。此外,工具包还提供了服务器虚拟化方案,以帮助识别未被充分利用的资源和硬件需要成功巩固服务器使用微软的Hyper - V技术规格。 ... [详细]
  • node.jsrequire和ES6导入导出的区别原 ... [详细]
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社区 版权所有