rclcpp  master
C++ ROS Client Library API
time_source.hpp
Go to the documentation of this file.
1 // Copyright 2017 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__TIME_SOURCE_HPP_
16 #define RCLCPP__TIME_SOURCE_HPP_
17 
18 #include <memory>
19 #include <vector>
20 
21 #include "rcl/time.h"
22 
23 #include "builtin_interfaces/msg/time.hpp"
24 #include "rosgraph_msgs/msg/clock.hpp"
25 #include "rcl_interfaces/msg/parameter_event.hpp"
26 
27 #include "rclcpp/node.hpp"
28 #include "rclcpp/executors.hpp"
30 
31 
32 namespace rclcpp
33 {
34 class Clock;
35 
51 {
52 public:
54 
61  explicit TimeSource(
62  rclcpp::Node::SharedPtr node,
63  const rclcpp::QoS & qos = rclcpp::ClockQoS(),
64  bool use_clock_thread = true);
65 
67 
73  explicit TimeSource(
74  const rclcpp::QoS & qos = rclcpp::ClockQoS(),
75  bool use_clock_thread = true);
76 
78 
82  void attachNode(rclcpp::Node::SharedPtr node);
83 
85 
98  void attachNode(
99  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_interface,
100  rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics_interface,
101  rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph_interface,
102  rclcpp::node_interfaces::NodeServicesInterface::SharedPtr node_services_interface,
103  rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_interface,
104  rclcpp::node_interfaces::NodeClockInterface::SharedPtr node_clock_interface,
105  rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters_interface);
106 
109  void detachNode();
110 
112 
117  void attachClock(rclcpp::Clock::SharedPtr clock);
118 
121  void detachClock(rclcpp::Clock::SharedPtr clock);
122 
125  ~TimeSource();
126 
127 protected:
128  // Dedicated thread for clock subscription.
131 
132 private:
133  // Preserve the node reference
134  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_;
135  rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics_;
136  rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph_;
137  rclcpp::node_interfaces::NodeServicesInterface::SharedPtr node_services_;
138  rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_;
139  rclcpp::node_interfaces::NodeClockInterface::SharedPtr node_clock_;
140  rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters_;
141 
142  // Store (and update on node attach) logger for logging.
143  Logger logger_;
144 
145  // QoS of the clock subscription.
146  rclcpp::QoS qos_;
147 
148  // The subscription for the clock callback
149  using MessageT = rosgraph_msgs::msg::Clock;
150  using Alloc = std::allocator<void>;
152  std::shared_ptr<SubscriptionT> clock_subscription_{nullptr};
153  std::mutex clock_sub_lock_;
154  rclcpp::CallbackGroup::SharedPtr clock_callback_group_;
155  rclcpp::executors::SingleThreadedExecutor::SharedPtr clock_executor_;
156  std::promise<void> cancel_clock_executor_promise_;
157 
158  // The clock callback itself
160 
161  // Create the subscription for the clock topic
162  void create_clock_sub();
163 
164  // Destroy the subscription for the clock topic
165  void destroy_clock_sub();
166 
167  // Parameter Event subscription
168  using ParamMessageT = rcl_interfaces::msg::ParameterEvent;
169  using ParamSubscriptionT = rclcpp::Subscription<ParamMessageT, Alloc>;
170  std::shared_ptr<ParamSubscriptionT> parameter_subscription_;
171 
172  // Callback for parameter updates
173  void on_parameter_event(std::shared_ptr<const rcl_interfaces::msg::ParameterEvent> event);
174 
175  // An enum to hold the parameter state
176  enum UseSimTimeParameterState {UNSET, SET_TRUE, SET_FALSE};
177  UseSimTimeParameterState parameter_state_;
178 
179  // An internal method to use in the clock callback that iterates and enables all clocks
180  void enable_ros_time();
181  // An internal method to use in the clock callback that iterates and disables all clocks
182  void disable_ros_time();
183 
184  // Internal helper functions used inside iterators
185  static void set_clock(
186  const builtin_interfaces::msg::Time::SharedPtr msg,
187  bool set_ros_time_enabled,
188  rclcpp::Clock::SharedPtr clock);
189 
190  // Local storage of validity of ROS time
191  // This is needed when new clocks are added.
192  bool ros_time_active_{false};
193  // Last set message to be passed to newly registered clocks
195 
196  // A lock to protect iterating the associated_clocks_ field.
197  std::mutex clock_list_lock_;
198  // A vector to store references to associated clocks.
199  std::vector<rclcpp::Clock::SharedPtr> associated_clocks_;
200  // A handler for the use_sim_time parameter callback.
201  rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr sim_time_cb_handler_{nullptr};
202 };
203 
204 } // namespace rclcpp
205 
206 #endif // RCLCPP__TIME_SOURCE_HPP_
rclcpp::TimeSource::attachClock
void attachClock(rclcpp::Clock::SharedPtr clock)
Attach a clock to the time source to be updated.
std::shared_ptr
rclcpp::ClockQoS
Definition: qos.hpp:351
std::vector< rclcpp::Clock::SharedPtr >
node.hpp
time.h
std::promise< void >
rclcpp::TimeSource::TimeSource
TimeSource(rclcpp::Node::SharedPtr node, const rclcpp::QoS &qos=rclcpp::ClockQoS(), bool use_clock_thread=true)
Constructor.
executors.hpp
rclcpp::TimeSource
Definition: time_source.hpp:50
rclcpp
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
rclcpp::Subscription
Subscription implementation, templated on the type of message this subscription receives.
Definition: subscription.hpp:69
RCLCPP_PUBLIC
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
rclcpp::TimeSource::use_clock_thread_
bool use_clock_thread_
Definition: time_source.hpp:129
rclcpp::QoS
Encapsulation of Quality of Service settings.
Definition: qos.hpp:110
std::thread
rclcpp::Logger
Definition: logger.hpp:91
rclcpp::TimeSource::detachClock
void detachClock(rclcpp::Clock::SharedPtr clock)
Detach a clock to the time source.
rclcpp::TimeSource::~TimeSource
~TimeSource()
TimeSource Destructor.
rclcpp::TimeSource::clock_executor_thread_
std::thread clock_executor_thread_
Definition: time_source.hpp:130
rclcpp::TimeSource::attachNode
void attachNode(rclcpp::Node::SharedPtr node)
Attack node to the time source.
rclcpp::TimeSource::detachNode
void detachNode()
Detach the node from the time source.
std::allocator
std::mutex
node_parameters_interface.hpp