ros2_control - jazzy
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 "hardware_interface/component_parser.hpp"
28#include "hardware_interface/handle.hpp"
29#include "hardware_interface/hardware_info.hpp"
30#include "hardware_interface/introspection.hpp"
31#include "hardware_interface/types/hardware_component_interface_params.hpp"
32#include "hardware_interface/types/hardware_component_params.hpp"
33#include "hardware_interface/types/hardware_interface_return_values.hpp"
34#include "hardware_interface/types/hardware_interface_type_values.hpp"
35#include "hardware_interface/types/lifecycle_state_names.hpp"
36#include "hardware_interface/types/trigger_type.hpp"
37#include "lifecycle_msgs/msg/state.hpp"
38#include "rclcpp/duration.hpp"
39#include "rclcpp/logger.hpp"
40#include "rclcpp/logging.hpp"
41#include "rclcpp/node_interfaces/node_clock_interface.hpp"
42#include "rclcpp/time.hpp"
43#include "rclcpp/version.h"
44#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
45#include "rclcpp_lifecycle/state.hpp"
46#include "realtime_tools/async_function_handler.hpp"
47
48namespace hardware_interface
49{
50
51using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;
52
61class HardwareComponentInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface
62{
63public:
65
67
72
74
76
79
86 [[deprecated("Use init(HardwareInfo, rclcpp::Logger, rclcpp::Clock::SharedPtr) instead.")]]
87 CallbackReturn init(
88 const HardwareInfo & hardware_info, rclcpp::Logger logger,
89 rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface)
90 {
91#pragma GCC diagnostic push
92#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
93 return this->init(hardware_info, logger, clock_interface->get_clock());
94#pragma GCC diagnostic pop
95 }
96
99
106 [[deprecated(
107 "Replaced by CallbackReturn init(const hardware_interface::HardwareComponentParams & "
108 "params). Initialization is handled by the Framework.")]]
109 CallbackReturn init(
110 const HardwareInfo & hardware_info, rclcpp::Logger logger, rclcpp::Clock::SharedPtr clock)
111 {
113 params.hardware_info = hardware_info;
114 params.clock = clock;
115 params.logger = logger;
116 return init(params);
117 };
118
121
131 CallbackReturn init(const hardware_interface::HardwareComponentParams & params);
132
134
139 [[deprecated("Use on_init(const HardwareComponentInterfaceParams & params) instead.")]]
140 virtual CallbackReturn on_init(const HardwareInfo & hardware_info);
141
143
152 virtual CallbackReturn on_init(
154
156
160 virtual rclcpp::NodeOptions define_custom_node_options() const;
161
163
174 [[deprecated(
175 "Replaced by vector<StateInterface::ConstSharedPtr> on_export_state_interfaces() method. "
176 "Exporting is handled by the Framework.")]] virtual std::vector<StateInterface>
178
185 virtual std::vector<hardware_interface::InterfaceDescription>
187
195 virtual std::vector<StateInterface::ConstSharedPtr> on_export_state_interfaces();
196
198
209 [[deprecated(
210 "Replaced by vector<CommandInterface::SharedPtr> on_export_command_interfaces() method. "
211 "Exporting is handled by the Framework.")]] virtual std::vector<CommandInterface>
213
220 virtual std::vector<hardware_interface::InterfaceDescription>
222
233 virtual std::vector<CommandInterface::SharedPtr> on_export_command_interfaces();
234
236
246 virtual return_type prepare_command_mode_switch(
247 const std::vector<std::string> & start_interfaces,
248 const std::vector<std::string> & stop_interfaces);
249
250 // Perform switching to the new command interface.
260 virtual return_type perform_command_mode_switch(
261 const std::vector<std::string> & start_interfaces,
262 const std::vector<std::string> & stop_interfaces);
263
265
276 const rclcpp::Time & time, const rclcpp::Duration & period);
277
279
288 virtual return_type read(const rclcpp::Time & time, const rclcpp::Duration & period) = 0;
289
291
301 const rclcpp::Time & time, const rclcpp::Duration & period);
302
304
312 virtual return_type write(const rclcpp::Time & time, const rclcpp::Duration & period);
313
315
318 const std::string & get_name() const;
319
321
324 const std::string & get_group_name() const;
325
327
333 const rclcpp_lifecycle::State & get_lifecycle_state() const;
334
336
342 void set_lifecycle_state(const rclcpp_lifecycle::State & new_state);
343
345 uint8_t get_lifecycle_id() const;
346
348
352 virtual bool has_state(const std::string & interface_name) const;
353
355
361 virtual const StateInterface::SharedPtr & get_state_interface_handle(
362 const std::string & interface_name) const;
363
365
375 template <typename T>
377 const StateInterface::SharedPtr & interface_handle, const T & value, bool wait_until_set)
378 {
379 if (!interface_handle)
380 {
381 throw std::runtime_error(
382 fmt::format(
383 "State interface handle is null in hardware component: {}, while calling set_state "
384 "method. This should not happen.",
385 info_.name));
386 }
387 return interface_handle->set_value(value, wait_until_set);
388 }
389
391
399 template <typename T>
400 void set_state(const std::string & interface_name, const T & value)
401 {
402 std::ignore = set_state(get_state_interface_handle(interface_name), value, true);
403 }
404
415 template <typename T>
417 const StateInterface::SharedPtr & interface_handle, T & state, bool wait_until_get) const
418 {
419 if (!interface_handle)
420 {
421 throw std::runtime_error(
422 fmt::format(
423 "State interface handle is null in hardware component: {}, while calling get_state "
424 "method. This should not happen.",
425 info_.name));
426 }
427 const bool success = interface_handle->get_value(state, wait_until_get);
428 if (!success && wait_until_get)
429 {
430 throw std::runtime_error(
431 fmt::format(
432 "Failed to get state value from interface: {} in hardware component: {}. This should "
433 "not happen.",
434 interface_handle->get_name(), info_.name));
435 }
436 return success;
437 }
438
440
448 template <typename T = double>
449 T get_state(const std::string & interface_name) const
450 {
451 T state;
452 get_state<T>(get_state_interface_handle(interface_name), state, true);
453 return state;
454 }
455
457
461 virtual bool has_command(const std::string & interface_name) const;
462
464
470 virtual const CommandInterface::SharedPtr & get_command_interface_handle(
471 const std::string & interface_name) const;
472
474
483 template <typename T>
485 const CommandInterface::SharedPtr & interface_handle, const T & value, bool wait_until_set)
486 {
487 if (!interface_handle)
488 {
489 throw std::runtime_error(
490 fmt::format(
491 "Command interface handle is null in hardware component: {}, while calling set_command "
492 "method. This should not happen.",
493 info_.name));
494 }
495 return interface_handle->set_value(value, wait_until_set);
496 }
497
499
507 template <typename T>
508 void set_command(const std::string & interface_name, const T & value)
509 {
510 std::ignore = set_command(get_command_interface_handle(interface_name), value, true);
511 }
512
523 template <typename T>
525 const CommandInterface::SharedPtr & interface_handle, T & command, bool wait_until_get) const
526 {
527 if (!interface_handle)
528 {
529 throw std::runtime_error(
530 fmt::format(
531 "Command interface handle is null in hardware component: {}, while calling get_command "
532 "method. This should not happen.",
533 info_.name));
534 }
535 const bool success = interface_handle->get_value(command, wait_until_get);
536 if (!success && wait_until_get)
537 {
538 throw std::runtime_error(
539 fmt::format(
540 "Failed to get command value from interface: {} in hardware component: {}. This should "
541 "not happen.",
542 interface_handle->get_name(), info_.name));
543 }
544 return success;
545 }
546
548
556 template <typename T = double>
557 T get_command(const std::string & interface_name) const
558 {
559 T command;
560 get_command<T>(get_command_interface_handle(interface_name), command, true);
561 return command;
562 }
563
565
568 virtual rclcpp::Logger get_logger() const;
569
571
574 virtual rclcpp::Clock::SharedPtr get_clock() const;
575
577
580 virtual rclcpp::Node::SharedPtr get_node() const;
581
583
586 const HardwareInfo & get_hardware_info() const;
587
589
594
596
600
602
605 void enable_introspection(bool enable);
606
607protected:
608 HardwareInfo info_;
609 // interface names to InterfaceDescription
610 std::unordered_map<std::string, InterfaceDescription> joint_state_interfaces_;
611 std::unordered_map<std::string, InterfaceDescription> joint_command_interfaces_;
612
613 std::unordered_map<std::string, InterfaceDescription> sensor_state_interfaces_;
614
615 std::unordered_map<std::string, InterfaceDescription> gpio_state_interfaces_;
616 std::unordered_map<std::string, InterfaceDescription> gpio_command_interfaces_;
617
618 std::unordered_map<std::string, InterfaceDescription> unlisted_state_interfaces_;
619 std::unordered_map<std::string, InterfaceDescription> unlisted_command_interfaces_;
620
621 rclcpp_lifecycle::State lifecycle_state_;
622 std::unique_ptr<realtime_tools::AsyncFunctionHandler<return_type>> async_handler_;
623
624 // Exported Command- and StateInterfaces in order they are listed in the hardware description.
625 std::vector<StateInterface::SharedPtr> joint_states_;
626 std::vector<CommandInterface::SharedPtr> joint_commands_;
627
628 std::vector<StateInterface::SharedPtr> sensor_states_;
629
630 std::vector<StateInterface::SharedPtr> gpio_states_;
631 std::vector<CommandInterface::SharedPtr> gpio_commands_;
632
633 std::vector<StateInterface::SharedPtr> unlisted_states_;
634 std::vector<CommandInterface::SharedPtr> unlisted_commands_;
635
636private:
637 class HardwareComponentInterfaceImpl;
638 std::unique_ptr<HardwareComponentInterfaceImpl> impl_;
639
640protected:
641 pal_statistics::RegistrationsRAII stats_registrations_;
642};
643
644} // namespace hardware_interface
645#endif // HARDWARE_INTERFACE__HARDWARE_COMPONENT_INTERFACE_HPP_
Virtual base class for all hardware components (Actuators, Sensors, and Systems).
Definition hardware_component_interface.hpp:62
const std::string & get_name() const
Get name of the hardware.
Definition hardware_component_interface.cpp:387
void prepare_for_activation()
Prepare for the activation of the hardware.
Definition hardware_component_interface.cpp:465
virtual std::vector< hardware_interface::InterfaceDescription > export_unlisted_state_interface_descriptions()
Definition hardware_component_interface.cpp:195
T get_state(const std::string &interface_name) const
Get the value from a state interface.
Definition hardware_component_interface.hpp:449
bool get_state(const StateInterface::SharedPtr &interface_handle, T &state, bool wait_until_get) const
Definition hardware_component_interface.hpp:416
const rclcpp_lifecycle::State & get_lifecycle_state() const
Get life-cycle state of the hardware.
Definition hardware_component_interface.cpp:391
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:381
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:484
virtual rclcpp::NodeOptions define_custom_node_options() const
Define custom node options for the hardware component interface.
Definition hardware_component_interface.cpp:176
void set_state(const std::string &interface_name, const T &value)
Set the value of a state interface.
Definition hardware_component_interface.hpp:400
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:318
virtual std::vector< CommandInterface::SharedPtr > on_export_command_interfaces()
Definition hardware_component_interface.cpp:263
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:141
void set_lifecycle_state(const rclcpp_lifecycle::State &new_state)
Set life-cycle state of the hardware.
Definition hardware_component_interface.cpp:396
virtual bool has_command(const std::string &interface_name) const
Does the command interface exist?
Definition hardware_component_interface.cpp:426
void pause_async_operations()
Pause any asynchronous operations.
Definition hardware_component_interface.cpp:456
const std::string & get_group_name() const
Get name of the hardware group to which it belongs to.
Definition hardware_component_interface.cpp:389
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:355
virtual std::vector< hardware_interface::InterfaceDescription > export_unlisted_command_interface_descriptions()
Definition hardware_component_interface.cpp:257
virtual std::vector< CommandInterface > export_command_interfaces()
Exports all command interfaces for this hardware interface.
Definition hardware_component_interface.cpp:248
bool get_command(const CommandInterface::SharedPtr &interface_handle, T &command, bool wait_until_get) const
Definition hardware_component_interface.hpp:524
CallbackReturn init(const HardwareInfo &hardware_info, rclcpp::Logger logger, rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface)
Definition hardware_component_interface.hpp:87
virtual const StateInterface::SharedPtr & get_state_interface_handle(const std::string &interface_name) const
Get the state interface handle.
Definition hardware_component_interface.cpp:412
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:402
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:376
void set_command(const std::string &interface_name, const T &value)
Set the value of a command interface.
Definition hardware_component_interface.hpp:508
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:304
virtual rclcpp::Node::SharedPtr get_node() const
Get the default node of the HardwareComponentInterface.
Definition hardware_component_interface.cpp:449
virtual std::vector< StateInterface::ConstSharedPtr > on_export_state_interfaces()
Definition hardware_component_interface.cpp:201
virtual std::vector< StateInterface > export_state_interfaces()
Exports all state interfaces for this hardware interface.
Definition hardware_component_interface.cpp:186
CallbackReturn init(const HardwareInfo &hardware_info, rclcpp::Logger logger, rclcpp::Clock::SharedPtr clock)
Definition hardware_component_interface.hpp:109
void enable_introspection(bool enable)
Enable or disable introspection of the hardware.
Definition hardware_component_interface.cpp:473
virtual const CommandInterface::SharedPtr & get_command_interface_handle(const std::string &interface_name) const
Get the command interface handle.
Definition hardware_component_interface.cpp:431
virtual rclcpp::Logger get_logger() const
Get the logger of the HardwareComponentInterface.
Definition hardware_component_interface.cpp:445
T get_command(const std::string &interface_name) const
Get the value from a command interface.
Definition hardware_component_interface.hpp:557
virtual rclcpp::Clock::SharedPtr get_clock() const
Get the clock.
Definition hardware_component_interface.cpp:447
const HardwareInfo & get_hardware_info() const
Get the hardware info of the HardwareComponentInterface.
Definition hardware_component_interface.cpp:454
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:311
virtual bool has_state(const std::string &interface_name) const
Does the state interface exist?
Definition hardware_component_interface.cpp:407
Definition actuator.hpp:22
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:360
std::string name
Name of the hardware.
Definition hardware_info.hpp:362