262 double start_time = yarp::os::Time::now();
266 double current_time = yarp::os::Time::now();
276 _dd =
new PolyDriver(options);
277 bool connected =_dd->isValid();
280 if (connected)
break;
283 if (current_time-start_time > 60.0)
285 yError(
"It is not possible to instantiate the device driver. I tried %d times!\n", trials);
289 yarp::os::Time::delay(5);
291 yWarning(
"\nUnable to connect the device driver, trying again...\n");
298 ok =
ok & _dd->view(encs);
301 yError(
"one or more devices has not been viewed\nreturning...");
308 bool respond(
const Bottle& command, Bottle& reply)
override
312 if (command.get(0).isInt32())
314 if (command.get(0).asInt32()==0)
316 yInfo(
"Asking recalibration...\n");
323 yInfo(
"Recalibration complete.\n");
324 reply.addString(
"Recalibrated");
329 if (command.get(0).isString())
331 if (command.get(0).asString()==
"help")
333 reply.addVocab32(
"many");
334 reply.addString(
"Available commands:");
335 reply.addString(
"calib all");
336 reply.addString(
"calib arms");
337 reply.addString(
"calib legs");
338 reply.addString(
"calib feet");
341 else if (command.get(0).asString()==
"calib")
343 yInfo(
"Asking recalibration...\n");
347 if (command.get(1).asString()==
"all") calib_code=
CALIB_ALL;
348 else if (command.get(1).asString()==
"arms") calib_code=
CALIB_ARMS;
349 else if (command.get(1).asString()==
"legs") calib_code=
CALIB_LEGS;
350 else if (command.get(1).asString()==
"feet") calib_code=
CALIB_FEET;
356 yInfo(
"Recalibration complete.\n");
357 reply.addString(
"Recalibrated");
362 reply.addString(
"Unknown command");
369 string local_name =
"wholeBodyDynamics";
370 if (rf.check(
"local"))
372 local_name=rf.find(
"local").asString();
378 if (rf.check(
"robot"))
379 robot_name = rf.find(
"robot").asString();
380 else robot_name =
"icub";
388 if (rf.check(
"headV2"))
390 yInfo(
"'headV2' option found. Using icubV2 head kinematics.\n");
394 if (rf.check(
"headV2.6"))
396 yInfo(
"'headV2.6' option found. Using icubV2.6 head kinematics.\n");
401 if (rf.check(
"headV2.7"))
403 yInfo(
"'headV2.7' option found. Using icubV2.7 head kinematics.\n");
409 if(rf.check(
"legsV2"))
411 yInfo(
"'legsV2' option found. Using legsV2 kinematics. \n");
417 if (rf.check(
"autoconnect"))
419 yInfo(
"'autoconnect' option enabled.\n");
428 if (rf.check(
"no_com"))
431 yInfo(
"'no_com' option found. COM computation will be disabled.\n");
435 if (rf.check(
"disable_w0_dw0"))
437 w0_dw0_enabled=
false;
438 yInfo(
"'disable_w0_dw0' option found. w0 and dw0 will be set to zero.\n");
441 if (rf.check(
"enable_w0_dw0"))
443 w0_dw0_enabled=
true;
444 yInfo(
"'enable_w0_dw0' option found. w0 and dw0 will be used.\n");
448 if (rf.check(
"experimental_com_vel"))
450 com_vel_enabled=
true;
451 yInfo(
"'enable_com_vel' option found. Extra COM velocity computation will be enabled.\n");
455 if (rf.check(
"no_legs"))
458 yInfo(
"'no_legs' option found. Legs will be disabled.\n");
462 if (rf.check(
"no_torso_legs"))
464 torso_enabled=
false;
466 yInfo(
"no_torso_legs' option found. Torso and legs will be disabled.\n");
468 if (rf.check(
"no_torso"))
470 torso_enabled=
false;
471 yInfo(
"'no_torso' option found. Torso will be disabled.\n");
476 if (rf.check(
"no_head"))
479 yInfo(
"'no_head' option found. Head will be disabled.\n");
483 if (rf.check(
"no_left_arm"))
485 left_arm_enabled=
false;
486 yInfo(
"'no_left_arm' option found. Left arm will be disabled.\n");
488 if (rf.check(
"no_right_arm"))
490 right_arm_enabled=
false;
491 yInfo(
"'no_right_arm' option found. Right arm will be disabled.\n");
496 if (rf.check(
"period"))
498 rate = rf.find(
"period").asInt32();
499 yInfo(
"rateThread working at %d ms\n", rate);
503 yInfo(
"Could not find period in the config file\nusing 10ms as default");
507 if (rf.check(
"rate"))
509 yError (
"'rate' parameter is deprecated. Use 'period' instead");
512 std::string remoteInertialName{
"/"+robot_name+
"/head/inertials"};
513 if (rf.check(
"imuPortName"))
515 remoteInertialName = rf.find(
"imuPortName").asString();
519 if (rf.check(
"dummy_ft"))
522 yInfo(
"Using dummy FT sensors (debug mode)\n");
526 if (rf.check(
"dumpvel"))
528 dump_vel_enabled =
true;
529 yInfo(
"Dumping joint velocities and accelerations (debug mode)\n");
532 if (rf.check(
"auto_drift_comp"))
534 auto_drift_comp =
true;
535 yInfo(
"Enabling automatic drift compensation (experimental)\n");
538 if (rf.check(
"default_ee_cont"))
540 default_ee_cont =
true;
541 yInfo(
"Default contact at the end effector\n");
547 OptionsHead.put(
"device",
"remote_controlboard");
548 OptionsHead.put(
"local",
"/"+local_name+
"/head/client");
549 OptionsHead.put(
"remote",
"/"+robot_name+
"/head");
553 yError(
"unable to create head device driver...quitting\n");
557 yInfo(
"device driver created\n");
560 if (left_arm_enabled)
563 Property mas_left_arm_FT_conf {{
"device",Value(
"multipleanalogsensorsclient")},
564 {
"local", Value(
"/"+local_name+
"/left_arm/FT")},
565 {
"remote",Value(
"/"+robot_name+
"/left_arm/FT")},
566 {
"timeout",Value(0.1)}};
568 if (!dd_left_arm_FT_MASClient.open(mas_left_arm_FT_conf))
570 yError(
"Unable to open the left_arm FT MAS client...quitting\n");
575 if (!dd_left_arm_FT_MASClient.view(m_left_arm_FT))
577 yError(
"View of one of the MAS interfaces for left_arm required failed...quitting\n");
582 OptionsLeftArm.put(
"device",
"remote_controlboard");
583 OptionsLeftArm.put(
"local",
"/"+local_name+
"/left_arm/client");
584 OptionsLeftArm.put(
"remote",
"/"+robot_name+
"/left_arm");
588 yError(
"unable to create left arm device driver...quitting\n");
593 if (right_arm_enabled)
596 Property mas_right_arm_FT_conf {{
"device",Value(
"multipleanalogsensorsclient")},
597 {
"local", Value(
"/"+local_name+
"/right_arm/FT")},
598 {
"remote",Value(
"/"+robot_name+
"/right_arm/FT")},
599 {
"timeout",Value(0.1)}};
601 if (!dd_right_arm_FT_MASClient.open(mas_right_arm_FT_conf))
603 yError(
"Unable to open the right_arm FT MAS client...quitting\n");
607 if (!dd_right_arm_FT_MASClient.view(m_right_arm_FT))
609 yError(
"View of one of the MAS interfaces for right_arm required failed...quitting\n");
613 OptionsRightArm.put(
"device",
"remote_controlboard");
614 OptionsRightArm.put(
"local",
"/"+local_name+
"/right_arm/client");
615 OptionsRightArm.put(
"remote",
"/"+robot_name+
"/right_arm");
619 yError(
"unable to create right arm device driver...quitting\n");
627 Property mas_left_leg_FT_conf {{
"device",Value(
"multipleanalogsensorsclient")},
628 {
"local", Value(
"/"+local_name+
"/left_leg/FT")},
629 {
"remote",Value(
"/"+robot_name+
"/left_leg/FT")},
630 {
"timeout",Value(0.1)}};
632 if (!dd_left_leg_FT_MASClient.open(mas_left_leg_FT_conf))
634 yError(
"Unable to open the left_leg FT MAS client...quitting\n");
638 if (!dd_left_leg_FT_MASClient.view(m_left_leg_FT))
640 yError(
"View of one of the MAS interfaces for left_leg required failed...quitting\n");
643 Property mas_right_leg_FT_conf {{
"device",Value(
"multipleanalogsensorsclient")},
644 {
"local", Value(
"/"+local_name+
"/right_leg/FT")},
645 {
"remote",Value(
"/"+robot_name+
"/right_leg/FT")},
646 {
"timeout",Value(0.1)}};
648 if (!dd_right_leg_FT_MASClient.open(mas_right_leg_FT_conf))
650 yError(
"Unable to open the right_leg FT MAS client...quitting\n");
654 if (!dd_right_leg_FT_MASClient.view(m_right_leg_FT))
656 yError(
"View of one of the MAS interfaces for right_leg required failed...quitting\n");
660 OptionsLeftLeg.put(
"device",
"remote_controlboard");
661 OptionsLeftLeg.put(
"local",
"/"+local_name+
"/left_leg/client");
662 OptionsLeftLeg.put(
"remote",
"/"+robot_name+
"/left_leg");
666 yError(
"unable to create left leg device driver...quitting\n");
670 OptionsRightLeg.put(
"device",
"remote_controlboard");
671 OptionsRightLeg.put(
"local",
"/"+local_name+
"/right_leg/client");
672 OptionsRightLeg.put(
"remote",
"/"+robot_name+
"/right_leg");
676 yError(
"unable to create right leg device driver...quitting\n");
683 OptionsTorso.put(
"device",
"remote_controlboard");
684 OptionsTorso.put(
"local",
"/"+local_name+
"/torso/client");
685 OptionsTorso.put(
"remote",
"/"+robot_name+
"/torso");
689 yError(
"unable to create head device driver...quitting\n");
693 yInfo(
"device driver created\n");
696 Property mas_head_inertial_conf {{
"device",Value(
"multipleanalogsensorsclient")},
697 {
"local", Value(
"/"+local_name+
"/inertials")},
698 {
"remote",Value(remoteInertialName)},
699 {
"timeout",Value(0.1)}};
701 if (!dd_head_inertial_MASClient.open(mas_head_inertial_conf))
703 yError(
"unable to open the head inertial MAS client...quitting\n");
707 if(!dd_head_inertial_MASClient.view(m_iAcc) || !dd_head_inertial_MASClient.view(m_iGyro))
709 yError(
"view of one of the MAS interfaces required failed...quitting\n");
714 string rpcPortName =
"/"+local_name+
"/rpc:i";
715 rpcPort.open(rpcPortName);
719 port_filtered_output.open(
"/"+local_name+
"/filtered/inertial:o");
720 inertialFilter =
new dataFilter(port_filtered_output, m_iGyro, m_iAcc);
723 inv_dyn =
new inverseDynamics(rate, dd_left_arm, dd_right_arm, dd_head, dd_left_leg, dd_right_leg, dd_torso, robot_name, local_name, icub_type, autoconnect, m_left_arm_FT, m_right_arm_FT, m_left_leg_FT, m_right_leg_FT);
732 yInfo(
"ft thread istantiated...\n");
735 inertialFilter->start();
737 yInfo(
"thread started\n");
744 yInfo(
"Closing wholeBodyDynamics module... \n");
751 yInfo(
"Setting the icub in stiff mode\n");
754 yInfo(
"Stopping the inv_dyn thread...");
756 yInfo(
"inv_dyn thread stopped\n");
763 yInfo(
"Stopping the inertial filter thread \n");
764 inertialFilter->stop();
765 delete inertialFilter;
766 inertialFilter=
nullptr;
767 dd_head_inertial_MASClient.close();
770 yInfo(
"Closing the filtered inertial output port \n");
771 port_filtered_output.interrupt();
772 port_filtered_output.close();
774 yInfo(
"Closing the rpc port \n");
779 yInfo(
"Closing dd_left_arm \n");
780 dd_left_arm_FT_MASClient.close();
781 dd_left_arm->close();
787 yInfo(
"Closing dd_right_arm \n");
788 dd_right_arm_FT_MASClient.close();
789 dd_right_arm->close();
791 dd_right_arm=
nullptr;
795 yInfo(
"Closing dd_head \n");
803 yInfo(
"Closing dd_left_leg \n");
804 dd_left_leg_FT_MASClient.close();
805 dd_left_leg->close();
811 yInfo(
"Closing dd_right_leg \n");
812 dd_right_leg_FT_MASClient.close();
813 dd_right_leg->close();
815 dd_right_leg=
nullptr;
819 yInfo(
"Closing dd_torso \n");
825 yInfo(
"wholeBodyDynamics module was closed successfully! \n");
835 double avgTime, stdDev, period;
836 period = inv_dyn->getPeriod();
837 inv_dyn->getEstimatedPeriod(avgTime, stdDev);
838 if(avgTime > 1.3 * period){
842 static unsigned long int alive_counter = 0;
843 static double curr_time = Time::now();
844 if (Time::now() - curr_time > 60)
846 yInfo (
"wholeBodyDynamics is alive! running for %ld mins.\n",++alive_counter);
847 curr_time = Time::now();
850 if (inv_dyn==
nullptr)
857 yError (
"wholeBodyDynamics module lost connection with iCubInterface, now closing...\n");
862 yInfo(
"wholeBodyDynamics module was closed successfully! \n");