|
| 1 | +// Copyright 2020 PAL Robotics S.L. |
| 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 | +/* |
| 16 | + * Author: Bence Magyar, Enrique Fernández, Manuel Meraz |
| 17 | + */ |
| 18 | + |
| 19 | +#ifndef DIFF_DRIVE_CONTROLLER__DIFF_DRIVE_CONTROLLER_HPP_ |
| 20 | +#define DIFF_DRIVE_CONTROLLER__DIFF_DRIVE_CONTROLLER_HPP_ |
| 21 | + |
| 22 | +#include <chrono> |
| 23 | +#include <cmath> |
| 24 | +#include <memory> |
| 25 | +#include <queue> |
| 26 | +#include <string> |
| 27 | +#include <vector> |
| 28 | + |
| 29 | +#include "controller_interface/controller_interface.hpp" |
| 30 | +#include "diff_drive_controller/odometry.hpp" |
| 31 | +#include "diff_drive_controller/speed_limiter.hpp" |
| 32 | +#include "diff_drive_controller/visibility_control.h" |
| 33 | +#include "geometry_msgs/msg/twist.hpp" |
| 34 | +#include "geometry_msgs/msg/twist_stamped.hpp" |
| 35 | +#include "hardware_interface/joint_command_handle.hpp" |
| 36 | +#include "hardware_interface/operation_mode_handle.hpp" |
| 37 | +#include "hardware_interface/robot_hardware.hpp" |
| 38 | +#include "nav_msgs/msg/odometry.hpp" |
| 39 | +#include "odometry.hpp" |
| 40 | +#include "rclcpp/rclcpp.hpp" |
| 41 | +#include "rclcpp_lifecycle/state.hpp" |
| 42 | +#include "realtime_tools/realtime_buffer.h" |
| 43 | +#include "realtime_tools/realtime_publisher.h" |
| 44 | +#include "sensor_msgs/msg/joint_state.hpp" |
| 45 | +#include "tf2_msgs/msg/tf_message.hpp" |
| 46 | + |
| 47 | +namespace diff_drive_controller |
| 48 | +{ |
| 49 | +class DiffDriveController : public controller_interface::ControllerInterface |
| 50 | +{ |
| 51 | + using Twist = geometry_msgs::msg::TwistStamped; |
| 52 | + |
| 53 | +public: |
| 54 | + DIFF_DRIVE_CONTROLLER_PUBLIC |
| 55 | + DiffDriveController(); |
| 56 | + |
| 57 | + DIFF_DRIVE_CONTROLLER_PUBLIC |
| 58 | + DiffDriveController( |
| 59 | + std::vector<std::string> left_wheel_names, |
| 60 | + std::vector<std::string> right_wheel_names, |
| 61 | + std::vector<std::string> operation_mode_names); |
| 62 | + |
| 63 | + DIFF_DRIVE_CONTROLLER_PUBLIC |
| 64 | + controller_interface::return_type |
| 65 | + init( |
| 66 | + std::weak_ptr<hardware_interface::RobotHardware> robot_hardware, |
| 67 | + const std::string & controller_name) override; |
| 68 | + |
| 69 | + DIFF_DRIVE_CONTROLLER_PUBLIC |
| 70 | + controller_interface::return_type update() override; |
| 71 | + |
| 72 | + DIFF_DRIVE_CONTROLLER_PUBLIC |
| 73 | + rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn |
| 74 | + on_configure(const rclcpp_lifecycle::State & previous_state) override; |
| 75 | + |
| 76 | + DIFF_DRIVE_CONTROLLER_PUBLIC |
| 77 | + rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn |
| 78 | + on_activate(const rclcpp_lifecycle::State & previous_state) override; |
| 79 | + |
| 80 | + DIFF_DRIVE_CONTROLLER_PUBLIC |
| 81 | + rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn |
| 82 | + on_deactivate(const rclcpp_lifecycle::State & previous_state) override; |
| 83 | + |
| 84 | + DIFF_DRIVE_CONTROLLER_PUBLIC |
| 85 | + rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn |
| 86 | + on_cleanup(const rclcpp_lifecycle::State & previous_state) override; |
| 87 | + |
| 88 | + DIFF_DRIVE_CONTROLLER_PUBLIC |
| 89 | + rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn |
| 90 | + on_error(const rclcpp_lifecycle::State & previous_state) override; |
| 91 | + |
| 92 | + DIFF_DRIVE_CONTROLLER_PUBLIC |
| 93 | + rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn |
| 94 | + on_shutdown(const rclcpp_lifecycle::State & previous_state) override; |
| 95 | + |
| 96 | +protected: |
| 97 | + struct WheelHandle |
| 98 | + { |
| 99 | + const hardware_interface::JointStateHandle * state = nullptr; |
| 100 | + hardware_interface::JointCommandHandle * command = nullptr; |
| 101 | + }; |
| 102 | + |
| 103 | + CallbackReturn configure_side( |
| 104 | + const std::string & side, |
| 105 | + const std::vector<std::string> & wheel_names, |
| 106 | + std::vector<WheelHandle> & registered_handles, |
| 107 | + hardware_interface::RobotHardware & robot_hardware); |
| 108 | + |
| 109 | + std::vector<std::string> left_wheel_names_; |
| 110 | + std::vector<std::string> right_wheel_names_; |
| 111 | + |
| 112 | + std::vector<WheelHandle> registered_left_wheel_handles_; |
| 113 | + std::vector<WheelHandle> registered_right_wheel_handles_; |
| 114 | + |
| 115 | + struct WheelParams |
| 116 | + { |
| 117 | + size_t wheels_per_side = 0; |
| 118 | + double separation = 0.0; // w.r.t. the midpoint of the wheel width |
| 119 | + double radius = 0.0; // Assumed to be the same for both wheels |
| 120 | + double separation_multiplier = 1.0; |
| 121 | + double left_radius_multiplier = 1.0; |
| 122 | + double right_radius_multiplier = 1.0; |
| 123 | + } wheel_params_; |
| 124 | + |
| 125 | + struct OdometryParams |
| 126 | + { |
| 127 | + bool open_loop = false; |
| 128 | + bool enable_odom_tf = true; |
| 129 | + std::string base_frame_id = "base_link"; |
| 130 | + std::string odom_frame_id = "odom"; |
| 131 | + std::array<double, 6> pose_covariance_diagonal; |
| 132 | + std::array<double, 6> twist_covariance_diagonal; |
| 133 | + } odom_params_; |
| 134 | + |
| 135 | + Odometry odometry_; |
| 136 | + |
| 137 | + std::shared_ptr<rclcpp_lifecycle::LifecyclePublisher<nav_msgs::msg::Odometry>> odometry_publisher_ |
| 138 | + = |
| 139 | + nullptr; |
| 140 | + std::shared_ptr<realtime_tools::RealtimePublisher<nav_msgs::msg::Odometry>> |
| 141 | + realtime_odometry_publisher_ = nullptr; |
| 142 | + |
| 143 | + std::shared_ptr<rclcpp_lifecycle::LifecyclePublisher<tf2_msgs::msg::TFMessage>> |
| 144 | + odometry_transform_publisher_ = nullptr; |
| 145 | + std::shared_ptr<realtime_tools::RealtimePublisher<tf2_msgs::msg::TFMessage>> |
| 146 | + realtime_odometry_transform_publisher_ = nullptr; |
| 147 | + |
| 148 | + // Timeout to consider cmd_vel commands old |
| 149 | + std::chrono::milliseconds cmd_vel_timeout_{500}; |
| 150 | + |
| 151 | + std::vector<std::string> write_op_names_; |
| 152 | + std::vector<hardware_interface::OperationModeHandle *> registered_operation_mode_handles_; |
| 153 | + |
| 154 | + bool subscriber_is_active_ = false; |
| 155 | + rclcpp::Subscription<Twist>::SharedPtr velocity_command_subscriber_ = nullptr; |
| 156 | + |
| 157 | + std::shared_ptr<Twist> received_velocity_msg_ptr_ = nullptr; |
| 158 | + |
| 159 | + std::queue<Twist> previous_commands_; // last two commands |
| 160 | + |
| 161 | + // speed limiters |
| 162 | + SpeedLimiter limiter_linear_; |
| 163 | + SpeedLimiter limiter_angular_; |
| 164 | + |
| 165 | + bool publish_limited_velocity_ = false; |
| 166 | + std::shared_ptr<rclcpp_lifecycle::LifecyclePublisher<Twist>> limited_velocity_publisher_ = |
| 167 | + nullptr; |
| 168 | + std::shared_ptr<realtime_tools::RealtimePublisher<Twist>> |
| 169 | + realtime_limited_velocity_publisher_ = nullptr; |
| 170 | + |
| 171 | + rclcpp::Time previous_update_timestamp_{0}; |
| 172 | + |
| 173 | + bool is_halted = false; |
| 174 | + |
| 175 | + bool reset(); |
| 176 | + void set_op_mode(const hardware_interface::OperationMode & mode); |
| 177 | + void halt(); |
| 178 | +}; |
| 179 | +} // namespace diff_drive_controller |
| 180 | +#endif // DIFF_DRIVE_CONTROLLER__DIFF_DRIVE_CONTROLLER_HPP_ |
0 commit comments