3333// In a real-world example it would be better to get those values from command line parameters / a
3434// better configuration system such as Boost.Program_options
3535
36+ #include < ur_client_library/example_robot_wrapper.h>
3637#include < ur_client_library/ur/dashboard_client.h>
3738#include < ur_client_library/ur/ur_driver.h>
3839#include < ur_client_library/types.h>
@@ -52,47 +53,18 @@ const std::string OUTPUT_RECIPE = "examples/resources/rtde_output_recipe.txt";
5253const std::string INPUT_RECIPE = " examples/resources/rtde_input_recipe.txt" ;
5354const std::string CALIBRATION_CHECKSUM = " calib_12788084448423163542" ;
5455
55- std::unique_ptr<UrDriver> g_my_driver;
56- std::unique_ptr<DashboardClient> g_my_dashboard;
57-
58- std::condition_variable g_program_running_cv;
59- std::mutex g_program_running_mutex;
60- bool g_program_running;
61-
62- // We need a callback function to register. See UrDriver's parameters for details.
63- void handleRobotProgramState (bool program_running)
64- {
65- // Print the text in green so we see it better
66- std::cout << " \033 [1;32mProgram running: " << std::boolalpha << program_running << " \033 [0m\n " << std::endl;
67- if (program_running)
68- {
69- std::lock_guard<std::mutex> lk (g_program_running_mutex);
70- g_program_running = program_running;
71- g_program_running_cv.notify_one ();
72- }
73- }
56+ std::unique_ptr<ExampleRobotWrapper> g_my_robot;
7457
7558void sendFreedriveMessageOrDie (const control::FreedriveControlMessage freedrive_action)
7659{
77- bool ret = g_my_driver ->writeFreedriveControlMessage (freedrive_action);
60+ bool ret = g_my_robot-> ur_driver_ ->writeFreedriveControlMessage (freedrive_action);
7861 if (!ret)
7962 {
8063 URCL_LOG_ERROR (" Could not send joint command. Is the robot in remote control?" );
8164 exit (1 );
8265 }
8366}
8467
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- }
95-
9668int main (int argc, char * argv[])
9769{
9870 urcl::setLogLevel (urcl::LogLevel::INFO);
@@ -110,50 +82,11 @@ int main(int argc, char* argv[])
11082 second_to_run = std::chrono::seconds (std::stoi (argv[2 ]));
11183 }
11284
113- // Making the robot ready for the program by:
114- // Connect the robot Dashboard
115- g_my_dashboard.reset (new DashboardClient (robot_ip));
116- if (!g_my_dashboard->connect ())
117- {
118- URCL_LOG_ERROR (" Could not connect to dashboard" );
119- return 1 ;
120- }
121-
122- // // Stop program, if there is one running
123- if (!g_my_dashboard->commandStop ())
124- {
125- URCL_LOG_ERROR (" Could not send stop program command" );
126- return 1 ;
127- }
85+ bool headless_mode = true ;
86+ g_my_robot = std::make_unique<ExampleRobotWrapper>(robot_ip, OUTPUT_RECIPE, INPUT_RECIPE, headless_mode,
87+ " external_control.urp" );
12888
129- // Power it off
130- // if (!g_my_dashboard->commandPowerOff())
131- // {
132- // URCL_LOG_ERROR("Could not send Power off command");
133- // return 1;
134- // }
135-
136- // Power it on
137- // if (!g_my_dashboard->commandPowerOn())
138- // {
139- // URCL_LOG_ERROR("Could not send Power on command");
140- // return 1;
141- // }
142-
143- // Release the brakes
144- // if (!g_my_dashboard->commandBrakeRelease())
145- // {
146- // URCL_LOG_ERROR("Could not send BrakeRelease command");
147- // return 1;
148- // }
149-
150- // Now the robot is ready to receive a program
151- std::unique_ptr<ToolCommSetup> tool_comm_setup;
152- const bool headless = true ;
153- g_my_driver.reset (new UrDriver (robot_ip, SCRIPT_FILE, OUTPUT_RECIPE, INPUT_RECIPE, &handleRobotProgramState, headless,
154- std::move (tool_comm_setup)));
155-
156- if (!g_my_driver->checkCalibration (CALIBRATION_CHECKSUM))
89+ if (!g_my_robot->ur_driver_ ->checkCalibration (CALIBRATION_CHECKSUM))
15790 {
15891 URCL_LOG_ERROR (" Calibration checksum does not match actual robot." );
15992 URCL_LOG_ERROR (" Use the ur_calibration tool to extract the correct calibration from the robot and pass that into "
@@ -163,7 +96,7 @@ int main(int argc, char* argv[])
16396 }
16497
16598 // Make sure that external control script is running
166- if (!waitForProgramRunning ())
99+ if (!g_my_robot-> waitForProgramRunning ())
167100 {
168101 URCL_LOG_ERROR (" External Control script not running." );
169102 return 1 ;
@@ -175,22 +108,24 @@ int main(int argc, char* argv[])
175108 // Task frame at the robot's base with limits being large enough to cover the whole workspace
176109 // Compliance in z axis and rotation around z axis
177110 bool success;
178- if (g_my_driver->getVersion ().major < 5 )
179- success = g_my_driver->startForceMode ({ 0 , 0 , 0 , 0 , 0 , 0 }, // Task frame at the robot's base
180- { 0 , 0 , 1 , 0 , 0 , 1 }, // Compliance in z axis and rotation around z axis
181- { 0 , 0 , -2 , 0 , 0 , 0 }, // Press in -z direction
182- 2 , // do not transform the force frame at all
183- { 0.1 , 0.1 , 1.5 , 3.14 , 3.14 , 0.5 }, // limits
184- 0.005 ); // damping_factor. See ScriptManual for details.
111+ if (g_my_robot->ur_driver_ ->getVersion ().major < 5 )
112+ success =
113+ g_my_robot->ur_driver_ ->startForceMode ({ 0 , 0 , 0 , 0 , 0 , 0 }, // Task frame at the robot's base
114+ { 0 , 0 , 1 , 0 , 0 , 1 }, // Compliance in z axis and rotation around z axis
115+ { 0 , 0 , -2 , 0 , 0 , 0 }, // Press in -z direction
116+ 2 , // do not transform the force frame at all
117+ { 0.1 , 0.1 , 1.5 , 3.14 , 3.14 , 0.5 }, // limits
118+ 0.005 ); // damping_factor. See ScriptManual for details.
185119 else
186120 {
187- success = g_my_driver->startForceMode ({ 0 , 0 , 0 , 0 , 0 , 0 }, // Task frame at the robot's base
188- { 0 , 0 , 1 , 0 , 0 , 1 }, // Compliance in z axis and rotation around z axis
189- { 0 , 0 , -2 , 0 , 0 , 0 }, // Press in -z direction
190- 2 , // do not transform the force frame at all
191- { 0.1 , 0.1 , 1.5 , 3.14 , 3.14 , 0.5 }, // limits
192- 0.005 , // damping_factor
193- 1.0 ); // gain_scaling. See ScriptManual for details.
121+ success =
122+ g_my_robot->ur_driver_ ->startForceMode ({ 0 , 0 , 0 , 0 , 0 , 0 }, // Task frame at the robot's base
123+ { 0 , 0 , 1 , 0 , 0 , 1 }, // Compliance in z axis and rotation around z axis
124+ { 0 , 0 , -2 , 0 , 0 , 0 }, // Press in -z direction
125+ 2 , // do not transform the force frame at all
126+ { 0.1 , 0.1 , 1.5 , 3.14 , 3.14 , 0.5 }, // limits
127+ 0.005 , // damping_factor
128+ 1.0 ); // gain_scaling. See ScriptManual for details.
194129 }
195130 if (!success)
196131 {
@@ -204,13 +139,13 @@ int main(int argc, char* argv[])
204139 auto stopwatch_now = stopwatch_last;
205140 while (time_done < timeout || second_to_run.count () == 0 )
206141 {
207- g_my_driver ->writeKeepalive ();
142+ g_my_robot-> ur_driver_ ->writeKeepalive ();
208143
209144 stopwatch_now = std::chrono::steady_clock::now ();
210145 time_done += stopwatch_now - stopwatch_last;
211146 stopwatch_last = stopwatch_now;
212147 std::this_thread::sleep_for (std::chrono::milliseconds (2 ));
213148 }
214149 URCL_LOG_INFO (" Timeout reached." );
215- g_my_driver ->endForceMode ();
150+ g_my_robot-> ur_driver_ ->endForceMode ();
216151}
0 commit comments