ros2_control - iron
Loading...
Searching...
No Matches
pid_controller.hpp
1// Copyright (c) 2023, Stogl Robotics Consulting UG (haftungsbeschränkt)
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// Authors: Daniel Azanov, Dr. Denis
16//
17
18#ifndef PID_CONTROLLER__PID_CONTROLLER_HPP_
19#define PID_CONTROLLER__PID_CONTROLLER_HPP_
20
21#include <memory>
22#include <string>
23#include <vector>
24
25#include "control_msgs/msg/multi_dof_command.hpp"
26#include "control_msgs/msg/multi_dof_state_stamped.hpp"
27#include "control_toolbox/pid_ros.hpp"
28#include "controller_interface/chainable_controller_interface.hpp"
29#include "pid_controller/visibility_control.h"
30#include "pid_controller_parameters.hpp"
31#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
32#include "rclcpp_lifecycle/state.hpp"
33#include "realtime_tools/realtime_buffer.h"
34#include "realtime_tools/realtime_publisher.h"
35#include "std_srvs/srv/set_bool.hpp"
36
37#include "control_msgs/msg/joint_controller_state.hpp"
38
39#include "control_msgs/msg/pid_state.hpp"
40#include "trajectory_msgs/msg/joint_trajectory_point.hpp"
41
42namespace pid_controller
43{
44
45enum class feedforward_mode_type : std::uint8_t
46{
47 OFF = 0,
48 ON = 1,
49};
50
52{
53public:
54 PID_CONTROLLER__VISIBILITY_PUBLIC
56
57 PID_CONTROLLER__VISIBILITY_PUBLIC
58 controller_interface::CallbackReturn on_init() override;
59
60 PID_CONTROLLER__VISIBILITY_PUBLIC
62
63 PID_CONTROLLER__VISIBILITY_PUBLIC
65
66 PID_CONTROLLER__VISIBILITY_PUBLIC
67 controller_interface::CallbackReturn on_cleanup(
68 const rclcpp_lifecycle::State & previous_state) override;
69
70 PID_CONTROLLER__VISIBILITY_PUBLIC
71 controller_interface::CallbackReturn on_configure(
72 const rclcpp_lifecycle::State & previous_state) override;
73
74 PID_CONTROLLER__VISIBILITY_PUBLIC
75 controller_interface::CallbackReturn on_activate(
76 const rclcpp_lifecycle::State & previous_state) override;
77
78 PID_CONTROLLER__VISIBILITY_PUBLIC
79 controller_interface::CallbackReturn on_deactivate(
80 const rclcpp_lifecycle::State & previous_state) override;
81
82 PID_CONTROLLER__VISIBILITY_PUBLIC
83 controller_interface::return_type update_reference_from_subscribers(
84 const rclcpp::Time & time, const rclcpp::Duration & period) override;
85
86 PID_CONTROLLER__VISIBILITY_PUBLIC
87 controller_interface::return_type update_and_write_commands(
88 const rclcpp::Time & time, const rclcpp::Duration & period) override;
89
90 using ControllerReferenceMsg = control_msgs::msg::MultiDOFCommand;
91 using ControllerMeasuredStateMsg = control_msgs::msg::MultiDOFCommand;
92 using ControllerModeSrvType = std_srvs::srv::SetBool;
93 using ControllerStateMsg = control_msgs::msg::MultiDOFStateStamped;
94
95protected:
96 std::shared_ptr<pid_controller::ParamListener> param_listener_;
97 pid_controller::Params params_;
98
99 std::vector<std::string> reference_and_state_dof_names_;
100 size_t dof_;
101 std::vector<double> measured_state_values_;
102
103 using PidPtr = std::shared_ptr<control_toolbox::PidROS>;
104 std::vector<PidPtr> pids_;
105 // Feed-forward velocity weight factor when calculating closed loop pid adapter's command
106 std::vector<double> feedforward_gain_;
107
108 // Command subscribers and Controller State publisher
109 rclcpp::Subscription<ControllerReferenceMsg>::SharedPtr ref_subscriber_ = nullptr;
111
112 rclcpp::Subscription<ControllerMeasuredStateMsg>::SharedPtr measured_state_subscriber_ = nullptr;
114
115 rclcpp::Service<ControllerModeSrvType>::SharedPtr set_feedforward_control_service_;
117
119
120 rclcpp::Publisher<ControllerStateMsg>::SharedPtr s_publisher_;
121 std::unique_ptr<ControllerStatePublisher> state_publisher_;
122
123 // override methods from ChainableControllerInterface
124 std::vector<hardware_interface::CommandInterface> on_export_reference_interfaces() override;
125
126 bool on_set_chained_mode(bool chained_mode) override;
127
128 // internal methods
129 void update_parameters();
130 controller_interface::CallbackReturn configure_parameters();
131
132private:
133 // callback for topic interface
134 PID_CONTROLLER__VISIBILITY_LOCAL
135 void reference_callback(const std::shared_ptr<ControllerReferenceMsg> msg);
136};
137
138} // namespace pid_controller
139
140#endif // PID_CONTROLLER__PID_CONTROLLER_HPP_
Definition chainable_controller_interface.hpp:35
Definition pid_controller.hpp:52
PID_CONTROLLER__VISIBILITY_PUBLIC controller_interface::return_type update_and_write_commands(const rclcpp::Time &time, const rclcpp::Duration &period) override
Execute calculations of the controller and update command interfaces.
Definition pid_controller.cpp:403
bool on_set_chained_mode(bool chained_mode) override
Virtual method that each chainable controller should implement to switch chained mode.
Definition pid_controller.cpp:354
PID_CONTROLLER__VISIBILITY_PUBLIC controller_interface::InterfaceConfiguration state_interface_configuration() const override
Get configuration for controller's required state interfaces.
Definition pid_controller.cpp:307
std::vector< hardware_interface::CommandInterface > on_export_reference_interfaces() override
Definition pid_controller.cpp:332
PID_CONTROLLER__VISIBILITY_PUBLIC controller_interface::return_type update_reference_from_subscribers(const rclcpp::Time &time, const rclcpp::Duration &period) override
Update reference from input topics when not in chained mode.
Definition pid_controller.cpp:382
PID_CONTROLLER__VISIBILITY_PUBLIC controller_interface::CallbackReturn on_init() override
Extending interface with initialization method which is individual for each controller.
Definition pid_controller.cpp:63
PID_CONTROLLER__VISIBILITY_PUBLIC controller_interface::InterfaceConfiguration command_interface_configuration() const override
Get configuration for controller's required command interfaces.
Definition pid_controller.cpp:293
Definition realtime_buffer.hpp:44
Definition realtime_publisher.hpp:54
Configuring what command/state interfaces to claim.
Definition controller_interface_base.hpp:56