2828 */
2929// ----------------------------------------------------------------------
3030
31+ #include < ur_client_library/ur/dashboard_client.h>
3132#include < ur_client_library/ur/ur_driver.h>
3233#include < ur_client_library/types.h>
3334
@@ -38,13 +39,14 @@ using namespace urcl;
3839
3940// In a real-world example it would be better to get those values from command line parameters / a
4041// better configuration system such as Boost.Program_options
41- const std::string ROBOT_IP = " 192.168.56.101 " ;
42+ const std::string DEFAULT_ROBOT_IP = " 127.0.0.1 " ;
4243const std::string SCRIPT_FILE = " resources/external_control.urscript" ;
4344const std::string OUTPUT_RECIPE = " examples/resources/rtde_output_recipe.txt" ;
4445const std::string INPUT_RECIPE = " examples/resources/rtde_input_recipe.txt" ;
4546const std::string CALIBRATION_CHECKSUM = " calib_12788084448423163542" ;
4647
4748std::unique_ptr<UrDriver> g_my_driver;
49+ std::unique_ptr<DashboardClient> my_dashboard;
4850vector6d_t g_joint_positions;
4951
5052// We need a callback function to register. See UrDriver's parameters for details.
@@ -56,15 +58,71 @@ void handleRobotProgramState(bool program_running)
5658
5759int main (int argc, char * argv[])
5860{
61+ urcl::setLogLevel (urcl::LogLevel::INFO);
62+
63+ // Parse the ip arguments if given
64+ std::string robot_ip = DEFAULT_ROBOT_IP;
65+ if (argc > 1 )
66+ {
67+ robot_ip = std::string (argv[1 ]);
68+ }
69+
70+ // Making the robot ready for the program by:
71+ // Connect the the robot Dashboard
72+ my_dashboard.reset (new DashboardClient (robot_ip));
73+ if (!my_dashboard->connect ())
74+ {
75+ URCL_LOG_ERROR (" Could not connect to dashboard" );
76+ return 1 ;
77+ }
78+
79+ // Stop program, if there is one running
80+ if (!my_dashboard->commandStop ())
81+ {
82+ URCL_LOG_ERROR (" Could not send stop program command" );
83+ return 1 ;
84+ }
85+
86+ // Power it on
87+ if (!my_dashboard->commandPowerOff ())
88+ {
89+ URCL_LOG_ERROR (" Could not send Power off command" );
90+ return 1 ;
91+ }
92+
93+ // Power it on
94+ if (!my_dashboard->commandPowerOn ())
95+ {
96+ URCL_LOG_ERROR (" Could not send Power on command" );
97+ return 1 ;
98+ }
99+
100+ // Release the brakes
101+ if (!my_dashboard->commandBreakeRelease ())
102+ {
103+ URCL_LOG_ERROR (" Could not send BreakeRelease command" );
104+ return 1 ;
105+ }
106+
107+ // Now the robot is ready to receive a program
108+
59109 std::unique_ptr<ToolCommSetup> tool_comm_setup;
60- g_my_driver.reset (new UrDriver (ROBOT_IP, SCRIPT_FILE, OUTPUT_RECIPE, INPUT_RECIPE, &handleRobotProgramState, false ,
110+ const bool HEADLESS = true ;
111+ g_my_driver.reset (new UrDriver (robot_ip, SCRIPT_FILE, OUTPUT_RECIPE, INPUT_RECIPE, &handleRobotProgramState, HEADLESS,
61112 std::move (tool_comm_setup), CALIBRATION_CHECKSUM));
113+
62114 // Once RTDE communication is started, we have to make sure to read from the interface buffer, as
63115 // otherwise we will get pipeline overflows. Therefor, do this directly before starting your main
64116 // loop.
117+
65118 g_my_driver->startRTDECommunication ();
119+
66120 double increment = 0.01 ;
67- while (true )
121+
122+ bool passed_slow_part = false ;
123+ bool passed_fast_part = false ;
124+ URCL_LOG_INFO (" Start moving the robot" );
125+ while (!(passed_slow_part && passed_fast_part))
68126 {
69127 // Read latest RTDE package. This will block for a hard-coded timeout (see UrDriver), so the
70128 // robot will effectively be in charge of setting the frequency of this loop.
@@ -84,20 +142,28 @@ int main(int argc, char* argv[])
84142 // Simple motion command of last joint
85143 if (g_joint_positions[5 ] > 3 )
86144 {
145+ passed_fast_part = increment > 0.01 || passed_fast_part;
87146 increment = -3 ; // this large jump will activate speed scaling
88147 }
89148 else if (g_joint_positions[5 ] < -3 )
90149 {
150+ passed_slow_part = increment < 0.01 || passed_slow_part;
91151 increment = 0.02 ;
92152 }
93153 g_joint_positions[5 ] += increment;
94- g_my_driver->writeJointCommand (g_joint_positions, comm::ControlMode::MODE_SERVOJ);
95- std::cout << data_pkg->toString () << std::endl;
154+ bool ret = g_my_driver->writeJointCommand (g_joint_positions, comm::ControlMode::MODE_SERVOJ);
155+ if (!ret)
156+ {
157+ URCL_LOG_ERROR (" Could not send joint command. Is the robot in remote control?" );
158+ return 1 ;
159+ }
160+ URCL_LOG_DEBUG (" data_pkg:\n %s" , data_pkg->toString ());
96161 }
97162 else
98163 {
99- std::cout << " Could not get fresh data package from robot" << std::endl ;
164+ URCL_LOG_WARN ( " Could not get fresh data package from robot" ) ;
100165 }
101166 }
167+ URCL_LOG_INFO (" Movement done" );
102168 return 0 ;
103169}
0 commit comments