3131#include " ur_client_library/ur/instruction_executor.h"
3232void urcl::InstructionExecutor::trajDoneCallback (const urcl::control::TrajectoryResult& result)
3333{
34- result_ = result;
35- trajectory_done_ = true ;
34+ std::unique_lock<std::mutex> lock (trajectory_result_mutex_);
35+ trajectory_result_ = result;
36+ trajectory_running_ = false ;
3637}
3738void urcl::InstructionExecutor::trajDisconnectCallback (const int filedescriptor)
3839{
3940 URCL_LOG_INFO (" Trajectory disconnect" );
40- result_ = urcl::control::TrajectoryResult::TRAJECTORY_RESULT_FAILURE;
41- trajectory_done_ = true ;
41+ std::unique_lock<std::mutex> lock (trajectory_result_mutex_);
42+ if (trajectory_running_)
43+ {
44+ trajectory_result_ = urcl::control::TrajectoryResult::TRAJECTORY_RESULT_FAILURE;
45+ trajectory_running_ = false ;
46+ }
4247}
43- void urcl::InstructionExecutor::executeMotion (
48+ bool urcl::InstructionExecutor::executeMotion (
4449 const std::vector<std::shared_ptr<control::MotionPrimitive>>& motion_sequence)
4550{
46- trajectory_done_ = false ;
4751 if (!driver_->writeTrajectoryControlMessage (urcl::control::TrajectoryControlMessage::TRAJECTORY_START,
4852 motion_sequence.size ()))
4953 {
5054 URCL_LOG_ERROR (" Cannot send trajectory control command. No client connected?" );
51- return ;
52- ;
55+ std::unique_lock<std::mutex> lock (trajectory_result_mutex_);
56+ trajectory_result_ = urcl::control::TrajectoryResult::TRAJECTORY_RESULT_FAILURE;
57+ return false ;
5358 }
5459
5560 for (const auto & primitive : motion_sequence)
@@ -69,43 +74,38 @@ void urcl::InstructionExecutor::executeMotion(
6974 urcl::vector6d_t pose_vec = { movel_primitive->target_pose .x , movel_primitive->target_pose .y ,
7075 movel_primitive->target_pose .z , movel_primitive->target_pose .rx ,
7176 movel_primitive->target_pose .ry , movel_primitive->target_pose .rz };
72- if (!driver_->writeTrajectoryPoint (pose_vec, primitive->acceleration , primitive->velocity , true ,
73- primitive->duration .count (), primitive->blend_radius ))
74- {
75- URCL_LOG_ERROR (" Cannot send trajectory control command. No client connected?" );
76- return ;
77- }
77+ driver_->writeTrajectoryPoint (pose_vec, primitive->acceleration , primitive->velocity , true ,
78+ primitive->duration .count (), primitive->blend_radius );
7879 break ;
7980 }
8081 default :
8182 URCL_LOG_ERROR (" Unsupported motion type" );
82- return ;
83+ std::unique_lock<std::mutex> lock (trajectory_result_mutex_);
84+ trajectory_result_ = urcl::control::TrajectoryResult::TRAJECTORY_RESULT_FAILURE;
85+ return false ;
8386 }
8487 }
88+ trajectory_running_ = true ;
8589
86- while (!trajectory_done_ )
90+ while (trajectory_running_ )
8791 {
8892 std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
89- if (!driver_->writeTrajectoryControlMessage (urcl::control::TrajectoryControlMessage::TRAJECTORY_NOOP))
90- {
91- URCL_LOG_ERROR (" Cannot send trajectory control command. No client connected?" );
92- return ;
93- ;
94- }
93+ driver_->writeTrajectoryControlMessage (urcl::control::TrajectoryControlMessage::TRAJECTORY_NOOP);
9594 }
96- URCL_LOG_INFO (" Trajectory done with result %d" , result_);
95+ URCL_LOG_INFO (" Trajectory done with result %d" , trajectory_result_);
96+ return true ;
9797}
9898bool urcl::InstructionExecutor::moveJ (const urcl::vector6d_t & target, const double acceleration, const double velocity,
9999 const double time, const double blend_radius)
100100{
101101 executeMotion ({ std::make_shared<control::MoveJPrimitive>(
102102 target, blend_radius, std::chrono::milliseconds (static_cast <int >(time * 1000 )), acceleration, velocity) });
103- return result_ == urcl::control::TrajectoryResult::TRAJECTORY_RESULT_SUCCESS;
103+ return trajectory_result_ == urcl::control::TrajectoryResult::TRAJECTORY_RESULT_SUCCESS;
104104}
105105bool urcl::InstructionExecutor::moveL (const urcl::Pose& target, const double acceleration, const double velocity,
106106 const double time, const double blend_radius)
107107{
108108 executeMotion ({ std::make_shared<control::MoveLPrimitive>(
109109 target, blend_radius, std::chrono::milliseconds (static_cast <int >(time * 1000 )), acceleration, velocity) });
110- return result_ == urcl::control::TrajectoryResult::TRAJECTORY_RESULT_SUCCESS;
111- }
110+ return trajectory_result_ == urcl::control::TrajectoryResult::TRAJECTORY_RESULT_SUCCESS;
111+ }
0 commit comments