ros2_control - rolling
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 <functional> // for std::reference_wrapper
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
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"
44 
45 // auto-generated by generate_parameter_library
46 #include "joint_trajectory_controller_parameters.hpp"
47 
48 using namespace std::chrono_literals; // NOLINT
49 
51 {
52 
54 {
55 public:
56  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
58 
62  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
63  controller_interface::InterfaceConfiguration command_interface_configuration() const override;
64 
68  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
69  controller_interface::InterfaceConfiguration state_interface_configuration() const override;
70 
71  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
72  controller_interface::return_type update(
73  const rclcpp::Time & time, const rclcpp::Duration & period) override;
74 
75  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
76  controller_interface::CallbackReturn on_init() override;
77 
78  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
79  controller_interface::CallbackReturn on_configure(
80  const rclcpp_lifecycle::State & previous_state) override;
81 
82  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
83  controller_interface::CallbackReturn on_activate(
84  const rclcpp_lifecycle::State & previous_state) override;
85 
86  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
87  controller_interface::CallbackReturn on_deactivate(
88  const rclcpp_lifecycle::State & previous_state) override;
89 
90  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
91  controller_interface::CallbackReturn on_cleanup(
92  const rclcpp_lifecycle::State & previous_state) override;
93 
94  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
95  controller_interface::CallbackReturn on_error(
96  const rclcpp_lifecycle::State & previous_state) override;
97 
98  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
99  controller_interface::CallbackReturn on_shutdown(
100  const rclcpp_lifecycle::State & previous_state) override;
101 
102 protected:
103  // To reduce number of variables and to make the code shorter the interfaces are ordered in types
104  // as the following constants
105  const std::vector<std::string> allowed_interface_types_ = {
110  };
111 
112  // Preallocate variables used in the realtime update() function
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_;
117 
118  // Degrees of freedom
119  size_t dof_;
120 
121  // Storing command joint names for interfaces
122  std::vector<std::string> command_joint_names_;
123 
124  // Parameters from ROS for joint_trajectory_controller
125  std::shared_ptr<ParamListener> param_listener_;
126  Params params_;
127 
128  trajectory_msgs::msg::JointTrajectoryPoint last_commanded_state_;
130  interpolation_methods::InterpolationMethod interpolation_method_{
131  interpolation_methods::DEFAULT_INTERPOLATION};
132 
133  // The interfaces are defined as the types in 'allowed_interface_types_' member.
134  // For convenience, for each type the interfaces are ordered so that i-th position
135  // matches i-th index in joint_names_
136  template <typename T>
137  using InterfaceReferences = std::vector<std::vector<std::reference_wrapper<T>>>;
138 
139  InterfaceReferences<hardware_interface::LoanedCommandInterface> joint_command_interface_;
140  InterfaceReferences<hardware_interface::LoanedStateInterface> joint_state_interface_;
141 
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;
149 
151  bool use_closed_loop_pid_adapter_ = false;
152  using PidPtr = std::shared_ptr<control_toolbox::Pid>;
153  std::vector<PidPtr> pids_;
154  // Feed-forward velocity weight factor when calculating closed loop pid adapter's command
155  std::vector<double> ff_velocity_scale_;
156  // Configuration for every joint if it wraps around (ie. is continuous, position error is
157  // normalized)
158  std::vector<bool> joints_angle_wraparound_;
159  // reserved storage for result of the command when closed loop pid adapter is used
160  std::vector<double> tmp_command_;
161 
162  // Timeout to consider commands old
163  double cmd_timeout_;
164  // True if holding position or repeating last trajectory point in case of success
166  // TODO(karsten1987): eventually activate and deactivate subscriber directly when its supported
167  bool subscriber_is_active_ = false;
168  rclcpp::Subscription<trajectory_msgs::msg::JointTrajectory>::SharedPtr joint_command_subscriber_ =
169  nullptr;
170 
171  rclcpp::Service<control_msgs::srv::QueryTrajectoryState>::SharedPtr query_state_srv_;
172 
173  std::shared_ptr<Trajectory> traj_external_point_ptr_ = nullptr;
175  traj_msg_external_point_ptr_;
176 
177  std::shared_ptr<trajectory_msgs::msg::JointTrajectory> hold_position_msg_ptr_ = nullptr;
178 
179  using ControllerStateMsg = control_msgs::msg::JointTrajectoryControllerState;
181  using StatePublisherPtr = std::unique_ptr<StatePublisher>;
182  rclcpp::Publisher<ControllerStateMsg>::SharedPtr publisher_;
183  StatePublisherPtr state_publisher_;
184 
185  using FollowJTrajAction = control_msgs::action::FollowJointTrajectory;
187  using RealtimeGoalHandlePtr = std::shared_ptr<RealtimeGoalHandle>;
189 
190  rclcpp_action::Server<FollowJTrajAction>::SharedPtr action_server_;
193  rclcpp::TimerBase::SharedPtr goal_handle_timer_;
194  rclcpp::Duration action_monitor_period_ = rclcpp::Duration(50ms);
195 
196  // callback for topic interface
197  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
198  void topic_callback(const std::shared_ptr<trajectory_msgs::msg::JointTrajectory> msg);
199 
200  // callbacks for action_server_
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);
210 
211  using JointTrajectoryPoint = trajectory_msgs::msg::JointTrajectoryPoint;
212 
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;
225  // fill trajectory_msg so it matches joints controlled by this controller
226  // positions set to current position, velocities, accelerations and efforts to 0.0
227  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
228  void fill_partial_goal(
229  std::shared_ptr<trajectory_msgs::msg::JointTrajectory> trajectory_msg) const;
230  // sorts the joints of the incoming message to our local order
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;
243 
244  // the tolerances from the node parameter
245  SegmentTolerances default_tolerances_;
246  // the tolerances used for the current goal
248 
249  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
250  void preempt_active_goal();
251 
254  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
255  std::shared_ptr<trajectory_msgs::msg::JointTrajectory> set_hold_position();
256 
261  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
262  std::shared_ptr<trajectory_msgs::msg::JointTrajectory> set_success_trajectory_point();
263 
264  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
265  bool reset();
266 
267  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
268  bool has_active_trajectory() const;
269 
270  JOINT_TRAJECTORY_CONTROLLER_PUBLIC
271  void publish_state(
272  const rclcpp::Time & time, const JointTrajectoryPoint & desired_state,
273  const JointTrajectoryPoint & current_state, const JointTrajectoryPoint & state_error);
274 
275  void read_state_from_state_interfaces(JointTrajectoryPoint & state);
276 
283  bool read_state_from_command_interfaces(JointTrajectoryPoint & state);
284  bool read_commands_from_command_interfaces(JointTrajectoryPoint & commands);
285 
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);
289 
290 private:
291  void update_pids();
292 
293  bool contains_interface_type(
294  const std::vector<std::string> & interface_type_list, const std::string & interface_type);
295 
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);
301 
310  template <typename T>
311  void assign_interface_from_point(
312  const T & joint_interface, const std::vector<double> & trajectory_point_interface)
313  {
314  for (size_t index = 0; index < dof_; ++index)
315  {
316  joint_interface[index].get().set_value(trajectory_point_interface[index]);
317  }
318  }
319 };
320 
321 } // namespace joint_trajectory_controller
322 
323 #endif // JOINT_TRAJECTORY_CONTROLLER__JOINT_TRAJECTORY_CONTROLLER_HPP_
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
Definition: realtime_publisher.h:54
Definition: realtime_server_goal_handle.h:44
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