28 bool bEveryThingisGood =
true;
30 string moduleName = rf.check(
"name", Value(
"babbling"),
"module name (string)").asString();
32 robot = rf.check(
"robot", Value(
"icub")).asString();
33 single_joint = rf.check(
"single_joint", Value(-1)).asInt();
35 Bottle &start_pos = rf.findGroup(
"start_position");
36 Bottle *b_start_commandHead = start_pos.find(
"head").asList();
37 Bottle *b_start_command = start_pos.find(
"arm").asList();
39 start_command_head.resize(3, 0.0);
40 if ((b_start_commandHead->isNull()) || (b_start_commandHead->size() < 3)) {
41 yWarning(
"Something is wrong in ini file. Default value is used");
42 start_command_head[0] = -20.0;
43 start_command_head[1] = -20.0;
44 start_command_head[2] = +10.0;
46 for (
unsigned int i = 0; i < b_start_commandHead->size(); i++) {
47 start_command_head[i] = b_start_commandHead->get(i).asDouble();
48 yDebug() << start_command_head[i];
52 if ((b_start_command->isNull()) || (b_start_command->size() < 16)) {
53 yWarning(
"Something is wrong in ini file. Default values are used");
54 start_command_arm[0] = -45.0; start_command_arm[1] = 35.0; start_command_arm[2] = 0.0;
55 start_command_arm[3] = 50.0; start_command_arm[4] = -45.0; start_command_arm[5] = 0.0;
56 start_command_arm[6] = 0.0; start_command_arm[7] = 0.0; start_command_arm[8] = 10.0;
57 start_command_arm[9] = 0.0; start_command_arm[10] = 0.0; start_command_arm[11] = 0.0;
58 start_command_arm[12] = 0.0; start_command_arm[13] = 0.0; start_command_arm[14] = 0.0;
59 start_command_arm[15] = 0.0;
61 for (
unsigned int i = 0; i < b_start_command->size(); i++)
62 start_command_arm[i] = b_start_command->get(i).asDouble();
65 Bottle &babbl_par = rf.findGroup(
"babbling_param");
66 freq = babbl_par.check(
"freq", Value(0.2)).asDouble();
67 amp = babbl_par.check(
"amp", Value(5.0)).asDouble();
68 duration = babbl_par.check(
"duration", Value(20.0)).asDouble();
70 setName(moduleName.c_str());
73 if (!handlerPort.open(
"/" + getName() +
"/rpc")) {
74 yError() << getName() <<
": Unable to open port " <<
"/" << getName() <<
"/rpc";
75 bEveryThingisGood =
false;
79 yInfo() <<
"Going to initialise iCub ...";
80 while (!init_iCub()) {
81 yDebug() << getName() <<
": initialising iCub... please wait... ";
84 yDebug() <<
"End configuration...";
88 return bEveryThingisGood;
92 handlerPort.interrupt();
100 yInfo() <<
"Closing module, please wait ... ";
106 handlerPort.interrupt();
115 string helpMessage = string(getName().c_str()) +
" commands are: \n " +
116 "babbling arm <left/right>: motor commands sent to all " 117 "the arm joints \n " +
118 "babbling hand <left/right>: motor commands sent to all " 119 "the hand joints \n " +
120 "babbling joint <int joint_number> <left/right>: motor " 121 "commands sent to joint_number only \n " +
122 "help \n " +
"quit \n";
126 if (command.get(0).asString() ==
"quit") {
127 reply.addString(
"Quitting");
129 }
else if (command.get(0).asString() ==
"help") {
130 yInfo() << helpMessage;
131 reply.addString(helpMessage);
132 }
else if (command.get(0).asString() ==
"babbling") {
133 if (command.get(1).asString() ==
"arm") {
135 part_babbling = command.get(1).asString();
137 if (command.get(2).asString() ==
"left" || command.get(2).asString() ==
"right") {
138 part = command.get(2).asString() +
"_arm";
139 yInfo() <<
"Babbling " + command.get(2).asString() +
" arm...";
141 if (command.size() >= 4) {
142 yInfo() <<
"Custom duration = " << command.get(3).asDouble();
143 if (command.get(3).asDouble() >= 0.0) {
144 double newDuration = command.get(3).asDouble();
145 double oldDuration = duration;
146 duration = newDuration;
147 yInfo() <<
"duration is changed from " << oldDuration <<
" to " << duration;
149 duration = oldDuration;
150 yInfo() <<
"Going back to duration from config file: " << duration;
151 reply.addString(
"ack");
154 yError(
"Invalid train duration: Must be a double >= 0.0");
155 yWarning(
"Doing the babbling anyway with default value");
160 reply.addString(
"ack");
163 yError(
"Invalid babbling part: specify LEFT or RIGHT after 'arm'.");
164 reply.addString(
"nack");
167 }
else if (command.get(1).asString() ==
"joint") {
168 single_joint = command.get(2).asInt();
169 if (single_joint < 16 && single_joint >= 0) {
170 if (command.get(3).asString() ==
"left" || command.get(3).asString() ==
"right") {
171 part = command.get(3).asString() +
"_arm";
172 yInfo() <<
"Babbling joint " << single_joint <<
" of " << part;
175 if (command.size() >= 5) {
176 yInfo() <<
"Custom duration = " << command.get(4).asDouble();
177 if (command.get(4).asDouble() >= 0.0) {
178 double newDuration = command.get(4).asDouble();
179 double oldDuration = duration;
180 duration = newDuration;
181 yInfo() <<
"duration is changed from " << oldDuration
182 <<
" to " << duration;
184 duration = oldDuration;
185 yInfo() <<
"Going back to duration from config file: " 187 reply.addString(
"ack");
190 yError(
"Invalid train duration: Must be a double >= 0.0");
191 yWarning(
"Doing the babbling anyway with default value");
196 reply.addString(
"ack");
199 yError(
"Invalid babbling part: specify LEFT or RIGHT after joint number.");
200 reply.addString(
"nack");
204 yError(
"Invalid joint number.");
205 reply.addString(
"nack");
208 }
else if (command.get(1).asString() ==
"hand") {
210 part_babbling = command.get(1).asString();
212 if (command.get(2).asString() ==
"left" ||
213 command.get(2).asString() ==
"right") {
214 part = command.get(2).asString() +
"_arm";
215 yInfo() <<
"Babbling " + command.get(2).asString() +
" hand...";
217 reply.addString(
"ack");
220 yError(
"Invalid babbling part: specify LEFT or RIGHT after 'hand'.");
221 reply.addString(
"nack");
225 yInfo() <<
"Command not found\n" << helpMessage;
226 reply.addString(
"nack");
227 reply.addString(
"command not found");
228 reply.addString(helpMessage);
245 bool homeStart = gotoStartPos();
247 yError() <<
"I got lost going home!";
252 for (
int i = 0; i < 16; i++) {
253 if (part ==
"right_arm") {
254 ictrlRightArm->setControlMode(i, VOCAB_CM_VELOCITY);
255 }
else if (part ==
"left_arm") {
256 ictrlLeftArm->setControlMode(i, VOCAB_CM_VELOCITY);
258 yError() <<
"Don't know which part to move to do babbling.";
263 double startTime = yarp::os::Time::now();
265 yDebug() <<
"============> babbling with COMMAND will START";
266 yInfo() <<
"AMP " << amp <<
"FREQ " << freq;
269 while (Time::now() < startTime + duration) {
270 double t = Time::now() - startTime;
271 yInfo() <<
"t = " << t <<
"/ " << duration;
273 babblingCommands(t, single_joint);
276 yDebug() <<
"============> babbling with COMMAND is FINISHED";
282 double ref_command[16];
283 yarp::sig::VectorOf<double>
command;
284 yarp::sig::VectorOf<double> encodersUsed;
287 for (
unsigned int l = 0; l < command.size(); l++) {
291 for (
unsigned int l = 0; l < 16; l++) {
292 ref_command[l] = start_command_arm[l] + amp * sin(freq * t * 2 * M_PI);
295 if (part ==
"right_arm" || part ==
"left_arm") {
296 bool okEncArm =
false;
298 if (part ==
"right_arm") {
299 encodersUsed = encodersRightArm;
300 okEncArm = encsRightArm->getEncoders(encodersUsed.data());
302 encodersUsed = encodersLeftArm;
303 okEncArm = encsLeftArm->getEncoders(encodersUsed.data());
306 yDebug() <<
"j_idx: " << j_idx;
310 yError() <<
"Error receiving encoders";
313 yDebug() <<
"command before correction = " << ref_command[j_idx];
314 yDebug() <<
"current encoders : " << encodersUsed[j_idx];
315 command[j_idx] = amp * sin(freq * t * 2 * M_PI);
316 yDebug() <<
"command after correction = " << command[j_idx];
317 if (command[j_idx] > 50) command[j_idx] = 50;
318 if (command[j_idx] < -50) command[j_idx] = -50;
319 yDebug() <<
"command after saturation = " << command[j_idx];
322 if (part_babbling ==
"arm") {
324 yError() <<
"Error receiving encoders";
325 for (
unsigned int l = 0; l < 7; l++) command[l] = 0;
327 for (
unsigned int l = 0; l < 7; l++) {
328 command[l] = 10 * (ref_command[l] - encodersUsed[l]);
329 if (command[l] > 20) command[l] = 20;
330 if (command[l] < -20) command[l] = -20;
333 }
else if (part_babbling ==
"hand") {
335 yError() <<
"Error receiving encoders";
336 for (
unsigned int l = 7; l < command.size(); l++) command[l] = 0;
338 for (
unsigned int l = 7; l < command.size(); l++) {
339 command[l] = 10 * (ref_command[l] - encodersUsed[l]);
340 if (command[l] > 20) command[l] = 20;
341 if (command[l] < -20) command[l] = -20;
345 yError(
"Can't babble the required body part.");
349 if (part ==
"right_arm") {
350 velRightArm->velocityMove(command.data());
351 }
else if (part ==
"left_arm") {
352 velLeftArm->velocityMove(command.data());
354 yError() <<
"Don't know which part to move to do babbling.";
357 yarp::os::Time::delay(0.05);
360 yError() <<
"Which arm to babble with?";
365 yarp::sig::VectorOf<double> ang = start_command_head;
366 if (part ==
"right_arm") {
369 return igaze->lookAtAbsAngles(ang);
373 igaze->stopControl();
377 yarp::os::Time::delay(2.0);
379 moveHeadToStartPos();
381 yarp::sig::VectorOf<double>
command;
385 if (part ==
"left_arm" || part ==
"right_arm") {
386 if (part ==
"left_arm") {
387 command = encodersLeftArm;
388 for (
int i = 0; i < 16; i++) {
389 ictrlLeftArm->setControlMode(i, VOCAB_CM_POSITION);
390 command[i] = start_command_arm[i];
392 posLeftArm->positionMove(command.data());
394 command = encodersRightArm;
395 for (
int i = 0; i < 16; i++) {
396 ictrlRightArm->setControlMode(i, VOCAB_CM_POSITION);
397 command[i] = start_command_arm[i];
399 posRightArm->positionMove(command.data());
402 bool done_head =
false;
403 bool done_arm =
false;
404 while (!done_head || !done_arm) {
405 yInfo() <<
"Wait for position moves to finish";
406 igaze->checkMotionDone(&done_head);
407 if (part ==
"left_arm") {
408 posLeftArm->checkMotionDone(&done_arm);
410 posRightArm->checkMotionDone(&done_arm);
418 yError() <<
"Don't know which part to move to start position.";
426 Property option_left;
428 string portnameLeftArm =
"left_arm";
429 option_left.put(
"robot", robot.c_str());
430 option_left.put(
"device",
"remote_controlboard");
431 Value &robotnameLeftArm = option_left.find(
"robot");
433 option_left.put(
"local",
"/babbling/" + robotnameLeftArm.asString() +
"/" + portnameLeftArm +
"/control");
434 option_left.put(
"remote",
"/" + robotnameLeftArm.asString() +
"/" + portnameLeftArm);
436 yDebug() <<
"option left arm: " << option_left.toString().c_str();
438 if (!leftArmDev.open(option_left)) {
439 yError() <<
"Device not available. Here are the known devices:";
440 yError() << yarp::dev::Drivers::factory().toString();
444 leftArmDev.view(posLeftArm);
445 leftArmDev.view(velLeftArm);
446 leftArmDev.view(itrqLeftArm);
447 leftArmDev.view(encsLeftArm);
448 leftArmDev.view(ictrlLeftArm);
449 leftArmDev.view(ictrlLimLeftArm);
451 double minLimArm[16];
452 double maxLimArm[16];
453 for (
int l = 0; l < 16; l++) {
454 ictrlLimLeftArm->getLimits(l, &minLimArm[l], &maxLimArm[l]);
457 for (
int l = 0; l < 16; l++) {
458 yInfo() <<
"Joint " << l <<
": limits = [" << minLimArm[l] <<
"," 459 << maxLimArm[l] <<
"]. start_commad = " << start_command_arm[l];
462 if (posLeftArm ==
nullptr || encsLeftArm ==
nullptr || velLeftArm ==
nullptr ||
463 itrqLeftArm ==
nullptr || ictrlLeftArm ==
nullptr || encsLeftArm ==
nullptr) {
464 yError() <<
"Cannot get interface to robot device for left arm";
469 posLeftArm->getAxes(&nj_left);
470 encodersLeftArm.resize(nj_left);
472 yInfo() <<
"Wait for arm encoders";
473 while (!encsLeftArm->getEncoders(encodersLeftArm.data())) {
475 yInfo() <<
"Wait for arm encoders";
483 Property option_right;
485 string portnameRightArm =
"right_arm";
486 option_right.put(
"robot", robot.c_str());
487 option_right.put(
"device",
"remote_controlboard");
488 Value &robotnameRightArm = option_right.find(
"robot");
490 option_right.put(
"local",
"/babbling/" + robotnameRightArm.asString() +
"/" + portnameRightArm +
"/control");
491 option_right.put(
"remote",
"/" + robotnameRightArm.asString() +
"/" + portnameRightArm);
493 yDebug() <<
"option right arm: " << option_right.toString().c_str();
495 if (!rightArmDev.open(option_right)) {
496 yError() <<
"Device not available. Here are the known devices:";
497 yError() << yarp::dev::Drivers::factory().toString();
501 rightArmDev.view(posRightArm);
502 rightArmDev.view(velRightArm);
503 rightArmDev.view(itrqRightArm);
504 rightArmDev.view(encsRightArm);
505 rightArmDev.view(ictrlRightArm);
506 rightArmDev.view(ictrlLimRightArm);
508 double minLimArm[16];
509 double maxLimArm[16];
510 for (
int l = 0; l < 16; l++) {
511 ictrlLimRightArm->getLimits(l, &minLimArm[l], &maxLimArm[l]);
514 for (
int l = 0; l < 16; l++) {
515 yInfo() <<
"Joint " << l <<
": limits = [" << minLimArm[l] <<
"," 516 << maxLimArm[l] <<
"]. start_commad = " << start_command_arm[l];
519 if (posRightArm ==
nullptr || encsRightArm ==
nullptr || velRightArm ==
nullptr ||
520 itrqRightArm ==
nullptr || ictrlRightArm ==
nullptr || encsRightArm ==
nullptr) {
521 yError() <<
"Cannot get interface to robot device for right arm";
526 posRightArm->getAxes(&nj_right);
527 encodersRightArm.resize(nj_right);
529 yInfo() <<
"Wait for arm encoders";
530 while (!encsRightArm->getEncoders(encodersRightArm.data())) {
532 yInfo() <<
"Wait for arm encoders";
539 if(!init_left_arm()) {
542 if(!init_right_arm()) {
546 yInfo() <<
"Arms initialized.";
549 Property option_head;
551 option_head.put(
"device",
"gazecontrollerclient");
552 option_head.put(
"remote",
"/iKinGazeCtrl");
553 option_head.put(
"local",
"/babbling/gaze");
555 if (!headDev.open(option_head)) {
556 yError() <<
"Device not available. Here are the known devices:";
557 yError() << yarp::dev::Drivers::factory().toString();
562 yInfo() <<
"Head initialized.";
564 yInfo() <<
"> Initialisation done.";
bool init_left_arm()
Create PolyDriver for left arm.
bool init_iCub()
Initializes all the device controllers.
bool gotoStartPos()
Move head + arm in start position.
bool init_right_arm()
Create PolyDriver for left arm.
bool moveHeadToStartPos()
Use iGazeController to move head in start position.
bool doBabbling()
Sets the joints to be in velocity control, then goes in a loop to call babblingCommands for duration ...
bool configure(yarp::os::ResourceFinder &rf)
void babblingCommands(double &t, int j_idx)
Issues the actual velocity commands for time t.
bool respond(const yarp::os::Bottle &command, yarp::os::Bottle &reply)