@@ -271,14 +271,34 @@ CallbackReturn DRHWInterface::on_init(const hardware_interface::HardwareInfo & i
271
271
}
272
272
273
273
Drfl.set_auto_servo_off (0 , 5.0 );
274
- // Drfl.connect_rt_control();
275
- // string version = "v1.0";
276
- // float period = 0.001; // 1 msec
277
- // int losscount = 4;
278
- // Drfl.set_rt_control_output(version, period, losscount);
279
- // usleep(nDelay);
280
- // Drfl.start_rt_control();
281
274
275
+ // Virtual controller doesn't support real time connection.
276
+ if (m_mode != " virtual" ) {
277
+ if (!Drfl.connect_rt_control (m_host)) {
278
+ RCLCPP_ERROR (rclcpp::get_logger (" dsr_hw_interface2" ), " Unable to connect RT control stream" );
279
+ return CallbackReturn::FAILURE;
280
+ }
281
+ RCLCPP_INFO (rclcpp::get_logger (" dsr_hw_interface2" ), " Connected RT control stream" );
282
+ const std::string version = " v1.0" ;
283
+ const float period = 0.001 ;
284
+ const int losscount = 4 ;
285
+ if (!Drfl.set_rt_control_output (version, period, losscount)) {
286
+ RCLCPP_ERROR (rclcpp::get_logger (" dsr_hw_interface2" ), " Unable to connect RT control stream" );
287
+ return CallbackReturn::FAILURE;
288
+ }
289
+
290
+ if (!Drfl.start_rt_control ()) {
291
+ RCLCPP_ERROR (rclcpp::get_logger (" dsr_hw_interface2" ), " Unable to start RT control" );
292
+ return CallbackReturn::FAILURE;
293
+ }
294
+
295
+ RCLCPP_INFO (rclcpp::get_logger (" dsr_hw_interface2" ), " Setting velocity and acceleration limits" );
296
+ float limit[6 ] = {70 .0f ,70 .0f ,70 .0f ,70 .0f ,70 .0f ,70 .0f };
297
+ if (!Drfl.set_velj_rt (limit)) return CallbackReturn::ERROR;
298
+ if (!Drfl.set_accj_rt (limit)) return CallbackReturn::ERROR;
299
+ }
300
+
301
+ Drfl.set_safety_mode (SAFETY_MODE_AUTONOMOUS,SAFETY_MODE_EVENT_MOVE);
282
302
return CallbackReturn::SUCCESS;
283
303
}
284
304
@@ -320,23 +340,36 @@ std::vector<hardware_interface::CommandInterface> DRHWInterface::export_command_
320
340
321
341
return_type DRHWInterface::read (const rclcpp::Time & /* time*/ , const rclcpp::Duration & /* period*/ )
322
342
{
323
-
324
- LPROBOT_POSE pose = Drfl.GetCurrentPose ();
325
- if (nullptr == pose) {
326
- RCLCPP_WARN (rclcpp::get_logger (" dsr_hw_interface2" ),
327
- " [read] GetCurrentPose retrieves nullptr" );
328
- return return_type::ERROR; // ? what effection of this to control node
329
- }
330
- for (int i=0 ;i<6 ;i++){
331
- joint_position_[i] = deg2rad (pose->_fPosition [i]);
332
- // RCLCPP_INFO(rclcpp::get_logger("dsr_hw_interface2"), " [READ] joint_pos[%d] : %.3f ", i, joint_position_[i]);
333
- }
343
+ if (m_mode == " real" ) {
344
+ const LPRT_OUTPUT_DATA_LIST data = Drfl.read_data_rt ();
345
+ for (int i=0 ;i<6 ;i++) {
346
+ joint_position_[i] = static_cast <float >(data->actual_joint_position [i] * (M_PI / 180 .0f ));
347
+ joint_velocities_[i] = static_cast <float >(data->actual_joint_velocity [i] * (M_PI / 180 .0f ));
348
+ }
349
+ }else if (m_mode == " virtual" ) {
350
+ LPROBOT_POSE pose = Drfl.GetCurrentPose ();
351
+ if (nullptr == pose) {
352
+ RCLCPP_WARN (rclcpp::get_logger (" dsr_hw_interface2" ),
353
+ " [read] GetCurrentPose retrieves nullptr" );
354
+ return return_type::ERROR; // ? what effection of this to control node
355
+ }
356
+ for (int i=0 ;i<6 ;i++){
357
+ joint_position_[i] = deg2rad (pose->_fPosition [i]);
358
+ }
359
+ }else {
360
+ RCLCPP_ERROR (rclcpp::get_logger (" dsr_hw_interface2" ),
361
+ " 'mode' is neither 'real' nor 'virtual.'" );
362
+ }
363
+ // RCLCPP_INFO(rclcpp::get_logger("dsr_hw_interface2"), "[READ] joint_position_ : {%.3f, %.3f, %.3f, %.3f, %.3f, %.3f}"
364
+ // ,joint_position_[0]
365
+ // ,joint_position_[1]
366
+ // ,joint_position_[2]
367
+ // ,joint_position_[3]
368
+ // ,joint_position_[4]
369
+ // ,joint_position_[5]);
334
370
return return_type::OK;
335
371
}
336
372
337
-
338
- std::vector<float > previous_joint_position_command_float (6 , std::numeric_limits<float >::quiet_NaN()); // 초기값 NaN
339
-
340
373
bool positionCommandRunning (const std::vector<double >& lhs, const std::vector<double >& rhs) {
341
374
double var = 0 ;
342
375
for (int i=0 ; i<lhs.size (); i++) {
@@ -346,84 +379,69 @@ bool positionCommandRunning(const std::vector<double>& lhs, const std::vector<do
346
379
}
347
380
348
381
vector<vector<float >> joint_position_commands;
349
- bool done = false ;
350
382
return_type DRHWInterface::write (const rclcpp::Time &, const rclcpp::Duration &dt)
351
383
{
384
+ // RCLCPP_INFO(rclcpp::get_logger("dsr_hw_interface2"), "[WRITE] dt : %.3f", float(dt.seconds()) );
385
+ // RCLCPP_INFO(rclcpp::get_logger("dsr_hw_interface2"), "[WRITE] joint_position_command_ : {%.3f, %.3f, %.3f, %.3f, %.3f, %.3f}"
386
+ // ,joint_position_command_[0]
387
+ // ,joint_position_command_[1]
388
+ // ,joint_position_command_[2]
389
+ // ,joint_position_command_[3]
390
+ // ,joint_position_command_[4]
391
+ // ,joint_position_command_[5]);
392
+ // RCLCPP_INFO(rclcpp::get_logger("dsr_hw_interface2"), "[WRITE] joint_velocities_command_ : {%.3f, %.3f, %.3f, %.3f, %.3f, %.3f}"
393
+ // ,joint_velocities_command_[0]
394
+ // ,joint_velocities_command_[1]
395
+ // ,joint_velocities_command_[2]
396
+ // ,joint_velocities_command_[3]
397
+ // ,joint_velocities_command_[4]
398
+ // ,joint_velocities_command_[5]);
399
+ static bool idle = false ;
400
+ // TODO: this seems to be a workaround. refer to hardware design of 'prepare_command_mode_switch'
352
401
if (positionCommandRunning (pre_joint_position_command_, joint_position_command_)) {
353
- // RCLCPP_WARN(rclcpp::get_logger("dsr_hw_interface2"), "Updated ");
354
- Drfl.set_safety_mode (SAFETY_MODE_AUTONOMOUS,SAFETY_MODE_EVENT_MOVE);
402
+ if (true == idle) {
403
+ // This is workaround to overcome issues :
404
+ // move_joint (drfl) API internally sent safety_off right after moving.
405
+ // which occurs problems like :
406
+ // "move_joint service command -> trajectory command => error ! "
407
+ Drfl.set_safety_mode (SAFETY_MODE_AUTONOMOUS,SAFETY_MODE_EVENT_MOVE);
408
+ idle = false ;
409
+ }
410
+
355
411
float pos[6 ];
356
- float limitVel[6 ] = {100 .0f , 100 .0f , 100 .0f , 100 .0f , 100 .0f , 100 .0f };
357
- float limitAcc[6 ] = {100 .0f , 100 .0f , 100 .0f , 100 .0f , 100 .0f , 100 .0f };
412
+ float targetVel[6 ];
358
413
for (int i=0 ;i<6 ;i++) {
359
- pos[i] = static_cast <float >(joint_position_command_[i]* (180 .0f / M_PI));
414
+ pos[i] = static_cast <float >(joint_position_command_[i] * (180 .0f / M_PI));
415
+ targetVel[i] = static_cast <float >(joint_velocities_command_[i] * (180 .0f / M_PI));
360
416
}
361
- // RCLCPP_INFO(rclcpp::get_logger("dsr_hw_interface2"), "[WRITE] pos : {%.3f, %.3f, %.3f, %.3f, %.3f, %.3f}"
362
- // ,pos[0] * ( M_PI / 180.0f)
363
- // ,pos[1] * ( M_PI / 180.0f)
364
- // ,pos[2] * ( M_PI / 180.0f)
365
- // ,pos[3] * ( M_PI / 180.0f)
366
- // ,pos[4] * ( M_PI / 180.0f)
367
- // ,pos[5] * ( M_PI / 180.0f)
368
- // );
369
- if (joint_position_commands.size () < 3 ) {
370
- joint_position_commands.emplace_back (std::vector<float > {pos[0 ],pos[1 ],pos[2 ],pos[3 ],pos[4 ],pos[5 ]});
371
- pre_joint_position_command_ = joint_position_command_;
372
- done = true ;
373
- return return_type::OK;
417
+ if (m_mode == " real" ) {
418
+ float margin = 1.5 ; // Setted margin since most host aren't RT.
419
+ float acc[6 ] = {0 .0f , 0 .0f , 0 .0f , 0 .0f , 0 .0f , 0 .0f }; // Complied with internal profile.
420
+ Drfl.servoj_rt (pos, targetVel, acc, float (dt.seconds () * margin));
374
421
}
375
- else {
376
- if (joint_position_commands.size () == 3 && done) {
377
-
378
- for (auto cmd : joint_position_commands) {
379
- // RCLCPP_INFO(rclcpp::get_logger("dsr_hw_interface2"), "[WRITE] cmd : {%.3f, %.3f, %.3f, %.3f, %.3f, %.3f}"
380
- // ,cmd[0] * ( M_PI / 180.0f)
381
- // ,cmd[1] * ( M_PI / 180.0f)
382
- // ,cmd[2] * ( M_PI / 180.0f)
383
- // ,cmd[3] * ( M_PI / 180.0f)
384
- // ,cmd[4] * ( M_PI / 180.0f)
385
- // ,cmd[5] * ( M_PI / 180.0f)
386
- // );
387
- if (m_nVersionDRCF >= 3000000 ) {
388
- Drfl.amovej (pos, limitVel, limitVel); // Workaround. needed updated.
389
- }
390
- else {
391
- // Drfl.servoj(cmd.data(), limitVel, limitAcc, float(dt.seconds())*1.7, DR_SERVO_OVERRIDE);
392
- Drfl.servoj (cmd.data (), limitVel, limitAcc, float (dt.seconds ()), DR_SERVO_QUEUE);
393
- }
394
- this_thread::sleep_for (chrono::milliseconds (1 ));
395
- done = false ;
396
- }
397
- }
398
- if (m_nVersionDRCF >= 3000000 ) {
399
- Drfl.amovej (pos, limitVel, limitVel); // Workaround. needed updated.
400
- }
401
- else {
402
- // Drfl.servoj(cmd.data(), limitVel, limitAcc, float(dt.seconds())*1.7, DR_SERVO_OVERRIDE);
403
- Drfl.servoj (pos, limitVel, limitAcc, float (dt.seconds ()), DR_SERVO_QUEUE);
404
- }
422
+ else { // "virtual"
423
+ float target_vel_acc[6 ] = {70 .0f , 70 .0f , 70 .0f , 70 .0f , 70 .0f , 70 .0f };
424
+ Drfl.amovej (pos, target_vel_acc, target_vel_acc); // Workaround. needed updated.
405
425
}
406
426
pre_joint_position_command_ = joint_position_command_;
407
427
return return_type::OK;
408
428
}
409
-
410
- joint_position_commands.clear ();
411
- done = false ;
412
- // RCLCPP_WARN(rclcpp::get_logger("dsr_hw_interface2"), "NOT Updated ");
429
+ idle = true ;
413
430
pre_joint_position_command_ = joint_position_command_;
414
431
return return_type::OK;
415
432
}
416
433
417
434
DRHWInterface::~DRHWInterface ()
418
435
{
419
436
Drfl.close_connection ();
437
+ Drfl.disconnect_rt_control ();
420
438
421
439
RCLCPP_INFO (rclcpp::get_logger (" dsr_hw_interface2" )," _______________________________________________\n " );
422
440
RCLCPP_INFO (rclcpp::get_logger (" dsr_hw_interface2" )," CONNECTION IS CLOSED" );
423
441
RCLCPP_INFO (rclcpp::get_logger (" dsr_hw_interface2" )," _______________________________________________\n " );
424
442
}
425
443
426
- }
444
+ }
427
445
428
446
429
447
const char * GetRobotStateString (int nState)
0 commit comments