@@ -53,14 +53,44 @@ const std::string INPUT_RECIPE = "examples/resources/rtde_input_recipe.txt";
5353
5454std::unique_ptr<ExampleRobotWrapper> g_my_robot;
5555
56- void sendFreedriveMessageOrDie ( const control::FreedriveControlMessage freedrive_action )
56+ void runFreedrive (UrDriver& urdriver, std::chrono::seconds duration )
5757{
58- bool ret = g_my_robot->getUrDriver ()->writeFreedriveControlMessage (freedrive_action);
59- if (!ret)
58+ URCL_LOG_INFO (" Starting freedrive mode" );
59+
60+ urdriver.writeFreedriveControlMessage (control::FreedriveControlMessage::FREEDRIVE_START );
61+
62+ auto start = std::chrono::steady_clock::now ();
63+ while (std::chrono::steady_clock::now () - start < duration || duration.count () == 0 )
64+ {
65+ // Keeping the robot in freedrive by sending NOOP messages
66+ urdriver.writeFreedriveControlMessage (control::FreedriveControlMessage::FREEDRIVE_NOOP );
67+ std::this_thread::sleep_for (std::chrono::milliseconds (2 ));
68+ }
69+
70+ urdriver.writeFreedriveControlMessage (control::FreedriveControlMessage::FREEDRIVE_STOP );
71+ URCL_LOG_INFO (" Stopping freedrive mode" );
72+ }
73+
74+ void runConstrainedFreedrive (UrDriver& urdriver, std::array<int32_t , 6 >& free_axes, std::array<double , 6 >& feature_pose,
75+ std::chrono::seconds duration)
76+ {
77+ URCL_LOG_INFO (" Starting constrained freedrive mode" );
78+
79+ urdriver.writeConstrainedFreedriveControlMessage (control::FreedriveControlMessage::FREEDRIVE_START , free_axes,
80+ feature_pose);
81+
82+ auto start = std::chrono::steady_clock::now ();
83+ while (std::chrono::steady_clock::now () - start < duration || duration.count () == 0 )
6084 {
61- URCL_LOG_ERROR (" Could not send joint command. Is there an external_control program running on the robot?" );
62- exit (1 );
85+ // Keeping the robot in constrained freedrive by sending NOOP messages
86+ urdriver.writeConstrainedFreedriveControlMessage (control::FreedriveControlMessage::FREEDRIVE_NOOP , free_axes,
87+ feature_pose);
88+ std::this_thread::sleep_for (std::chrono::milliseconds (2 ));
6389 }
90+
91+ urdriver.writeConstrainedFreedriveControlMessage (control::FreedriveControlMessage::FREEDRIVE_STOP , free_axes,
92+ feature_pose);
93+ URCL_LOG_INFO (" Stopping constrained freedrive mode" );
6494}
6595
6696int main (int argc, char * argv[])
@@ -73,15 +103,26 @@ int main(int argc, char* argv[])
73103 robot_ip = std::string (argv[1 ]);
74104 }
75105
106+ // Select the freedrive mode
107+ bool constrained = false ;
108+ if (argc > 2 )
109+ {
110+ std::string arg = argv[2 ];
111+ constrained = (arg == " true" || arg == " 1" );
112+ }
113+
76114 // Parse how many seconds to run
77115 auto second_to_run = std::chrono::seconds (0 );
78- if (argc > 2 )
116+ if (argc > 3 )
79117 {
80- second_to_run = std::chrono::seconds (std::stoi (argv[2 ]));
118+ second_to_run = std::chrono::seconds (std::stoi (argv[3 ]));
81119 }
82120
83- bool headless_mode = true ;
121+ std::array<int32_t , 6 > free_axes = { 1 , 0 , 1 , 1 , 0 , 1 };
122+ std::array<double , 6 > feature_pose = { 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 };
84123
124+ // Initialize robot connection
125+ bool headless_mode = true ;
85126 g_my_robot = std::make_unique<ExampleRobotWrapper>(robot_ip, OUTPUT_RECIPE , INPUT_RECIPE , headless_mode,
86127 " external_control.urp" );
87128
@@ -91,24 +132,13 @@ int main(int argc, char* argv[])
91132 return 1 ;
92133 }
93134
94- URCL_LOG_INFO (" Starting freedrive mode" );
95- sendFreedriveMessageOrDie (control::FreedriveControlMessage::FREEDRIVE_START );
96-
97- std::chrono::duration<double > time_done (0 );
98- std::chrono::duration<double > timeout (second_to_run);
99- auto stopwatch_last = std::chrono::steady_clock::now ();
100- auto stopwatch_now = stopwatch_last;
101-
102- while (time_done < timeout || second_to_run.count () == 0 )
135+ // Execute selected freedrive mode
136+ if (constrained)
103137 {
104- sendFreedriveMessageOrDie (control::FreedriveControlMessage::FREEDRIVE_NOOP );
105-
106- stopwatch_now = std::chrono::steady_clock::now ();
107- time_done += stopwatch_now - stopwatch_last;
108- stopwatch_last = stopwatch_now;
109- std::this_thread::sleep_for (std::chrono::milliseconds (2 ));
138+ runConstrainedFreedrive (*g_my_robot->getUrDriver (), free_axes, feature_pose, second_to_run);
139+ }
140+ else
141+ {
142+ runFreedrive (*g_my_robot->getUrDriver (), second_to_run);
110143 }
111-
112- URCL_LOG_INFO (" Stopping freedrive mode" );
113- sendFreedriveMessageOrDie (control::FreedriveControlMessage::FREEDRIVE_STOP );
114144}
0 commit comments