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 
33 using namespace urcl;
34 
35 // In a real-world example it would be better to get those values from command line parameters / a better configuration
36 // system such as Boost.Program_options
37 const std::string ROBOT_IP = "192.168.56.101";
38 const std::string OUTPUT_RECIPE = "examples/resources/rtde_output_recipe.txt";
39 const std::string INPUT_RECIPE = "examples/resources/rtde_input_recipe.txt";
40 const std::chrono::milliseconds READ_TIMEOUT{ 100 };
41 
42 int main(int argc, char* argv[])
43 {
44  // TODO: Write good docstring for notifier
45  comm::INotifier notifier;
47  my_client.init();
48 
49  // We will use the speed_slider_fraction as an example how to write to RTDE
50  double speed_slider_fraction = 1.0;
51  double speed_slider_increment = 0.01;
52 
53  // Once RTDE communication is started, we have to make sure to read from the interface buffer, as
54  // otherwise we will get pipeline overflows. Therefor, do this directly before starting your main
55  // loop.
56  my_client.start();
57  while (true)
58  {
59  // Read latest RTDE package. This will block for READ_TIMEOUT, so the
60  // robot will effectively be in charge of setting the frequency of this loop unless RTDE
61  // communication doesn't work in which case the user will be notified.
62  // In a real-world application this thread should be scheduled with real-time priority in order
63  // to ensure that this is called in time.
64  std::unique_ptr<rtde_interface::DataPackage> data_pkg = my_client.getDataPackage(READ_TIMEOUT);
65  if (data_pkg)
66  {
67  std::cout << data_pkg->toString() << std::endl;
68  }
69  else
70  {
71  std::cout << "Could not get fresh data package from robot" << std::endl;
72  }
73 
74  if (!my_client.getWriter().sendSpeedSlider(speed_slider_fraction))
75  {
76  // This will happen for example, when the required keys are not configured inside the input
77  // recipe.
78  std::cout << "\033[1;31mSending RTDE data failed."
79  << "\033[0m\n"
80  << std::endl;
81  }
82 
83  // Change the speed slider so that it will move between 0 and 1 all the time. This is for
84  // demonstration purposes only and gains no real value.
85  if (speed_slider_increment > 0)
86  {
87  if (speed_slider_fraction + speed_slider_increment > 1.0)
88  {
89  speed_slider_increment *= -1;
90  }
91  }
92  else if (speed_slider_fraction + speed_slider_increment < 0.0)
93  {
94  speed_slider_increment *= -1;
95  }
96  speed_slider_fraction += speed_slider_increment;
97  }
98 
99  return 0;
100 }
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 INPUT_RECIPE
const std::string ROBOT_IP
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:209
const std::string OUTPUT_RECIPE
The RTDEClient class manages communication over the RTDE interface. It contains the RTDE handshake an...
Definition: rtde_client.h:81
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:65


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Sun May 9 2021 02:16:26