ROS 2 rclcpp + rcl - kilted  kilted
ROS 2 C++ Client Library with ROS Client Library
rate.hpp
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__RATE_HPP_
16 #define RCLCPP__RATE_HPP_
17 
18 #include <chrono>
19 #include <memory>
20 #include <thread>
21 
22 #include "rclcpp/clock.hpp"
23 #include "rclcpp/duration.hpp"
24 #include "rclcpp/macros.hpp"
25 #include "rclcpp/utilities.hpp"
26 #include "rclcpp/visibility_control.hpp"
27 
28 namespace rclcpp
29 {
30 
31 class RateBase
32 {
33 public:
34  RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(RateBase)
35 
36  RCLCPP_PUBLIC
37  virtual ~RateBase() {}
38 
39  RCLCPP_PUBLIC
40  virtual bool sleep() = 0;
41 
42  RCLCPP_PUBLIC
43  virtual rcl_clock_type_t get_type() const = 0;
44 
45  RCLCPP_PUBLIC
46  virtual void reset() = 0;
47 };
48 
49 using std::chrono::duration;
50 using std::chrono::duration_cast;
51 using std::chrono::nanoseconds;
52 
53 class Rate : public RateBase
54 {
55 public:
56  RCLCPP_SMART_PTR_DEFINITIONS(Rate)
57 
58  RCLCPP_PUBLIC
59  explicit Rate(
60  const double rate,
61  Clock::SharedPtr clock = std::make_shared<Clock>(RCL_SYSTEM_TIME));
62 
63  RCLCPP_PUBLIC
64  explicit Rate(
65  const Duration & period,
66  Clock::SharedPtr clock = std::make_shared<Clock>(RCL_SYSTEM_TIME));
67 
68  RCLCPP_PUBLIC
69  virtual bool
70  sleep();
71 
72  RCLCPP_PUBLIC
73  virtual rcl_clock_type_t
74  get_type() const;
75 
76  RCLCPP_PUBLIC
77  virtual void
78  reset();
79 
80  RCLCPP_PUBLIC
81  std::chrono::nanoseconds
82  period() const;
83 
84 private:
85  RCLCPP_DISABLE_COPY(Rate)
86 
87  Clock::SharedPtr clock_;
88  Duration period_;
89  Time last_interval_;
90 };
91 
92 class WallRate : public Rate
93 {
94 public:
95  RCLCPP_PUBLIC
96  explicit WallRate(const double rate);
97 
98  RCLCPP_PUBLIC
99  explicit WallRate(const Duration & period);
100 };
101 
102 } // namespace rclcpp
103 
104 #endif // RCLCPP__RATE_HPP_
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
enum rcl_clock_type_e rcl_clock_type_t
Time source type, used to indicate the source of a time measurement.
@ RCL_SYSTEM_TIME
Use system time.
Definition: time.h:68