Skip to content

Commit e40eb26

Browse files
committed
Use ExampleRobotWrapper in test_instruction_executor
1 parent aecbe07 commit e40eb26

File tree

1 file changed

+19
-93
lines changed

1 file changed

+19
-93
lines changed

tests/test_instruction_executor.cpp

Lines changed: 19 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@
3030

3131
#include <gtest/gtest.h>
3232
#include <thread>
33-
#include "ur_client_library/ur/dashboard_client.h"
3433
#include "ur_client_library/ur/instruction_executor.h"
3534
#include "ur_client_library/control/motion_primitives.h"
3635

36+
#include <ur_client_library/example_robot_wrapper.h>
37+
3738
using namespace urcl;
3839
using namespace urcl::control;
3940

@@ -43,55 +44,7 @@ const std::string INPUT_RECIPE = "resources/rtde_input_recipe.txt";
4344
const std::string CALIBRATION_CHECKSUM = "calib_12788084448423163542";
4445
std::string ROBOT_IP = "192.168.56.101";
4546

46-
bool g_program_running;
47-
std::condition_variable g_program_not_running_cv;
48-
std::mutex g_program_not_running_mutex;
49-
std::condition_variable g_program_running_cv;
50-
std::mutex g_program_running_mutex;
51-
52-
std::unique_ptr<DashboardClient> g_dashboard_client;
53-
std::shared_ptr<UrDriver> g_ur_driver;
54-
55-
// Helper functions for the driver
56-
void handleRobotProgramState(bool program_running)
57-
{
58-
// Print the text in green so we see it better
59-
std::cout << "\033[1;32mProgram running: " << std::boolalpha << program_running << "\033[0m\n" << std::endl;
60-
if (program_running)
61-
{
62-
std::lock_guard<std::mutex> lk(g_program_running_mutex);
63-
g_program_running = program_running;
64-
g_program_running_cv.notify_one();
65-
}
66-
else
67-
{
68-
std::lock_guard<std::mutex> lk(g_program_not_running_mutex);
69-
g_program_running = program_running;
70-
g_program_not_running_cv.notify_one();
71-
}
72-
}
73-
74-
bool waitForProgramNotRunning(int milliseconds = 100)
75-
{
76-
std::unique_lock<std::mutex> lk(g_program_not_running_mutex);
77-
if (g_program_not_running_cv.wait_for(lk, std::chrono::milliseconds(milliseconds)) == std::cv_status::no_timeout ||
78-
g_program_running == false)
79-
{
80-
return true;
81-
}
82-
return false;
83-
}
84-
85-
bool waitForProgramRunning(int milliseconds = 100)
86-
{
87-
std::unique_lock<std::mutex> lk(g_program_running_mutex);
88-
if (g_program_running_cv.wait_for(lk, std::chrono::milliseconds(milliseconds)) == std::cv_status::no_timeout ||
89-
g_program_running == true)
90-
{
91-
return true;
92-
}
93-
return false;
94-
}
47+
std::unique_ptr<ExampleRobotWrapper> g_my_robot;
9548

