ros2_control - humble
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.hpp"
34#include "realtime_tools/realtime_publisher.hpp"
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_and_write_commands(
84 const rclcpp::Time & time, const rclcpp::Duration & period) override;
85
86 using ControllerReferenceMsg = control_msgs::msg::MultiDOFCommand;
87 using ControllerMeasuredStateMsg = control_msgs::msg::MultiDOFCommand;
88 using ControllerModeSrvType = std_srvs::srv::SetBool;
89 using ControllerStateMsg = control_msgs::msg::MultiDOFStateStamped;
90
91protected:
92 controller_interface::return_type update_reference_from_subscribers() override;
93
94 std::shared_ptr<pid_controller::ParamListener> param_listener_;
95 pid_controller::Params params_;
96
97 std::vector<std::string> reference_and_state_dof_names_;
98 size_t dof_;
99 std::vector<double> measured_state_values_;
100
101 using PidPtr = std::shared_ptr<control_toolbox::PidROS>;
102 std::vector<PidPtr> pids_;
103 // Feed-forward velocity weight factor when calculating closed loop pid adapter's command
104 std::vector<double> feedforward_gain_;
105
106 // Command subscribers and Controller State publisher
107 rclcpp::Subscription<ControllerReferenceMsg>::SharedPtr ref_subscriber_ = nullptr;
109
110 rclcpp::Subscription<ControllerMeasuredStateMsg>::SharedPtr measured_state_subscriber_ = nullptr;
112
113 rclcpp::Service<ControllerModeSrvType>::SharedPtr set_feedforward_control_service_;
115
117
118 rclcpp::Publisher<ControllerStateMsg>::SharedPtr s_publisher_;
119 std::unique_ptr<ControllerStatePublisher> state_publisher_;
120
121 // override methods from ChainableControllerInterface
122 std::vector<hardware_interface::CommandInterface> on_export_reference_interfaces() override;
123
124 bool on_set_chained_mode(bool chained_mode) override;
125
126 // internal methods
127 void update_parameters();
128 controller_interface::CallbackReturn configure_parameters();
129
130private:
131 // callback for topic interface
132 PID_CONTROLLER__VISIBILITY_LOCAL
133 void reference_callback(const std::shared_ptr<ControllerReferenceMsg> msg);
134};
135
136} // namespace pid_controller
137
138#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:408
controller_interface::return_type update_reference_from_subscribers() override
Update reference from input topics when not in chained mode.
Definition pid_controller.cpp:388
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:360
PID_CONTROLLER__VISIBILITY_PUBLIC controller_interface::InterfaceConfiguration state_interface_configuration() const override
Get configuration for controller's required state interfaces.
Definition pid_controller.cpp:313
std::vector< hardware_interface::CommandInterface > on_export_reference_interfaces() override
Definition pid_controller.cpp:338
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:69
PID_CONTROLLER__VISIBILITY_PUBLIC controller_interface::InterfaceConfiguration command_interface_configuration() const override
Get configuration for controller's required command interfaces.
Definition pid_controller.cpp:299
Definition realtime_buffer.hpp:44
Definition realtime_publisher.hpp:54
Configuring what command/state interfaces to claim.
Definition controller_interface_base.hpp:56