2727
2828#include < ur_client_library/rtde/rtde_client.h>
2929
30+ #include < cmath>
3031#include < iostream>
3132#include < memory>
3233#include < ctime>
@@ -40,6 +41,20 @@ const std::string OUTPUT_RECIPE = "examples/resources/rtde_output_recipe.txt";
4041const std::string INPUT_RECIPE = " examples/resources/rtde_input_recipe.txt" ;
4142const std::chrono::milliseconds READ_TIMEOUT{ 100 };
4243
44+ void printFraction (const double fraction, const std::string& label, const size_t width = 20 )
45+ {
46+ std::cout << " \r " << label << " : [" ;
47+ for (size_t i = 0 ; i < std::ceil (fraction * width); i++)
48+ {
49+ std::cout << " #" ;
50+ }
51+ for (size_t i = 0 ; i < std::floor ((1.0 - fraction) * width); i++)
52+ {
53+ std::cout << " -" ;
54+ }
55+ std::cout << " ]" << std::flush;
56+ }
57+
4358int main (int argc, char * argv[])
4459{
4560 // Parse the ip arguments if given
@@ -56,22 +71,25 @@ int main(int argc, char* argv[])
5671 second_to_run = std::stoi (argv[2 ]);
5772 }
5873
59- // TODO: Write good docstring for notifier
6074 comm::INotifier notifier;
61- rtde_interface::RTDEClient my_client (robot_ip, notifier, OUTPUT_RECIPE, INPUT_RECIPE);
75+ const double rtde_frequency = 50 ; // Hz
76+ rtde_interface::RTDEClient my_client (robot_ip, notifier, OUTPUT_RECIPE, INPUT_RECIPE, rtde_frequency);
6277 my_client.init ();
6378
6479 // We will use the speed_slider_fraction as an example how to write to RTDE
6580 double speed_slider_fraction = 1.0 ;
81+ double target_speed_fraction = 1.0 ;
6682 double speed_slider_increment = 0.01 ;
6783
6884 // Once RTDE communication is started, we have to make sure to read from the interface buffer, as
6985 // otherwise we will get pipeline overflows. Therefor, do this directly before starting your main
7086 // loop.
7187 my_client.start ();
7288
73- unsigned long start_time = clock ();
74- while (second_to_run < 0 || ((clock () - start_time) / CLOCKS_PER_SEC) < static_cast <unsigned int >(second_to_run))
89+ auto start_time = std::chrono::steady_clock::now ();
90+ while (second_to_run <= 0 ||
91+ std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now () - start_time).count () <
92+ second_to_run)
7593 {
7694 // Read latest RTDE package. This will block for READ_TIMEOUT, so the
7795 // robot will effectively be in charge of setting the frequency of this loop unless RTDE
@@ -81,22 +99,19 @@ int main(int argc, char* argv[])
8199 std::unique_ptr<rtde_interface::DataPackage> data_pkg = my_client.getDataPackage (READ_TIMEOUT);
82100 if (data_pkg)
83101 {
84- std::cout << data_pkg->toString () << std::endl;
102+ // Data fields in the data package are accessed by their name. Only names present in the
103+ // output recipe can be accessed. Otherwise this function will return false.
104+ data_pkg->getData (" target_speed_fraction" , target_speed_fraction);
105+ printFraction (target_speed_fraction, " target_speed_fraction" );
85106 }
86107 else
87108 {
109+ // The client isn't connected properly anymore / doesn't receive any data anymore. Stop the
110+ // program.
88111 std::cout << " Could not get fresh data package from robot" << std::endl;
89112 return 1 ;
90113 }
91114
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." << " \033 [0m\n " << std::endl;
97- return 1 ;
98- }
99-
100115 // Change the speed slider so that it will move between 0 and 1 all the time. This is for
101116 // demonstration purposes only and gains no real value.
102117 if (speed_slider_increment > 0 )
@@ -111,6 +126,14 @@ int main(int argc, char* argv[])
111126 speed_slider_increment *= -1 ;
112127 }
113128 speed_slider_fraction += speed_slider_increment;
129+
130+ if (!my_client.getWriter ().sendSpeedSlider (speed_slider_fraction))
131+ {
132+ // This will happen for example, when the required keys are not configured inside the input
133+ // recipe.
134+ std::cout << " \033 [1;31mSending RTDE data failed." << " \033 [0m\n " << std::endl;
135+ return 1 ;
136+ }
114137 }
115138
116139 // Resetting the speedslider back to 100%
0 commit comments