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,16 @@ 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- }
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" );
12188
122- // // Stop program, if there is one running
123- if (!g_my_dashboard->commandStop ())
89+ if (!g_my_robot->isHealthy ())
12490 {
125- URCL_LOG_ERROR (" Could not send stop program command " );
91+ URCL_LOG_ERROR (" Something in the robot initialization went wrong. Exiting. Please check the output above. " );
12692 return 1 ;
12793 }
128-
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))
94+ if (!g_my_robot->ur_driver_ ->checkCalibration (CALIBRATION_CHECKSUM))
15795 {
15896 URCL_LOG_ERROR (" Calibration checksum does not match actual robot." );
15997 URCL_LOG_ERROR (" Use the ur_calibration tool to extract the correct calibration from the robot and pass that into "
@@ -162,35 +100,31 @@ int main(int argc, char* argv[])
162100 " for details." );
163101 }
164102
165- // Make sure that external control script is running
166- if (!waitForProgramRunning ())
167- {
168- URCL_LOG_ERROR (" External Control script not running." );
169- return 1 ;
170- }
171103 // End of initialization -- We've started the external control program, which means we have to
172104 // write keepalive signals from now on. Otherwise the connection will be dropped.
173105
174106 // Start force mode
175107 // Task frame at the robot's base with limits being large enough to cover the whole workspace
176108 // Compliance in z axis and rotation around z axis
177109 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.
110+ if (g_my_robot->ur_driver_ ->getVersion ().major < 5 )
111+ success =
112+ g_my_robot->ur_driver_ ->startForceMode ({ 0 , 0 , 0 , 0 , 0 , 0 }, // Task frame at the robot's base
113+ { 0 , 0 , 1 , 0 , 0 , 1 }, // Compliance in z axis and rotation around z axis
114+ { 0 , 0 , -2 , 0 , 0 , 0 }, // Press in -z direction
115+ 2 , // do not transform the force frame at all
116+ { 0.1 , 0.1 , 1.5 , 3.14 , 3.14 , 0.5 }, // limits
117+ 0.005 ); // damping_factor. See ScriptManual for details.
185118 else
186119 {
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.
120+ success =
121+ g_my_robot->ur_driver_ ->startForceMode ({ 0 , 0 , 0 , 0 , 0 , 0 }, // Task frame at the robot's base
122+ { 0 , 0 , 1 , 0 , 0 , 1 }, // Compliance in z axis and rotation around z axis
123+ { 0 , 0 , -2 , 0 , 0 , 0 }, // Press in -z direction
124+ 2 , // do not transform the force frame at all
125+ { 0.1 , 0.1 , 1.5 , 3.14 , 3.14 , 0.5 }, // limits
126+ 0.005 , // damping_factor
127+ 1.0 ); // gain_scaling. See ScriptManual for details.
194128 }
195129 if (!success)
196130 {
@@ -204,13 +138,13 @@ int main(int argc, char* argv[])
204138 auto stopwatch_now = stopwatch_last;
205139 while (time_done < timeout || second_to_run.count () == 0 )
206140 {
207- g_my_driver ->writeKeepalive ();
141+ g_my_robot-> ur_driver_ ->writeKeepalive ();
208142
209143 stopwatch_now = std::chrono::steady_clock::now ();
210144 time_done += stopwatch_now - stopwatch_last;
211145 stopwatch_last = stopwatch_now;
212146 std::this_thread::sleep_for (std::chrono::milliseconds (2 ));
213147 }
214148 URCL_LOG_INFO (" Timeout reached." );
215- g_my_driver ->endForceMode ();
149+ g_my_robot-> ur_driver_ ->endForceMode ();
216150}
0 commit comments