@@ -162,72 +162,55 @@ int main(int argc, char* argv[])
162162 " for details." );
163163 }
164164
165- // Once RTDE communication is started, we have to make sure to read from the interface buffer, as
166- // otherwise we will get pipeline overflows. Therefore, do this directly before starting your main
167- // loop.
168- g_my_driver->startRTDECommunication ();
169-
170- std::chrono::duration<double > time_done (0 );
171- std::chrono::duration<double > timeout (second_to_run);
172- auto stopwatch_last = std::chrono::steady_clock::now ();
173- auto stopwatch_now = stopwatch_last;
174165 // Make sure that external control script is running
175166 if (!waitForProgramRunning ())
176167 {
177168 URCL_LOG_ERROR (" External Control script not running." );
178169 return 1 ;
179170 }
171+ // End of initialization -- We've started the external control program, which means we have to
172+ // write keepalive signals from now on. Otherwise the connection will be dropped.
173+
174+ // Start force mode
180175 // Task frame at the robot's base with limits being large enough to cover the whole workspace
181176 // Compliance in z axis and rotation around z axis
182177 bool success;
183178 if (g_my_driver->getVersion ().major < 5 )
184- success = g_my_driver->startForceMode ({ 0 , 0 , 0 , 0 , 0 , 0 }, // Task frame at the robot's base
185- { 0 , 0 , 1 , 0 , 0 , 1 }, // Compliance in z axis and rotation around z axis
186- { 0 , 0 , 0 , 0 , 0 , 0 }, // do not apply any active wrench
187- 2 , // do not transform the force frame at all
179+ success = g_my_driver->startForceMode ({ 0 , 0 , 0 , 0 , 0 , 0 }, // Task frame at the robot's base
180+ { 0 , 0 , 1 , 0 , 0 , 1 }, // Compliance in z axis and rotation around z axis
181+ { 0 , 0 , - 2 , 0 , 0 , 0 }, // Press in -z direction
182+ 2 , // do not transform the force frame at all
188183 { 0.1 , 0.1 , 1.5 , 3.14 , 3.14 , 0.5 }, // limits
189- 0.025 ); // damping_factor. See ScriptManual for details.
184+ 0.005 ); // damping_factor. See ScriptManual for details.
190185 else
191186 {
192- success = g_my_driver->startForceMode ({ 0 , 0 , 0 , 0 , 0 , 0 }, // Task frame at the robot's base
193- { 0 , 0 , 1 , 0 , 0 , 1 }, // Compliance in z axis and rotation around z axis
194- { 0 , 0 , 0 , 0 , 0 , 0 }, // do not apply any active wrench
195- 2 , // do not transform the force frame at all
187+ success = g_my_driver->startForceMode ({ 0 , 0 , 0 , 0 , 0 , 0 }, // Task frame at the robot's base
188+ { 0 , 0 , 1 , 0 , 0 , 1 }, // Compliance in z axis and rotation around z axis
189+ { 0 , 0 , - 2 , 0 , 0 , 0 }, // Press in -z direction
190+ 2 , // do not transform the force frame at all
196191 { 0.1 , 0.1 , 1.5 , 3.14 , 3.14 , 0.5 }, // limits
197- 0.025 , // damping_factor
198- 0.8 ); // gain_scaling. See ScriptManual for details.
192+ 0.005 , // damping_factor
193+ 1.0 ); // gain_scaling. See ScriptManual for details.
199194 }
200195 if (!success)
201196 {
202197 URCL_LOG_ERROR (" Failed to start force mode." );
203198 return 1 ;
204199 }
205200
206- while (true )
201+ std::chrono::duration<double > time_done (0 );
202+ std::chrono::duration<double > timeout (second_to_run);
203+ auto stopwatch_last = std::chrono::steady_clock::now ();
204+ auto stopwatch_now = stopwatch_last;
205+ while (time_done < timeout || second_to_run.count () == 0 )
207206 {
208- // Read latest RTDE package. This will block for a hard-coded timeout (see UrDriver), so the
209- // robot will effectively be in charge of setting the frequency of this loop.
210- // In a real-world application this thread should be scheduled with real-time priority in order
211- // to ensure that this is called in time.
212- std::unique_ptr<rtde_interface::DataPackage> data_pkg = g_my_driver->getDataPackage ();
213- if (data_pkg)
214- {
215- g_my_driver->writeKeepalive ();
216-
217- if (time_done > timeout && second_to_run.count () != 0 )
218- {
219- URCL_LOG_INFO (" Timeout reached." );
220- break ;
221- }
222- }
223- else
224- {
225- URCL_LOG_WARN (" Could not get fresh data package from robot" );
226- }
207+ g_my_driver->writeKeepalive ();
227208
228209 stopwatch_now = std::chrono::steady_clock::now ();
229210 time_done += stopwatch_now - stopwatch_last;
230211 stopwatch_last = stopwatch_now;
212+ std::this_thread::sleep_for (std::chrono::milliseconds (2 ));
231213 }
214+ URCL_LOG_INFO (" Timeout reached." );
232215 g_my_driver->endForceMode ();
233216}
0 commit comments