15 #ifndef JOINT_TRAJECTORY_CONTROLLER__JOINT_TRAJECTORY_CONTROLLER_HPP_
16 #define JOINT_TRAJECTORY_CONTROLLER__JOINT_TRAJECTORY_CONTROLLER_HPP_
23 #include "control_msgs/action/follow_joint_trajectory.hpp"
24 #include "control_msgs/msg/joint_trajectory_controller_state.hpp"
25 #include "control_msgs/srv/query_trajectory_state.hpp"
26 #include "control_toolbox/pid.hpp"
27 #include "controller_interface/controller_interface.hpp"
28 #include "hardware_interface/types/hardware_interface_type_values.hpp"
29 #include "joint_trajectory_controller/interpolation_methods.hpp"
30 #include "joint_trajectory_controller/tolerances.hpp"
31 #include "joint_trajectory_controller/trajectory.hpp"
32 #include "joint_trajectory_controller/visibility_control.h"
33 #include "rclcpp/duration.hpp"
34 #include "rclcpp/subscription.hpp"
35 #include "rclcpp/time.hpp"
36 #include "rclcpp/timer.hpp"
37 #include "rclcpp_action/server.hpp"
38 #include "rclcpp_lifecycle/state.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"
46 #include "joint_trajectory_controller_parameters.hpp"
48 using namespace std::chrono_literals;
56 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
62 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
68 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
71 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
72 controller_interface::return_type update(
73 const rclcpp::Time & time,
const rclcpp::Duration & period)
override;
75 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
76 controller_interface::CallbackReturn on_init()
override;
78 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
79 controller_interface::CallbackReturn on_configure(
80 const rclcpp_lifecycle::State & previous_state)
override;
82 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
83 controller_interface::CallbackReturn on_activate(
84 const rclcpp_lifecycle::State & previous_state)
override;
86 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
87 controller_interface::CallbackReturn on_deactivate(
88 const rclcpp_lifecycle::State & previous_state)
override;
90 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
91 controller_interface::CallbackReturn on_cleanup(
92 const rclcpp_lifecycle::State & previous_state)
override;
94 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
95 controller_interface::CallbackReturn on_error(
96 const rclcpp_lifecycle::State & previous_state)
override;
98 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
99 controller_interface::CallbackReturn on_shutdown(
100 const rclcpp_lifecycle::State & previous_state)
override;
105 const std::vector<std::string> allowed_interface_types_ = {
113 trajectory_msgs::msg::JointTrajectoryPoint state_current_;
114 trajectory_msgs::msg::JointTrajectoryPoint command_current_;
115 trajectory_msgs::msg::JointTrajectoryPoint state_desired_;
116 trajectory_msgs::msg::JointTrajectoryPoint state_error_;
122 std::vector<std::string> command_joint_names_;
125 std::shared_ptr<ParamListener> param_listener_;
128 trajectory_msgs::msg::JointTrajectoryPoint last_commanded_state_;
130 interpolation_methods::InterpolationMethod interpolation_method_{
131 interpolation_methods::DEFAULT_INTERPOLATION};
136 template <
typename T>
137 using InterfaceReferences = std::vector<std::vector<std::reference_wrapper<T>>>;
139 InterfaceReferences<hardware_interface::LoanedCommandInterface> joint_command_interface_;
140 InterfaceReferences<hardware_interface::LoanedStateInterface> joint_state_interface_;
142 bool has_position_state_interface_ =
false;
143 bool has_velocity_state_interface_ =
false;
144 bool has_acceleration_state_interface_ =
false;
145 bool has_position_command_interface_ =
false;
146 bool has_velocity_command_interface_ =
false;
147 bool has_acceleration_command_interface_ =
false;
148 bool has_effort_command_interface_ =
false;
151 bool use_closed_loop_pid_adapter_ =
false;
152 using PidPtr = std::shared_ptr<control_toolbox::Pid>;
153 std::vector<PidPtr> pids_;
155 std::vector<double> ff_velocity_scale_;
158 std::vector<bool> joints_angle_wraparound_;
160 std::vector<double> tmp_command_;
167 bool subscriber_is_active_ =
false;
168 rclcpp::Subscription<trajectory_msgs::msg::JointTrajectory>::SharedPtr joint_command_subscriber_ =
171 rclcpp::Service<control_msgs::srv::QueryTrajectoryState>::SharedPtr query_state_srv_;
173 std::shared_ptr<Trajectory> traj_external_point_ptr_ =
nullptr;
175 traj_msg_external_point_ptr_;
177 std::shared_ptr<trajectory_msgs::msg::JointTrajectory> hold_position_msg_ptr_ =
nullptr;
179 using ControllerStateMsg = control_msgs::msg::JointTrajectoryControllerState;
181 using StatePublisherPtr = std::unique_ptr<StatePublisher>;
182 rclcpp::Publisher<ControllerStateMsg>::SharedPtr publisher_;
183 StatePublisherPtr state_publisher_;
185 using FollowJTrajAction = control_msgs::action::FollowJointTrajectory;
187 using RealtimeGoalHandlePtr = std::shared_ptr<RealtimeGoalHandle>;
190 rclcpp_action::Server<FollowJTrajAction>::SharedPtr action_server_;
193 rclcpp::TimerBase::SharedPtr goal_handle_timer_;
194 rclcpp::Duration action_monitor_period_ = rclcpp::Duration(50ms);
197 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
198 void topic_callback(
const std::shared_ptr<trajectory_msgs::msg::JointTrajectory> msg);
201 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
202 rclcpp_action::GoalResponse goal_received_callback(
203 const rclcpp_action::GoalUUID & uuid, std::shared_ptr<const FollowJTrajAction::Goal> goal);
204 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
205 rclcpp_action::CancelResponse goal_cancelled_callback(
206 const std::shared_ptr<rclcpp_action::ServerGoalHandle<FollowJTrajAction>> goal_handle);
207 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
208 void goal_accepted_callback(
209 std::shared_ptr<rclcpp_action::ServerGoalHandle<FollowJTrajAction>> goal_handle);
211 using JointTrajectoryPoint = trajectory_msgs::msg::JointTrajectoryPoint;
221 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
222 void compute_error_for_joint(
223 JointTrajectoryPoint & error,
const size_t index,
const JointTrajectoryPoint & current,
224 const JointTrajectoryPoint & desired)
const;
227 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
228 void fill_partial_goal(
229 std::shared_ptr<trajectory_msgs::msg::JointTrajectory> trajectory_msg)
const;
231 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
232 void sort_to_local_joint_order(
233 std::shared_ptr<trajectory_msgs::msg::JointTrajectory> trajectory_msg)
const;
234 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
235 bool validate_trajectory_msg(
const trajectory_msgs::msg::JointTrajectory & trajectory)
const;
236 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
237 void add_new_trajectory_msg(
238 const std::shared_ptr<trajectory_msgs::msg::JointTrajectory> & traj_msg);
239 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
240 bool validate_trajectory_point_field(
241 size_t joint_names_size,
const std::vector<double> & vector_field,
242 const std::string & string_for_vector_field,
size_t i,
bool allow_empty)
const;
249 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
250 void preempt_active_goal();
254 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
255 std::shared_ptr<trajectory_msgs::msg::JointTrajectory> set_hold_position();
261 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
262 std::shared_ptr<trajectory_msgs::msg::JointTrajectory> set_success_trajectory_point();
264 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
267 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
268 bool has_active_trajectory()
const;
270 JOINT_TRAJECTORY_CONTROLLER_PUBLIC
272 const rclcpp::Time & time,
const JointTrajectoryPoint & desired_state,
273 const JointTrajectoryPoint & current_state,
const JointTrajectoryPoint & state_error);
275 void read_state_from_state_interfaces(JointTrajectoryPoint & state);
283 bool read_state_from_command_interfaces(JointTrajectoryPoint & state);
284 bool read_commands_from_command_interfaces(JointTrajectoryPoint & commands);
286 void query_state_service(
287 const std::shared_ptr<control_msgs::srv::QueryTrajectoryState::Request> request,
288 std::shared_ptr<control_msgs::srv::QueryTrajectoryState::Response> response);
293 bool contains_interface_type(
294 const std::vector<std::string> & interface_type_list,
const std::string & interface_type);
296 void init_hold_position_msg();
297 void resize_joint_trajectory_point(
298 trajectory_msgs::msg::JointTrajectoryPoint & point,
size_t size);
299 void resize_joint_trajectory_point_command(
300 trajectory_msgs::msg::JointTrajectoryPoint & point,
size_t size);
310 template <
typename T>
311 void assign_interface_from_point(
312 const T & joint_interface,
const std::vector<double> & trajectory_point_interface)
314 for (
size_t index = 0; index < dof_; ++index)
316 joint_interface[index].get().set_value(trajectory_point_interface[index]);
Definition: controller_interface.hpp:28
Definition: joint_trajectory_controller.hpp:54
realtime_tools::RealtimeBuffer< bool > rt_has_pending_goal_
Is there a pending action goal?
Definition: joint_trajectory_controller.hpp:192
RealtimeGoalHandleBuffer rt_active_goal_
Currently active action goal, if any.
Definition: joint_trajectory_controller.hpp:191
constexpr char HW_IF_EFFORT[]
Constant defining effort interface name.
Definition: hardware_interface_type_values.hpp:27
constexpr char HW_IF_ACCELERATION[]
Constant defining acceleration interface name.
Definition: hardware_interface_type_values.hpp:25
constexpr char HW_IF_VELOCITY[]
Constant defining velocity interface name.
Definition: hardware_interface_type_values.hpp:23
constexpr char HW_IF_POSITION[]
Constant defining position interface name.
Definition: hardware_interface_type_values.hpp:21
Definition: interpolation_methods.hpp:24
Configuring what command/state interfaces to claim.
Definition: controller_interface_base.hpp:56
Trajectory segment tolerances.
Definition: tolerances.hpp:59