9649
class InstructionExecutorTest : public ::testing::Test
9750
{
@@ -100,57 +53,30 @@ class InstructionExecutorTest : public ::testing::Test
10053

10154
static void SetUpTestSuite()
10255
{
103-
g_dashboard_client.reset(new DashboardClient(ROBOT_IP));
104-
ASSERT_TRUE(g_dashboard_client->connect());
105-
106-
// Make robot ready for test
107-
timeval tv;
108-
tv.tv_sec = 10;
109-
tv.tv_usec = 0;
110-
g_dashboard_client->setReceiveTimeout(tv);
111-
112-
// Stop running program if there is one
113-
ASSERT_TRUE(g_dashboard_client->commandStop());
114-
115-
// if the robot is not powered on and ready
116-
ASSERT_TRUE(g_dashboard_client->commandBrakeRelease());
117-
11856
// Setup driver
119-
std::unique_ptr<ToolCommSetup> tool_comm_setup;
120-
const bool headless = true;
121-
try
122-
{
123-
g_ur_driver.reset(new UrDriver(ROBOT_IP, SCRIPT_FILE, OUTPUT_RECIPE, INPUT_RECIPE, &handleRobotProgramState,
124-
headless, std::move(tool_comm_setup), CALIBRATION_CHECKSUM));
125-
}
126-
catch (UrException& exp)
127-
{
128-
std::cout << "caught exception " << exp.what() << " while launch driver, retrying once in 10 seconds"
129-
<< std::endl;
130-
std::this_thread::sleep_for(std::chrono::seconds(10));
131-
g_ur_driver.reset(new UrDriver(ROBOT_IP, SCRIPT_FILE, OUTPUT_RECIPE, INPUT_RECIPE, &handleRobotProgramState,
132-
headless, std::move(tool_comm_setup), CALIBRATION_CHECKSUM));
133-
}
57+
bool headless_mode = false;
58+
g_my_robot = std::make_unique<ExampleRobotWrapper>(ROBOT_IP, OUTPUT_RECIPE, INPUT_RECIPE, headless_mode,
59+
"external_control.urp", SCRIPT_FILE);
13460
}
13561
void SetUp() override
13662
{
137-
executor_ = std::make_unique<InstructionExecutor>(g_ur_driver);
63+
executor_ = std::make_unique<InstructionExecutor>(g_my_robot->ur_driver_);
13864
std::string safety_status;
139-
g_dashboard_client->commandSafetyStatus(safety_status);
65+
g_my_robot->dashboard_client_->commandSafetyStatus(safety_status);
14066
bool is_protective_stopped = safety_status.find("PROTECTIVE_STOP") != std::string::npos;
14167
if (is_protective_stopped)
14268
{
14369
// We forced a protective stop above. Some versions require waiting 5 seconds before releasing
14470
// the protective stop.
14571
std::this_thread::sleep_for(std::chrono::seconds(5));
146-
g_dashboard_client->commandCloseSafetyPopup();
147-
ASSERT_TRUE(g_dashboard_client->commandUnlockProtectiveStop());
72+
g_my_robot->dashboard_client_->commandCloseSafetyPopup();
73+
ASSERT_TRUE(g_my_robot->dashboard_client_->commandUnlockProtectiveStop());
14874
}
14975
// Make sure script is running on the robot
150-
if (g_program_running == false)
76+
if (!g_my_robot->waitForProgramRunning())
15177
{
152-
g_ur_driver->sendRobotProgram();
153-
ASSERT_TRUE(waitForProgramRunning(1000));
78+
g_my_robot->resendRobotProgram();
79+
ASSERT_TRUE(g_my_robot->waitForProgramRunning());
15480
}
15581
}
15682
};
@@ -252,8 +178,8 @@ TEST_F(InstructionExecutorTest, execute_movel_success)
252178

253179
TEST_F(InstructionExecutorTest, sending_commands_without_reverse_interface_connected_fails)
254180
{
255-
g_dashboard_client->commandStop();
256-
ASSERT_TRUE(waitForProgramNotRunning(1000));
181+
g_my_robot->dashboard_client_->commandStop();
182+
ASSERT_TRUE(g_my_robot->waitForProgramNotRunning(1000));
257183

258184
ASSERT_FALSE(executor_->moveJ({ -1.57, -1.6, 1.6, -0.7, 0.7, 0.2 }));
259185
ASSERT_FALSE(executor_->moveL({ -0.203, 0.263, 0.559, 0.68, -1.083, -2.076 }));
@@ -271,17 +197,17 @@ TEST_F(InstructionExecutorTest, sending_commands_without_reverse_interface_conne
271197
ASSERT_FALSE(executor_->executeMotion(motion_sequence));
272198

273199
// Disconnect mid-motion
274-
g_ur_driver->sendRobotProgram();
275-
ASSERT_TRUE(waitForProgramRunning(1000));
200+
g_my_robot->ur_driver_->sendRobotProgram();
201+
ASSERT_TRUE(g_my_robot->waitForProgramRunning(1000));
276202

277203
// move to first pose
278204
ASSERT_TRUE(executor_->moveJ({ -1.57, -1.57, 0, 0, 0, 0 }));
279205
// move to second pose asynchronoysly
280206
auto motion_thread = std::thread([&]() { ASSERT_FALSE(executor_->moveJ({ -1.57, -1.6, 1.6, -0.7, 0.7, 0.2 })); });
281207

282208
std::this_thread::sleep_for(std::chrono::milliseconds(300));
283-
g_dashboard_client->commandStop();
284-
ASSERT_TRUE(waitForProgramNotRunning(1000));
209+
g_my_robot->dashboard_client_->commandStop();
210+
ASSERT_TRUE(g_my_robot->waitForProgramNotRunning(1000));
285211
motion_thread.join();
286212
}
287213

0 commit comments

Comments
 (0)