examples/rtde_client.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 // -- END LICENSE BLOCK ------------------------------------------------
18 
19 //----------------------------------------------------------------------
26 //----------------------------------------------------------------------
27 
29 
30 #include <iostream>
31 #include <memory>
32 #include <ctime>
33 
34 using namespace urcl;
35 
36 // In a real-world example it would be better to get those values from command line parameters / a better configuration
37 // system such as Boost.Program_options
38 const std::string DEFAULT_ROBOT_IP = "192.168.56.101";
39 const std::string OUTPUT_RECIPE = "examples/resources/rtde_output_recipe.txt";
40 const std::string INPUT_RECIPE = "examples/resources/rtde_input_recipe.txt";
41 const std::chrono::milliseconds READ_TIMEOUT{ 100 };
42 
43 int main(int argc, char* argv[])
44 {
45  // Parse the ip arguments if given
46  std::string robot_ip = DEFAULT_ROBOT_IP;
47  if (argc > 1)
48  {
49  robot_ip = std::string(argv[1]);
50  }
51 
52  // Parse how may seconds to run
53  int second_to_run = -1;
54  if (argc > 2)
55  {
56  second_to_run = std::stoi(argv[2]);
57  }
58 
59  // TODO: Write good docstring for notifier
60  comm::INotifier notifier;
61  rtde_interface::RTDEClient my_client(robot_ip, notifier, OUTPUT_RECIPE, INPUT_RECIPE);
62  my_client.init();
63 
64  // We will use the speed_slider_fraction as an example how to write to RTDE
65  double speed_slider_fraction = 1.0;
66  double speed_slider_increment = 0.01;
67 
68  // Once RTDE communication is started, we have to make sure to read from the interface buffer, as
69  // otherwise we will get pipeline overflows. Therefor, do this directly before starting your main
70  // loop.
71  my_client.start();
72 
73  unsigned long startTime = clock();
74  while (second_to_run < 0 || ((clock() - startTime) / CLOCKS_PER_SEC) < static_cast<unsigned int>(second_to_run))
75  {
76  // Read latest RTDE package. This will block for READ_TIMEOUT, so the
77  // robot will effectively be in charge of setting the frequency of this loop unless RTDE
78  // communication doesn't work in which case the user will be notified.
79  // In a real-world application this thread should be scheduled with real-time priority in order
80  // to ensure that this is called in time.
81  std::unique_ptr<rtde_interface::DataPackage> data_pkg = my_client.getDataPackage(READ_TIMEOUT);
82  if (data_pkg)
83  {
84  std::cout << data_pkg->toString() << std::endl;
85  }
86  else
87  {
88  std::cout << "Could not get fresh data package from robot" << std::endl;
89  return 1;
90  }
91 
92  if (!my_client.getWriter().sendSpeedSlider(speed_slider_fraction))
93  {
94  // This will happen for example, when the required keys are not configured inside the input
95  // recipe.
96  std::cout << "\033[1;31mSending RTDE data failed."
97  << "\033[0m\n"
98  << std::endl;
99  return 1;
100  }
101 
102  // Change the speed slider so that it will move between 0 and 1 all the time. This is for
103  // demonstration purposes only and gains no real value.
104  if (speed_slider_increment > 0)
105  {
106  if (speed_slider_fraction + speed_slider_increment > 1.0)
107  {
108  speed_slider_increment *= -1;
109  }
110  }
111  else if (speed_slider_fraction + speed_slider_increment < 0.0)
112  {
113  speed_slider_increment *= -1;
114  }
115  speed_slider_fraction += speed_slider_increment;
116  }
117 
118  // Resetting the speedslider back to 100%
119  my_client.getWriter().sendSpeedSlider(1);
120 
121  return 0;
122 }
const std::chrono::milliseconds READ_TIMEOUT
RTDEWriter & getWriter()
Getter for the RTDE writer, which is used to send data via the RTDE interface to the robot...
bool start()
Triggers the robot to start sending RTDE data packages in the negotiated format.
int main(int argc, char *argv[])
const std::string DEFAULT_ROBOT_IP
const std::string INPUT_RECIPE
std::unique_ptr< rtde_interface::DataPackage > getDataPackage(std::chrono::milliseconds timeout)
Reads the pipeline to fetch the next data package.
Parent class for notifiers.
Definition: pipeline.h:210
const std::string OUTPUT_RECIPE
The RTDEClient class manages communication over the RTDE interface. It contains the RTDE handshake an...
Definition: rtde_client.h:92
bool init()
Sets up RTDE communication with the robot. The handshake includes negotiation of the used protocol ve...
bool sendSpeedSlider(double speed_slider_fraction)
Creates a package to request setting a new value for the speed slider.
Definition: rtde_writer.cpp:66


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