|
29 | 29 | // -- END LICENSE BLOCK ------------------------------------------------ |
30 | 30 |
|
31 | 31 | #include <gtest/gtest.h> |
| 32 | +#include <algorithm> |
32 | 33 | #include <iostream> |
33 | 34 | #include <thread> |
34 | 35 | #include "ur_client_library/ur/instruction_executor.h" |
@@ -70,6 +71,21 @@ class InstructionExecutorTest : public ::testing::Test |
70 | 71 | ASSERT_TRUE(g_my_robot->waitForProgramRunning()); |
71 | 72 | } |
72 | 73 | } |
| 74 | + void TearDown() override |
| 75 | + { |
| 76 | + g_my_robot->ur_driver_->stopControl(); |
| 77 | + g_my_robot->waitForProgramNotRunning(1000); |
| 78 | + while (g_my_robot->ur_driver_->isTrajectoryInterfaceConnected()) |
| 79 | + { |
| 80 | + std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 81 | + } |
| 82 | + URCL_LOG_INFO("Stopped robot control."); |
| 83 | + } |
| 84 | + |
| 85 | + static void TearDownTestSuite() |
| 86 | + { |
| 87 | + g_my_robot.reset(); |
| 88 | + } |
73 | 89 | }; |
74 | 90 |
|
75 | 91 | TEST_F(InstructionExecutorTest, execute_motion_sequence_success) |
@@ -219,6 +235,69 @@ TEST_F(InstructionExecutorTest, unfeasible_movej_target_results_in_failure) |
219 | 235 | ASSERT_FALSE(executor_->moveJ({ -123, 0, 0, 0, 0, 0 })); |
220 | 236 | } |
221 | 237 |
|
| 238 | +TEST_F(InstructionExecutorTest, canceling_without_running_trajectory_returns_false) |
| 239 | +{ |
| 240 | + ASSERT_FALSE(executor_->isTrajectoryRunning()); |
| 241 | + ASSERT_FALSE(executor_->cancelMotion()); |
| 242 | +} |
| 243 | + |
| 244 | +TEST(InstructionExecutorTestStandalone, canceling_without_receiving_answer_returns_false) |
| 245 | +{ |
| 246 | + std::ifstream in_file(SCRIPT_FILE); |
| 247 | + std::ofstream out_file; |
| 248 | + const std::string test_script_file = "test_script.urscript"; |
| 249 | + out_file.open(test_script_file); |
| 250 | + |
| 251 | + std::string line; |
| 252 | + std::string pattern = "socket_send_int(TRAJECTORY_RESULT_CANCELED, \"trajectory_socket\")"; |
| 253 | + while (std::getline(in_file, line)) |
| 254 | + { |
| 255 | + if (line.find(pattern) == std::string::npos) |
| 256 | + { |
| 257 | + out_file << line << std::endl; |
| 258 | + } |
| 259 | + } |
| 260 | + out_file.close(); |
| 261 | + auto my_robot = std::make_unique<ExampleRobotWrapper>(ROBOT_IP, OUTPUT_RECIPE, INPUT_RECIPE, g_HEADLESS, |
| 262 | + "external_control.urp", test_script_file); |
| 263 | + auto executor = std::make_unique<InstructionExecutor>(my_robot->ur_driver_); |
| 264 | + my_robot->clearProtectiveStop(); |
| 265 | + // Make sure script is running on the robot |
| 266 | + if (!my_robot->waitForProgramRunning()) |
| 267 | + { |
| 268 | + my_robot->resendRobotProgram(); |
| 269 | + ASSERT_TRUE(my_robot->waitForProgramRunning()); |
| 270 | + } |
| 271 | + ASSERT_TRUE(executor->moveJ({ -1.59, -1.72, -2.2, -0.8, 1.6, 0.2 }, 2.0, 2.0)); |
| 272 | + std::thread move_thread([&executor]() { executor->moveJ({ -1.57, -1.6, 1.6, -0.7, 0.7, 2.7 }, 0.1, 0.1); }); |
| 273 | + bool is_trajectory_running = false; |
| 274 | + std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); |
| 275 | + while (!is_trajectory_running || std::chrono::steady_clock::now() > start + std::chrono::seconds(5)) |
| 276 | + { |
| 277 | + is_trajectory_running = executor->isTrajectoryRunning(); |
| 278 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 279 | + } |
| 280 | + ASSERT_TRUE(executor->isTrajectoryRunning()); |
| 281 | + ASSERT_FALSE(executor->cancelMotion()); |
| 282 | + move_thread.join(); |
| 283 | + std::remove(test_script_file.c_str()); |
| 284 | +} |
| 285 | + |
| 286 | +TEST_F(InstructionExecutorTest, canceling_with_running_trajectory_succeeds) |
| 287 | +{ |
| 288 | + ASSERT_TRUE(executor_->moveJ({ -1.59, -1.72, -2.2, -0.8, 1.6, 0.2 }, 2.0, 2.0)); |
| 289 | + std::thread move_thread([this]() { executor_->moveJ({ -1.57, -1.6, 1.6, -0.7, 0.7, 2.7 }); }); |
| 290 | + bool is_trajectory_running = false; |
| 291 | + std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); |
| 292 | + while (!is_trajectory_running || std::chrono::steady_clock::now() > start + std::chrono::seconds(5)) |
| 293 | + { |
| 294 | + is_trajectory_running = executor_->isTrajectoryRunning(); |
| 295 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 296 | + } |
| 297 | + ASSERT_TRUE(executor_->cancelMotion()); |
| 298 | + move_thread.join(); |
| 299 | +} |
| 300 | + |
222 | 301 | TEST_F(InstructionExecutorTest, unfeasible_movel_target_results_in_failure) |
223 | 302 | { |
224 | 303 | // move to a feasible starting pose |
|
0 commit comments