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/format.h>
19
20#include <cstdint>
21#include <memory>
22#include <stdexcept>
23#include <string>
24#include <tuple>
25#include <unordered_map>
26#include <vector>
27
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/clock.hpp"
40#include "rclcpp/duration.hpp"
41#include "rclcpp/logger.hpp"
42#include "rclcpp/logging.hpp"
43#include "rclcpp/node.hpp"
44#include "rclcpp/node_interfaces/node_clock_interface.hpp"
45#include "rclcpp/node_options.hpp"
46#include "rclcpp/time.hpp"
47#include "rclcpp/version.h"
48#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
49#include "rclcpp_lifecycle/state.hpp"
50#include "realtime_tools/async_function_handler.hpp"
51
52namespace hardware_interface
53{
54
55using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;
56
65class HardwareComponentInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface
66{
67public:
69
71
76
78
80
83
90 [[deprecated("Use init(HardwareInfo, rclcpp::Logger, rclcpp::Clock::SharedPtr) instead.")]]
91 CallbackReturn init(
92 const HardwareInfo & hardware_info, rclcpp::Logger logger,
93 rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface)
94 {
95#pragma GCC diagnostic push
96#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
97 return this->init(hardware_info, logger, clock_interface->get_clock());
98#pragma GCC diagnostic pop
99 }
100
103
110 [[deprecated(
111 "Replaced by CallbackReturn init(const hardware_interface::HardwareComponentParams & "
112 "params). Initialization is handled by the Framework.")]]
113 CallbackReturn init(
114 const HardwareInfo & hardware_info, rclcpp::Logger logger, rclcpp::Clock::SharedPtr clock)
115 {
117 params.hardware_info = hardware_info;
118 params.clock = clock;
119 params.logger = logger;
120 return init(params);
121 };
122
125
135 CallbackReturn init(const hardware_interface::HardwareComponentParams & params);
136
138
143 [[deprecated("Use on_init(const HardwareComponentInterfaceParams & params) instead.")]]
144 virtual CallbackReturn on_init(const HardwareInfo & hardware_info);
145
147
156 virtual CallbackReturn on_init(
158
160
164 virtual rclcpp::NodeOptions define_custom_node_options() const;
165
167
178 [[deprecated(
179 "Replaced by vector<StateInterface::ConstSharedPtr> on_export_state_interfaces() method. "
180 "Exporting is handled by the Framework.")]] virtual std::vector<StateInterface>
182
189 virtual std::vector<hardware_interface::InterfaceDescription>
191
199 virtual std::vector<StateInterface::ConstSharedPtr> on_export_state_interfaces();
200
202
213 [[deprecated(
214 "Replaced by vector<CommandInterface::SharedPtr> on_export_command_interfaces() method. "
215 "Exporting is handled by the Framework.")]] virtual std::vector<CommandInterface>
217
224 virtual std::vector<hardware_interface::InterfaceDescription>
226
237 virtual std::vector<CommandInterface::SharedPtr> on_export_command_interfaces();
238
240
250 virtual return_type prepare_command_mode_switch(
251 const std::vector<std::string> & start_interfaces,
252 const std::vector<std::string> & stop_interfaces);
253
254 // Perform switching to the new command interface.
264 virtual return_type perform_command_mode_switch(
265 const std::vector<std::string> & start_interfaces,
266 const std::vector<std::string> & stop_interfaces);
267
269
280 const rclcpp::Time & time, const rclcpp::Duration & period);
281
283
292 virtual return_type read(const rclcpp::Time & time, const rclcpp::Duration & period) = 0;
293
295
305 const rclcpp::Time & time, const rclcpp::Duration & period);
306
308
316 virtual return_type write(const rclcpp::Time & time, const rclcpp::Duration & period);
317
319
322 const std::string & get_name() const;
323
325
328 const std::string & get_group_name() const;
329
331
337 const rclcpp_lifecycle::State & get_lifecycle_state() const;
338
340
346 void set_lifecycle_state(const rclcpp_lifecycle::State & new_state);
347
349 uint8_t get_lifecycle_id() const;
350
352
356 virtual bool has_state(const std::string & interface_name) const;
357
359
365 virtual const StateInterface::SharedPtr & get_state_interface_handle(
366 const std::string & interface_name) const;
367
369
379 template <typename T>
381 const StateInterface::SharedPtr & interface_handle, const T & value, bool wait_until_set)
382 {
383 if (!interface_handle)
384 {
385 throw std::runtime_error(
386 fmt::format(
387 "State interface handle is null in hardware component: {}, while calling set_state "
388 "method. This should not happen.",
389 info_.name));
390 }
391 return interface_handle->set_value(value, wait_until_set);
392 }
393
395
403 template <typename T>
404 void set_state(const std::string & interface_name, const T & value)
405 {
406 std::ignore = set_state(get_state_interface_handle(interface_name), value, true);
407 }
408
419 template <typename T>
421 const StateInterface::SharedPtr & interface_handle, T & state, bool wait_until_get) const
422 {
423 if (!interface_handle)
424 {
425 throw std::runtime_error(
426 fmt::format(
427 "State interface handle is null in hardware component: {}, while calling get_state "
428 "method. This should not happen.",
429 info_.name));
430 }
431 const bool success = interface_handle->get_value(state, wait_until_get);
432 if (!success && wait_until_get)
433 {
434 throw std::runtime_error(
435 fmt::format(
436 "Failed to get state value from interface: {} in hardware component: {}. This should "
437 "not happen.",
438 interface_handle->get_name(), info_.name));
439 }
440 return success;
441 }
442
444
452 template <typename T = double>
453 T get_state(const std::string & interface_name) const
454 {
455 T state;
456 get_state<T>(get_state_interface_handle(interface_name), state, true);
457 return state;
458 }
459
461
465 virtual bool has_command(const std::string & interface_name) const;
466
468
474 virtual const CommandInterface::SharedPtr & get_command_interface_handle(
475 const std::string & interface_name) const;
476
478
487 template <typename T>
489 const CommandInterface::SharedPtr & interface_handle, const T & value, bool wait_until_set)
490 {
491 if (!interface_handle)
492 {
493 throw std::runtime_error(
494 fmt::format(
495 "Command interface handle is null in hardware component: {}, while calling set_command "
496 "method. This should not happen.",
497 info_.name));
498 }
499 return interface_handle->set_value(value, wait_until_set);
500 }
501
503
511 template <typename T>
512 void set_command(const std::string & interface_name, const T & value)
513 {
514 std::ignore = set_command(get_command_interface_handle(interface_name), value, true);
515 }
516
527 template <typename T>
529 const CommandInterface::SharedPtr & interface_handle, T & command, bool wait_until_get) const
530 {
531 if (!interface_handle)
532 {
533 throw std::runtime_error(
534 fmt::format(
535 "Command interface handle is null in hardware component: {}, while calling get_command "
536 "method. This should not happen.",
537 info_.name));
538 }
539 const bool success = interface_handle->get_value(command, wait_until_get);
540 if (!success && wait_until_get)
541 {
542 throw std::runtime_error(
543 fmt::format(
544 "Failed to get command value from interface: {} in hardware component: {}. This should "
545 "not happen.",
546 interface_handle->get_name(), info_.name));
547 }
548 return success;
549 }
550
552
560 template <typename T = double>
561 T get_command(const std::string & interface_name) const
562 {
563 T command;
564 get_command<T>(get_command_interface_handle(interface_name), command, true);
565 return command;
566 }
567
569
572 virtual rclcpp::Logger get_logger() const;
573
575
578 virtual rclcpp::Clock::SharedPtr get_clock() const;
579
581
584 virtual rclcpp::Node::SharedPtr get_node() const;
585
587
590 const HardwareInfo & get_hardware_info() const;
591
593
598
600
603 void stop_async_handler();
604
606
610
612
615 void enable_introspection(bool enable);
616
617protected:
618 HardwareInfo info_;
619 // interface names to InterfaceDescription
620 std::unordered_map<std::string, InterfaceDescription> joint_state_interfaces_;
621 std::unordered_map<std::string, InterfaceDescription> joint_command_interfaces_;
622
623 std::unordered_map<std::string, InterfaceDescription> sensor_state_interfaces_;
624
625 std::unordered_map<std::string, InterfaceDescription> gpio_state_interfaces_;
626 std::unordered_map<std::string, InterfaceDescription> gpio_command_interfaces_;
627
628 std::unordered_map<std::string, InterfaceDescription> unlisted_state_interfaces_;
629 std::unordered_map<std::string, InterfaceDescription> unlisted_command_interfaces_;
630
631 rclcpp_lifecycle::State lifecycle_state_;
632 std::unique_ptr<realtime_tools::AsyncFunctionHandler<return_type>> async_handler_;
633
634 // Exported Command- and StateInterfaces in order they are listed in the hardware description.
635 std::vector<StateInterface::SharedPtr> joint_states_;
636 std::vector<CommandInterface::SharedPtr> joint_commands_;
637
638 std::vector<StateInterface::SharedPtr> sensor_states_;
639
640 std::vector<StateInterface::SharedPtr> gpio_states_;
641 std::vector<CommandInterface::SharedPtr> gpio_commands_;
642
643 std::vector<StateInterface::SharedPtr> unlisted_states_;
644 std::vector<CommandInterface::SharedPtr> unlisted_commands_;
645
646private:
647 class HardwareComponentInterfaceImpl;
648 std::unique_ptr<HardwareComponentInterfaceImpl> impl_;
649
650protected:
651 pal_statistics::RegistrationsRAII stats_registrations_;
652};
653
654} // namespace hardware_interface
655#endif // HARDWARE_INTERFACE__HARDWARE_COMPONENT_INTERFACE_HPP_
Virtual base class for all hardware components (Actuators, Sensors, and Systems).
Definition hardware_component_interface.hpp:66
const std::string & get_name() const
Get name of the hardware.
Definition hardware_component_interface.cpp:400
void prepare_for_activation()
Prepare for the activation of the hardware.
Definition hardware_component_interface.cpp:486
virtual std::vector< hardware_interface::InterfaceDescription > export_unlisted_state_interface_descriptions()
Definition hardware_component_interface.cpp:208
T get_state(const std::string &interface_name) const
Get the value from a state interface.
Definition hardware_component_interface.hpp:453
bool get_state(const StateInterface::SharedPtr &interface_handle, T &state, bool wait_until_get) const
Definition hardware_component_interface.hpp:420
const rclcpp_lifecycle::State & get_lifecycle_state() const
Get life-cycle state of the hardware.
Definition hardware_component_interface.cpp:404
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:394
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:488
virtual rclcpp::NodeOptions define_custom_node_options() const
Define custom node options for the hardware component interface.
Definition hardware_component_interface.cpp:189
void set_state(const std::string &interface_name, const T &value)
Set the value of a state interface.
Definition hardware_component_interface.hpp:404
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:331
virtual std::vector< CommandInterface::SharedPtr > on_export_command_interfaces()
Definition hardware_component_interface.cpp:276
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:154
void set_lifecycle_state(const rclcpp_lifecycle::State &new_state)
Set life-cycle state of the hardware.
Definition hardware_component_interface.cpp:409
virtual bool has_command(const std::string &interface_name) const
Does the command interface exist?
Definition hardware_component_interface.cpp:439
void pause_async_operations()
Pause any asynchronous operations.
Definition hardware_component_interface.cpp:469
const std::string & get_group_name() const
Get name of the hardware group to which it belongs to.
Definition hardware_component_interface.cpp:402
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:368
virtual std::vector< hardware_interface::InterfaceDescription > export_unlisted_command_interface_descriptions()
Definition hardware_component_interface.cpp:270
virtual std::vector< CommandInterface > export_command_interfaces()
Exports all command interfaces for this hardware interface.
Definition hardware_component_interface.cpp:261
bool get_command(const CommandInterface::SharedPtr &interface_handle, T &command, bool wait_until_get) const
Definition hardware_component_interface.hpp:528
CallbackReturn init(const HardwareInfo &hardware_info, rclcpp::Logger logger, rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface)
Definition hardware_component_interface.hpp:91
virtual const StateInterface::SharedPtr & get_state_interface_handle(const std::string &interface_name) const
Get the state interface handle.
Definition hardware_component_interface.cpp:425
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:415
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:380
void set_command(const std::string &interface_name, const T &value)
Set the value of a command interface.
Definition hardware_component_interface.hpp:512
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:317
virtual rclcpp::Node::SharedPtr get_node() const
Get the default node of the HardwareComponentInterface.
Definition hardware_component_interface.cpp:462
virtual std::vector< StateInterface::ConstSharedPtr > on_export_state_interfaces()
Definition hardware_component_interface.cpp:214
virtual std::vector< StateInterface > export_state_interfaces()
Exports all state interfaces for this hardware interface.
Definition hardware_component_interface.cpp:199
CallbackReturn init(const HardwareInfo &hardware_info, rclcpp::Logger logger, rclcpp::Clock::SharedPtr clock)
Definition hardware_component_interface.hpp:113
void enable_introspection(bool enable)
Enable or disable introspection of the hardware.
Definition hardware_component_interface.cpp:494
virtual const CommandInterface::SharedPtr & get_command_interface_handle(const std::string &interface_name) const
Get the command interface handle.
Definition hardware_component_interface.cpp:444
virtual rclcpp::Logger get_logger() const
Get the logger of the HardwareComponentInterface.
Definition hardware_component_interface.cpp:458
T get_command(const std::string &interface_name) const
Get the value from a command interface.
Definition hardware_component_interface.hpp:561
virtual rclcpp::Clock::SharedPtr get_clock() const
Get the clock.
Definition hardware_component_interface.cpp:460
const HardwareInfo & get_hardware_info() const
Get the hardware info of the HardwareComponentInterface.
Definition hardware_component_interface.cpp:467
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:324
virtual bool has_state(const std::string &interface_name) const
Does the state interface exist?
Definition hardware_component_interface.cpp:420
void stop_async_handler()
Stop and release the async handler thread.
Definition hardware_component_interface.cpp:477
Definition mujoco_system_interface.hpp:57
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:375
std::string name
Name of the hardware.
Definition hardware_info.hpp:377