timer_impl.h
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2015, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL SOUTHWEST RESEARCH INSTITUTE BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
29 #ifndef SWRI_ROSCPP_H_
30 #define SWRI_ROSCPP_H_
31 
32 namespace swri
33 {
34 class Timer;
35 class TimerImpl
36 {
37  protected:
40 
41  int ticks_;
42 
45 
49 
53 
54  void tickBegin()
55  {
56  tick_begin_wall_ = ros::WallTime::now();
57 
58  ros::Time now = ros::Time::now();
59  if (ticks_ > 0) {
60  ros::Duration period = now - tick_begin_normal_;
61  total_periods_ += period;
62 
63  if (ticks_ == 1) {
64  min_period_ = period;
65  max_period_ = period;
66  } else {
67  min_period_ = std::min(min_period_, period);
68  max_period_ = std::max(max_period_, period);
69  }
70  }
71  tick_begin_normal_ = now;
72  }
73 
74  void tickEnd()
75  {
76  ros::WallTime end_time_ = ros::WallTime::now();
77  ros::WallDuration duration = end_time_ - tick_begin_wall_;
78  total_durations_ += duration;
79  if (ticks_ == 0) {
80  min_duration_ = duration;
81  max_duration_ = duration;
82  } else {
83  min_duration_ = std::min(min_duration_, duration);
84  max_duration_ = std::max(max_duration_, duration);
85  }
86  ticks_++;
87  }
88 
89  public:
91  {
93  }
94 
96  {
97  return desired_period_;
98  }
99 
101  {
102  ticks_ = 0;
103  }
104 
105  // The number of times the timer callback has been called.
106  size_t ticks() const
107  {
108  return ticks_;
109  }
110 
111  double meanFrequencyHz() const
112  {
113  if (ticks_ < 2) {
114  return 0.0;
115  } else {
116  return 1e9 / meanPeriod().toNSec();
117  }
118  }
119 
121  {
122  if (ticks_ < 2) {
123  return ros::DURATION_MAX;
124  } else {
125  return ros::Duration(total_periods_.toSec() / (ticks_ - 1));
126  }
127  }
128 
130  {
131  if (ticks_ < 2) {
132  return ros::DURATION_MAX;
133  } else {
134  return min_period_;
135  }
136  }
137 
139  {
140  if (ticks_ < 2) {
141  return ros::DURATION_MAX;
142  } else {
143  return max_period_;
144  }
145  }
146 
148  {
149  if (ticks_ == 0) {
150  return ros::WallDuration(0.0);
151  } else {
152  return ros::WallDuration(total_durations_.toSec() / ticks_);
153  }
154  }
155 
157  {
158  if (ticks_ == 0) {
159  return ros::WallDuration(0.0);
160  } else {
161  return min_duration_;
162  }
163  }
164 
166  {
167  if (ticks_ == 0) {
168  return ros::WallDuration(0.0);
169  } else {
170  return max_duration_;
171  }
172  }
173 }; // class TimerImpl
174 
175 template<class T>
176 class TypedTimerImpl : public TimerImpl
177 {
178  T *obj_;
179  void (T::*callback_)(const ros::TimerEvent &);
180 
181  public:
183  ros::NodeHandle &nh,
184  ros::Duration period,
185  void(T::*callback)(const ros::TimerEvent&),
186  T *obj)
187  {
188  callback_ = callback;
189  obj_ = obj;
190 
191  desired_period_ = period;
192  timer_ = nh.createTimer(period,
194  this);
195  }
196 
197  void handleTimer(const ros::TimerEvent &event)
198  {
199  tickBegin();
200  (obj_->*callback_)(event);
201  tickEnd();
202  }
203 }; // class TypedTimerImpl
204 } // namespace swri
205 #endif // SWRI_ROSCPP_H_
ros::WallDuration maxDuration() const
Definition: timer_impl.h:165
double meanFrequencyHz() const
Definition: timer_impl.h:111
ros::Duration maxPeriod() const
Definition: timer_impl.h:138
ros::WallDuration total_durations_
Definition: timer_impl.h:50
void handleTimer(const ros::TimerEvent &event)
Definition: timer_impl.h:197
void resetStatistics()
Definition: timer_impl.h:100
ros::WallDuration minDuration() const
Definition: timer_impl.h:156
ros::Duration desiredPeriod() const
Definition: timer_impl.h:95
ros::WallDuration max_duration_
Definition: timer_impl.h:52
ros::Duration max_period_
Definition: timer_impl.h:48
int64_t toNSec() const
ros::Duration desired_period_
Definition: timer_impl.h:39
ros::WallDuration min_duration_
Definition: timer_impl.h:51
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
static WallTime now()
void tickEnd()
Definition: timer_impl.h:74
ros::Timer timer_
Definition: timer_impl.h:38
void tickBegin()
Definition: timer_impl.h:54
static Time now()
ros::Duration minPeriod() const
Definition: timer_impl.h:129
ros::WallDuration meanDuration() const
Definition: timer_impl.h:147
ros::Duration total_periods_
Definition: timer_impl.h:46
ros::Time tick_begin_normal_
Definition: timer_impl.h:44
ROSTIME_DECL const Duration DURATION_MAX
size_t ticks() const
Definition: timer_impl.h:106
TypedTimerImpl(ros::NodeHandle &nh, ros::Duration period, void(T::*callback)(const ros::TimerEvent &), T *obj)
Definition: timer_impl.h:182
ros::Duration min_period_
Definition: timer_impl.h:47
ros::WallTime tick_begin_wall_
Definition: timer_impl.h:43
ros::Duration meanPeriod() const
Definition: timer_impl.h:120


swri_roscpp
Author(s):
autogenerated on Fri Jun 7 2019 22:05:50