104 #include <yarp/os/all.h>
105 #include <yarp/sig/all.h>
106 #include <yarp/dev/all.h>
118 using namespace yarp::os;
119 using namespace yarp::sig;
120 using namespace yarp::math;
130 BufferedPort<Vector> &port_filtered_output;
132 IThreeAxisGyroscopes* m_iGyro{
nullptr};
133 IThreeAxisLinearAccelerometers* m_iAcc{
nullptr};
137 IThreeAxisGyroscopes* iGyro,
138 IThreeAxisLinearAccelerometers* iAcc): PeriodicThread(0.01),
139 port_filtered_output(_port_filtered_output),
148 if (!m_iGyro || !m_iAcc || port_filtered_output.isClosed()) {
149 yError()<<
"dataFilter: something went wrong during configuration closing";
154 double acc_ts{0.0}, gyro_ts{0.0};
157 ok &= m_iAcc->getThreeAxisLinearAccelerometerMeasure(0,
acc, acc_ts);
158 ok &= m_iGyro->getThreeAxisGyroscopeMeasure(0, gyro, gyro_ts);
160 yError()<<
"dataFilter: error while reading from inertial sensor";
164 info.update(gyro_ts);
166 temp.setSubvector(0,
acc);
167 temp.setSubvector(3,gyro);
168 for(
size_t i=0;i<temp.size();i++)
180 port_filtered_output.prepare() = g;
181 port_filtered_output.setEnvelope(info);
182 port_filtered_output.write();
191 Property OptionsLeftArm;
192 Property OptionsRightArm;
193 Property OptionsHead;
194 Property OptionsLeftLeg;
195 Property OptionsRightLeg;
196 Property OptionsTorso;
201 bool com_vel_enabled;
202 bool left_arm_enabled;
203 bool right_arm_enabled;
206 bool dump_vel_enabled;
207 bool auto_drift_comp;
208 bool default_ee_cont;
212 BufferedPort<Vector> port_filtered_output;
216 IThreeAxisLinearAccelerometers* m_iAcc{
nullptr};
217 IThreeAxisGyroscopes* m_iGyro{
nullptr};
218 ISixAxisForceTorqueSensors* m_left_arm_FT{
nullptr};
219 ISixAxisForceTorqueSensors* m_right_arm_FT{
nullptr};
220 ISixAxisForceTorqueSensors* m_left_leg_FT{
nullptr};
221 ISixAxisForceTorqueSensors* m_right_leg_FT{
nullptr};
223 PolyDriver *dd_left_arm;
224 PolyDriver *dd_right_arm;
226 PolyDriver *dd_left_leg;
227 PolyDriver *dd_right_leg;
228 PolyDriver *dd_torso;
229 PolyDriver dd_head_inertial_MASClient;
230 PolyDriver dd_left_arm_FT_MASClient;
231 PolyDriver dd_right_arm_FT_MASClient;
232 PolyDriver dd_left_leg_FT_MASClient;
233 PolyDriver dd_right_leg_FT_MASClient;
240 dd_right_arm=
nullptr;
243 dd_right_leg=
nullptr;
245 com_vel_enabled=
false;
248 torso_enabled =
true;
249 left_arm_enabled =
true;
250 right_arm_enabled =
true;
252 w0_dw0_enabled =
false;
254 dump_vel_enabled =
false;
255 auto_drift_comp =
false;
256 default_ee_cont =
false;
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");
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");
551 if (!createDriver(dd_head, OptionsHead))
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");
586 if (!createDriver(dd_left_arm, OptionsLeftArm))
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");
617 if (!createDriver(dd_right_arm, OptionsRightArm))
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");
664 if (!createDriver(dd_left_leg, OptionsLeftLeg))
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");
674 if (!createDriver(dd_right_leg, OptionsRightLeg))
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");
687 if (!createDriver(dd_torso, OptionsTorso))
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");
873 rf.setDefaultContext(
"wholeBodyDynamics");
874 rf.setDefaultConfigFile(
"wholeBodyDynamics.ini");
875 rf.configure(
argc,argv);
877 if (rf.check(
"help"))
879 cout <<
"Options:" << endl << endl;
880 cout <<
"\t--context context: where to find the called resource (referred to $ICUB_ROOT/app: default wholeBodyDynamics)" << endl;
881 cout <<
"\t--from from: the name of the file.ini to be used for calibration" << endl;
882 cout <<
"\t--period period: the period used by the module. default: 10ms" << endl;
883 cout <<
"\t--robot robot: the robot name. default: iCub" << endl;
884 cout <<
"\t--local name: the prefix of the ports opened by the module. defualt: wholeBodyDynamics" << endl;
885 cout <<
"\t--autoconnect automatically connects the module ports to iCubInterface" << endl;
886 cout <<
"\t--no_legs this option disables the dynamics computation for the legs joints" << endl;
887 cout <<
"\t--headV2 use the model of the headV2" << endl;
888 cout <<
"\t--headV2.6 use the model of the headV2.6" << endl;
889 cout <<
"\t--headV2.7 use the model of the headV2.7" << endl;
890 cout <<
"\t--legsV2 use the model of legsV2" << endl;
891 cout <<
"\t--no_left_arm disables the left arm" << endl;
892 cout <<
"\t--no_right_arm disables the right arm" << endl;
893 cout <<
"\t--no_com disables the com computation" << endl;
894 cout <<
"\t--dummy_ft uses fake FT sensors (debug use only)" << endl;
895 cout <<
"\t--dumpvel dumps joint velocities and accelerations (debug use only)" << endl;
896 cout <<
"\t--experimental_com_vel enables com velocity computation (experimental)" << endl;
897 cout <<
"\t--auto_drift_comp enables automatic drift compensation (experimental, under debug)" << endl;
903 if (!
yarp.checkNetwork())
905 yError(
"Sorry YARP network does not seem to be available, is the yarp server available?\n");
910 return obs.runModule(rf);
dataFilter(BufferedPort< Vector > &_port_filtered_output, IThreeAxisGyroscopes *iGyro, IThreeAxisLinearAccelerometers *iAcc)
void calibrateOffset(calib_enum calib_code=CALIB_ALL)
thread_status_enum getThreadStatus()
virtual bool createDriver(PolyDriver *&_dd, Property options)
bool respond(const Bottle &command, Bottle &reply) override
double getPeriod() override
bool updateModule() override
bool configure(ResourceFinder &rf) override
int main(int argc, char *argv[])
Copyright (C) 2008 RobotCub Consortium.
double lpf_ord1_3hz(double input, int j)