15 #ifndef HARDWARE_INTERFACE__ASYNC_COMPONENTS_HPP_
16 #define HARDWARE_INTERFACE__ASYNC_COMPONENTS_HPP_
22 #include "hardware_interface/actuator.hpp"
23 #include "hardware_interface/sensor.hpp"
24 #include "hardware_interface/system.hpp"
25 #include "lifecycle_msgs/msg/state.hpp"
26 #include "rclcpp/time.hpp"
35 unsigned int update_rate,
36 rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface)
37 : cm_update_rate_(update_rate), clock_interface_(clock_interface)
43 void register_component(T * component)
45 hardware_component_ = component;
54 terminated_.store(
true, std::memory_order_seq_cst);
55 if (write_and_read_.joinable())
57 write_and_read_.join();
75 using TimePoint = std::chrono::system_clock::time_point;
78 [
this](
auto & component)
80 auto previous_time = clock_interface_->get_clock()->now();
81 while (!terminated_.load(std::memory_order_relaxed))
83 auto const period = std::chrono::nanoseconds(1'000'000'000 / cm_update_rate_);
84 TimePoint next_iteration_time =
85 TimePoint(std::chrono::nanoseconds(clock_interface_->get_clock()->now().nanoseconds()));
88 component->get_lifecycle_state().id() ==
89 lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE)
91 auto current_time = clock_interface_->get_clock()->now();
92 auto measured_period = current_time - previous_time;
93 previous_time = current_time;
97 component->write(clock_interface_->get_clock()->now(), measured_period);
99 component->read(clock_interface_->get_clock()->now(), measured_period);
100 first_iteration = false;
102 next_iteration_time += period;
103 std::this_thread::sleep_until(next_iteration_time);
106 hardware_component_);
110 std::atomic<bool> terminated_{
false};
111 std::variant<Actuator *, System *, Sensor *> hardware_component_;
112 std::thread write_and_read_{};
114 unsigned int cm_update_rate_;
115 bool first_iteration =
true;
116 rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface_;
Definition: async_components.hpp:32
void write_and_read()
Periodically execute the component's write and read methods.
Definition: async_components.hpp:73
void activate()
Creates the component's thread.
Definition: async_components.hpp:65
Definition: actuator.hpp:34