69 #include <yarp/os/RFModule.h>
70 #include <yarp/os/Time.h>
71 #include <yarp/os/Network.h>
72 #include <yarp/os/Log.h>
73 #include <yarp/os/LogStream.h>
74 #include <yarp/dev/PolyDriver.h>
75 #include <yarp/dev/ControlBoardInterfaces.h>
83 using namespace yarp::os;
84 using namespace yarp::sig;
85 using namespace yarp::math;
99 Property OptionsLeftArm;
100 Property OptionsRightArm;
101 Property OptionsHead;
102 Property OptionsLeftLeg;
103 Property OptionsRightLeg;
104 Property OptionsTorso;
108 PolyDriver *dd_left_arm;
109 PolyDriver *dd_right_arm;
111 PolyDriver *dd_left_leg;
112 PolyDriver *dd_right_leg;
113 PolyDriver *dd_torso;
116 bool right_arm_enabled;
117 bool left_arm_enabled;
127 left_arm_enabled =
true;
128 right_arm_enabled =
true;
129 torso_enabled =
true;
131 dd_left_arm =
nullptr;
132 dd_right_arm =
nullptr;
134 dd_left_leg =
nullptr;
135 dd_right_leg =
nullptr;
144 double start_time = yarp::os::Time::now();
148 double current_time = yarp::os::Time::now();
158 _dd =
new PolyDriver(options);
159 bool connected =_dd->isValid();
162 if (connected)
break;
165 if (current_time-start_time > 60.0)
167 yError(
"It is not possible to instantiate the device driver. I tried %d times!\n", trials);
171 yarp::os::Time::delay(5);
173 yWarning(
"Unable to connect the device driver, trying again...\n");
177 IEncoders *
encs =
nullptr;
178 IControlMode *ctrlMode =
nullptr;
179 IImpedanceControl *imp =
nullptr;
180 ITorqueControl *tqs =
nullptr;
184 ok =
ok & _dd->view(ctrlMode);
185 ok =
ok & _dd->view(imp);
186 ok =
ok & _dd->view(tqs);
189 yError(
"One or more devices has not been viewed\nreturning...");
198 string fwdSlash =
"/";
201 name =
"gravityCompensator";
203 if (rf.check(
"period"))
204 rate = rf.find(
"period").asInt32();
207 if (rf.check(
"rate"))
209 yError (
"'rate' parameter is deprecated. Use 'period' instead");
215 if (rf.check(
"robot"))
216 robot_name = rf.find(
"robot").asString();
217 else robot_name =
"icub";
221 if (rf.check(
"headV2"))
223 yInfo(
"'headV2' option found. Using icubV2 head kinematics.\n");
227 if (rf.check(
"headV2.6"))
229 yInfo(
"'headV2.6' option found. Using icubV2.6 head kinematics.\n");
234 if (rf.check(
"headV2.7"))
236 yInfo(
"'headV2.7' option found. Using icubV2.7 head kinematics.\n");
242 if (rf.check(
"no_legs"))
245 yInfo(
"'no_legs' option found. Legs will be disabled.\n");
248 if (rf.check(
"no_left_arm"))
250 left_arm_enabled=
false;
251 yInfo(
"'no_left_arm' option found. Left arm will be disabled.\n");
254 if (rf.check(
"no_right_arm"))
256 right_arm_enabled=
false;
257 yInfo(
"'no_right_arm' option found. Right arm will be disabled.\n");
260 if (rf.check(
"no_torso_legs"))
262 torso_enabled=
false;
264 yInfo(
"no_torso_legs' option found. Torso and legs will be disabled.\n");
266 if (rf.check(
"no_torso"))
268 torso_enabled=
false;
269 yInfo(
"'no_torso' option found. Torso will be disabled.\n");
272 if (rf.check(
"no_head"))
275 yInfo(
"'no_head' option found. Head will be disabled.\n");
280 OptionsHead.put(
"device",
"remote_controlboard");
281 OptionsHead.put(
"local",
"/gravityCompensator/head/client");
282 OptionsHead.put(
"remote",
"/"+robot_name+
"/head");
284 if (!createDriver(dd_head, OptionsHead))
286 yError(
"unable to create head device driver...quitting\n");
291 if (left_arm_enabled)
293 OptionsLeftArm.put(
"device",
"remote_controlboard");
294 OptionsLeftArm.put(
"local",
"/gravityCompensator/left_arm/client");
295 OptionsLeftArm.put(
"remote",
"/"+robot_name+
"/left_arm");
296 if (!createDriver(dd_left_arm,OptionsLeftArm))
298 yError(
"unable to create left arm device driver...quitting\n");
303 if (right_arm_enabled)
305 OptionsRightArm.put(
"device",
"remote_controlboard");
306 OptionsRightArm.put(
"local",
"/gravityCompensator/right_arm/client");
307 OptionsRightArm.put(
"remote",
"/"+robot_name+
"/right_arm");
308 if (!createDriver(dd_right_arm,OptionsRightArm))
310 yError(
"unable to create right arm device driver...quitting\n");
317 OptionsLeftLeg.put(
"device",
"remote_controlboard");
318 OptionsLeftLeg.put(
"local",
"/gravityCompensator/left_leg/client");
319 OptionsLeftLeg.put(
"remote",
"/"+robot_name+
"/left_leg");
320 if (!createDriver(dd_left_leg,OptionsLeftLeg))
322 yError(
"unable to create left leg device driver...quitting\n");
326 OptionsRightLeg.put(
"device",
"remote_controlboard");
327 OptionsRightLeg.put(
"local",
"/gravityCompensator/right_leg/client");
328 OptionsRightLeg.put(
"remote",
"/"+robot_name+
"/right_leg");
329 if (!createDriver(dd_right_leg,OptionsRightLeg))
331 yError(
"unable to create right leg device driver...quitting\n");
338 OptionsTorso.put(
"device",
"remote_controlboard");
339 OptionsTorso.put(
"local",
"/gravityCompensator/torso/client");
340 OptionsTorso.put(
"remote",
"/"+robot_name+
"/torso");
342 if (!createDriver(dd_torso,OptionsTorso))
344 yError(
"unable to create torso device driver...quitting\n");
349 yInfo(
"device driver created\n");
351 rpcPort.open(
"/"+name+
"/rpc");
355 std::string wholeBodyName =
"wholeBodyDynamics";
356 if (rf.check(
"wholebody_name"))
358 wholeBodyName = rf.find(
"wholebody_name").asString();
359 yInfo(
"'wholeBodyName' option found. Using /%s prefix for connections.\n", wholeBodyName.c_str());
363 bool inertial_enabled=
true;
364 if (rf.check(
"no_inertial"))
366 inertial_enabled=
false;
367 yInfo(
"'no_inertial' option found. Disabling inertial measurment.\n");
372 g_comp =
new gravityCompensatorThread(wholeBodyName, rate, dd_left_arm, dd_right_arm, dd_head, dd_left_leg, dd_right_leg, dd_torso, icub_type, inertial_enabled);
373 yInfo(
"ft thread istantiated...\n");
375 yInfo(
"thread started\n");
378 if (rf.check(
"gravity_on"))
382 if (rf.check(
"gravity_off"))
386 if (rf.check(
"external_on"))
390 if (rf.check(
"external_off"))
396 if (g_comp->external_mode==
EXTERNAL_TRQ_ON) yInfo(
"external input on");
397 else if (g_comp->external_mode==
EXTERNAL_TRQ_OFF) yInfo(
"external input off");
402 bool respond(
const Bottle& command, Bottle& reply)
override
404 reply.addVocab32(
"many");
405 reply.addString(
"Available commands:");
406 reply.addString(
"calib all");
409 string helpMessage = getName() +
410 " commands are: \n" +
411 "help to display this message\n" +
412 "gravity_on to enable the gravity compensation \n" +
413 "gravity_off to disbale the gravity compensation \n" +
414 "external_on to enable the external input torque \n" +
415 "external_off to disable the external input torque \n";
418 if (command.get(0).asString()==
"help")
421 reply.addVocab32(
"many");
422 reply.addString(
" commands are: \n" );
423 reply.addString(helpMessage.c_str());
425 else if (command.get(0).asString()==
"gravity_on")
430 reply.addString(
"gravity compensation on");
433 else if (command.get(0).asString()==
"gravity_off")
438 reply.addString(
"gravity compensation off");
441 else if (command.get(0).asString()==
"external_on")
446 reply.addString(
"external input on");
449 else if (command.get(0).asString()==
"external_off")
454 reply.addString(
"external input off");
459 reply.addString(
"unknown command. type help.");
472 delete g_comp; g_comp =
nullptr;
476 if (dd_left_arm) {
delete dd_left_arm; dd_left_arm=
nullptr; }
477 if (dd_right_arm) {
delete dd_right_arm; dd_right_arm=
nullptr; }
478 if (dd_left_leg) {
delete dd_left_leg; dd_left_leg=
nullptr; }
479 if (dd_right_leg) {
delete dd_right_leg; dd_right_leg=
nullptr; }
480 if (dd_head) {
delete dd_head; dd_head=
nullptr; }
481 if (dd_torso) {
delete dd_torso; dd_torso=
nullptr; }
493 static unsigned long int alive_counter = 0;
494 static double curr_time = Time::now();
495 if (Time::now() - curr_time > 60)
497 yInfo (
"gravityCompensator is alive! running for %ld mins.\n",++alive_counter);
498 curr_time = Time::now();
501 if (g_comp==
nullptr)
return false;
509 yError(
"gravityCompensator module lost connection with iCubInterface, now closing...\n");
514 yInfo(
"gravityCompensator module was closed successfully! \n");
524 rf.configure(
argc,argv);
526 if (rf.check(
"help"))
528 yInfo() <<
"Options:";
529 yInfo() <<
"--context context: where to find the called resource (referred to $ICUB_ROOT/app:)";
530 yInfo() <<
"--from from: the name of the file.ini to be used for calibration";
531 yInfo() <<
"--rate rate: the period used by the module. default 100ms (not less than 15ms)";
532 yInfo() <<
"--no_legs this option disables the gravity compensation for the legs joints" ;
533 yInfo() <<
"--headV2 use the model of the headV2";
534 yInfo() <<
"--headV2.6 use the model of the headV2.6";
535 yInfo() <<
"--headV2.7 use the model of the headV2.7";
536 yInfo() <<
"--no_left_arm disables the left arm";
537 yInfo() <<
"--no_right_arm disables the right arm";
538 yInfo() <<
"--no_legs disables the legs";
539 yInfo() <<
"--no_left_arm disables the left arm";
540 yInfo() <<
"--no_torso disables the torso";
541 yInfo() <<
"--no_torso_legs disables the torso and the legs";
542 yInfo() <<
"--no_head disables the head";
543 yInfo() <<
"--wholebody_name the wholeBodyDyanmics port prefix (e.g. 'wholeBodyDynamics' / 'wholeBodyDynamicsTree')";
544 yInfo() <<
"--no_inertial disables the inertial";
545 yInfo() <<
"--gravity_on enables gravity compensation (default)";
546 yInfo() <<
"--gravity_off disables gravity compensation";
547 yInfo() <<
"--external_on enables external torque command (default)";
548 yInfo() <<
"--external_off disables external torque command";
554 if (!
yarp.checkNetwork())
559 return gcomp.runModule(rf);
bool respond(const Bottle &command, Bottle &reply) override
bool updateModule() override
double getPeriod() override
virtual bool createDriver(PolyDriver *&_dd, Property options)
bool configure(ResourceFinder &rf) override
gravityModuleCompensator()
@ GRAVITY_COMPENSATION_ON
@ GRAVITY_COMPENSATION_OFF
int main(int argc, char *argv[])
Copyright (C) 2008 RobotCub Consortium.