|
24 | 24 | #include <utility> |
25 | 25 | #include <vector> |
26 | 26 |
|
| 27 | +#include "control_msgs/msg/hardware_status.hpp" |
27 | 28 | #include "hardware_interface/component_parser.hpp" |
28 | 29 | #include "hardware_interface/handle.hpp" |
29 | 30 | #include "hardware_interface/hardware_info.hpp" |
|
44 | 45 | #include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp" |
45 | 46 | #include "rclcpp_lifecycle/state.hpp" |
46 | 47 | #include "realtime_tools/async_function_handler.hpp" |
| 48 | +#include "realtime_tools/realtime_publisher.hpp" |
| 49 | +#include "realtime_tools/realtime_thread_safe_box.hpp" |
47 | 50 |
|
48 | 51 | namespace hardware_interface |
49 | 52 | { |
@@ -101,9 +104,8 @@ class HardwareComponentInterface : public rclcpp_lifecycle::node_interfaces::Lif |
101 | 104 | */ |
102 | 105 | [[deprecated( |
103 | 106 | "Replaced by CallbackReturn init(const hardware_interface::HardwareComponentParams & " |
104 | | - "params). Initialization is handled by the Framework.")]] |
105 | | - CallbackReturn init( |
106 | | - const HardwareInfo & hardware_info, rclcpp::Logger logger, rclcpp::Clock::SharedPtr clock) |
| 107 | + "params). Initialization is handled by the Framework.")]] CallbackReturn |
| 108 | + init(const HardwareInfo & hardware_info, rclcpp::Logger logger, rclcpp::Clock::SharedPtr clock) |
107 | 109 | { |
108 | 110 | hardware_interface::HardwareComponentParams params; |
109 | 111 | params.hardware_info = hardware_info; |
@@ -202,20 +204,145 @@ class HardwareComponentInterface : public rclcpp_lifecycle::node_interfaces::Lif |
202 | 204 | params.hardware_info.name.c_str()); |
203 | 205 | } |
204 | 206 |
|
| 207 | + double publish_rate = 0.0; |
| 208 | + auto it = info_.hardware_parameters.find("status_publish_rate"); |
| 209 | + if (it != info_.hardware_parameters.end()) |
| 210 | + { |
| 211 | + try |
| 212 | + { |
| 213 | + publish_rate = hardware_interface::stod(it->second); |
| 214 | + } |
| 215 | + catch (const std::invalid_argument &) |
| 216 | + { |
| 217 | + RCLCPP_WARN( |
| 218 | + get_logger(), "Invalid 'status_publish_rate' parameter. Using default %.1f Hz.", |
| 219 | + publish_rate); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + if (publish_rate == 0.0) |
| 224 | + { |
| 225 | + RCLCPP_INFO( |
| 226 | + get_logger(), |
| 227 | + "`status_publish_rate` is set to 0.0, hardware status publisher will not be created."); |
| 228 | + } |
| 229 | + else |
| 230 | + { |
| 231 | + control_msgs::msg::HardwareStatus status_msg_template; |
| 232 | + if (init_hardware_status_message(status_msg_template) != CallbackReturn::SUCCESS) |
| 233 | + { |
| 234 | + RCLCPP_ERROR(get_logger(), "User-defined 'init_hardware_status_message' failed."); |
| 235 | + return CallbackReturn::ERROR; |
| 236 | + } |
| 237 | + |
| 238 | + if (!status_msg_template.hardware_device_states.empty()) |
| 239 | + { |
| 240 | + if (!hardware_component_node_) |
| 241 | + { |
| 242 | + RCLCPP_WARN( |
| 243 | + get_logger(), |
| 244 | + "Hardware status message was configured, but no node is available for the publisher. " |
| 245 | + "Publisher will not be created."); |
| 246 | + } |
| 247 | + else |
| 248 | + { |
| 249 | + try |
| 250 | + { |
| 251 | + hardware_status_publisher_ = |
| 252 | + hardware_component_node_->create_publisher<control_msgs::msg::HardwareStatus>( |
| 253 | + "~/hardware_status", rclcpp::SystemDefaultsQoS()); |
| 254 | + |
| 255 | + hardware_status_timer_ = hardware_component_node_->create_wall_timer( |
| 256 | + std::chrono::duration<double>(1.0 / publish_rate), |
| 257 | + [this]() |
| 258 | + { |
| 259 | + std::optional<control_msgs::msg::HardwareStatus> msg_to_publish_opt; |
| 260 | + hardware_status_box_.get(msg_to_publish_opt); |
| 261 | + |
| 262 | + if (msg_to_publish_opt.has_value() && hardware_status_publisher_) |
| 263 | + { |
| 264 | + control_msgs::msg::HardwareStatus & msg = msg_to_publish_opt.value(); |
| 265 | + if (update_hardware_status_message(msg) != return_type::OK) |
| 266 | + { |
| 267 | + RCLCPP_WARN_THROTTLE( |
| 268 | + get_logger(), *clock_, 1000, |
| 269 | + "User's update_hardware_status_message() failed for '%s'.", |
| 270 | + info_.name.c_str()); |
| 271 | + return; |
| 272 | + } |
| 273 | + msg.header.stamp = this->get_clock()->now(); |
| 274 | + hardware_status_publisher_->publish(msg); |
| 275 | + } |
| 276 | + }); |
| 277 | + hardware_status_box_.set(std::make_optional(status_msg_template)); |
| 278 | + } |
| 279 | + catch (const std::exception & e) |
| 280 | + { |
| 281 | + RCLCPP_ERROR( |
| 282 | + get_logger(), "Exception during publisher/timer setup for hardware status: %s", |
| 283 | + e.what()); |
| 284 | + return CallbackReturn::ERROR; |
| 285 | + } |
| 286 | + } |
| 287 | + } |
| 288 | + else |
| 289 | + { |
| 290 | + RCLCPP_WARN( |
| 291 | + get_logger(), |
| 292 | + "`status_publish_rate` was set to a non-zero value, but no hardware status message was " |
| 293 | + "configured. Publisher will not be created. Are you sure " |
| 294 | + "init_hardware_status_message() is set up properly?"); |
| 295 | + } |
| 296 | + } |
| 297 | + |
205 | 298 | hardware_interface::HardwareComponentInterfaceParams interface_params; |
206 | 299 | interface_params.hardware_info = info_; |
207 | 300 | interface_params.executor = params.executor; |
208 | 301 | return on_init(interface_params); |
209 | 302 | }; |
210 | 303 |
|
| 304 | + /// User-overridable method to configure the structure of the HardwareStatus message. |
| 305 | + /** |
| 306 | + * To enable status publishing, override this method to pre-allocate the message structure |
| 307 | + * and fill in static information like device IDs and interface names. This method is called |
| 308 | + * once during the non-realtime `init()` phase. If the `hardware_device_states` vector is |
| 309 | + * left empty, publishing will be disabled. |
| 310 | + * |
| 311 | + * \param[out] msg_template A reference to a HardwareStatus message to be configured. |
| 312 | + * \returns CallbackReturn::SUCCESS if configured successfully, CallbackReturn::ERROR on failure. |
| 313 | + */ |
| 314 | + virtual CallbackReturn init_hardware_status_message( |
| 315 | + control_msgs::msg::HardwareStatus & /*msg_template*/) |
| 316 | + { |
| 317 | + // Default implementation does nothing, disabling the feature. |
| 318 | + return CallbackReturn::SUCCESS; |
| 319 | + } |
| 320 | + |
| 321 | + /// User-overridable method to fill the hardware status message with real-time data. |
| 322 | + /** |
| 323 | + * This real-time safe method is called by the framework within the `trigger_read()` loop. |
| 324 | + * Override this method to populate the `value` fields of the pre-allocated message with the |
| 325 | + * latest hardware states that were updated in your `read()` method. |
| 326 | + * |
| 327 | + * \param[in,out] msg The pre-allocated message to be filled with the latest values. |
| 328 | + * \returns return_type::OK on success, return_type::ERROR on failure. |
| 329 | + */ |
| 330 | + virtual return_type update_hardware_status_message(control_msgs::msg::HardwareStatus & /*msg*/) |
| 331 | + { |
| 332 | + // Default implementation does nothing. |
| 333 | + return return_type::OK; |
| 334 | + } |
| 335 | + |
211 | 336 | /// Initialization of the hardware interface from data parsed from the robot's URDF. |
212 | 337 | /** |
213 | 338 | * \param[in] hardware_info structure with data from URDF. |
214 | 339 | * \returns CallbackReturn::SUCCESS if required data are provided and can be parsed. |
215 | 340 | * \returns CallbackReturn::ERROR if any error happens or data are missing. |
216 | 341 | */ |
217 | | - [[deprecated("Use on_init(const HardwareComponentInterfaceParams & params) instead.")]] |
218 | | - virtual CallbackReturn on_init(const HardwareInfo & hardware_info) |
| 342 | + [[deprecated( |
| 343 | + "Use on_init(const HardwareComponentInterfaceParams & params) " |
| 344 | + "instead.")]] virtual CallbackReturn |
| 345 | + on_init(const HardwareInfo & hardware_info) |
219 | 346 | { |
220 | 347 | info_ = hardware_info; |
221 | 348 | if (info_.type == "actuator") |
@@ -273,8 +400,8 @@ class HardwareComponentInterface : public rclcpp_lifecycle::node_interfaces::Lif |
273 | 400 | */ |
274 | 401 | [[deprecated( |
275 | 402 | "Replaced by vector<StateInterface::ConstSharedPtr> on_export_state_interfaces() method. " |
276 | | - "Exporting is handled by the Framework.")]] |
277 | | - virtual std::vector<StateInterface> export_state_interfaces() |
| 403 | + "Exporting is handled by the Framework.")]] virtual std::vector<StateInterface> |
| 404 | + export_state_interfaces() |
278 | 405 | { |
279 | 406 | // return empty vector by default. For backward compatibility we try calling |
280 | 407 | // export_state_interfaces() and only when empty vector is returned call |
@@ -363,8 +490,8 @@ class HardwareComponentInterface : public rclcpp_lifecycle::node_interfaces::Lif |
363 | 490 | */ |
364 | 491 | [[deprecated( |
365 | 492 | "Replaced by vector<CommandInterface::SharedPtr> on_export_command_interfaces() method. " |
366 | | - "Exporting is handled by the Framework.")]] |
367 | | - virtual std::vector<CommandInterface> export_command_interfaces() |
| 493 | + "Exporting is handled by the Framework.")]] virtual std::vector<CommandInterface> |
| 494 | + export_command_interfaces() |
368 | 495 | { |
369 | 496 | // return empty vector by default. For backward compatibility we try calling |
370 | 497 | // export_command_interfaces() and only when empty vector is returned call |
@@ -837,6 +964,10 @@ class HardwareComponentInterface : public rclcpp_lifecycle::node_interfaces::Lif |
837 | 964 |
|
838 | 965 | protected: |
839 | 966 | pal_statistics::RegistrationsRAII stats_registrations_; |
| 967 | + std::shared_ptr<rclcpp::Publisher<control_msgs::msg::HardwareStatus>> hardware_status_publisher_; |
| 968 | + realtime_tools::RealtimeThreadSafeBox<std::optional<control_msgs::msg::HardwareStatus>> |
| 969 | + hardware_status_box_; |
| 970 | + rclcpp::TimerBase::SharedPtr hardware_status_timer_; |
840 | 971 | }; |
841 | 972 |
|
842 | 973 | } // namespace hardware_interface |
|
0 commit comments