rclcpp  master
C++ ROS Client Library API
timer.hpp
Go to the documentation of this file.
1 // Copyright 2014 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RCLCPP__TIMER_HPP_
16 #define RCLCPP__TIMER_HPP_
17 
18 #include <chrono>
19 #include <functional>
20 #include <memory>
21 #include <sstream>
22 #include <thread>
23 #include <type_traits>
24 #include <utility>
25 
27 #include "rclcpp/macros.hpp"
28 #include "rclcpp/rate.hpp"
29 #include "rclcpp/utilities.hpp"
31 
32 #include "rcl/error_handling.h"
33 #include "rcl/timer.h"
34 
35 #include "rmw/error_handling.h"
36 #include "rmw/rmw.h"
37 
38 namespace rclcpp
39 {
40 namespace timer
41 {
42 
43 class TimerBase
44 {
45 public:
47 
49  explicit TimerBase(std::chrono::nanoseconds period);
50 
52  ~TimerBase();
53 
55  void
56  cancel();
57 
59  void
60  reset();
61 
63  virtual void
64  execute_callback() = 0;
65 
67  const rcl_timer_t *
69 
71 
73  std::chrono::nanoseconds
75 
77 
78  virtual bool is_steady() = 0;
79 
81 
87  bool is_ready();
88 
89 protected:
91 };
92 
93 
94 using VoidCallbackType = std::function<void()>;
95 using TimerCallbackType = std::function<void(TimerBase &)>;
96 
98 template<
99  typename FunctorT,
100  class Clock,
101  typename std::enable_if<
104  Clock::is_steady
105  >::type * = nullptr
106 >
107 class GenericTimer : public TimerBase
108 {
109 public:
111 
112 
113 
117  GenericTimer(std::chrono::nanoseconds period, FunctorT && callback)
118  : TimerBase(period), callback_(std::forward<FunctorT>(callback))
119  {
120  }
121 
123  virtual ~GenericTimer()
124  {
125  // Stop the timer from running.
126  cancel();
128  fprintf(stderr, "Failed to clean up rcl timer handle: %s\n", rcl_get_error_string_safe());
129  }
130  }
131 
132  void
134  {
136  if (ret == RCL_RET_TIMER_CANCELED) {
137  return;
138  }
139  if (ret != RCL_RET_OK) {
140  throw std::runtime_error("Failed to notify timer that callback occurred");
141  }
142  execute_callback_delegate<>();
143  }
144 
145  // void specialization
146  template<
147  typename CallbackT = FunctorT,
148  typename std::enable_if<
150  >::type * = nullptr
151  >
152  void
154  {
155  callback_();
156  }
157 
158  template<
159  typename CallbackT = FunctorT,
160  typename std::enable_if<
162  >::type * = nullptr
163  >
164  void
166  {
167  callback_(*this);
168  }
169 
170  virtual bool
172  {
173  return Clock::is_steady;
174  }
175 
176 protected:
178 
179  FunctorT callback_;
180 };
181 
182 template<typename CallbackType>
183 using WallTimer = GenericTimer<CallbackType, std::chrono::steady_clock>;
184 
185 } // namespace timer
186 } // namespace rclcpp
187 
188 #endif // RCLCPP__TIMER_HPP_
TimerBase(std::chrono::nanoseconds period)
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
virtual ~GenericTimer()
Default destructor.
Definition: timer.hpp:123
rcl_timer_t timer_handle_
Definition: timer.hpp:90
rmw_ret_t rcl_ret_t
void execute_callback()
Definition: timer.hpp:133
#define RCL_RET_TIMER_CANCELED
Definition: timer.hpp:43
std::function< void()> VoidCallbackType
Definition: timer.hpp:94
Definition: allocator_common.hpp:24
rcl_ret_t rcl_timer_fini(rcl_timer_t *timer)
Definition: parameter.hpp:235
rcl_timer_t rcl_get_zero_initialized_timer(void)
const rcl_timer_t * get_timer_handle()
rcl_ret_t rcl_timer_call(rcl_timer_t *timer)
#define RCL_RET_OK
void execute_callback_delegate()
Definition: timer.hpp:153
virtual bool is_steady()
Is the clock steady (i.e. is the time between ticks constant?)
Definition: timer.hpp:171
#define RCLCPP_SMART_PTR_DEFINITIONS(...)
Definition: macros.hpp:36
std::function< void(TimerBase &)> TimerCallbackType
Definition: timer.hpp:95
Definition: function_traits.hpp:141
#define rcl_get_error_string_safe
#define RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(...)
Definition: macros.hpp:51
virtual void execute_callback()=0
std::chrono::nanoseconds time_until_trigger()
Check how long the timer has until its next scheduled callback.
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
Generic timer templated on the clock type. Periodically executes a user-specified callback...
Definition: timer.hpp:107
bool is_ready()
Check if the timer is ready to trigger the callback.
virtual bool is_steady()=0
Is the clock steady (i.e. is the time between ticks constant?)