ros2_control - jazzy
Loading...
Searching...
No Matches
diff_drive_controller.hpp
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 <memory>
24#include <queue>
25#include <string>
26#include <vector>
27
28#include "controller_interface/chainable_controller_interface.hpp"
29#include "diff_drive_controller/odometry.hpp"
30#include "diff_drive_controller/speed_limiter.hpp"
31#include "geometry_msgs/msg/twist_stamped.hpp"
32#include "nav_msgs/msg/odometry.hpp"
33#include "odometry.hpp"
34#include "rclcpp_lifecycle/state.hpp"
35#include "realtime_tools/realtime_publisher.hpp"
36#include "realtime_tools/realtime_thread_safe_box.hpp"
37#include "tf2_msgs/msg/tf_message.hpp"
38
39// auto-generated by generate_parameter_library
40#include "diff_drive_controller/diff_drive_controller_parameters.hpp"
41
42namespace diff_drive_controller
43{
45{
46 using TwistStamped = geometry_msgs::msg::TwistStamped;
47
48public:
50
52
54
55 // Chainable controller replaces update() with the following two functions
56 controller_interface::return_type update_reference_from_subscribers(
57 const rclcpp::Time & time, const rclcpp::Duration & period) override;
58
59 controller_interface::return_type update_and_write_commands(
60 const rclcpp::Time & time, const rclcpp::Duration & period) override;
61
62 controller_interface::CallbackReturn on_init() override;
63
64 controller_interface::CallbackReturn on_configure(
65 const rclcpp_lifecycle::State & previous_state) override;
66
67 controller_interface::CallbackReturn on_activate(
68 const rclcpp_lifecycle::State & previous_state) override;
69
70 controller_interface::CallbackReturn on_deactivate(
71 const rclcpp_lifecycle::State & previous_state) override;
72
73 controller_interface::CallbackReturn on_cleanup(
74 const rclcpp_lifecycle::State & previous_state) override;
75
76 controller_interface::CallbackReturn on_error(
77 const rclcpp_lifecycle::State & previous_state) override;
78
79protected:
80 bool on_set_chained_mode(bool chained_mode) override;
81
82 std::vector<hardware_interface::CommandInterface::SharedPtr> on_export_reference_interfaces_list()
83 override;
84
86 {
87 std::optional<std::reference_wrapper<const hardware_interface::LoanedStateInterface>> feedback;
88 std::reference_wrapper<hardware_interface::LoanedCommandInterface> velocity;
89 };
90
91 const char * feedback_type() const;
92 controller_interface::CallbackReturn configure_side(
93 const std::string & side, const std::vector<std::string> & wheel_names,
94 std::vector<WheelHandle> & registered_handles);
95
96 std::vector<WheelHandle> registered_left_wheel_handles_;
97 std::vector<WheelHandle> registered_right_wheel_handles_;
98
99 // Parameters from ROS for diff_drive_controller
100 std::shared_ptr<ParamListener> param_listener_;
101 Params params_;
102 /* Number of wheels on each side of the robot. This is important to take the wheels slip into
103 * account when multiple wheels on each side are present. If there are more wheels then control
104 * signals for each side, you should enter number for control signals. For example, Husky has two
105 * wheels on each side, but they use one control signal, in this case '1' is the correct value of
106 * the parameter. */
107 int wheels_per_side_;
108
109 Odometry odometry_;
110
111 // Timeout to consider cmd_vel commands old
112 rclcpp::Duration cmd_vel_timeout_ = rclcpp::Duration::from_seconds(0.5);
113
114 std::shared_ptr<rclcpp::Publisher<nav_msgs::msg::Odometry>> odometry_publisher_ = nullptr;
115 std::shared_ptr<realtime_tools::RealtimePublisher<nav_msgs::msg::Odometry>>
116 realtime_odometry_publisher_ = nullptr;
117 nav_msgs::msg::Odometry odometry_message_;
118
119 std::shared_ptr<rclcpp::Publisher<tf2_msgs::msg::TFMessage>> odometry_transform_publisher_ =
120 nullptr;
121 std::shared_ptr<realtime_tools::RealtimePublisher<tf2_msgs::msg::TFMessage>>
122 realtime_odometry_transform_publisher_ = nullptr;
123 tf2_msgs::msg::TFMessage odometry_transform_message_;
124
125 bool subscriber_is_active_ = false;
126 rclcpp::Subscription<TwistStamped>::SharedPtr velocity_command_subscriber_ = nullptr;
127
128 // the realtime container to exchange the reference from subscriber
130 // save the last reference in case of unable to get value from box
131 TwistStamped command_msg_;
132
133 std::queue<std::array<double, 2>> previous_two_commands_;
134 // speed limiters
135 std::unique_ptr<SpeedLimiter> limiter_linear_;
136 std::unique_ptr<SpeedLimiter> limiter_angular_;
137
138 std::shared_ptr<rclcpp::Publisher<TwistStamped>> limited_velocity_publisher_ = nullptr;
139 std::shared_ptr<realtime_tools::RealtimePublisher<TwistStamped>>
140 realtime_limited_velocity_publisher_ = nullptr;
141 TwistStamped limited_velocity_message_;
142
143 rclcpp::Time previous_update_timestamp_{0};
144
145 // publish rate limiter
146 double publish_rate_ = 50.0;
147 rclcpp::Duration publish_period_ = rclcpp::Duration::from_nanoseconds(0);
148 rclcpp::Time previous_publish_timestamp_{0, 0, RCL_CLOCK_UNINITIALIZED};
149
150 bool command_timed_out_ = false;
151
152 bool reset();
153 void halt();
154
155private:
156 void reset_buffers();
157};
158} // namespace diff_drive_controller
159#endif // DIFF_DRIVE_CONTROLLER__DIFF_DRIVE_CONTROLLER_HPP_
Virtual class to implement when integrating a controller that can be preceded by other controllers.
Definition chainable_controller_interface.hpp:38
Definition diff_drive_controller.hpp:45
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 diff_drive_controller.cpp:107
bool on_set_chained_mode(bool chained_mode) override
Virtual method that each chainable controller should implement to switch chained mode.
Definition diff_drive_controller.cpp:751
std::vector< hardware_interface::CommandInterface::SharedPtr > on_export_reference_interfaces_list() override
Virtual method that each chainable controller should implement to export its read/write chainable int...
Definition diff_drive_controller.cpp:754
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 diff_drive_controller.cpp:154
controller_interface::InterfaceConfiguration state_interface_configuration() const override
Get configuration for controller's required state interfaces.
Definition diff_drive_controller.cpp:88
controller_interface::CallbackReturn on_init() override
Extending interface with initialization method which is individual for each controller.
Definition diff_drive_controller.cpp:57
controller_interface::InterfaceConfiguration command_interface_configuration() const override
Get configuration for controller's required command interfaces.
Definition diff_drive_controller.cpp:74
Definition odometry.hpp:36
Definition realtime_thread_safe_box.hpp:68
Configuring what command/state interfaces to claim.
Definition controller_interface_base.hpp:78
Definition diff_drive_controller.hpp:86