ros2_control - rolling
Loading...
Searching...
No Matches
system_interface.hpp
1// Copyright 2020 - 2021 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__SYSTEM_INTERFACE_HPP_
16#define HARDWARE_INTERFACE__SYSTEM_INTERFACE_HPP_
17
18#include <limits>
19#include <memory>
20#include <string>
21#include <unordered_map>
22#include <utility>
23#include <vector>
24
25#include "hardware_interface/component_parser.hpp"
26#include "hardware_interface/handle.hpp"
27#include "hardware_interface/hardware_info.hpp"
28#include "hardware_interface/types/hardware_interface_return_values.hpp"
29#include "hardware_interface/types/hardware_interface_type_values.hpp"
30#include "hardware_interface/types/lifecycle_state_names.hpp"
31#include "hardware_interface/types/trigger_type.hpp"
32#include "lifecycle_msgs/msg/state.hpp"
33#include "rclcpp/duration.hpp"
34#include "rclcpp/logger.hpp"
35#include "rclcpp/logging.hpp"
36#include "rclcpp/node_interfaces/node_clock_interface.hpp"
37#include "rclcpp/time.hpp"
38#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
39#include "rclcpp_lifecycle/state.hpp"
40#include "realtime_tools/async_function_handler.hpp"
41
42namespace hardware_interface
43{
45
80using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;
81
82class SystemInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface
83{
84public:
86 : lifecycle_state_(rclcpp_lifecycle::State(
87 lifecycle_msgs::msg::State::PRIMARY_STATE_UNKNOWN, lifecycle_state_names::UNKNOWN)),
88 system_logger_(rclcpp::get_logger("system_interface"))
89 {
90 }
91
93
97 SystemInterface(const SystemInterface & other) = delete;
98
99 SystemInterface(SystemInterface && other) = default;
100
101 virtual ~SystemInterface() = default;
102
105
113 const HardwareInfo & hardware_info, rclcpp::Logger logger,
114 rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface)
115 {
116 clock_interface_ = clock_interface;
117 system_logger_ = logger.get_child("hardware_component.system." + hardware_info.name);
118 info_ = hardware_info;
119 if (info_.is_async)
120 {
121 RCLCPP_INFO_STREAM(
122 get_logger(), "Starting async handler with scheduler priority: " << info_.thread_priority);
123 async_handler_ = std::make_unique<realtime_tools::AsyncFunctionHandler<return_type>>();
124 async_handler_->init(
125 [this](const rclcpp::Time & time, const rclcpp::Duration & period)
126 {
127 if (next_trigger_ == TriggerType::READ)
128 {
129 const auto ret = read(time, period);
130 next_trigger_ = TriggerType::WRITE;
131 return ret;
132 }
133 else
134 {
135 const auto ret = write(time, period);
136 next_trigger_ = TriggerType::READ;
137 return ret;
138 }
139 },
140 info_.thread_priority);
141 async_handler_->start_thread();
142 }
143 return on_init(hardware_info);
144 };
145
147
152 virtual CallbackReturn on_init(const HardwareInfo & hardware_info)
153 {
154 info_ = hardware_info;
155 parse_state_interface_descriptions(info_.joints, joint_state_interfaces_);
156 parse_state_interface_descriptions(info_.sensors, sensor_state_interfaces_);
157 parse_state_interface_descriptions(info_.gpios, gpio_state_interfaces_);
158 parse_command_interface_descriptions(info_.joints, joint_command_interfaces_);
159 parse_command_interface_descriptions(info_.gpios, gpio_command_interfaces_);
160 return CallbackReturn::SUCCESS;
161 };
162
164
175 [[deprecated(
176 "Replaced by vector<StateInterface::ConstSharedPtr> on_export_state_interfaces() method. "
177 "Exporting is handled "
178 "by the Framework.")]] virtual std::vector<StateInterface>
180 {
181 // return empty vector by default. For backward compatibility we try calling
182 // export_state_interfaces() and only when empty vector is returned call
183 // on_export_state_interfaces()
184 return {};
185 }
186
193 virtual std::vector<hardware_interface::InterfaceDescription>
195 {
196 // return empty vector by default.
197 return {};
198 }
199
207 virtual std::vector<StateInterface::ConstSharedPtr> on_export_state_interfaces()
208 {
209 // import the unlisted interfaces
210 std::vector<hardware_interface::InterfaceDescription> unlisted_interface_descriptions =
212
213 std::vector<StateInterface::ConstSharedPtr> state_interfaces;
214 state_interfaces.reserve(
215 unlisted_interface_descriptions.size() + joint_state_interfaces_.size() +
216 sensor_state_interfaces_.size() + gpio_state_interfaces_.size());
217
218 // add InterfaceDescriptions and create the StateInterfaces from the descriptions and add to
219 // maps.
220 for (const auto & description : unlisted_interface_descriptions)
221 {
222 auto name = description.get_name();
223 unlisted_state_interfaces_.insert(std::make_pair(name, description));
224 auto state_interface = std::make_shared<StateInterface>(description);
225 system_states_.insert(std::make_pair(name, state_interface));
226 unlisted_states_.push_back(state_interface);
227 state_interfaces.push_back(std::const_pointer_cast<const StateInterface>(state_interface));
228 }
229
230 for (const auto & [name, descr] : joint_state_interfaces_)
231 {
232 auto state_interface = std::make_shared<StateInterface>(descr);
233 system_states_.insert(std::make_pair(name, state_interface));
234 joint_states_.push_back(state_interface);
235 state_interfaces.push_back(std::const_pointer_cast<const StateInterface>(state_interface));
236 }
237 for (const auto & [name, descr] : sensor_state_interfaces_)
238 {
239 auto state_interface = std::make_shared<StateInterface>(descr);
240 system_states_.insert(std::make_pair(name, state_interface));
241 sensor_states_.push_back(state_interface);
242 state_interfaces.push_back(std::const_pointer_cast<const StateInterface>(state_interface));
243 }
244 for (const auto & [name, descr] : gpio_state_interfaces_)
245 {
246 auto state_interface = std::make_shared<StateInterface>(descr);
247 system_states_.insert(std::make_pair(name, state_interface));
248 gpio_states_.push_back(state_interface);
249 state_interfaces.push_back(std::const_pointer_cast<const StateInterface>(state_interface));
250 }
251 return state_interfaces;
252 }
253
255
266 [[deprecated(
267 "Replaced by vector<CommandInterface::SharedPtr> on_export_command_interfaces() method. "
268 "Exporting is "
269 "handled "
270 "by the Framework.")]] virtual std::vector<CommandInterface>
272 {
273 // return empty vector by default. For backward compatibility we try calling
274 // export_command_interfaces() and only when empty vector is returned call
275 // on_export_command_interfaces()
276 return {};
277 }
278
285 virtual std::vector<hardware_interface::InterfaceDescription>
287 {
288 // return empty vector by default.
289 return {};
290 }
291
299 virtual std::vector<CommandInterface::SharedPtr> on_export_command_interfaces()
300 {
301 // import the unlisted interfaces
302 std::vector<hardware_interface::InterfaceDescription> unlisted_interface_descriptions =
304
305 std::vector<CommandInterface::SharedPtr> command_interfaces;
306 command_interfaces.reserve(
307 unlisted_interface_descriptions.size() + joint_command_interfaces_.size() +
308 gpio_command_interfaces_.size());
309
310 // add InterfaceDescriptions and create the CommandInterfaces from the descriptions and add to
311 // maps.
312 for (const auto & description : unlisted_interface_descriptions)
313 {
314 auto name = description.get_name();
315 unlisted_command_interfaces_.insert(std::make_pair(name, description));
316 auto command_interface = std::make_shared<CommandInterface>(description);
317 system_commands_.insert(std::make_pair(name, command_interface));
318 unlisted_commands_.push_back(command_interface);
319 command_interfaces.push_back(command_interface);
320 }
321
322 for (const auto & [name, descr] : joint_command_interfaces_)
323 {
324 auto command_interface = std::make_shared<CommandInterface>(descr);
325 system_commands_.insert(std::make_pair(name, command_interface));
326 joint_commands_.push_back(command_interface);
327 command_interfaces.push_back(command_interface);
328 }
329
330 for (const auto & [name, descr] : gpio_command_interfaces_)
331 {
332 auto command_interface = std::make_shared<CommandInterface>(descr);
333 system_commands_.insert(std::make_pair(name, command_interface));
334 gpio_commands_.push_back(command_interface);
335 command_interfaces.push_back(command_interface);
336 }
337 return command_interfaces;
338 }
339
341
353 virtual return_type prepare_command_mode_switch(
354 const std::vector<std::string> & /*start_interfaces*/,
355 const std::vector<std::string> & /*stop_interfaces*/)
356 {
357 return return_type::OK;
358 }
359
360 // Perform switching to the new command interface.
372 virtual return_type perform_command_mode_switch(
373 const std::vector<std::string> & /*start_interfaces*/,
374 const std::vector<std::string> & /*stop_interfaces*/)
375 {
376 return return_type::OK;
377 }
378
380
390 return_type trigger_read(const rclcpp::Time & time, const rclcpp::Duration & period)
391 {
392 return_type result = return_type::ERROR;
393 if (info_.is_async)
394 {
395 bool trigger_status = true;
396 if (next_trigger_ == TriggerType::WRITE)
397 {
398 RCLCPP_WARN(
399 get_logger(),
400 "Trigger read called while write async handler call is still pending for hardware "
401 "interface : '%s'. Skipping read cycle and will wait for a write cycle!",
402 info_.name.c_str());
403 return return_type::OK;
404 }
405 std::tie(trigger_status, result) = async_handler_->trigger_async_callback(time, period);
406 if (!trigger_status)
407 {
408 RCLCPP_WARN(
409 get_logger(),
410 "Trigger read called while write async trigger is still in progress for hardware "
411 "interface : '%s'. Failed to trigger read cycle!",
412 info_.name.c_str());
413 return return_type::OK;
414 }
415 }
416 else
417 {
418 result = read(time, period);
419 }
420 return result;
421 }
422
424
433 virtual return_type read(const rclcpp::Time & time, const rclcpp::Duration & period) = 0;
434
436
445 return_type trigger_write(const rclcpp::Time & time, const rclcpp::Duration & period)
446 {
447 return_type result = return_type::ERROR;
448 if (info_.is_async)
449 {
450 bool trigger_status = true;
451 if (next_trigger_ == TriggerType::READ)
452 {
453 RCLCPP_WARN(
454 get_logger(),
455 "Trigger write called while read async handler call is still pending for hardware "
456 "interface : '%s'. Skipping write cycle and will wait for a read cycle!",
457 info_.name.c_str());
458 return return_type::OK;
459 }
460 std::tie(trigger_status, result) = async_handler_->trigger_async_callback(time, period);
461 if (!trigger_status)
462 {
463 RCLCPP_WARN(
464 get_logger(),
465 "Trigger write called while read async trigger is still in progress for hardware "
466 "interface : '%s'. Failed to trigger write cycle!",
467 info_.name.c_str());
468 return return_type::OK;
469 }
470 }
471 else
472 {
473 result = write(time, period);
474 }
475 return result;
476 }
477
479
487 virtual return_type write(const rclcpp::Time & time, const rclcpp::Duration & period) = 0;
488
490
493 virtual std::string get_name() const { return info_.name; }
494
496
499 virtual std::string get_group_name() const { return info_.group; }
500
502
505 const rclcpp_lifecycle::State & get_lifecycle_state() const { return lifecycle_state_; }
506
508
511 void set_lifecycle_state(const rclcpp_lifecycle::State & new_state)
512 {
513 lifecycle_state_ = new_state;
514 }
515
516 void set_state(const std::string & interface_name, const double & value)
517 {
518 system_states_.at(interface_name)->set_value(value);
519 }
520
521 double get_state(const std::string & interface_name) const
522 {
523 return system_states_.at(interface_name)->get_value();
524 }
525
526 void set_command(const std::string & interface_name, const double & value)
527 {
528 system_commands_.at(interface_name)->set_value(value);
529 }
530
531 double get_command(const std::string & interface_name) const
532 {
533 return system_commands_.at(interface_name)->get_value();
534 }
535
537
540 rclcpp::Logger get_logger() const { return system_logger_; }
541
543
546 rclcpp::Clock::SharedPtr get_clock() const { return clock_interface_->get_clock(); }
547
549
552 const HardwareInfo & get_hardware_info() const { return info_; }
553
554protected:
555 HardwareInfo info_;
556 // interface names to InterfaceDescription
557 std::unordered_map<std::string, InterfaceDescription> joint_state_interfaces_;
558 std::unordered_map<std::string, InterfaceDescription> joint_command_interfaces_;
559
560 std::unordered_map<std::string, InterfaceDescription> sensor_state_interfaces_;
561
562 std::unordered_map<std::string, InterfaceDescription> gpio_state_interfaces_;
563 std::unordered_map<std::string, InterfaceDescription> gpio_command_interfaces_;
564
565 std::unordered_map<std::string, InterfaceDescription> unlisted_state_interfaces_;
566 std::unordered_map<std::string, InterfaceDescription> unlisted_command_interfaces_;
567
568 rclcpp_lifecycle::State lifecycle_state_;
569 std::unique_ptr<realtime_tools::AsyncFunctionHandler<return_type>> async_handler_;
570
571 // Exported Command- and StateInterfaces in order they are listed in the hardware description.
572 std::vector<StateInterface::SharedPtr> joint_states_;
573 std::vector<CommandInterface::SharedPtr> joint_commands_;
574
575 std::vector<StateInterface::SharedPtr> sensor_states_;
576
577 std::vector<StateInterface::SharedPtr> gpio_states_;
578 std::vector<CommandInterface::SharedPtr> gpio_commands_;
579
580 std::vector<StateInterface::SharedPtr> unlisted_states_;
581 std::vector<CommandInterface::SharedPtr> unlisted_commands_;
582
583private:
584 rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface_;
585 rclcpp::Logger system_logger_;
586 // interface names to Handle accessed through getters/setters
587 std::unordered_map<std::string, StateInterface::SharedPtr> system_states_;
588 std::unordered_map<std::string, CommandInterface::SharedPtr> system_commands_;
589 std::atomic<TriggerType> next_trigger_ = TriggerType::READ;
590};
591
592} // namespace hardware_interface
593#endif // HARDWARE_INTERFACE__SYSTEM_INTERFACE_HPP_
Definition system_interface.hpp:83
virtual std::vector< CommandInterface::SharedPtr > on_export_command_interfaces()
Definition system_interface.hpp:299
return_type trigger_read(const rclcpp::Time &time, const rclcpp::Duration &period)
Triggers the read method synchronously or asynchronously depending on the HardwareInfo.
Definition system_interface.hpp:390
virtual return_type perform_command_mode_switch(const std::vector< std::string > &, const std::vector< std::string > &)
Definition system_interface.hpp:372
virtual std::vector< hardware_interface::InterfaceDescription > export_unlisted_command_interface_descriptions()
Definition system_interface.hpp:286
virtual std::vector< CommandInterface > export_command_interfaces()
Exports all command interfaces for this hardware interface.
Definition system_interface.hpp:271
virtual std::vector< StateInterface > export_state_interfaces()
Exports all state interfaces for this hardware interface.
Definition system_interface.hpp:179
virtual std::string get_group_name() const
Get name of the actuator hardware group to which it belongs to.
Definition system_interface.hpp:499
virtual std::vector< hardware_interface::InterfaceDescription > export_unlisted_state_interface_descriptions()
Definition system_interface.hpp:194
const HardwareInfo & get_hardware_info() const
Get the hardware info of the SystemInterface.
Definition system_interface.hpp:552
virtual std::string get_name() const
Get name of the actuator hardware.
Definition system_interface.hpp:493
virtual CallbackReturn on_init(const HardwareInfo &hardware_info)
Initialization of the hardware interface from data parsed from the robot's URDF.
Definition system_interface.hpp:152
rclcpp::Logger get_logger() const
Get the logger of the SystemInterface.
Definition system_interface.hpp:540
return_type trigger_write(const rclcpp::Time &time, const rclcpp::Duration &period)
Triggers the write method synchronously or asynchronously depending on the HardwareInfo.
Definition system_interface.hpp:445
void set_lifecycle_state(const rclcpp_lifecycle::State &new_state)
Set life-cycle state of the actuator hardware.
Definition system_interface.hpp:511
virtual return_type prepare_command_mode_switch(const std::vector< std::string > &, const std::vector< std::string > &)
Prepare for a new command interface switch.
Definition system_interface.hpp:353
rclcpp::Clock::SharedPtr get_clock() const
Get the clock of the SystemInterface.
Definition system_interface.hpp:546
CallbackReturn init(const HardwareInfo &hardware_info, rclcpp::Logger logger, rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface)
Definition system_interface.hpp:112
const rclcpp_lifecycle::State & get_lifecycle_state() const
Get life-cycle state of the actuator hardware.
Definition system_interface.hpp:505
virtual return_type read(const rclcpp::Time &time, const rclcpp::Duration &period)=0
Read the current state values from the actuator.
virtual std::vector< StateInterface::ConstSharedPtr > on_export_state_interfaces()
Definition system_interface.hpp:207
SystemInterface(const SystemInterface &other)=delete
SystemInterface copy constructor is actively deleted.
virtual return_type write(const rclcpp::Time &time, const rclcpp::Duration &period)=0
Write the current command values to the actuator.
Definition actuator.hpp:33
std::vector< InterfaceDescription > parse_command_interface_descriptions(const std::vector< ComponentInfo > &component_info)
Definition component_parser.cpp:1036
std::vector< InterfaceDescription > parse_state_interface_descriptions(const std::vector< ComponentInfo > &component_info)
Definition component_parser.cpp:1003
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn CallbackReturn
Virtual Class to implement when integrating a 1 DoF actuator into ros2_control.
Definition actuator_interface.hpp:77
This structure stores information about hardware defined in a robot's URDF.
Definition hardware_info.hpp:170
int thread_priority
Async thread priority.
Definition hardware_info.hpp:182
std::string group
Hardware group to which the hardware belongs.
Definition hardware_info.hpp:176
bool is_async
Component is async.
Definition hardware_info.hpp:180
std::vector< ComponentInfo > gpios
Definition hardware_info.hpp:205
std::vector< ComponentInfo > joints
Definition hardware_info.hpp:191
std::string name
Name of the hardware.
Definition hardware_info.hpp:172
std::vector< ComponentInfo > sensors
Definition hardware_info.hpp:200