5151#include " rclcpp_action/create_server.hpp"
5252#include " rclcpp/rclcpp.hpp"
5353#include " rclcpp_action/server_goal_handle.hpp"
54+ #include " rclcpp/time.hpp"
55+ #include " rclcpp/duration.hpp"
5456
5557#include " trajectory_msgs/msg/joint_trajectory.hpp"
5658#include " trajectory_msgs/msg/joint_trajectory_point.hpp"
@@ -63,15 +65,36 @@ namespace ur_controllers
6365{
6466enum CommandInterfaces
6567{
68+ /* The PASSTHROUGH_TRAJECTORY_TRANSFER_STATE value is used to keep track of which stage the transfer is in.
69+ 0.0: No trajectory to forward, the controller is idling and ready to receive a new trajectory.
70+ 1.0: The controller has received and accepted a new trajecotry. When the state is 1.0, the controller will write a
71+ point to the hardware interface.
72+ 2.0: The hardware interface will read the point written from the controller. The state will switch between 1.0
73+ and 2.0 until all points have been read by the hardware interface.
74+ 3.0: The hardware interface has read all the points, and will now write all the points to the robot driver.
75+ 4.0: The robot is moving through the trajectory.
76+ 5.0: The robot finished executing the trajectory. */
6677 PASSTHROUGH_TRAJECTORY_TRANSFER_STATE = 0 ,
67- PASSTHROUGH_POINT_WRITTEN = 1 ,
68- PASSTHROUGH_TRAJECTORY_NUMBER_OF_POINTS = 2 ,
69- PASSTHROUGH_TRAJECTORY_CANCEL = 3 ,
70- PASSTHROUGH_TRAJECTORY_DIMENSIONS = 4 ,
71- PASSTHROUGH_TRAJECTORY_POSITIONS_ = 5 ,
72- PASSTHROUGH_TRAJECTORY_VELOCITIES_ = 11 ,
73- PASSTHROUGH_TRAJECTORY_ACCELERATIONS_ = 17 ,
74- PASSTHROUGH_TRAJECTORY_TIME_FROM_START = 23
78+ /* The PASSTHROUGH_TRAJECTORY_CANCEL value is used to indicate whether the trajectory has been cancelled from the
79+ * hardware interface.*/
80+ PASSTHROUGH_TRAJECTORY_CANCEL = 1 ,
81+ /* The PASSTHROUGH_TRAJECTORY_DIMENSIONS is used to indicate what combination of joint positions, velocities and
82+ * accelerations the trajectory consists of, see check_dimensions() for a description of what the values mean. */
83+ PASSTHROUGH_TRAJECTORY_DIMENSIONS = 2 ,
84+ /* Arrays to hold the values of a point. */
85+ PASSTHROUGH_TRAJECTORY_POSITIONS_ = 3 ,
86+ PASSTHROUGH_TRAJECTORY_VELOCITIES_ = 9 ,
87+ PASSTHROUGH_TRAJECTORY_ACCELERATIONS_ = 15 ,
88+ PASSTHROUGH_TRAJECTORY_TIME_FROM_START = 21
89+ };
90+
91+ enum StateInterfaces
92+ {
93+ /* State interface 0 - 17 are joint state interfaces*/
94+
95+ SPEED_SCALING_FACTOR = 18 ,
96+ NUMBER_OF_JOINTS = 19 ,
97+ CONTROLLER_RUNNING = 20
7598};
7699
77100class PassthroughTrajectoryController : public controller_interface ::ControllerInterface
@@ -93,32 +116,64 @@ class PassthroughTrajectoryController : public controller_interface::ControllerI
93116 controller_interface::return_type update (const rclcpp::Time& time, const rclcpp::Duration& period) override ;
94117
95118private:
119+ /* Start an action server with an action called: /passthrough_trajectory_controller/forward_joint_trajectory. */
96120 void start_action_server (void );
97121
122+ void end_goal ();
123+
124+ bool check_goal_tolerance ();
125+
98126 std::shared_ptr<passthrough_trajectory_controller::ParamListener> passthrough_param_listener_;
99127 passthrough_trajectory_controller::Params passthrough_params_;
100128
101- rclcpp_action::Server<control_msgs::action::JointTrajectory >::SharedPtr send_trajectory_action_server_;
129+ rclcpp_action::Server<control_msgs::action::FollowJointTrajectory >::SharedPtr send_trajectory_action_server_;
102130
103- rclcpp_action::GoalResponse goal_received_callback (
104- const rclcpp_action::GoalUUID& uuid, std::shared_ptr<const control_msgs::action::JointTrajectory::Goal> goal);
131+ rclcpp_action::GoalResponse
132+ goal_received_callback (const rclcpp_action::GoalUUID& uuid,
133+ std::shared_ptr<const control_msgs::action::FollowJointTrajectory::Goal> goal);
105134
106135 rclcpp_action::CancelResponse goal_cancelled_callback (
107- const std::shared_ptr<rclcpp_action::ServerGoalHandle<control_msgs::action::JointTrajectory >> goal_handle);
136+ const std::shared_ptr<rclcpp_action::ServerGoalHandle<control_msgs::action::FollowJointTrajectory >> goal_handle);
108137
109138 void goal_accepted_callback (
110- const std::shared_ptr<rclcpp_action::ServerGoalHandle<control_msgs::action::JointTrajectory>> goal_handle);
139+ const std::shared_ptr<rclcpp_action::ServerGoalHandle<control_msgs::action::FollowJointTrajectory>> goal_handle);
140+
141+ void execute (
142+ const std::shared_ptr<rclcpp_action::ServerGoalHandle<control_msgs::action::FollowJointTrajectory>> goal_handle);
143+
144+ std::vector<std::string> joint_names_;
145+ std::vector<std::string> state_interface_types_;
146+
147+ std::vector<std::reference_wrapper<hardware_interface::LoanedStateInterface>> joint_position_state_interface_;
148+ std::vector<std::reference_wrapper<hardware_interface::LoanedStateInterface>> joint_velocity_state_interface_;
149+
150+ /*
151+ If there are values in the velocities and accelerations vectors, they should have as many elements as there are
152+ joints, and all be the same size.
153+ The return value indicates what combination of joint positions, velocities and accelerations is present;
154+ 0: The trajectory is invalid, and will be rejected.
155+ 1: Only positions are defined for the trajectory.
156+ 2: Positions and velocities are defined for the trajectory.
157+ 3: Positions and accelerations are defined for the trajectory.
158+ 4: Both positions, velocities and accelerations are defined for the trajectory.
159+ */
160+ int check_dimensions (std::shared_ptr<const control_msgs::action::FollowJointTrajectory::Goal> goal);
111161
112- void
113- execute (const std::shared_ptr<rclcpp_action::ServerGoalHandle<control_msgs::action::JointTrajectory>> goal_handle);
114- int check_dimensions (std::shared_ptr<const control_msgs::action::JointTrajectory::Goal> goal);
115162 trajectory_msgs::msg::JointTrajectory active_joint_traj_;
163+ std::vector<control_msgs::msg::JointTolerance> path_tolerance_;
164+ std::vector<control_msgs::msg::JointTolerance> goal_tolerance_;
165+ rclcpp::Duration goal_time_tolerance_ = rclcpp::Duration::from_nanoseconds(0 );
166+
116167 uint32_t current_point_;
117168 bool trajectory_active_;
118- uint64_t active_trajectory_elapsed_time_;
119- uint64_t max_trajectory_time_;
169+ uint64_t period_ns;
170+ uint64_t last_time_ns;
171+ uint64_t now_ns;
172+ double active_trajectory_elapsed_time_;
173+ double max_trajectory_time_;
120174 double scaling_factor_;
121- std::shared_ptr<rclcpp_action::ServerGoalHandle<control_msgs::action::JointTrajectory>> current_handle;
175+ uint32_t number_of_joints_;
176+ std::shared_ptr<rclcpp_action::ServerGoalHandle<control_msgs::action::FollowJointTrajectory>> active_goal_;
122177};
123178} // namespace ur_controllers
124179#endif // UR_CONTROLLERS__PASSTHROUGH_TRAJECTORY_CONTROLLER_HPP_
0 commit comments