ros2_control - foxy
Loading...
Searching...
No Matches
joint_trajectory_controller.hpp
1// Copyright (c) 2021 ros2_control Development Team
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#ifndef JOINT_TRAJECTORY_CONTROLLER__JOINT_TRAJECTORY_CONTROLLER_HPP_
16#define JOINT_TRAJECTORY_CONTROLLER__JOINT_TRAJECTORY_CONTROLLER_HPP_
17
18#include <chrono>
19#include <memory>
20#include <mutex>
21#include <string>
22#include <utility>
23#include <vector>
24
25#include "control_msgs/action/follow_joint_trajectory.hpp"
26#include "control_msgs/msg/joint_trajectory_controller_state.hpp"
27#include "controller_interface/controller_interface.hpp"
28#include "hardware_interface/types/hardware_interface_type_values.hpp"
29#include "joint_trajectory_controller/tolerances.hpp"
30#include "joint_trajectory_controller/visibility_control.h"
31#include "rclcpp/duration.hpp"
32#include "rclcpp/subscription.hpp"
33#include "rclcpp/time.hpp"
34#include "rclcpp/timer.hpp"
35#include "rclcpp_action/server.hpp"
36#include "rclcpp_action/types.hpp"
37#include "rclcpp_lifecycle/lifecycle_publisher.hpp"
38#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
39#include "realtime_tools/realtime_buffer.h"
40#include "realtime_tools/realtime_publisher.h"
41#include "realtime_tools/realtime_server_goal_handle.h"
42#include "trajectory_msgs/msg/joint_trajectory.hpp"
43#include "trajectory_msgs/msg/joint_trajectory_point.hpp"
44
45using namespace std::chrono_literals; // NOLINT
46
47namespace rclcpp_action
48{
49template <typename ActionT>
51} // namespace rclcpp_action
52namespace rclcpp_lifecycle
53{
54class State;
55} // namespace rclcpp_lifecycle
56
58{
59class Trajectory;
60
62{
63public:
64 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
66
67 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
68 controller_interface::return_type init(const std::string & controller_name) override;
69
74 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
76
81 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
83
84 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
85 controller_interface::return_type update() override;
86
87 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
88 rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn on_configure(
89 const rclcpp_lifecycle::State & previous_state) override;
90
91 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
92 rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn on_activate(
93 const rclcpp_lifecycle::State & previous_state) override;
94
95 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
96 rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn on_deactivate(
97 const rclcpp_lifecycle::State & previous_state) override;
98
99 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
100 rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn on_cleanup(
101 const rclcpp_lifecycle::State & previous_state) override;
102
103 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
104 rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn on_error(
105 const rclcpp_lifecycle::State & previous_state) override;
106
107 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
108 rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn on_shutdown(
109 const rclcpp_lifecycle::State & previous_state) override;
110
111protected:
112 std::vector<std::string> joint_names_;
113 std::vector<std::string> command_interface_types_;
114 std::vector<std::string> state_interface_types_;
115
116 // To reduce number of variables and to make the code shorter the interfaces are ordered in types
117 // as the following constants
118 const std::vector<std::string> allowed_interface_types_ = {
119 hardware_interface::HW_IF_POSITION,
120 hardware_interface::HW_IF_VELOCITY,
121 hardware_interface::HW_IF_ACCELERATION,
122 hardware_interface::HW_IF_EFFORT,
123 };
124
125 // Parameters for some special cases, e.g. hydraulics powered robots
128 bool open_loop_control_ = false;
129 trajectory_msgs::msg::JointTrajectoryPoint last_commanded_state_;
130
131 // The interfaces are defined as the types in 'allowed_interface_types_' member.
132 // For convenience, for each type the interfaces are ordered so that i-th position
133 // matches i-th index in joint_names_
134 template <typename T>
135 using InterfaceReferences = std::vector<std::vector<std::reference_wrapper<T>>>;
136
137 InterfaceReferences<hardware_interface::LoanedCommandInterface> joint_command_interface_;
138 InterfaceReferences<hardware_interface::LoanedStateInterface> joint_state_interface_;
139
140 bool has_velocity_state_interface_ = false;
141 bool has_acceleration_state_interface_ = false;
142 bool has_position_command_interface_ = false;
143 bool has_velocity_command_interface_ = false;
144 bool has_acceleration_command_interface_ = false;
145
147 // TODO(anyone): This flag is not used for now
148 // There should be PID-approach used as in ROS1:
149 // https://github.com/ros-controls/ros_controllers/blob/noetic-devel/joint_trajectory_controller/include/joint_trajectory_controller/hardware_interface_adapter.h#L283
151
152 // TODO(karsten1987): eventually activate and deactivate subscriber directly when its supported
153 bool subscriber_is_active_ = false;
154 rclcpp::Subscription<trajectory_msgs::msg::JointTrajectory>::SharedPtr joint_command_subscriber_ =
155 nullptr;
156
157 std::shared_ptr<Trajectory> * traj_point_active_ptr_ = nullptr;
158 std::shared_ptr<Trajectory> traj_external_point_ptr_ = nullptr;
159 std::shared_ptr<Trajectory> traj_home_point_ptr_ = nullptr;
160 std::shared_ptr<trajectory_msgs::msg::JointTrajectory> traj_msg_home_ptr_ = nullptr;
162 traj_msg_external_point_ptr_;
163
164 // The controller should be in halted state after creation otherwise memory corruption
165 // TODO(anyone): Is the variable relevant, since we are using lifecycle?
166 bool is_halted_ = true;
167
168 using ControllerStateMsg = control_msgs::msg::JointTrajectoryControllerState;
170 using StatePublisherPtr = std::unique_ptr<StatePublisher>;
171 rclcpp::Publisher<ControllerStateMsg>::SharedPtr publisher_;
172 StatePublisherPtr state_publisher_;
173
174 rclcpp::Duration state_publisher_period_ = rclcpp::Duration(20ms);
175 rclcpp::Time last_state_publish_time_;
176
177 using FollowJTrajAction = control_msgs::action::FollowJointTrajectory;
179 using RealtimeGoalHandlePtr = std::shared_ptr<RealtimeGoalHandle>;
181
182 rclcpp_action::Server<FollowJTrajAction>::SharedPtr action_server_;
183 bool allow_partial_joints_goal_ = false;
185 rclcpp::TimerBase::SharedPtr goal_handle_timer_;
186 rclcpp::Duration action_monitor_period_ = rclcpp::Duration(50ms);
187
188 // callbacks for action_server_
189 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
190 rclcpp_action::GoalResponse goal_callback(
191 const rclcpp_action::GoalUUID & uuid, std::shared_ptr<const FollowJTrajAction::Goal> goal);
192 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
193 rclcpp_action::CancelResponse cancel_callback(
194 const std::shared_ptr<rclcpp_action::ServerGoalHandle<FollowJTrajAction>> goal_handle);
195 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
196 void feedback_setup_callback(
197 std::shared_ptr<rclcpp_action::ServerGoalHandle<FollowJTrajAction>> goal_handle);
198
199 // fill trajectory_msg so it matches joints controlled by this controller
200 // positions set to current position, velocities, accelerations and efforts to 0.0
201 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
202 void fill_partial_goal(
203 std::shared_ptr<trajectory_msgs::msg::JointTrajectory> trajectory_msg) const;
204 // sorts the joints of the incoming message to our local order
205 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
206 void sort_to_local_joint_order(
207 std::shared_ptr<trajectory_msgs::msg::JointTrajectory> trajectory_msg);
208 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
209 bool validate_trajectory_msg(const trajectory_msgs::msg::JointTrajectory & trajectory) const;
210 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
211 void add_new_trajectory_msg(
212 const std::shared_ptr<trajectory_msgs::msg::JointTrajectory> & traj_msg);
213 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
214 bool validate_trajectory_point_field(
215 size_t joint_names_size, const std::vector<double> & vector_field,
216 const std::string & string_for_vector_field, size_t i, bool allow_empty) const;
217
218 SegmentTolerances default_tolerances_;
219
220 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
221 void preempt_active_goal();
222 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
223 void set_hold_position();
224
225 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
226 bool reset();
227
228 using JointTrajectoryPoint = trajectory_msgs::msg::JointTrajectoryPoint;
229 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
230 void publish_state(
231 const JointTrajectoryPoint & desired_state, const JointTrajectoryPoint & current_state,
232 const JointTrajectoryPoint & state_error);
233
234 void read_state_from_hardware(JointTrajectoryPoint & state);
235
236 bool read_state_from_command_interfaces(JointTrajectoryPoint & state);
237
238private:
239 bool contains_interface_type(
240 const std::vector<std::string> & interface_type_list, const std::string & interface_type);
241
242 void resize_joint_trajectory_point(
243 trajectory_msgs::msg::JointTrajectoryPoint & point, size_t size);
244};
245
246} // namespace joint_trajectory_controller
247
248#endif // JOINT_TRAJECTORY_CONTROLLER__JOINT_TRAJECTORY_CONTROLLER_HPP_
Definition controller_interface.hpp:69
Definition joint_trajectory_controller.hpp:62
bool use_closed_loop_pid_adapter
If true, a velocity feedforward term plus corrective PID term is used.
Definition joint_trajectory_controller.hpp:150
JOINT_TRAJECTORY_CONTROLLER_PUBLIC controller_interface::InterfaceConfiguration state_interface_configuration() const override
command_interface_configuration This controller requires the position and velocity state interfaces f...
Definition joint_trajectory_controller.cpp:93
JOINT_TRAJECTORY_CONTROLLER_PUBLIC controller_interface::InterfaceConfiguration command_interface_configuration() const override
command_interface_configuration This controller requires the position command interfaces for the cont...
Definition joint_trajectory_controller.cpp:77
bool open_loop_control_
Definition joint_trajectory_controller.hpp:128
RealtimeGoalHandleBuffer rt_active_goal_
Currently active action goal, if any.
Definition joint_trajectory_controller.hpp:184
Definition trajectory.hpp:32
Definition joint_trajectory_controller.hpp:50
Definition realtime_buffer.h:51
Definition realtime_publisher.h:56
Definition realtime_server_goal_handle.h:44
Definition joint_trajectory_controller.hpp:58
Configuring what command/state interfaces to claim.
Definition controller_interface.hpp:63
Trajectory segment tolerances.
Definition tolerances.hpp:60