|
| 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 | +#include "control_msgs/msg/joint_jog.hpp" |
| 39 | + |
| 40 | +#include "control_msgs/msg/pid_state.hpp" |
| 41 | +#include "trajectory_msgs/msg/joint_trajectory_point.hpp" |
| 42 | + |
| 43 | +namespace pid_controller |
| 44 | +{ |
| 45 | + |
| 46 | +enum class feedforward_mode_type : std::uint8_t |
| 47 | +{ |
| 48 | + OFF = 0, |
| 49 | + ON = 1, |
| 50 | +}; |
| 51 | + |
| 52 | +class PidController : public controller_interface::ChainableControllerInterface |
| 53 | +{ |
| 54 | +public: |
| 55 | + PID_CONTROLLER__VISIBILITY_PUBLIC |
| 56 | + PidController(); |
| 57 | + |
| 58 | + PID_CONTROLLER__VISIBILITY_PUBLIC |
| 59 | + controller_interface::CallbackReturn on_init() override; |
| 60 | + |
| 61 | + PID_CONTROLLER__VISIBILITY_PUBLIC |
| 62 | + controller_interface::InterfaceConfiguration command_interface_configuration() const override; |
| 63 | + |
| 64 | + PID_CONTROLLER__VISIBILITY_PUBLIC |
| 65 | + controller_interface::InterfaceConfiguration state_interface_configuration() const override; |
| 66 | + |
| 67 | + PID_CONTROLLER__VISIBILITY_PUBLIC |
| 68 | + controller_interface::CallbackReturn on_cleanup( |
| 69 | + const rclcpp_lifecycle::State & previous_state) override; |
| 70 | + |
| 71 | + PID_CONTROLLER__VISIBILITY_PUBLIC |
| 72 | + controller_interface::CallbackReturn on_configure( |
| 73 | + const rclcpp_lifecycle::State & previous_state) override; |
| 74 | + |
| 75 | + PID_CONTROLLER__VISIBILITY_PUBLIC |
| 76 | + controller_interface::CallbackReturn on_activate( |
| 77 | + const rclcpp_lifecycle::State & previous_state) override; |
| 78 | + |
| 79 | + PID_CONTROLLER__VISIBILITY_PUBLIC |
| 80 | + controller_interface::CallbackReturn on_deactivate( |
| 81 | + const rclcpp_lifecycle::State & previous_state) override; |
| 82 | + |
| 83 | + PID_CONTROLLER__VISIBILITY_PUBLIC |
| 84 | + controller_interface::return_type update_reference_from_subscribers( |
| 85 | + const rclcpp::Time & time, const rclcpp::Duration & period) override; |
| 86 | + |
| 87 | + PID_CONTROLLER__VISIBILITY_PUBLIC |
| 88 | + controller_interface::return_type update_and_write_commands( |
| 89 | + const rclcpp::Time & time, const rclcpp::Duration & period) override; |
| 90 | + |
| 91 | + using ControllerReferenceMsg = control_msgs::msg::MultiDOFCommand; |
| 92 | + using ControllerMeasuredStateMsg = control_msgs::msg::MultiDOFCommand; |
| 93 | + using ControllerModeSrvType = std_srvs::srv::SetBool; |
| 94 | + using ControllerStateMsg = control_msgs::msg::MultiDOFStateStamped; |
| 95 | + |
| 96 | +protected: |
| 97 | + std::shared_ptr<pid_controller::ParamListener> param_listener_; |
| 98 | + pid_controller::Params params_; |
| 99 | + |
| 100 | + std::vector<std::string> reference_and_state_dof_names_; |
| 101 | + size_t dof_; |
| 102 | + std::vector<double> measured_state_values_; |
| 103 | + |
| 104 | + using PidPtr = std::shared_ptr<control_toolbox::PidROS>; |
| 105 | + std::vector<PidPtr> pids_; |
| 106 | + // Feed-forward velocity weight factor when calculating closed loop pid adapter's command |
| 107 | + std::vector<double> feedforward_gain_; |
| 108 | + |
| 109 | + // Command subscribers and Controller State publisher |
| 110 | + rclcpp::Subscription<ControllerReferenceMsg>::SharedPtr ref_subscriber_ = nullptr; |
| 111 | + realtime_tools::RealtimeBuffer<std::shared_ptr<ControllerReferenceMsg>> input_ref_; |
| 112 | + |
| 113 | + rclcpp::Subscription<ControllerMeasuredStateMsg>::SharedPtr measured_state_subscriber_ = nullptr; |
| 114 | + realtime_tools::RealtimeBuffer<std::shared_ptr<ControllerMeasuredStateMsg>> measured_state_; |
| 115 | + |
| 116 | + rclcpp::Service<ControllerModeSrvType>::SharedPtr set_feedforward_control_service_; |
| 117 | + realtime_tools::RealtimeBuffer<feedforward_mode_type> control_mode_; |
| 118 | + |
| 119 | + using ControllerStatePublisher = realtime_tools::RealtimePublisher<ControllerStateMsg>; |
| 120 | + |
| 121 | + rclcpp::Publisher<ControllerStateMsg>::SharedPtr s_publisher_; |
| 122 | + std::unique_ptr<ControllerStatePublisher> state_publisher_; |
| 123 | + |
| 124 | + // override methods from ChainableControllerInterface |
| 125 | + std::vector<hardware_interface::CommandInterface> on_export_reference_interfaces() override; |
| 126 | + |
| 127 | + bool on_set_chained_mode(bool chained_mode) override; |
| 128 | + |
| 129 | + // internal methods |
| 130 | + void update_parameters(); |
| 131 | + controller_interface::CallbackReturn configure_parameters(); |
| 132 | + |
| 133 | +private: |
| 134 | + // callback for topic interface |
| 135 | + PID_CONTROLLER__VISIBILITY_LOCAL |
| 136 | + void reference_callback(const std::shared_ptr<ControllerReferenceMsg> msg); |
| 137 | +}; |
| 138 | + |
| 139 | +} // namespace pid_controller |
| 140 | + |
| 141 | +#endif // PID_CONTROLLER__PID_CONTROLLER_HPP_ |
0 commit comments