@@ -193,7 +193,7 @@ rclcpp_action::CancelResponse PassthroughTrajectoryController::goal_cancelled_ca
193193 const std::shared_ptr<rclcpp_action::ServerGoalHandle<control_msgs::action::JointTrajectory>> goal_handle)
194194{
195195 RCLCPP_INFO (get_node ()->get_logger (), " Canceling active trajectory because cancel callback received." );
196- command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_PRESENT ].set_value (0.0 );
196+ command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_TRANSFER_STATE ].set_value (0.0 );
197197 current_point_ = 0 ;
198198 command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_CANCEL].set_value (1.0 );
199199 std::shared_ptr<control_msgs::action::JointTrajectory::Result> result =
@@ -215,16 +215,7 @@ void PassthroughTrajectoryController::goal_accepted_callback(
215215 active_joint_traj_.points .back ().time_from_start .nanosec ;
216216 command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_NUMBER_OF_POINTS].set_value (
217217 active_joint_traj_.points .size ());
218- command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_PRESENT].set_value (1.0 );
219- for (int i = 0 ; i < 6 ; i++) {
220- command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_POSITIONS_ + i].set_value (
221- active_joint_traj_.points [current_point_].positions [i]);
222- }
223- command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_TIME_FROM_START].set_value (
224- active_joint_traj_.points [current_point_].time_from_start .sec +
225- (active_joint_traj_.points [current_point_].time_from_start .nanosec / 1000000000 ));
226- command_interfaces_[CommandInterfaces::PASSTHROUGH_POINT_WRITTEN].set_value (0.0 );
227- current_point_++;
218+ command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_TRANSFER_STATE].set_value (1.0 );
228219 std::thread{ std::bind (&PassthroughTrajectoryController::execute, this , std::placeholders::_1), goal_handle }
229220 .detach ();
230221 return ;
@@ -235,44 +226,50 @@ void PassthroughTrajectoryController::execute(
235226{
236227 std::cout << " ------------------Entered Execute loop--------------------------" << std::endl;
237228 rclcpp::Rate loop_rate (500 );
238- while (command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_PRESENT ].get_value () == 1 .0 ) {
229+ while (command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_TRANSFER_STATE ].get_value () != 0 .0 ) {
239230 if (command_interfaces_[PASSTHROUGH_TRAJECTORY_CANCEL].get_value () == 1.0 ) {
240231 std::cout << " ------------------Entered cancel--------------------------" << std::endl;
241232 std::shared_ptr<control_msgs::action::JointTrajectory::Result> result =
242233 std::make_shared<control_msgs::action::JointTrajectory::Result>();
243234 goal_handle->abort (result);
244- command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_PRESENT ].set_value (0.0 );
235+ command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_TRANSFER_STATE ].set_value (0.0 );
245236 trajectory_active_ = false ;
246237 return ;
247238 }
239+
248240 if (goal_handle->is_canceling ()) {
249241 std::shared_ptr<control_msgs::action::JointTrajectory::Result> result =
250242 std::make_shared<control_msgs::action::JointTrajectory::Result>();
251243 goal_handle->canceled (result);
252- } else {
253- // Write a new point to the command interface, if the previous point has been written to the hardware.
254- if (command_interfaces_[CommandInterfaces::PASSTHROUGH_POINT_WRITTEN].get_value () == 1.0 &&
255- current_point_ < active_joint_traj_.points .size ()) {
256- command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_TIME_FROM_START].set_value (
257- active_joint_traj_.points [current_point_].time_from_start .sec +
258- (active_joint_traj_.points [current_point_].time_from_start .nanosec / 1000000000 ));
259- for (int i = 0 ; i < 6 ; i++) {
260- command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_POSITIONS_ + i].set_value (
261- active_joint_traj_.points [current_point_].positions [i]);
262- }
263- command_interfaces_[CommandInterfaces::PASSTHROUGH_POINT_WRITTEN].set_value (0.0 );
264- current_point_++;
244+ return ;
245+ }
246+
247+ // Write a new point to the command interface, if the previous point has been written to the hardware interface.
248+ if (command_interfaces_[CommandInterfaces::PASSTHROUGH_POINT_WRITTEN].get_value () == 1.0 &&
249+ current_point_ < active_joint_traj_.points .size ()) {
250+ command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_TIME_FROM_START].set_value (
251+ active_joint_traj_.points [current_point_].time_from_start .sec +
252+ (active_joint_traj_.points [current_point_].time_from_start .nanosec / 1000000000 ));
253+ for (int i = 0 ; i < 6 ; i++) {
254+ command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_POSITIONS_ + i].set_value (
255+ active_joint_traj_.points [current_point_].positions [i]);
265256 }
257+ command_interfaces_[CommandInterfaces::PASSTHROUGH_POINT_WRITTEN].set_value (0.0 );
258+ current_point_++;
266259 }
267260 // Check if all points have been written to the hardware
268261 if (command_interfaces_[CommandInterfaces::PASSTHROUGH_POINT_WRITTEN].get_value () == 1.0 &&
269262 current_point_ == active_joint_traj_.points .size () &&
270- command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_PRESENT].get_value () == 1.0 ) {
271- RCLCPP_INFO (get_node ()->get_logger (), " All points sent to the robot!" );
272- command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_PRESENT].set_value (0.0 );
263+ command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_TRANSFER_STATE].get_value () == 1.0 ) {
264+ RCLCPP_INFO (get_node ()->get_logger (), " All points sent to the hardware interface, trajecotry will now execute!" );
265+ command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_TRANSFER_STATE].set_value (2.0 );
266+ }
267+ if (command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_TRANSFER_STATE].get_value () == 4.0 ) {
273268 std::shared_ptr<control_msgs::action::JointTrajectory::Result> result =
274269 std::make_shared<control_msgs::action::JointTrajectory::Result>();
275270 goal_handle->succeed (result);
271+ command_interfaces_[CommandInterfaces::PASSTHROUGH_TRAJECTORY_TRANSFER_STATE].set_value (0.0 );
272+ return ;
276273 }
277274 loop_rate.sleep ();
278275 }
0 commit comments