29
29
// -- END LICENSE BLOCK ------------------------------------------------
30
30
31
31
#include " ur_client_library/ur/instruction_executor.h"
32
+ #include < mutex>
32
33
#include " ur_client_library/control/trajectory_point_interface.h"
33
34
void urcl::InstructionExecutor::trajDoneCallback (const urcl::control::TrajectoryResult& result)
34
35
{
36
+ URCL_LOG_DEBUG (" Trajectory result received: %s" , control::trajectoryResultToString (result).c_str ());
35
37
std::unique_lock<std::mutex> lock (trajectory_result_mutex_);
36
38
trajectory_result_ = result;
37
39
trajectory_running_ = false ;
@@ -40,6 +42,7 @@ void urcl::InstructionExecutor::trajDisconnectCallback(const int filedescriptor)
40
42
{
41
43
URCL_LOG_INFO (" Trajectory disconnect" );
42
44
std::unique_lock<std::mutex> lock (trajectory_result_mutex_);
45
+ trajectory_done_cv_.notify_all ();
43
46
if (trajectory_running_)
44
47
{
45
48
trajectory_result_ = urcl::control::TrajectoryResult::TRAJECTORY_RESULT_FAILURE;
@@ -86,17 +89,20 @@ bool urcl::InstructionExecutor::executeMotion(
86
89
}
87
90
}
88
91
trajectory_running_ = true ;
92
+ cancel_requested_ = false ;
89
93
90
- while (trajectory_running_)
94
+ while (trajectory_running_ && !cancel_requested_ )
91
95
{
92
- std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
93
96
driver_->writeTrajectoryControlMessage (urcl::control::TrajectoryControlMessage::TRAJECTORY_NOOP);
97
+ std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
94
98
}
99
+ if (!cancel_requested_)
95
100
{
96
101
std::unique_lock<std::mutex> lock (trajectory_result_mutex_);
97
102
URCL_LOG_INFO (" Trajectory done with result %s" , control::trajectoryResultToString (trajectory_result_).c_str ());
98
103
return trajectory_result_ == urcl::control::TrajectoryResult::TRAJECTORY_RESULT_SUCCESS;
99
104
}
105
+ return false ;
100
106
}
101
107
bool urcl::InstructionExecutor::moveJ (const urcl::vector6d_t & target, const double acceleration, const double velocity,
102
108
const double time, const double blend_radius)
@@ -110,3 +116,19 @@ bool urcl::InstructionExecutor::moveL(const urcl::Pose& target, const double acc
110
116
return executeMotion ({ std::make_shared<control::MoveLPrimitive>(
111
117
target, blend_radius, std::chrono::milliseconds (static_cast <int >(time * 1000 )), acceleration, velocity) });
112
118
}
119
+
120
+ bool urcl::InstructionExecutor::cancelMotion ()
121
+ {
122
+ cancel_requested_ = true ;
123
+ if (trajectory_running_)
124
+ {
125
+ URCL_LOG_INFO (" Cancel motion" );
126
+ driver_->writeTrajectoryControlMessage (urcl::control::TrajectoryControlMessage::TRAJECTORY_CANCEL);
127
+ std::unique_lock<std::mutex> lock (trajectory_result_mutex_);
128
+ if (trajectory_done_cv_.wait_for (lock, std::chrono::milliseconds (1000 )) == std::cv_status::no_timeout)
129
+ {
130
+ return trajectory_result_ == urcl::control::TrajectoryResult::TRAJECTORY_RESULT_CANCELED;
131
+ }
132
+ }
133
+ return false ;
134
+ }
0 commit comments