Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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 Rate::Rate(const Duration& d)
00049 : start_(Time::now())
00050 , expected_cycle_time_(d.sec, d.nsec)
00051 , actual_cycle_time_(0.0)
00052 { }
00053
00054
00055
00056 bool Rate::sleep()
00057 {
00058 Time expected_end = start_ + expected_cycle_time_;
00059
00060 Time actual_end = Time::now();
00061
00062
00063 if (actual_end < start_)
00064 {
00065 expected_end = actual_end + expected_cycle_time_;
00066 }
00067
00068
00069 Duration sleep_time = expected_end - actual_end;
00070
00071
00072 actual_cycle_time_ = actual_end - start_;
00073
00074
00075 start_ = expected_end;
00076
00077
00078 if(sleep_time <= Duration(0.0))
00079 {
00080
00081
00082 if (actual_end > expected_end + expected_cycle_time_)
00083 {
00084 start_ = actual_end;
00085 }
00086
00087 return false;
00088 }
00089
00090 return sleep_time.sleep();
00091 }
00092
00093 void Rate::reset()
00094 {
00095 start_ = Time::now();
00096 }
00097
00098 Duration Rate::cycleTime() const
00099 {
00100 return actual_cycle_time_;
00101 }
00102
00103 WallRate::WallRate(double frequency)
00104 : start_(WallTime::now())
00105 , expected_cycle_time_(1.0 / frequency)
00106 , actual_cycle_time_(0.0)
00107 {}
00108
00109 WallRate::WallRate(const Duration& d)
00110 : start_(WallTime::now())
00111 , expected_cycle_time_(d.sec, d.nsec)
00112 , actual_cycle_time_(0.0)
00113 {}
00114
00115 bool WallRate::sleep()
00116 {
00117 WallTime expected_end = start_ + expected_cycle_time_;
00118
00119 WallTime actual_end = WallTime::now();
00120
00121
00122 if (actual_end < start_)
00123 {
00124 expected_end = actual_end + expected_cycle_time_;
00125 }
00126
00127
00128 WallDuration sleep_time = expected_end - actual_end;
00129
00130
00131 actual_cycle_time_ = actual_end - start_;
00132
00133
00134 start_ = expected_end;
00135
00136
00137 if(sleep_time <= WallDuration(0.0))
00138 {
00139
00140
00141 if (actual_end > expected_end + expected_cycle_time_)
00142 {
00143 start_ = actual_end;
00144 }
00145 return true;
00146 }
00147
00148 return sleep_time.sleep();
00149 }
00150
00151 void WallRate::reset()
00152 {
00153 start_ = WallTime::now();
00154 }
00155
00156 WallDuration WallRate::cycleTime() const
00157 {
00158 return actual_cycle_time_;
00159 }
00160
00161
00162 }