ros2_control - kilted
Loading...
Searching...
No Matches
hardware_component_interface.hpp
1// Copyright 2025 ros2_control Development Team
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 HARDWARE_INTERFACE__HARDWARE_COMPONENT_INTERFACE_HPP_
16#define HARDWARE_INTERFACE__HARDWARE_COMPONENT_INTERFACE_HPP_
17
18#include <fmt/compile.h>
19
20#include <limits>
21#include <memory>
22#include <string>
23#include <unordered_map>
24#include <utility>
25#include <vector>
26
27#include "control_msgs/msg/hardware_status.hpp"
28#include "hardware_interface/component_parser.hpp"
29#include "hardware_interface/handle.hpp"
30#include "hardware_interface/hardware_info.hpp"
31#include "hardware_interface/introspection.hpp"
32#include "hardware_interface/types/hardware_component_interface_params.hpp"
33#include "hardware_interface/types/hardware_component_params.hpp"
34#include "hardware_interface/types/hardware_interface_return_values.hpp"
35#include "hardware_interface/types/hardware_interface_type_values.hpp"
36#include "hardware_interface/types/lifecycle_state_names.hpp"
37#include "hardware_interface/types/trigger_type.hpp"
38#include "lifecycle_msgs/msg/state.hpp"
39#include "rclcpp/duration.hpp"
40#include "rclcpp/logger.hpp"
41#include "rclcpp/logging.hpp"
42#include "rclcpp/node_interfaces/node_clock_interface.hpp"
43#include "rclcpp/time.hpp"
44#include "rclcpp/version.h"
45#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
46#include "rclcpp_lifecycle/state.hpp"
47#include "realtime_tools/async_function_handler.hpp"
48#include "realtime_tools/realtime_publisher.hpp"
49#include "realtime_tools/realtime_thread_safe_box.hpp"
50
51namespace hardware_interface
52{
53
54using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;
55
64class HardwareComponentInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface
65{
66public:
68
70
75
77
79
82
89 [[deprecated(
90 "Replaced by CallbackReturn init(const hardware_interface::HardwareComponentParams & "
91 "params). Initialization is handled by the Framework.")]] CallbackReturn
92 init(const HardwareInfo & hardware_info, rclcpp::Logger logger, rclcpp::Clock::SharedPtr clock)
93 {
95 params.hardware_info = hardware_info;
96 params.clock = clock;
97 params.logger = logger;
98 return init(params);
99 };
100
103
113 CallbackReturn init(const hardware_interface::HardwareComponentParams & params);
114
116
125 virtual CallbackReturn init_hardware_status_message(
126 control_msgs::msg::HardwareStatus & msg_template);
127
129
137 virtual return_type update_hardware_status_message(control_msgs::msg::HardwareStatus & msg);
138
140
145 [[deprecated("Use on_init(const HardwareComponentInterfaceParams & params) instead.")]]
146 virtual CallbackReturn on_init(const HardwareInfo & hardware_info);
147
149
158 virtual CallbackReturn on_init(
160
162
166 virtual rclcpp::NodeOptions define_custom_node_options() const;
167
169
180 [[deprecated(
181 "Replaced by vector<StateInterface::ConstSharedPtr> on_export_state_interfaces() method. "
182 "Exporting is handled by the Framework.")]] virtual std::vector<StateInterface>
184
191 virtual std::vector<hardware_interface::InterfaceDescription>
193
201 virtual std::vector<StateInterface::ConstSharedPtr> on_export_state_interfaces();
202
204
215 [[deprecated(
216 "Replaced by vector<CommandInterface::SharedPtr> on_export_command_interfaces() method. "
217 "Exporting is handled by the Framework.")]] virtual std::vector<CommandInterface>
219
226 virtual std::vector<hardware_interface::InterfaceDescription>
228
239 virtual std::vector<CommandInterface::SharedPtr> on_export_command_interfaces();
240
242
252 virtual return_type prepare_command_mode_switch(
253 const std::vector<std::string> & start_interfaces,
254 const std::vector<std::string> & stop_interfaces);
255
256 // Perform switching to the new command interface.
266 virtual return_type perform_command_mode_switch(
267 const std::vector<std::string> & start_interfaces,
268 const std::vector<std::string> & stop_interfaces);
269
271
282 const rclcpp::Time & time, const rclcpp::Duration & period);
283
285
294 virtual return_type read(const rclcpp::Time & time, const rclcpp::Duration & period) = 0;
295
297
307 const rclcpp::Time & time, const rclcpp::Duration & period);
308
310
318 virtual return_type write(const rclcpp::Time & time, const rclcpp::Duration & period);
319
321
324 const std::string & get_name() const;
325
327
330 const std::string & get_group_name() const;
331
333
339 const rclcpp_lifecycle::State & get_lifecycle_state() const;
340
342
348 void set_lifecycle_state(const rclcpp_lifecycle::State & new_state);
349
351 uint8_t get_lifecycle_id() const;
352
354
358 virtual bool has_state(const std::string & interface_name) const;
359
361
367 virtual const StateInterface::SharedPtr & get_state_interface_handle(
368 const std::string & interface_name) const;
369
371
381 template <typename T>
383 const StateInterface::SharedPtr & interface_handle, const T & value, bool wait_until_set)
384 {
385 if (!interface_handle)
386 {
387 throw std::runtime_error(
388 fmt::format(
389 "State interface handle is null in hardware component: {}, while calling set_state "
390 "method. This should not happen.",
391 info_.name));
392 }
393 return interface_handle->set_value(value, wait_until_set);
394 }
395
397
405 template <typename T>
406 void set_state(const std::string & interface_name, const T & value)
407 {
408 std::ignore = set_state(get_state_interface_handle(interface_name), value, true);
409 }
410
421 template <typename T>
423 const StateInterface::SharedPtr & interface_handle, T & state, bool wait_until_get) const
424 {
425 if (!interface_handle)
426 {
427 throw std::runtime_error(
428 fmt::format(
429 "State interface handle is null in hardware component: {}, while calling get_state "
430 "method. This should not happen.",
431 info_.name));
432 }
433 const bool success = interface_handle->get_value(state, wait_until_get);
434 if (!success && wait_until_get)
435 {
436 throw std::runtime_error(
437 fmt::format(
438 "Failed to get state value from interface: {} in hardware component: {}. This should "
439 "not happen.",
440 interface_handle->get_name(), info_.name));
441 }
442 return success;
443 }
444
446
454 template <typename T = double>
455 T get_state(const std::string & interface_name) const
456 {
457 T state;
458 get_state<T>(get_state_interface_handle(interface_name), state, true);
459 return state;
460 }
461
463
467 virtual bool has_command(const std::string & interface_name) const;
468
470
476 virtual const CommandInterface::SharedPtr & get_command_interface_handle(
477 const std::string & interface_name) const;
478
480
489 template <typename T>
491 const CommandInterface::SharedPtr & interface_handle, const T & value, bool wait_until_set)
492 {
493 if (!interface_handle)
494 {
495 throw std::runtime_error(
496 fmt::format(
497 "Command interface handle is null in hardware component: {}, while calling set_command "
498 "method. This should not happen.",
499 info_.name));
500 }
501 return interface_handle->set_value(value, wait_until_set);
502 }
503
505
513 template <typename T>
514 void set_command(const std::string & interface_name, const T & value)
515 {
516 std::ignore = set_command(get_command_interface_handle(interface_name), value, true);
517 }
518
529 template <typename T>
531 const CommandInterface::SharedPtr & interface_handle, T & command, bool wait_until_get) const
532 {
533 if (!interface_handle)
534 {
535 throw std::runtime_error(
536 fmt::format(
537 "Command interface handle is null in hardware component: {}, while calling get_command "
538 "method. This should not happen.",
539 info_.name));
540 }
541 const bool success = interface_handle->get_value(command, wait_until_get);
542 if (!success && wait_until_get)
543 {
544 throw std::runtime_error(
545 fmt::format(
546 "Failed to get command value from interface: {} in hardware component: {}. This should "
547 "not happen.",
548 interface_handle->get_name(), info_.name));
549 }
550 return success;
551 }
552
554
562 template <typename T = double>
563 T get_command(const std::string & interface_name) const
564 {
565 T command;
566 get_command<T>(get_command_interface_handle(interface_name), command, true);
567 return command;
568 }
569
571
574 virtual rclcpp::Logger get_logger() const;
575
577
580 virtual rclcpp::Clock::SharedPtr get_clock() const;
581
583
586 virtual rclcpp::Node::SharedPtr get_node() const;
587
589
592 const HardwareInfo & get_hardware_info() const;
593
595
600
602
606
608
611 void enable_introspection(bool enable);
612
613protected:
614 HardwareInfo info_;
615 // interface names to InterfaceDescription
616 std::unordered_map<std::string, InterfaceDescription> joint_state_interfaces_;
617 std::unordered_map<std::string, InterfaceDescription> joint_command_interfaces_;
618
619 std::unordered_map<std::string, InterfaceDescription> sensor_state_interfaces_;
620
621 std::unordered_map<std::string, InterfaceDescription> gpio_state_interfaces_;
622 std::unordered_map<std::string, InterfaceDescription> gpio_command_interfaces_;
623
624 std::unordered_map<std::string, InterfaceDescription> unlisted_state_interfaces_;
625 std::unordered_map<std::string, InterfaceDescription> unlisted_command_interfaces_;
626
627 rclcpp_lifecycle::State lifecycle_state_;
628 std::unique_ptr<realtime_tools::AsyncFunctionHandler<return_type>> async_handler_;
629
630 // Exported Command- and StateInterfaces in order they are listed in the hardware description.
631 std::vector<StateInterface::SharedPtr> joint_states_;
632 std::vector<CommandInterface::SharedPtr> joint_commands_;
633
634 std::vector<StateInterface::SharedPtr> sensor_states_;
635
636 std::vector<StateInterface::SharedPtr> gpio_states_;
637 std::vector<CommandInterface::SharedPtr> gpio_commands_;
638
639 std::vector<StateInterface::SharedPtr> unlisted_states_;
640 std::vector<CommandInterface::SharedPtr> unlisted_commands_;
641
642private:
643 class HardwareComponentInterfaceImpl;
644 std::unique_ptr<HardwareComponentInterfaceImpl> impl_;
645
646protected:
647 pal_statistics::RegistrationsRAII stats_registrations_;
648};
649
650} // namespace hardware_interface
651#endif // HARDWARE_INTERFACE__HARDWARE_COMPONENT_INTERFACE_HPP_
Virtual base class for all hardware components (Actuators, Sensors, and Systems).
Definition hardware_component_interface.hpp:65
const std::string & get_name() const
Get name of the hardware.
Definition hardware_component_interface.cpp:493
void prepare_for_activation()
Prepare for the activation of the hardware.
Definition hardware_component_interface.cpp:571
virtual std::vector< hardware_interface::InterfaceDescription > export_unlisted_state_interface_descriptions()
Definition hardware_component_interface.cpp:301
T get_state(const std::string &interface_name) const
Get the value from a state interface.
Definition hardware_component_interface.hpp:455
bool get_state(const StateInterface::SharedPtr &interface_handle, T &state, bool wait_until_get) const
Definition hardware_component_interface.hpp:422
const rclcpp_lifecycle::State & get_lifecycle_state() const
Get life-cycle state of the hardware.
Definition hardware_component_interface.cpp:497
virtual return_type read(const rclcpp::Time &time, const rclcpp::Duration &period)=0
Read the current state values from the hardware.
virtual return_type write(const rclcpp::Time &time, const rclcpp::Duration &period)
Write the current command values to the hardware.
Definition hardware_component_interface.cpp:487
bool set_command(const CommandInterface::SharedPtr &interface_handle, const T &value, bool wait_until_set)
Set the value of a command interface.
Definition hardware_component_interface.hpp:490
virtual rclcpp::NodeOptions define_custom_node_options() const
Define custom node options for the hardware component interface.
Definition hardware_component_interface.cpp:282
void set_state(const std::string &interface_name, const T &value)
Set the value of a state interface.
Definition hardware_component_interface.hpp:406
HardwareComponentCycleStatus trigger_read(const rclcpp::Time &time, const rclcpp::Duration &period)
Triggers the read method synchronously or asynchronously depending on the HardwareInfo.
Definition hardware_component_interface.cpp:424
virtual return_type update_hardware_status_message(control_msgs::msg::HardwareStatus &msg)
User-overridable method to fill the hardware status message with real-time data.
Definition hardware_component_interface.cpp:241
virtual std::vector< CommandInterface::SharedPtr > on_export_command_interfaces()
Definition hardware_component_interface.cpp:369
virtual CallbackReturn on_init(const HardwareInfo &hardware_info)
Initialization of the hardware interface from data parsed from the robot's URDF.
Definition hardware_component_interface.cpp:248
void set_lifecycle_state(const rclcpp_lifecycle::State &new_state)
Set life-cycle state of the hardware.
Definition hardware_component_interface.cpp:502
virtual bool has_command(const std::string &interface_name) const
Does the command interface exist?
Definition hardware_component_interface.cpp:532
void pause_async_operations()
Pause any asynchronous operations.
Definition hardware_component_interface.cpp:562
const std::string & get_group_name() const
Get name of the hardware group to which it belongs to.
Definition hardware_component_interface.cpp:495
virtual CallbackReturn init_hardware_status_message(control_msgs::msg::HardwareStatus &msg_template)
User-overridable method to configure the structure of the HardwareStatus message.
Definition hardware_component_interface.cpp:234
HardwareComponentCycleStatus trigger_write(const rclcpp::Time &time, const rclcpp::Duration &period)
Triggers the write method synchronously or asynchronously depending on the HardwareInfo.
Definition hardware_component_interface.cpp:461
virtual std::vector< hardware_interface::InterfaceDescription > export_unlisted_command_interface_descriptions()
Definition hardware_component_interface.cpp:363
virtual std::vector< CommandInterface > export_command_interfaces()
Exports all command interfaces for this hardware interface.
Definition hardware_component_interface.cpp:354
bool get_command(const CommandInterface::SharedPtr &interface_handle, T &command, bool wait_until_get) const
Definition hardware_component_interface.hpp:530
virtual const StateInterface::SharedPtr & get_state_interface_handle(const std::string &interface_name) const
Get the state interface handle.
Definition hardware_component_interface.cpp:518
HardwareComponentInterface(const HardwareComponentInterface &other)=delete
HardwareComponentInterface copy constructor is actively deleted.
uint8_t get_lifecycle_id() const
Get the lifecycle id of the hardware component interface.
Definition hardware_component_interface.cpp:508
bool set_state(const StateInterface::SharedPtr &interface_handle, const T &value, bool wait_until_set)
Set the value of a state interface.
Definition hardware_component_interface.hpp:382
void set_command(const std::string &interface_name, const T &value)
Set the value of a command interface.
Definition hardware_component_interface.hpp:514
virtual return_type prepare_command_mode_switch(const std::vector< std::string > &start_interfaces, const std::vector< std::string > &stop_interfaces)
Prepare for a new command interface switch.
Definition hardware_component_interface.cpp:410
virtual rclcpp::Node::SharedPtr get_node() const
Get the default node of the HardwareComponentInterface.
Definition hardware_component_interface.cpp:555
virtual std::vector< StateInterface::ConstSharedPtr > on_export_state_interfaces()
Definition hardware_component_interface.cpp:307
virtual std::vector< StateInterface > export_state_interfaces()
Exports all state interfaces for this hardware interface.
Definition hardware_component_interface.cpp:292
CallbackReturn init(const HardwareInfo &hardware_info, rclcpp::Logger logger, rclcpp::Clock::SharedPtr clock)
Definition hardware_component_interface.hpp:92
void enable_introspection(bool enable)
Enable or disable introspection of the hardware.
Definition hardware_component_interface.cpp:579
virtual const CommandInterface::SharedPtr & get_command_interface_handle(const std::string &interface_name) const
Get the command interface handle.
Definition hardware_component_interface.cpp:537
virtual rclcpp::Logger get_logger() const
Get the logger of the HardwareComponentInterface.
Definition hardware_component_interface.cpp:551
T get_command(const std::string &interface_name) const
Get the value from a command interface.
Definition hardware_component_interface.hpp:563
virtual rclcpp::Clock::SharedPtr get_clock() const
Get the clock.
Definition hardware_component_interface.cpp:553
const HardwareInfo & get_hardware_info() const
Get the hardware info of the HardwareComponentInterface.
Definition hardware_component_interface.cpp:560
virtual return_type perform_command_mode_switch(const std::vector< std::string > &start_interfaces, const std::vector< std::string > &stop_interfaces)
Definition hardware_component_interface.cpp:417
virtual bool has_state(const std::string &interface_name) const
Does the state interface exist?
Definition hardware_component_interface.cpp:513
Definition mujoco_system_interface.hpp:59
Definition hardware_interface_return_values.hpp:41
Parameters required for the initialization of a specific hardware component plugin....
Definition hardware_component_interface_params.hpp:32
Parameters required for the initialization of a specific hardware component plugin....
Definition hardware_component_params.hpp:33
rclcpp::Logger logger
A logger instance taken from resource manager.
Definition hardware_component_params.hpp:45
rclcpp::Clock::SharedPtr clock
Shared pointer to the rclcpp::Clock to be used by this hardware component. Typically,...
Definition hardware_component_params.hpp:51
hardware_interface::HardwareInfo hardware_info
Reference to the HardwareInfo struct for this specific component, parsed from the URDF....
Definition hardware_component_params.hpp:40
This structure stores information about hardware defined in a robot's URDF.
Definition hardware_info.hpp:363
std::string name
Name of the hardware.
Definition hardware_info.hpp:365