Skip to content

Commit 000a6b7

Browse files
committed
Add test for cancel timeout
1 parent 360553d commit 000a6b7

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/ur/instruction_executor.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include "ur_client_library/ur/instruction_executor.h"
3232
#include <mutex>
3333
#include "ur_client_library/control/trajectory_point_interface.h"
34+
#include "ur_client_library/log.h"
35+
#include "ur_client_library/ur/robot_receive_timeout.h"
3436
void urcl::InstructionExecutor::trajDoneCallback(const urcl::control::TrajectoryResult& result)
3537
{
3638
URCL_LOG_DEBUG("Trajectory result received: %s", control::trajectoryResultToString(result).c_str());
@@ -124,12 +126,19 @@ bool urcl::InstructionExecutor::cancelMotion()
124126
if (trajectory_running_)
125127
{
126128
URCL_LOG_INFO("Cancel motion");
127-
driver_->writeTrajectoryControlMessage(urcl::control::TrajectoryControlMessage::TRAJECTORY_CANCEL);
129+
driver_->writeTrajectoryControlMessage(urcl::control::TrajectoryControlMessage::TRAJECTORY_CANCEL, -1,
130+
RobotReceiveTimeout::millisec(2000));
128131
std::unique_lock<std::mutex> lock(trajectory_result_mutex_);
129-
if (trajectory_done_cv_.wait_for(lock, std::chrono::milliseconds(1000)) == std::cv_status::no_timeout)
132+
if (trajectory_done_cv_.wait_for(lock, std::chrono::milliseconds(200)) == std::cv_status::no_timeout)
130133
{
131134
return trajectory_result_ == urcl::control::TrajectoryResult::TRAJECTORY_RESULT_CANCELED;
132135
}
136+
else
137+
{
138+
URCL_LOG_ERROR("Sent a canceling request to the robot but waiting for the answer timed out.");
139+
return false;
140+
}
133141
}
142+
URCL_LOG_WARN("Canceling motion requested without a motion running.");
134143
return false;
135144
}

tests/test_instruction_executor.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
// -- END LICENSE BLOCK ------------------------------------------------
3030

3131
#include <gtest/gtest.h>
32+
#include <algorithm>
3233
#include <iostream>
34+
#include <regex>
3335
#include <thread>
3436
#include "ur_client_library/ur/instruction_executor.h"
3537
#include "ur_client_library/control/motion_primitives.h"
@@ -70,6 +72,16 @@ class InstructionExecutorTest : public ::testing::Test
7072
ASSERT_TRUE(g_my_robot->waitForProgramRunning());
7173
}
7274
}
75+
void TearDown()
76+
{
77+
g_my_robot->ur_driver_->stopControl();
78+
g_my_robot->waitForProgramNotRunning(1000);
79+
}
80+
81+
static void TearDownTestSuite()
82+
{
83+
g_my_robot.reset();
84+
}
7385
};
7486

7587
TEST_F(InstructionExecutorTest, execute_motion_sequence_success)
@@ -225,6 +237,49 @@ TEST_F(InstructionExecutorTest, canceling_without_running_trajectory_returns_fal
225237
ASSERT_FALSE(executor_->cancelMotion());
226238
}
227239

240+
TEST(InstructionExecutorTestStandalone, canceling_without_receiving_answer_returns_false)
241+
{
242+
std::ifstream in_file(SCRIPT_FILE);
243+
std::ofstream out_file;
244+
const std::string test_script_file = "test_script.urscript";
245+
out_file.open(test_script_file);
246+
247+
std::string line;
248+
std::regex delete_me("socket_send_int\\(TRAJECTORY_RESULT_CANCELED,\\s*\"trajectory_socket\"\\)");
249+
while (std::getline(in_file, line))
250+
{
251+
std::smatch delete_match;
252+
if (!std::regex_search(line, delete_match, delete_me))
253+
{
254+
out_file << line << std::endl;
255+
}
256+
}
257+
out_file.close();
258+
auto my_robot = std::make_unique<ExampleRobotWrapper>(ROBOT_IP, OUTPUT_RECIPE, INPUT_RECIPE, g_HEADLESS,
259+
"external_control.urp", test_script_file);
260+
auto executor = std::make_unique<InstructionExecutor>(my_robot->ur_driver_);
261+
my_robot->clearProtectiveStop();
262+
// Make sure script is running on the robot
263+
if (!my_robot->waitForProgramRunning())
264+
{
265+
my_robot->resendRobotProgram();
266+
ASSERT_TRUE(my_robot->waitForProgramRunning());
267+
}
268+
ASSERT_TRUE(executor->moveJ({ -1.59, -1.72, -2.2, -0.8, 1.6, 0.2 }, 2.0, 2.0));
269+
std::thread move_thread([&executor]() { executor->moveJ({ -1.57, -1.6, 1.6, -0.7, 0.7, 2.7 }, 0.1, 0.1); });
270+
bool is_trajectory_running = false;
271+
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
272+
while (!is_trajectory_running || std::chrono::steady_clock::now() > start + std::chrono::seconds(5))
273+
{
274+
is_trajectory_running = executor->isTrajectoryRunning();
275+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
276+
}
277+
ASSERT_TRUE(executor->isTrajectoryRunning());
278+
ASSERT_FALSE(executor->cancelMotion());
279+
move_thread.join();
280+
std::remove(test_script_file.c_str());
281+
}
282+
228283
TEST_F(InstructionExecutorTest, canceling_with_running_trajectory_succeeds)
229284
{
230285
ASSERT_TRUE(executor_->moveJ({ -1.59, -1.72, -2.2, -0.8, 1.6, 0.2 }, 2.0, 2.0));

0 commit comments

Comments
 (0)