3636#include < ur_client_library/ur/dashboard_client.h>
3737#include < ur_client_library/ur/ur_driver.h>
3838#include < ur_client_library/types.h>
39+ #include < ur_client_library/example_robot_wrapper.h>
3940
4041#include < chrono>
4142#include < cstdlib>
4748using namespace urcl ;
4849
4950const std::string DEFAULT_ROBOT_IP = " 192.168.56.101" ;
50- const std::string SCRIPT_FILE = " resources/external_control.urscript" ;
5151const std::string OUTPUT_RECIPE = " examples/resources/rtde_output_recipe.txt" ;
5252const std::string INPUT_RECIPE = " examples/resources/rtde_input_recipe.txt" ;
53- const std::string CALIBRATION_CHECKSUM = " calib_12788084448423163542" ;
5453
55- std::unique_ptr<UrDriver> g_my_driver;
56- std::unique_ptr<DashboardClient> g_my_dashboard;
57-
58- // We need a callback function to register. See UrDriver's parameters for details.
59- void handleRobotProgramState (bool program_running)
60- {
61- // Print the text in green so we see it better
62- std::cout << " \033 [1;32mProgram running: " << std::boolalpha << program_running << " \033 [0m\n " << std::endl;
63- }
54+ std::unique_ptr<ExampleRobotWrapper> g_my_robot;
6455
6556void sendFreedriveMessageOrDie (const control::FreedriveControlMessage freedrive_action)
6657{
67- bool ret = g_my_driver ->writeFreedriveControlMessage (freedrive_action);
58+ bool ret = g_my_robot-> ur_driver_ ->writeFreedriveControlMessage (freedrive_action);
6859 if (!ret)
6960 {
70- URCL_LOG_ERROR (" Could not send joint command. Is the robot in remote control?" );
61+ URCL_LOG_ERROR (" Could not send freedrive command. Is the robot in remote control?" );
7162 exit (1 );
7263 }
7364}
@@ -89,86 +80,31 @@ int main(int argc, char* argv[])
8980 second_to_run = std::chrono::seconds (std::stoi (argv[2 ]));
9081 }
9182
92- // Making the robot ready for the program by:
93- // Connect the robot Dashboard
94- g_my_dashboard.reset (new DashboardClient (robot_ip));
95- if (!g_my_dashboard->connect ())
96- {
97- URCL_LOG_ERROR (" Could not connect to dashboard" );
98- return 1 ;
99- }
100-
101- // // Stop program, if there is one running
102- if (!g_my_dashboard->commandStop ())
103- {
104- URCL_LOG_ERROR (" Could not send stop program command" );
105- return 1 ;
106- }
107-
108- // Power it off
109- if (!g_my_dashboard->commandPowerOff ())
110- {
111- URCL_LOG_ERROR (" Could not send Power off command" );
112- return 1 ;
113- }
83+ g_my_robot = std::make_unique<ExampleRobotWrapper>(robot_ip, OUTPUT_RECIPE, INPUT_RECIPE, true );
11484
115- // Power it on
116- if (!g_my_dashboard->commandPowerOn ())
85+ if (!g_my_robot->waitForProgramRunning ())
11786 {
118- URCL_LOG_ERROR (" Could not send Power on command " );
87+ URCL_LOG_ERROR (" External Control script not running. " );
11988 return 1 ;
12089 }
12190
122- // Release the brakes
123- if (!g_my_dashboard->commandBrakeRelease ())
124- {
125- URCL_LOG_ERROR (" Could not send BrakeRelease command" );
126- return 1 ;
127- }
128-
129- // Now the robot is ready to receive a program
130- std::unique_ptr<ToolCommSetup> tool_comm_setup;
131- const bool headless = true ;
132- g_my_driver.reset (new UrDriver (robot_ip, SCRIPT_FILE, OUTPUT_RECIPE, INPUT_RECIPE, &handleRobotProgramState, headless,
133- std::move (tool_comm_setup), CALIBRATION_CHECKSUM));
134-
135- // Once RTDE communication is started, we have to make sure to read from the interface buffer, as
136- // otherwise we will get pipeline overflows. Therefore, do this directly before starting your main
137- // loop.
138- g_my_driver->startRTDECommunication ();
91+ URCL_LOG_INFO (" Starting freedrive mode" );
92+ sendFreedriveMessageOrDie (control::FreedriveControlMessage::FREEDRIVE_START);
13993
14094 std::chrono::duration<double > time_done (0 );
14195 std::chrono::duration<double > timeout (second_to_run);
14296 auto stopwatch_last = std::chrono::steady_clock::now ();
14397 auto stopwatch_now = stopwatch_last;
144- g_my_driver->writeKeepalive ();
145- sendFreedriveMessageOrDie (control::FreedriveControlMessage::FREEDRIVE_START);
14698
147- while (true )
99+ while (time_done < timeout || second_to_run. count () == 0 )
148100 {
149- // Read latest RTDE package. This will block for a hard-coded timeout (see UrDriver), so the
150- // robot will effectively be in charge of setting the frequency of this loop.
151- // In a real-world application this thread should be scheduled with real-time priority in order
152- // to ensure that this is called in time.
153- std::unique_ptr<rtde_interface::DataPackage> data_pkg = g_my_driver->getDataPackage ();
154- if (data_pkg)
155- {
156- sendFreedriveMessageOrDie (control::FreedriveControlMessage::FREEDRIVE_NOOP);
157-
158- if (time_done > timeout && second_to_run.count () != 0 )
159- {
160- URCL_LOG_INFO (" Timeout reached." );
161- break ;
162- }
163- }
164- else
165- {
166- URCL_LOG_WARN (" Could not get fresh data package from robot" );
167- }
101+ sendFreedriveMessageOrDie (control::FreedriveControlMessage::FREEDRIVE_NOOP);
168102
169103 stopwatch_now = std::chrono::steady_clock::now ();
170104 time_done += stopwatch_now - stopwatch_last;
171105 stopwatch_last = stopwatch_now;
172106 }
107+
108+ URCL_LOG_INFO (" Stopping freedrive mode" );
173109 sendFreedriveMessageOrDie (control::FreedriveControlMessage::FREEDRIVE_STOP);
174110}
0 commit comments