ros2_control - humble
Loading...
Searching...
No Matches
gpio_command_controller.hpp
1// Copyright 2022 ICUBE Laboratory, University of Strasbourg
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 GPIO_CONTROLLERS__GPIO_COMMAND_CONTROLLER_HPP_
16#define GPIO_CONTROLLERS__GPIO_COMMAND_CONTROLLER_HPP_
17
18#include <memory>
19#include <string>
20#include <unordered_map>
21#include <vector>
22
23#include "control_msgs/msg/dynamic_interface_group_values.hpp"
24#include "controller_interface/controller_interface.hpp"
25#include "gpio_command_controller_parameters.hpp"
26#include "gpio_controllers/visibility_control.h"
27#include "hardware_interface/hardware_info.hpp"
28#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
29#include "rclcpp_lifecycle/state.hpp"
30#include "realtime_tools/realtime_buffer.hpp"
31#include "realtime_tools/realtime_publisher.hpp"
32
33namespace gpio_controllers
34{
35using CmdType = control_msgs::msg::DynamicInterfaceGroupValues;
36using StateType = control_msgs::msg::DynamicInterfaceGroupValues;
37using CallbackReturn = controller_interface::CallbackReturn;
38using InterfacesNames = std::vector<std::string>;
39using MapOfReferencesToCommandInterfaces = std::unordered_map<
40 std::string, std::reference_wrapper<hardware_interface::LoanedCommandInterface>>;
41using MapOfReferencesToStateInterfaces =
42 std::unordered_map<std::string, std::reference_wrapper<hardware_interface::LoanedStateInterface>>;
43using StateInterfaces =
44 std::vector<std::reference_wrapper<hardware_interface::LoanedStateInterface>>;
45
47{
48public:
49 GPIO_COMMAND_CONTROLLER_PUBLIC
51
52 GPIO_COMMAND_CONTROLLER_PUBLIC
54
55 GPIO_COMMAND_CONTROLLER_PUBLIC
57
58 GPIO_COMMAND_CONTROLLER_PUBLIC
59 CallbackReturn on_init() override;
60
61 GPIO_COMMAND_CONTROLLER_PUBLIC
62 CallbackReturn on_configure(const rclcpp_lifecycle::State & previous_state) override;
63
64 GPIO_COMMAND_CONTROLLER_PUBLIC
65 CallbackReturn on_activate(const rclcpp_lifecycle::State & previous_state) override;
66
67 GPIO_COMMAND_CONTROLLER_PUBLIC
68 CallbackReturn on_deactivate(const rclcpp_lifecycle::State & previous_state) override;
69
70 GPIO_COMMAND_CONTROLLER_PUBLIC
71 controller_interface::return_type update(
72 const rclcpp::Time & time, const rclcpp::Duration & period) override;
73
74private:
75 void store_command_interface_types();
76 void store_state_interface_types();
77 void initialize_gpio_state_msg();
78 void update_gpios_states();
79 controller_interface::return_type update_gpios_commands();
80 template <typename T>
81 std::unordered_map<std::string, std::reference_wrapper<T>> create_map_of_references_to_interfaces(
82 const InterfacesNames & interfaces_from_params, std::vector<T> & configured_interfaces);
83 template <typename T>
84 bool check_if_configured_interfaces_matches_received(
85 const InterfacesNames & interfaces_from_params, const T & configured_interfaces);
86 void apply_state_value(
87 StateType & state_msg, std::size_t gpio_index, std::size_t interface_index) const;
88 void apply_command(
89 const CmdType & gpio_commands, std::size_t gpio_index,
90 std::size_t command_interface_index) const;
91 InterfacesNames get_gpios_state_interfaces_names(const std::string & gpio_name) const;
92 bool update_dynamic_map_parameters();
93 std::vector<hardware_interface::ComponentInfo> get_gpios_from_urdf() const;
94
95protected:
96 InterfacesNames command_interface_types_;
97 InterfacesNames state_interface_types_;
98 MapOfReferencesToCommandInterfaces command_interfaces_map_;
99 MapOfReferencesToStateInterfaces state_interfaces_map_;
100
102 rclcpp::Subscription<CmdType>::SharedPtr gpios_command_subscriber_{};
103
104 std::shared_ptr<rclcpp::Publisher<StateType>> gpio_state_publisher_{};
105 std::shared_ptr<realtime_tools::RealtimePublisher<StateType>> realtime_gpio_state_publisher_{};
106
107 std::shared_ptr<gpio_command_controller_parameters::ParamListener> param_listener_{};
108 gpio_command_controller_parameters::Params params_;
109};
110
111} // namespace gpio_controllers
112
113#endif // GPIO_CONTROLLERS__GPIO_COMMAND_CONTROLLER_HPP_
Definition controller_interface.hpp:29
Definition gpio_command_controller.hpp:47
GPIO_COMMAND_CONTROLLER_PUBLIC controller_interface::return_type update(const rclcpp::Time &time, const rclcpp::Duration &period) override
Definition gpio_command_controller.cpp:139
GPIO_COMMAND_CONTROLLER_PUBLIC controller_interface::InterfaceConfiguration command_interface_configuration() const override
Get configuration for controller's required command interfaces.
Definition gpio_command_controller.cpp:94
GPIO_COMMAND_CONTROLLER_PUBLIC CallbackReturn on_init() override
Extending interface with initialization method which is individual for each controller.
Definition gpio_command_controller.cpp:42
GPIO_COMMAND_CONTROLLER_PUBLIC controller_interface::InterfaceConfiguration state_interface_configuration() const override
Get configuration for controller's required state interfaces.
Definition gpio_command_controller.cpp:103
Definition realtime_buffer.hpp:44
Configuring what command/state interfaces to claim.
Definition controller_interface_base.hpp:56