full_driver.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // Copyright 2020 FZI Forschungszentrum Informatik
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
18 // -- END LICENSE BLOCK ------------------------------------------------
19 
20 //----------------------------------------------------------------------
29 //----------------------------------------------------------------------
30 
34 
35 #include <iostream>
36 #include <memory>
37 
38 using namespace urcl;
39 
40 // In a real-world example it would be better to get those values from command line parameters / a
41 // better configuration system such as Boost.Program_options
42 const std::string DEFAULT_ROBOT_IP = "192.168.56.101";
43 const std::string SCRIPT_FILE = "resources/external_control.urscript";
44 const std::string OUTPUT_RECIPE = "examples/resources/rtde_output_recipe.txt";
45 const std::string INPUT_RECIPE = "examples/resources/rtde_input_recipe.txt";
46 const std::string CALIBRATION_CHECKSUM = "calib_12788084448423163542";
47 
48 std::unique_ptr<UrDriver> g_my_driver;
49 std::unique_ptr<DashboardClient> g_my_dashboard;
51 
52 // We need a callback function to register. See UrDriver's parameters for details.
53 void handleRobotProgramState(bool program_running)
54 {
55  // Print the text in green so we see it better
56  std::cout << "\033[1;32mProgram running: " << std::boolalpha << program_running << "\033[0m\n" << std::endl;
57 }
58 
59 int main(int argc, char* argv[])
60 {
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  g_my_dashboard.reset(new DashboardClient(robot_ip));
73  if (!g_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 (!g_my_dashboard->commandStop())
81  {
82  URCL_LOG_ERROR("Could not send stop program command");
83  return 1;
84  }
85 
86  // Power it off
87  if (!g_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 (!g_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 (!g_my_dashboard->commandBrakeRelease())
102  {
103  URCL_LOG_ERROR("Could not send BrakeRelease command");
104  return 1;
105  }
106 
107  // Now the robot is ready to receive a program
108 
109  std::unique_ptr<ToolCommSetup> tool_comm_setup;
110  const bool HEADLESS = true;
112  std::move(tool_comm_setup), CALIBRATION_CHECKSUM));
113  g_my_driver->setKeepaliveCount(5); // This is for example purposes only. This will make the example running more
114  // reliable on non-realtime systems. Do not use this in productive applications.
115 
116  // Once RTDE communication is started, we have to make sure to read from the interface buffer, as
117  // otherwise we will get pipeline overflows. Therefor, do this directly before starting your main
118  // loop.
119 
120  g_my_driver->startRTDECommunication();
121 
122  double increment = 0.01;
123 
124  bool passed_slow_part = false;
125  bool passed_fast_part = false;
126  URCL_LOG_INFO("Start moving the robot");
127  while (!(passed_slow_part && passed_fast_part))
128  {
129  // Read latest RTDE package. This will block for a hard-coded timeout (see UrDriver), so the
130  // robot will effectively be in charge of setting the frequency of this loop.
131  // In a real-world application this thread should be scheduled with real-time priority in order
132  // to ensure that this is called in time.
133  std::unique_ptr<rtde_interface::DataPackage> data_pkg = g_my_driver->getDataPackage();
134  if (data_pkg)
135  {
136  // Read current joint positions from robot data
137  if (!data_pkg->getData("actual_q", g_joint_positions))
138  {
139  // This throwing should never happen unless misconfigured
140  std::string error_msg = "Did not find 'actual_q' in data sent from robot. This should not happen!";
141  throw std::runtime_error(error_msg);
142  }
143 
144  // Simple motion command of last joint
145  if (g_joint_positions[5] > 3)
146  {
147  passed_fast_part = increment > 0.01 || passed_fast_part;
148  increment = -3; // this large jump will activate speed scaling
149  }
150  else if (g_joint_positions[5] < -3)
151  {
152  passed_slow_part = increment < 0.01 || passed_slow_part;
153  increment = 0.02;
154  }
155  g_joint_positions[5] += increment;
156  bool ret = g_my_driver->writeJointCommand(g_joint_positions, comm::ControlMode::MODE_SERVOJ);
157  if (!ret)
158  {
159  URCL_LOG_ERROR("Could not send joint command. Is the robot in remote control?");
160  return 1;
161  }
162  URCL_LOG_DEBUG("data_pkg:\n%s", data_pkg->toString());
163  }
164  else
165  {
166  URCL_LOG_WARN("Could not get fresh data package from robot");
167  }
168  }
169  URCL_LOG_INFO("Movement done");
170  return 0;
171 }
#define URCL_LOG_ERROR(...)
Definition: log.h:26
This is the main class for interfacing the driver.
Definition: ur_driver.h:52
void handleRobotProgramState(bool program_running)
Definition: full_driver.cpp:53
const std::string INPUT_RECIPE
Definition: full_driver.cpp:45
const std::string OUTPUT_RECIPE
Definition: full_driver.cpp:44
const std::string DEFAULT_ROBOT_IP
Definition: full_driver.cpp:42
const std::string CALIBRATION_CHECKSUM
Definition: full_driver.cpp:46
const std::string SCRIPT_FILE
Definition: full_driver.cpp:43
int main(int argc, char *argv[])
Definition: full_driver.cpp:59
Set when servoj control is active.
void setLogLevel(LogLevel level)
Set log level this will disable messages with lower log level.
Definition: log.cpp:101
vector6d_t g_joint_positions
Definition: full_driver.cpp:50
#define URCL_LOG_DEBUG(...)
Definition: log.h:23
#define URCL_LOG_WARN(...)
Definition: log.h:24
std::unique_ptr< UrDriver > g_my_driver
Definition: full_driver.cpp:48
This class is a wrapper around the dashboard server.
std::unique_ptr< DashboardClient > g_my_dashboard
Definition: full_driver.cpp:49
std::array< double, 6 > vector6d_t
Definition: types.h:30
#define URCL_LOG_INFO(...)
Definition: log.h:25


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Tue Jul 4 2023 02:09:47