31 string moduleName = rf.check(
"name", Value(
"proactiveTagging")).asString().c_str();
32 setName(moduleName.c_str());
34 GrammarAskNameObject = rf.findFileByName(rf.check(
"GrammarAskNameObject", Value(
"GrammarAskNameObject.xml")).toString());
35 GrammarAskNameAgent = rf.findFileByName(rf.check(
"GrammarAskNameAgent", Value(
"GrammarAskNameAgent.xml")).toString());
36 GrammarAskNameBodypart = rf.findFileByName(rf.check(
"GrammarAskNameBodypart", Value(
"GrammarAskNameBodypart.xml")).toString());
38 babblingArm = rf.check(
"babblingArm", Value(
"left")).toString();
40 thresholdDistinguishObjectsRatio = rf.check(
"thresholdDistinguishObjectsRatio", Value(3.0)).asDouble();
41 thresholdSalienceDetection = rf.check(
"thresholdSalienceDetection", Value(1.5)).asDouble();
43 yDebug() <<
"------------> babblingArm: " << babblingArm;
45 period = rf.check(
"period", Value(0.1)).asDouble();
48 bool isRFVerbose =
true;
49 iCub =
new ICubClient(moduleName,
"proactiveTagging",
"client.ini", isRFVerbose);
50 iCub->opc->isVerbose &=
true;
52 while (!iCub->connect()) {
53 yWarning() <<
"iCubClient : Some dependencies are not running...";
56 iCub->opc->checkout();
60 std::string ttsOptions = rf.check(
"ttsOptions", yarp::os::Value(
"iCub")).toString();
61 if (ttsOptions !=
"iCub") {
62 if (iCub->getSpeechClient())
63 iCub->getSpeechClient()->SetOptions(ttsOptions);
68 portToPasar.open((
"/" + moduleName +
"/pasar:o").c_str());
69 if (!Network::connect(portToPasar.getName().c_str(),
"/pasar/rpc")) {
70 yWarning() <<
"PASAR NOT CONNECTED: will not engage pointing";
71 iCub->say(
"PASAR NOT CONNECTED");
75 portFromTouchDetector.open((
"/" + moduleName +
"/fromTouch:i").c_str());
76 touchDetectorRpc = rf.check(
"touchDetectorOut", Value(
"/touchDetector/touchClean:o")).asString().c_str();
78 if (!Network::connect(touchDetectorRpc.c_str(), portFromTouchDetector.getName().c_str())) {
79 yWarning() <<
"TOUCH DETECTOR NOT CONNECTED: selfTagging will not work";
80 iCub->say(
"TOUCH DETECTOR NOT CONNECTED");
83 if (!iCub->getRecogClient()) {
84 iCub->say(
"speech recognizer not connected");
85 yWarning() <<
"SPEECH RECOGNIZER NOT CONNECTED";
89 rpcPort.open((
"/" + moduleName +
"/rpc").c_str());
92 iCub->say(
"pro active tagging is ready",
false);
93 yInfo() <<
"\n \n" <<
"----------------------------------------------" <<
"\n \n" << moduleName <<
" ready ! \n \n ";
102 portFromTouchDetector.interrupt();
103 portToPasar.interrupt();
115 portFromTouchDetector.interrupt();
116 portFromTouchDetector.close();
118 portToPasar.interrupt();
129 string helpMessage = string(getName().c_str()) +
130 " commands are: \n" +
133 "change_name oldname newname \n" +
134 "exploreUnknownEntity entity_type entity_name \n" +
135 "searchingEntity entity_type entity_name \n";
138 if (command.get(0).asString() ==
"quit") {
139 reply.addString(
"quitting");
141 rpcPort.reply(reply);
144 else if(command.get(0).asString() ==
"change_name") {
145 string oldname = command.get(1).toString();
146 string newname = command.get(2).toString();
147 yInfo() <<
"change_name from" << oldname <<
"to" << newname;
148 Entity *e = iCub->opc->getEntity(oldname);
150 iCub->say(
"No entity with name " + oldname);
151 yError() <<
"No entity with name " << oldname;
152 reply.addString(
"nack");
155 reply.addString(
"ack");
158 else if (command.get(0).asString() ==
"exploreUnknownEntity") {
160 iCub->getRecogClient()->enableEars(
false);
162 yInfo() <<
" exploreUnknownEntity";
163 string type = command.get(1).toString();
164 string name = command.get(2).toString();
165 yDebug() <<
"exploreUnknownEntity with name = " << name;
168 iCub->opc->checkout();
171 yError() <<
"Could not cast" << name <<
"to bodypart";
172 iCub->say(
"Could not cast " + name +
" to bodypart");
175 yInfo() <<
"Going to tag skin part";
176 reply = exploreTactileEntityWithName(command);
178 yWarning(
"Not sure what to do, name and tactile information already known");
182 yInfo() <<
"Going to tag bodypart (include babbling)";
183 reply = exploreUnknownEntity(command);
186 yInfo() <<
"Going to tag object (include a pointing)";
187 reply = exploreUnknownEntity(command);
189 yInfo() <<
"Going to tag an agent (face recog)";
190 reply = exploreUnknownEntity(command);
192 yWarning() <<
"Type = " << type <<
" is NON valid for exploreUnknownEntity -> doing nothing";
193 reply.addString(
"error");
194 reply.addString(
"Type is NON valid for exploreUnknownEntity");
198 iCub->getRecogClient()->enableEars(
true);
200 else if (command.get(0).asString() ==
"searchingEntity") {
201 yInfo() <<
" searchingEntity";
202 reply = searchingEntity(command);
205 yInfo() << helpMessage;
206 reply.addString(helpMessage);
209 rpcPort.reply(reply);
220 iCub->say(
"Have we met before?",
false);
222 if (iCub->getSAMClient())
224 Bottle SAMreply = iCub->getSAMClient()->askXLabel(
"face");
225 if(SAMreply.get(0).asString() ==
"true")
227 string sNameSAM = SAMreply.get(1).asString();
229 if(sNameSAM !=
"unknown" && sNameSAM !=
"" && sNameSAM !=
"None")
231 Entity* e = iCub->opc->getEntity(sNameTarget);
235 yDebug() <<
"Changing name from " << TARGET->
name() <<
" to " << sNameSAM;
236 iCub->changeName(TARGET,sNameSAM);
237 iCub->opc->commit(TARGET);
239 iCub->say(
"Yes, I remember. Nice to see you, " + sNameSAM);
240 yarp::os::Time::delay(0.2);
243 bOutput.addString(
"success");
244 bOutput.addString(currentEntityType);
246 bOutput.addString(
"nack");
247 yError() <<
"Could not cast" << e->
name() <<
"to Agent";
250 yError() << sNameTarget <<
"is not an entity";
251 bOutput.addString(
"nack");
254 yError() <<
"Classification not recognised";
255 bOutput.addString(
"nack");
258 yError() <<
"askXlabel failed";
259 bOutput.addString(
"nack");
262 yError() <<
"getSAMClient returned false";
263 bOutput.addString(
"nack");
271 if (bInput.size() != 3)
273 yError() <<
" proactiveTagging::exploreTactileEntityWithName | Problem in input size.";
274 bOutput.addString(
"nack");
275 bOutput.addString(
"Problem in input size");
276 iCub->say(
"Error in input size explore tactile with name");
280 string sName = bInput.get(2).toString();
283 iCub->opc->checkout();
284 Bodypart* BPentity =
dynamic_cast<Bodypart*
>(iCub->opc->getEntity(sName,
true));
286 iCub->say(
"Could not cast to bodypart in exploreTactileEntityWithName");
287 yError() <<
"Could not cast to bodypart in exploreTactileEntityWithName";
288 bOutput.addString(
"nack");
292 if (!Network::isConnected(touchDetectorRpc.c_str(), portFromTouchDetector.getName().c_str())) {
293 if (!Network::connect(touchDetectorRpc.c_str(), portFromTouchDetector.getName().c_str())) {
294 yError() <<
" proactiveTagging::exploreTactileEntityWithName | touch detector not connected.";
295 bOutput.addString(
"nack");
296 bOutput.addString(
"touch detector not connected");
297 iCub->say(
"I cannot feel my skin, sorry!");
303 string sAsking =
"I know how to move my " + getBodyPartNameForSpeech(sName) +
", but how does it feel when I touch something? Can you touch my " + getBodyPartNameForSpeech(sName) +
" when I move it, please?";
304 iCub->lookAtPartner();
305 iCub->say(sAsking,
false);
306 portFromTouchDetector.read(
false);
308 yarp::os::Time::delay(1.0);
311 yInfo() <<
"Cast okay : name BP = " << BPentity->
name();
314 yInfo() <<
"Start babbling";
315 double babbling_duration = 3.0;
316 iCub->babbling(joint, babblingArm, babbling_duration);
319 bool gotTouch =
false;
322 while(!gotTouch && timeout<10) {
323 bTactile = portFromTouchDetector.read(
false);
324 if(bTactile !=
nullptr) {
328 yarp::os::Time::delay(0.5);
334 yError() <<
" error in proactiveTagging::exploreTactileEntityWithName | for " << sName <<
" | Touch not detected!" ;
335 bOutput.addString(
"nack");
336 bOutput.addString(
"I did not feel any touch.");
337 iCub->say(
"I did not feel any touch.",
false);
345 bOutput.addString(
"ack");
346 bOutput.addInt(bTactile->get(0).asInt());
347 yDebug() <<
"Assigned" << BPentity->
name() <<
"(id)" << BPentity->
opc_id() <<
"tactile_number to" << bTactile->get(0).asInt();
348 iCub->opc->commit(BPentity);
351 string sThank =
" Thank you, now I know when I am touching object with my " + getBodyPartNameForSpeech(sName);
352 iCub->lookAtPartner();
362 if (bInput.size() != 3)
364 yInfo() <<
" proactiveTagging::exploreEntity | Problem in input size.";
365 bOutput.addString(
"Problem in input size");
369 iCub->opc->checkout();
371 string currentEntityType = bInput.get(1).toString();
372 string sNameTarget = bInput.get(2).toString();
374 yInfo() <<
" EntityType : " << currentEntityType;
379 iCub->lookAtPartner();
382 if(iCub->getSAMClient()) {
383 Bottle recogFromSAM = getNameFromSAM(sNameTarget, currentEntityType);
385 if (recogFromSAM.get(0).toString() ==
"nack" ){
386 sQuestion =
" No, I don't know you. What is your name?";
394 sQuestion =
" Hello, I don't know you. Who are you?";
398 iCub->look(sNameTarget);
399 sQuestion =
" Hum, what is this object?";
402 iCub->lookAtPartner();
403 sQuestion =
" Watch please, I will move a part of my body";
406 yError() <<
" error in proactiveTagging::exploreUnknownEntity | for " << currentEntityType <<
" | Entity Type not managed";
407 bOutput.addString(
"nack");
408 bOutput.addString(
"Entity Type not managed");
412 yInfo() << sQuestion;
413 iCub->say(sQuestion,
false);
416 if (currentEntityType ==
"bodypart") {
417 Bodypart* BPtemp =
dynamic_cast<Bodypart*
>(iCub->opc->getEntity(sNameTarget));
419 bOutput.addString(
"nack");
420 bOutput.addString(
"Could not cast to Bodypart");
421 iCub->say(bOutput.toString());
426 yInfo() <<
"Start babbling";
427 double babbling_duration=3.0;
428 iCub->babbling(joint, babblingArm, babbling_duration);
430 iCub->lookAtPartner();
431 sQuestion =
" How do you call this part of my body?";
433 yInfo() << sQuestion;
434 iCub->say(sQuestion,
false);
437 yDebug() <<
"Going to point to " << sNameTarget;
438 iCub->point(sNameTarget);
442 Bottle bName = recogName(currentEntityType);
445 if (bName.get(0).asString() ==
"error") {
449 sName = bName.get(0).asString();
454 Entity* e = iCub->opc->getEntity(sNameTarget);
457 iCub->lookAtPartner();
459 sReply =
" Nice to meet you " + sName;
462 sReply =
" I get it, this is a " + sName;
465 sReply =
" Nice, I know that I have a " + getBodyPartNameForSpeech(sName);
467 iCub->say(
"I do not know this entity type");
470 iCub->say(sReply,
false);
471 yarp::os::Time::delay(1.5);
475 bOutput.addString(
"success");
476 bOutput.addString(currentEntityType);
478 yInfo() <<
"End of exploreUnknownEntity";
487 if (bInput.size() < 3) {
488 yInfo() <<
" proactiveTagging::searchingEntity | Problem in input size.";
489 bOutput.addString(
"error");
490 bOutput.addString(
"Problem in input size");
494 string sTypeTarget = bInput.get(1).toString();
495 string sNameTarget = bInput.get(2).toString();
496 bool verboseSearch =
true;
497 if(bInput.size() == 4) {
498 verboseSearch = bInput.get(3).asInt() > 0;
500 yInfo() <<
" Entity to find: " << sNameTarget <<
"(Type: " << sTypeTarget <<
", verbosity: " << verboseSearch <<
")";
502 int unknownEntitiesPresent = 0;
505 if (iCub->opc->isConnected()) {
506 iCub->opc->checkout();
507 list<shared_ptr<Entity>> lEntities = iCub->opc->EntitiesCacheCopy();
509 for (
auto& entity : lEntities) {
510 if (entity->name() == sNameTarget && entity->entity_type() == sTypeTarget) {
511 yInfo() <<
" Entity " << sNameTarget <<
" is already known.";
512 bOutput.addString(
"warning");
513 bOutput.addString(
"entity already exists");
518 unknownEntitiesPresent++;
522 if(unknownEntitiesPresent == 0) {
523 yInfo() <<
"No unknown entity is present.";
524 bOutput.addString(
"nack");
525 bOutput.addString(
"No unknown entity is present.");
527 }
else if(unknownEntitiesPresent == 1) {
528 yInfo() <<
"There is only one unknown entity";
529 for (
auto& entity : lEntities) {
532 iCub->lookAtPartner();
533 iCub->say(
"There was only one object which I didn't know");
535 yDebug() <<
"Changed name for" << sNameTarget;
536 iCub->say(
"Now I know the " + sNameTarget,
false);
539 bOutput.addString(
"name changed");
545 yWarning() <<
" in proactiveTagging::searchingEntity | OPC not Connected";
546 bOutput.addString(
"error");
547 bOutput.addString(
"OPC not connected");
556 sSentence =
"I don't known which of these objects is a " + sNameTarget +
". Can you show me the " + sNameTarget;
558 sSentence =
"I don't known my " + sNameTarget +
". Can you please touch my " + sNameTarget;
561 iCub->lookAtPartner();
562 iCub->say(sSentence);
568 bool success = setPasarPointing(
true);
570 yError() <<
"Problem with pasar when setPasarPointing(true)";
571 bOutput.addString(
"error");
572 bOutput.addString(
"Problem with pasar");
577 string sNameBestEntity = getBestEntity(sTypeTarget);
578 if(sNameBestEntity==
"none") {
579 iCub->say(
"you did not point at any object");
580 yError() <<
"you did not point at any object";
581 bOutput.addString(
"error");
582 bOutput.addString(
"no pointing received");
587 Object* TARGET =
dynamic_cast<Object*
>(iCub->opc->getEntity(sNameBestEntity));
589 iCub->look(TARGET->
name());
594 yInfo() <<
" name changed: " << sNameBestEntity <<
" is now " << sNameTarget;
595 bOutput.addString(
"name changed");
596 iCub->say(
"Now I know the " + sNameTarget,
false);
600 bool success = setPasarPointing(
false);
602 yError() <<
"Problem with pasar when setPasarPointing(false)";
603 bOutput.addString(
"error");
604 bOutput.addString(
"Problem with pasar");
int opc_id() const
Return the id of an entity (which has to be unique within the OPC) Typically, modules should employ t...
Represent any entity that can be stored within the OPC.
Grants access to high level motor commands (grasp, touch, look, goto, etc) of the robot as well as it...
double m_present
Is the object present in the scene A value of 1.0 means that the object currently is in the scene A v...
bool configure(yarp::os::ResourceFinder &rf)
yarp::os::Bottle searchingEntity(const yarp::os::Bottle &bInput)
Search for the entity corresponding to a certain name in all the unknown entities.
int m_tactile_number
Tactile number of the represented body part.
Represent any physical entity (including objects and agents) that can be stored within the OPC...
void changeName(std::string sName)
int m_joint_number
Joint number of the represented body part.
Represents a body part of the robot.
yarp::os::Bottle exploreUnknownEntity(const yarp::os::Bottle &bInput)
Explore an unknown entity by asking for the name (response via speech recognition) ...
std::string name() const
Return the name of an entity (which has to be unique within the OPC)
yarp::os::Bottle getNameFromSAM(std::string sNameTarget, std::string currentEntityType)
Send request to SAM to use its face recognition to recognise partner.
bool respond(const yarp::os::Bottle &cmd, yarp::os::Bottle &reply)
yarp::os::Bottle exploreTactileEntityWithName(yarp::os::Bottle bInput)
Explore an unknown tactile entity (e.g.
std::string entity_type() const
Return the specific type of an entity.