rate.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2009, Willow Garage, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of the Willow Garage nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * Author: Eitan Marder-Eppstein
36  *********************************************************************/
37 #include <ros/rate.h>
38 
39 namespace ros
40 {
41 
42 Rate::Rate(double frequency)
43 : start_(Time::now())
44 , expected_cycle_time_(1.0 / frequency)
45 , actual_cycle_time_(0.0)
46 { }
47 
48 Rate::Rate(const Duration& d)
49  : start_(Time::now())
50  , expected_cycle_time_(d.sec, d.nsec)
51  , actual_cycle_time_(0.0)
52 { }
53 
54 bool Rate::sleep()
55 {
56  Time expected_end = start_ + expected_cycle_time_;
57 
58  Time actual_end = Time::now();
59 
60  // detect backward jumps in time
61  if (actual_end < start_)
62  {
63  expected_end = actual_end + expected_cycle_time_;
64  }
65 
66  //calculate the time we'll sleep for
67  Duration sleep_time = expected_end - actual_end;
68 
69  //set the actual amount of time the loop took in case the user wants to know
70  actual_cycle_time_ = actual_end - start_;
71 
72  //make sure to reset our start time
73  start_ = expected_end;
74 
75  //if we've taken too much time we won't sleep
76  if(sleep_time <= Duration(0.0))
77  {
78  // if we've jumped forward in time, or the loop has taken more than a full extra
79  // cycle, reset our cycle
80  if (actual_end > expected_end + expected_cycle_time_)
81  {
82  start_ = actual_end;
83  }
84  // return false to show that the desired rate was not met
85  return false;
86  }
87 
88  return sleep_time.sleep();
89 }
90 
91 void Rate::reset()
92 {
93  start_ = Time::now();
94 }
95 
96 Duration Rate::cycleTime() const
97 {
98  return actual_cycle_time_;
99 }
100 
101 WallRate::WallRate(double frequency)
102 : start_(WallTime::now())
103 , expected_cycle_time_(1.0 / frequency)
104 , actual_cycle_time_(0.0)
105 {}
106 
107 WallRate::WallRate(const Duration& d)
108 : start_(WallTime::now())
109 , expected_cycle_time_(d.sec, d.nsec)
110 , actual_cycle_time_(0.0)
111 {}
112 
113 bool WallRate::sleep()
114 {
115  WallTime expected_end = start_ + expected_cycle_time_;
116 
117  WallTime actual_end = WallTime::now();
118 
119  // detect backward jumps in time
120  if (actual_end < start_)
121  {
122  expected_end = actual_end + expected_cycle_time_;
123  }
124 
125  //calculate the time we'll sleep for
126  WallDuration sleep_time = expected_end - actual_end;
127 
128  //set the actual amount of time the loop took in case the user wants to know
129  actual_cycle_time_ = actual_end - start_;
130 
131  //make sure to reset our start time
132  start_ = expected_end;
133 
134  //if we've taken too much time we won't sleep
135  if(sleep_time <= WallDuration(0.0))
136  {
137  // if we've jumped forward in time, or the loop has taken more than a full extra
138  // cycle, reset our cycle
139  if (actual_end > expected_end + expected_cycle_time_)
140  {
141  start_ = actual_end;
142  }
143  return false;
144  }
145 
146  return sleep_time.sleep();
147 }
148 
149 void WallRate::reset()
150 {
151  start_ = WallTime::now();
152 }
153 
155 {
156  return actual_cycle_time_;
157 }
158 
159 }
ros::WallRate::cycleTime
WallDuration cycleTime() const
Get the actual run time of a cycle from start to sleep.
Definition: rate.cpp:189
ros::WallDuration::sleep
bool sleep() const
sleep for the amount of time specified by this Duration. If a signal interrupts the sleep,...
Definition: src/time.cpp:509
ros
rate.h
ros::Rate::Rate
Rate(double frequency)
Constructor, creates a Rate.
Definition: rate.cpp:77
ros::WallRate::sleep
bool sleep()
Sleeps for any leftover time in a cycle. Calculated from the last time sleep, reset,...
Definition: rate.cpp:148
ros::WallTime::now
static WallTime now()
Returns the current wall clock time.
Definition: src/time.cpp:479
ros::WallRate::actual_cycle_time_
WallDuration actual_cycle_time_
Definition: rate.h:161
ros::WallRate::WallRate
WallRate(double frequency)
Constructor, creates a Rate.
Definition: rate.cpp:136
ros::WallRate::expected_cycle_time_
WallDuration expected_cycle_time_
Definition: rate.h:161
ros::WallRate::reset
void reset()
Sets the start time for the rate to now.
Definition: rate.cpp:184
ros::WallDuration
Duration representation for use with the WallTime class.
Definition: duration.h:155
ros::WallRate::start_
WallTime start_
Definition: rate.h:160


rostime
Author(s): Josh Faust, Dirk Thomas
autogenerated on Sat Jun 17 2023 02:32:37