23 #include <yarp/math/Rand.h>
24 #include <yarp/math/Math.h>
26 #include "iCub/module.h"
29 using namespace yarp::os;
30 using namespace yarp::sig;
31 using namespace yarp::math;
33 #define RET_INVALID -1
35 #define CMD_TRAIN yarp::os::createVocab('t','r','a','i')
36 #define CMD_EXECUTE yarp::os::createVocab('e','x','e','c')
37 #define CMD_TOOLEXTEND yarp::os::createVocab('e','x','t','d')
40 bool Manager::configure(ResourceFinder &rf)
42 name=rf.find(
"name").asString();
43 camera=rf.find(
"camera").asString();
44 if ((camera!=
"left") && (camera!=
"right"))
47 hand=rf.find(
"hand").asString();
48 if ((hand!=
"left") && (hand!=
"right"))
52 particleFilter.open(
"/"+name+
"/particle:i");
53 pointedLoc.open(
"/"+name+
"/point:i");
54 blobExtractor.open(
"/"+name+
"/blobs:i");
57 segmentPoint.open(
"/"+name+
"/segmentTarget:o");
58 iolStateMachine.open(
"/"+name+
"/iolState:o");
61 rpcMIL.open(
"/"+name+
"/mil:rpc");
62 rpcHuman.open(
"/"+name+
"/human:rpc");
63 rpcMotorAre.open(
"/"+name+
"/are:rpc");
64 rpcMotorKarma.open(
"/"+name+
"/karma:rpc");
65 rpcKarmaLearn.open(
"/"+name+
"/learn:rpc");
66 rpcReconstruct.open(
"/"+name+
"/reconstruct:rpc");
67 rpcGraspEstimate.open(
"/"+name+
"/graspestimate:rpc");
68 rpcOPC.open(
"/"+name+
"/opc:rpc");
73 randActions[1] = 30.0;
74 randActions[2] = 150.0;
75 randActions[3] = 180.0;
76 randActions[4] = 225.0;
78 randActions[5] = 315.0;
100 bool Manager::interruptModule()
102 segmentPoint.interrupt();
103 blobExtractor.interrupt();
104 rpcHuman.interrupt();
105 rpcMotorAre.interrupt();
106 rpcMotorKarma.interrupt();
107 particleFilter.interrupt();
108 pointedLoc.interrupt();
109 iolStateMachine.interrupt();
111 rpcKarmaLearn.interrupt();
112 rpcReconstruct.interrupt();
113 rpcGraspEstimate.interrupt();
119 bool Manager::close()
121 segmentPoint.close();
122 blobExtractor.close();
125 rpcMotorKarma.close();
126 particleFilter.close();
128 iolStateMachine.close();
130 rpcKarmaLearn.close();
131 rpcReconstruct.close();
132 rpcGraspEstimate.close();
138 int Manager::processHumanCmd(
const Bottle &cmd, Bottle &b)
140 int ret=Vocab::encode(cmd.get(0).asString());
144 if (cmd.get(1).isList())
145 b=*cmd.get(1).asList();
152 bool Manager::updateModule()
162 fprintf(stdout,
"waiting for connection from iolStateMachineHandler\n");
163 if (iolStateMachine.getOutputCount() > 0)
165 fprintf(stdout,
"sending home \n");
166 Bottle cmdIol, replyIol;
167 cmdIol.addString(
"home");
168 iolStateMachine.write(cmdIol,replyIol);
169 fprintf(stdout,
"%s\n", replyIol.toString().c_str());
170 fprintf(stdout,
"stopping attention...\n");
173 cmdIol.addString(
"attention");
174 cmdIol.addString(
"stop");
175 iolStateMachine.write(cmdIol,replyIol);
176 fprintf(stdout,
"%s\n", replyIol.toString().c_str());
178 fprintf(stdout,
"have successfully initialize it all\n");
180 executeSpeech (
"ok I am ready");
184 Bottle cmd, val, reply;
185 rpcHuman.read(cmd,
true);
187 int rxCmd=processHumanCmd(cmd,val);
188 if (rxCmd==Vocab::encode(
"train"))
190 obj=cmd.get(1).asString();
194 fprintf(stdout,
"with user data\n");
195 userTheta=cmd.get(2).asDouble();
199 fprintf(stdout,
"no user data\n");
205 reply.addString(
"ack");
206 rpcHuman.reply(reply);
209 if (rxCmd==Vocab::encode(
"test"))
211 obj=cmd.get(1).asString();
216 reply.addString(
"ack");
217 rpcHuman.reply(reply);
220 if (rxCmd==Vocab::encode(
"handle"))
223 reply.addString(
"ack");
224 rpcHuman.reply(reply);
227 if (rxCmd==Vocab::encode(
"extend"))
231 fprintf(stdout,
"Will now use user selected arm and camera \n");
232 hand = cmd.get(1).asString();
233 camera = cmd.get(2).asString();
237 fprintf(stdout,
"Will now use default arm and cam \n");
241 lastTool = executeToolLearning();
242 reply.addString(
"ack");
243 rpcHuman.reply(reply);
245 fprintf(stdout,
"GOT TOOL at %s, \n",lastTool.toString().c_str());
248 if (rxCmd==Vocab::encode(
"tooltip"))
252 toSend.addDouble(lastTool.get(0).asDouble());
253 toSend.addDouble(lastTool.get(1).asDouble());
254 toSend.addDouble(lastTool.get(2).asDouble());
256 rpcHuman.reply(toSend);
258 fprintf(stdout,
"Replied with %s, \n",toSend.toString().c_str());
261 if (rxCmd==Vocab::encode(
"grasp"))
263 obj=cmd.get(1).asString();
264 if ( executePCLGrasp(obj) )
265 reply.addString(
"ack");
267 reply.addString(
"nack");
269 if (rxCmd==Vocab::encode(
"blobLoc"))
272 blobLoc = findBlobLoc();
273 if (blobLoc.size()<1)
275 reply.addString(
"nack");
276 rpcHuman.reply(reply);
279 rpcHuman.reply(blobLoc);
282 if (rxCmd==Vocab::encode(
"objRecog"))
285 obj=cmd.get(1).asString();
286 blobLoc = executeBlobRecog(obj);
288 if (blobLoc.size()<1)
290 reply.addString(
"nack");
291 rpcHuman.reply(reply);
294 rpcHuman.reply(blobLoc);
296 if (rxCmd==Vocab::encode(
"close"))
298 int arm=cmd.get(1).asInt();
299 executeCloseHand(arm);
300 reply.addString(
"ack");
301 rpcHuman.reply(reply);
303 if (rxCmd==Vocab::encode(
"give"))
305 int arm=cmd.get(1).asInt();
306 executeGiveAction(arm);
307 reply.addString(
"ack");
308 rpcHuman.reply(reply);
310 if (rxCmd==Vocab::encode(
"class"))
312 reply = classifyThem();
313 rpcHuman.reply(reply);
323 Bottle Manager::classifyThem()
327 cmdIol.clear(), replyIol.clear();
328 cmdIol.addString(
"class");
329 iolStateMachine.write(cmdIol,replyIol);
330 fprintf(stdout,
"%s\n", replyIol.toString().c_str());
335 void Manager::segmentAndTrack(
int x,
int y )
342 toSegment.addInt(80);
343 toSegment.addInt(80);
344 fprintf(stdout,
"segmenting cmd is %s\n",toSegment.toString().c_str());
345 segmentPoint.write(toSegment);
347 Bottle cmdAre, replyAre;
348 cmdAre.addString(
"track");
349 cmdAre.addString(
"track");
350 cmdAre.addString(
"no_sacc");
351 rpcMotorAre.write(cmdAre,replyAre);
352 fprintf(stdout,
"tracking started%s:\n",replyAre.toString().c_str());
355 void Manager::goHomeArmsHead()
357 Bottle cmdAre, replyAre;
360 cmdAre.addString(
"home");
361 cmdAre.addString(
"arms");
362 cmdAre.addString(
"head");
363 rpcMotorAre.write(cmdAre,replyAre);
364 fprintf(stdout,
"gone home %s:\n",replyAre.toString().c_str());
367 void Manager::goHome()
369 Bottle cmdAre, replyAre;
372 cmdAre.addString(
"home");
373 cmdAre.addString(
"all");
374 rpcMotorAre.write(cmdAre,replyAre);
375 fprintf(stdout,
"gone home %s:\n",replyAre.toString().c_str());
378 void Manager::takeMotionARE()
381 fprintf(stdout,
"Will now start take motion proceedure:\n");
382 Bottle cmdAre,replyAre;
385 cmdAre.addString(
"take");
386 cmdAre.addString(
"motion");
387 fprintf(stdout,
"%s\n",cmdAre.toString().c_str());
388 rpcMotorAre.write(cmdAre, replyAre);
389 fprintf(stdout,
"action is %s:\n",replyAre.toString().c_str());
393 Bottle Manager::executeToolLearning()
396 fprintf(stdout,
"Will now start the tool learn proceedure:\n");
397 Bottle karmaMotor,KarmaReply;
400 karmaMotor.addString(
"find");
401 karmaMotor.addString(hand);
402 karmaMotor.addString(camera);
403 fprintf(stdout,
"%s\n",karmaMotor.toString().c_str());
404 rpcMotorKarma.write(karmaMotor, KarmaReply);
405 fprintf(stdout,
"action is %s:\n",KarmaReply.toString().c_str());
409 toReturn.addDouble( KarmaReply.get(1).asDouble() );
410 toReturn.addDouble( KarmaReply.get(2).asDouble() );
411 toReturn.addDouble( KarmaReply.get(3).asDouble() );
415 Bottle Manager::executeBlobRecog(
const string &objName)
419 fprintf(stdout,
"\n\n\nexecuteBlobRecog****************************************************************************\n\n" );
423 bool invalid =
false;
430 fprintf (stdout,
"INVALID BLOB SIZE\n");
437 Bottle classResults = classifyThem();
439 for (
int x=0; x < blobs.size(); x++)
441 pointLocation = getBlobCOG(blobs,x);
442 fprintf (stdout,
"point is %d %d \n", pointLocation.x, pointLocation.y);
444 mutexResources.lock();
445 closestBlob=findClosestBlob(blobs,pointLocation);
446 mutexResources.unlock();
449 cog.x = closestBlob.get(0).asInt();
450 cog.y = closestBlob.get(1).asInt();
453 index = closestBlob.get(2).asInt();
455 fprintf(stdout,
"closest blob is x:%d y:%d with index %d\n\n",cog.x, cog.y, index);
467 fprintf(stdout,
"The type is: %s and object name requested is: %s\n\n", classResults.get(x).asString().c_str(), objName.c_str() );
469 if (classResults.get(x).asString() == objName)
472 pointLocation = getBlobCOG(blobs,index);
473 fprintf (stdout,
"point is %d %d \n", pointLocation.x, pointLocation.y);
474 fprintf(stdout,
"I have found the requested object %s, at: %d %d\n",objName.c_str(), pointLocation.x, pointLocation.y );
475 loc.addInt (pointLocation.x);
476 loc.addInt (pointLocation.y);
485 int Manager::executeToolOnLoc()
487 fprintf(stdout,
"\n\n\n****************************************************************************\n\n" );
498 pointLocation = getBlobCOG(blobs,0);
499 fprintf (stdout,
"point is %d %d \n", pointLocation.x, pointLocation.y);
504 fprintf (stdout,
"I see more than two blobs\n");
510 mutexResources.lock();
511 closestBlob=findClosestBlob(blobs,pointLocation);
512 mutexResources.unlock();
515 cog.x = closestBlob.get(0).asInt();
516 cog.y = closestBlob.get(1).asInt();
521 segmentAndTrack(cog.x, cog.y);
524 index = closestBlob.get(2).asInt();
527 fprintf(stdout,
"closest blob is x:%d y:%d with index %d\n\n", cog.x, cog.y, index);
532 Bottle Manager::findBlobLoc()
536 fprintf(stdout,
"\n\n\nfindBlobLoc****************************************************************************\n\n" );
539 bool invalid =
false;
545 fprintf (stdout,
"INVALID BLOB SIZE\n");
551 if (blobs.size()<2 && !invalid)
553 pointLocation = getBlobCOG(blobs,0);
554 fprintf (stdout,
"point is %d %d \n", pointLocation.x, pointLocation.y);
559 fprintf (stdout,
"I either see more than two blobs or nothing at all\n");
565 mutexResources.lock();
566 closestBlob=findClosestBlob(blobs,pointLocation);
567 mutexResources.unlock();
570 cog.x = closestBlob.get(0).asInt();
571 cog.y = closestBlob.get(1).asInt();
579 index = closestBlob.get(2).asInt();
581 fprintf(stdout,
"closest blob is x:%d y:%d with index %d\n\n", cog.x, cog.y, index);
584 if (get3DPosition(cog,initPos))
586 loc.addDouble(initPos[0]);
587 loc.addDouble(initPos[1]);
588 loc.addDouble(initPos[2]);
589 fprintf(stdout,
"Got a 3D point at %lf %lf %lf \n", initPos[0], initPos[1],initPos[2]);
596 bool Manager::executePCLGrasp(
const string &objName )
598 fprintf(stdout,
"\n\n\nexecutePCLGrasp****************************************************************************\n\n" );
599 executeSpeech (
"ok, will now try to grasp the "+ objName);
602 bool invalid =
false;
603 bool isGrasped =
false;
608 fprintf(stdout,
"releasing head\n");
610 Bottle cmdAre, replyAre;
613 cmdAre.addString(
"release");
614 rpcMotorAre.write(cmdAre,replyAre);
615 fprintf(stdout,
"the reply is: %s \n",replyAre.toString().c_str());
630 result = executeBlobRecog(objName);
631 locObj.x = result.get(0).asInt();
632 locObj.y = result.get(1).asInt();
634 fprintf (stdout,
"point is %d %d \n", locObj.x, locObj.y);
639 fprintf (stdout,
"I have an issue spotting the %s\n",objName.c_str());
640 executeSpeech (
"I can't seem to find the "+ objName);
646 if (get3DPosition(locObj,objectPos))
648 fprintf(stdout,
"Got a 3D point at %lf %lf %lf \n", objectPos[0], objectPos[1],objectPos[2]);
651 if (objectPos[0] < -0.47 )
653 executeSpeech (
"I cannot safely reach for the "+ objName +
", will try to find a solution");
654 fprintf(stdout,
"I cannot reach for this object, will find a solution\n");
655 Bottle cmdHome, cmdReply;
658 cmdHome.addString(
"home");
659 cmdHome.addString(
"all");
660 rpcMotorAre.write(cmdHome,cmdReply);
662 executeToolSearchOnLoc( objName );
667 fprintf(stdout,
"Will now send the blob to graphsegmentation:\n");
668 Bottle segCmd, segReply;
671 segCmd.addInt(locObj.x);
672 segCmd.addInt(locObj.y);
673 fprintf(stdout,
"the cmd is: %s \n",segCmd.toString().c_str());
674 rpcReconstruct.write(segCmd, segReply);
675 fprintf(stdout,
"the reply is: %s \n",segReply.toString().c_str());
677 fprintf(stdout,
"Will now ask for 3Dreconstruction:\n");
680 segCmd.addString(
"3Drec");
681 segCmd.addString(objName);
683 fprintf(stdout,
"the cmd is: %s \n", segCmd.toString().c_str());
684 rpcReconstruct.write(segCmd, segReply);
685 fprintf(stdout,
"the reply is: %s \n",segReply.toString().c_str());
689 latchTimer=Time::now();
695 cmd.addString(
"isGrasped");
696 fprintf(stdout,
"the cmd is: %s \n", cmd.toString().c_str());
697 rpcGraspEstimate.write(cmd,reply);
698 string rep=reply.toString().c_str();
699 rep.erase (rep.begin());
700 rep.erase (rep.begin()+4);
706 if ((Time::now()-latchTimer)>idleTmo)
708 fprintf(stdout,
"--- Timeout elapsed ---\n");
715 fprintf(stdout,
"Grasped finished\n");
717 fprintf(stdout,
"Now Releasing...\n");
721 cmd.addString(
"release");
722 fprintf(stdout,
"the cmd is: %s \n", cmd.toString().c_str());
723 rpcGraspEstimate.write(cmd,reply);
724 fprintf(stdout,
"the reply is: %s \n",reply.toString().c_str());
732 cmd.addString(
"home");
733 cmd.addString(
"all");
734 rpcMotorAre.write(cmd,rep);
736 Bottle cmdAre, replyAre;
739 cmdAre.addString(
"release");
740 rpcMotorAre.write(cmdAre,replyAre);
741 fprintf(stdout,
"the reply is: %s \n",replyAre.toString().c_str());
743 executeSpeech (
"sorry I could not figure out how to do this");
744 fprintf(stdout,
"did not seem to be able to grasp correctly or has been aborted\n");
748 fprintf(stdout,
"Finished the grasping sequence \n");
753 cmd.addString(
"home");
754 cmd.addString(
"all");
755 rpcMotorAre.write(cmd,rep);
763 int Manager::executeToolSearchOnLoc(
const string &objName )
765 fprintf(stdout,
"\n\n\nexecuteToolSearchOnLoc****************************************************************************\n\n" );
779 result = executeBlobRecog(objName);
780 objLoc.x = result.get(0).asInt();
781 objLoc.y = result.get(1).asInt();
783 blobsDetails =
new blobsData[blobs.size()];
785 Bottle memCmd, memRep;
786 memCmd.clear(); memRep.clear();
787 memCmd.addVocab(Vocab::encode(
"ask"));
788 Bottle &tmp=memCmd.addList();
789 tmp.addString (
"all");
790 rpcOPC.write(memCmd,memRep);
792 fprintf(stdout,
"THE REPLY IS %s \n",memRep.toString().c_str());
796 if (memRep.get(0).asVocab()==Vocab::encode(
"ack"))
798 if (Bottle *idField = memRep.get(1).asList())
800 if (Bottle *idVals = idField->get(1).asList())
802 sizeInMem = idVals->size();
803 sizeInMem = sizeInMem - 1;
804 fprintf (stdout,
"******************************************size is %d \n", sizeInMem);
812 if (blobs.size() == sizeInMem)
815 executeSpeech (
"sorry Something is wrong with the objects");
818 Bottle classResults = classifyThem();
823 for (
int x=0; x < blobs.size(); x++)
825 pointLocation = getBlobCOG(blobs,x);
826 fprintf (stdout,
"point is %d %d \n", pointLocation.x, pointLocation.y);
827 fprintf (stdout,
"object is %d %d \n", objLoc.x, objLoc.y);
830 mutexResources.lock();
831 closestBlob=findClosestBlob(blobs,pointLocation);
832 mutexResources.unlock();
834 fprintf(stdout,
"checkin if objDiff x %d objDiff y %d \n",abs( objLoc.x - pointLocation.x), abs( objLoc.y - pointLocation.y));
836 if ( abs( objLoc.x - pointLocation.x) < 5 && abs( objLoc.y - pointLocation.y) < 5)
839 fprintf(stdout,
"I have found the object of interest and not considering it.\n");
843 fprintf(stdout,
"\n\n\n\nI AM IN SETTING UP BLOBS with blob size = %d and x= %d\n\n\n\n",blobs.size(), x);
849 blobsDetails[x].posistion.x = (int) closestBlob.get(0).asDouble();
850 blobsDetails[x].posistion.y = (int) closestBlob.get(1).asDouble();
851 blobsDetails[x].index = closestBlob.get(2).asInt();
852 blobsDetails[x].lenght = 0.0;
853 blobsDetails[x].lenght = getBlobLenght(blobs, x);
854 blobsDetails[x].vdrawError = 0.0;
865 blobsDetails[x].name = classResults.get(x).asString();
867 fprintf(stdout,
"SO: name is: %s x is %d u is %d index is %d\n",blobsDetails[x].name.c_str(),blobsDetails[x].posistion.x, blobsDetails[x].posistion.y, blobsDetails[x].index);
881 fprintf(stdout,
"\n\n\n\nI AM OUT SETTING UP BLOBS \n\n\n\n");
882 Bottle homeAfterLookCmd, homeAfterLookReply;
883 homeAfterLookCmd.clear();
884 homeAfterLookReply.clear();
885 homeAfterLookCmd.addString(
"home");
886 homeAfterLookCmd.addString(
"head");
887 rpcMotorAre.write(homeAfterLookCmd,homeAfterLookReply);
889 int toolLenght = 1000;
892 fprintf(stdout,
"\n\n");
893 for (
int x=0; x < blobs.size(); x++)
897 fprintf(stdout,
"The lenght is %lf, with index %d\n",blobsDetails[x].lenght, x);
899 if (blobsDetails[x].lenght < toolLenght )
901 toolLenght=(int)blobsDetails[x].lenght;
906 for (
int x=0; x < blobs.size(); x++)
908 if ( x!=objIndex && x!=smallIndex)
914 fprintf(stdout,
"The small tool is the blob index %d \n",smallIndex);
915 blobsDetails[smallIndex].name =
"small";
917 fprintf(stdout,
"The big tool is the blob index %d \n", bigIndex);
918 blobsDetails[bigIndex].name =
"big";
920 fprintf(stdout,
"\n\n");
925 small = executeKarmaOptimize(toolSmall, blobsDetails[smallIndex].name);
926 blobsDetails[smallIndex].bestAngle = small.get(1).asDouble();
927 blobsDetails[smallIndex].bestDistance = small.get(2).asDouble();
930 big = executeKarmaOptimize(toolBig, blobsDetails[bigIndex].name);
931 blobsDetails[bigIndex].bestAngle = big.get(1).asDouble();
932 blobsDetails[bigIndex].bestDistance = big.get(2).asDouble();
934 fprintf(stdout,
"\n\n");
937 executeToolAttach(toolSmall);
938 double virtualtmp = 0.01;
941 while (virtualtmp > 0.0 && virtualtmp < 0.08 )
943 virtualtmp = executeVirtualDraw(blobsDetails[smallIndex]);
944 if (virtualtmp > 0.1)
945 blobsDetails[smallIndex].bestDistance -= 0.005;
946 else if (virtualtmp > 1.0)
947 blobsDetails[smallIndex].bestDistance -= 0.1;
949 blobsDetails[smallIndex].bestDistance += 0.005;
951 fprintf(stdout,
"the reply is: %lf \n",virtualtmp);
955 blobsDetails[smallIndex].vdrawError = executeVirtualDraw(blobsDetails[smallIndex]);
956 fprintf (stdout,
"\n\nTHE BEST ANGLE IS %lf WITH DISTANCE %lf with confidence %lf\n\n",blobsDetails[smallIndex].bestAngle, blobsDetails[smallIndex].bestDistance, blobsDetails[smallIndex].vdrawError );
959 executeToolAttach(toolBig);
962 while (virtualtmp > 0.0 && virtualtmp < 0.08 )
964 virtualtmp = executeVirtualDraw(blobsDetails[bigIndex]);
965 if (virtualtmp > 0.1)
966 blobsDetails[bigIndex].bestDistance -= 0.005;
967 else if (virtualtmp > 1.0)
968 blobsDetails[bigIndex].bestDistance -= 0.1;
970 blobsDetails[bigIndex].bestDistance += 0.005;
972 fprintf(stdout,
"the reply is: %lf \n",virtualtmp);
976 blobsDetails[bigIndex].vdrawError = executeVirtualDraw(blobsDetails[bigIndex]);
977 fprintf (stdout,
"\n\nTHE BEST ANGLE IS %lf WITH DISTANCE %lf and confidence %lf\n\n",blobsDetails[bigIndex].bestAngle, blobsDetails[bigIndex].bestDistance, blobsDetails[bigIndex].vdrawError );
980 Bottle cmdHome, cmdReply;
983 cmdHome.addString(
"home");
984 cmdHome.addString(
"head");
985 rpcMotorAre.write(cmdHome,cmdReply);
988 double bestChoice = 1000.0;
990 double bestDistance = -1;
991 for (
int x=0; x < blobs.size(); x++)
993 if (blobsDetails[x].vdrawError < bestChoice && blobsDetails[x].bestDistance > bestDistance && x != objIndex)
996 bestDistance = blobsDetails[x].bestDistance;
1003 string tmpObjName = blobsDetails[bestIndex].name;
1004 executeSpeech (
"can you give me the " + tmpObjName +
"please?");
1012 blobsDetails[bestIndex].bestDistance = fabs(objectPos[0]) - fabs( max );
1014 fprintf(stdout,
"*********************************************************************************\n");
1015 fprintf(stdout,
"THE MAX DISTANCE I WILL DO IS %lf\n,",blobsDetails[bestIndex].bestDistance);
1016 fprintf(stdout,
"*********************************************************************************\n");
1020 Bottle cmdAre, replyAre;
1023 cmdAre.addString(
"point");
1024 Bottle &tmp=cmdAre.addList();
1025 tmp.addInt (blobsDetails[bestIndex].posistion.x);
1026 tmp.addInt (blobsDetails[bestIndex].posistion.y);
1028 bool sendAction =
true;
1029 if (blobsDetails[bestIndex].posistion.x > 0 && blobsDetails[bestIndex].posistion.x < 160 )
1032 cmdAre.addString(
"left");
1034 else if (blobsDetails[bestIndex].posistion.x > 160 && blobsDetails[bestIndex].posistion.x < 320 )
1037 cmdAre.addString(
"right");
1041 executeSpeech (
"oh my...I seemed to got confused...sorry");
1042 fprintf(stdout,
"something is wrong with the action:\n");
1046 fprintf(stdout,
"the cmd is: %s \n",cmdAre.toString().c_str());
1049 rpcMotorAre.write(cmdAre,replyAre);
1050 fprintf(stdout,
"the reply is: %s \n",replyAre.toString().c_str());
1056 executeGiveAction(whichArm);
1061 executeCloseHand(whichArm);
1065 Bottle homeCmd, homeReply;
1068 homeCmd.addString(
"home");
1069 homeCmd.addString(
"arms");
1070 homeCmd.addString(
"head");
1071 rpcMotorAre.write(homeCmd,homeReply);
1073 if (tmpObjName ==
"small")
1074 executeToolAttach(toolSmall);
1076 executeToolAttach(toolBig);
1079 executeToolDrawNear(blobsDetails[bestIndex]);
1081 Bottle homeToolcmd, homeToolrep;
1082 homeToolcmd.clear();
1083 homeToolrep.clear();
1084 homeToolcmd.addString(
"home");
1085 homeToolcmd.addString(
"arms");
1086 homeToolcmd.addString(
"head");
1087 rpcMotorAre.write(homeToolcmd,homeToolrep);
1089 executeSpeech (
"Ok, thank you");
1092 executeDropAway(whichArm);
1095 executePCLGrasp( objName );
1101 bool Manager::executeDropAway(
int ARM)
1103 Bottle cmdAre, replyAre;
1106 cmdAre.addString(
"drop");
1107 cmdAre.addString(
"away");
1109 cmdAre.addString(
"left");
1111 cmdAre.addString(
"right");
1113 rpcMotorAre.write(cmdAre,replyAre);
1115 printf(
"done deploying\n");
1119 int Manager::executeToolAttach(
const Vector &tool)
1121 fprintf(stdout,
"Will now send to karmaMotor:\n");
1122 Bottle karmaMotor,KarmaReply;
1123 karmaMotor.addString(
"tool");
1124 karmaMotor.addString(
"remove");
1125 rpcMotorKarma.write(karmaMotor, KarmaReply);
1127 karmaMotor.clear();KarmaReply.clear();
1128 karmaMotor.addString(
"tool");
1129 karmaMotor.addString(
"attach");
1130 karmaMotor.addString(
"right");
1131 karmaMotor.addDouble(tool[0]);
1132 karmaMotor.addDouble(tool[1]);
1133 karmaMotor.addDouble(tool[2]);
1134 fprintf(stdout,
"%s\n",karmaMotor.toString().c_str());
1135 rpcMotorKarma.write(karmaMotor, KarmaReply);
1136 fprintf(stdout,
"reply is %s:\n",KarmaReply.toString().c_str());
1140 Bottle Manager::executeKarmaOptimize(
const Vector &tool,
const string &name)
1142 Bottle cmdLearn, cmdReply;
1145 cmdLearn.addString(
"optimize");
1146 cmdLearn.addString(name);
1147 fprintf(stdout,
"the cmd is: %s \n",cmdLearn.toString().c_str());
1148 rpcKarmaLearn.write(cmdLearn, cmdReply);
1149 fprintf(stdout,
"the reply is: %s \n",cmdReply.toString().c_str());
1151 double optAng = cmdReply.get(1).asDouble();
1152 double optDisp = cmdReply.get(2).asDouble();
1154 fprintf(stdout,
"the optimum angle is %lf with optimum disp %lf\n ",optAng , optDisp);
1158 double Manager::executeToolDrawNear(blobsData &blobsDetails)
1160 double result = 0.0;
1162 fprintf(stdout,
"Will now send to karmaMotor using %s as a tool:\n", blobsDetails.name.c_str());
1163 Bottle karmaMotor,KarmaReply;
1164 karmaMotor.addString(
"draw");
1165 karmaMotor.addDouble(objectPos[0]);
1166 karmaMotor.addDouble(objectPos[1]);
1167 karmaMotor.addDouble(objectPos[2]);
1168 karmaMotor.addDouble(blobsDetails.bestAngle);
1169 karmaMotor.addDouble(0.12);
1170 karmaMotor.addDouble(blobsDetails.bestDistance);
1171 fprintf(stdout,
"%s\n",karmaMotor.toString().c_str());
1172 rpcMotorKarma.write(karmaMotor, KarmaReply);
1173 fprintf(stdout,
"vdraw is %s:\n",KarmaReply.toString().c_str());
1174 result = KarmaReply.get(1).asDouble();
1178 double Manager::executeVirtualDraw(blobsData &blobsDetails)
1180 double result = 0.0;
1182 fprintf(stdout,
"Will now send to karmaMotor:\n");
1183 Bottle karmaMotor,KarmaReply;
1184 karmaMotor.addString(
"vdraw");
1185 karmaMotor.addDouble(objectPos[0]);
1186 karmaMotor.addDouble(objectPos[1]);
1187 karmaMotor.addDouble(objectPos[2]);
1188 karmaMotor.addDouble(blobsDetails.bestAngle);
1189 karmaMotor.addDouble(0.1);
1190 karmaMotor.addDouble(blobsDetails.bestDistance);
1193 fprintf(stdout,
"%s\n",karmaMotor.toString().c_str());
1194 rpcMotorKarma.write(karmaMotor, KarmaReply);
1195 fprintf(stdout,
"vdraw is %s:\n",KarmaReply.toString().c_str());
1196 result = KarmaReply.get(1).asDouble();
1200 bool Manager::executeGiveAction(
int ARM)
1203 Bottle cmdAre, replyAre;
1206 cmdAre.addString(
"tato");
1208 cmdAre.addString(
"left");
1210 cmdAre.addString(
"right");
1212 rpcMotorAre.write(cmdAre,replyAre);
1217 bool Manager::executeCloseHand (
int ARM)
1220 Bottle cmdAre, replyAre;
1223 cmdAre.addString(
"close");
1225 cmdAre.addString(
"left");
1227 cmdAre.addString(
"right");
1229 rpcMotorAre.write(cmdAre,replyAre);
1231 printf(
"done closing\n");
1236 bool Manager::executeSpeech (
const string &speech)
1240 cmdIol.clear(), replyIol.clear();
1241 cmdIol.addString(
"say");
1242 cmdIol.addString(speech);
1243 iolStateMachine.write(cmdIol,replyIol);
1244 fprintf(stdout,
"%s\n", replyIol.toString().c_str());
1249 int Manager::executeOnLoc(
bool shouldTrain)
1251 fprintf(stdout,
"\n\n\n****************************************************************************\n\n" );
1258 if (blobs.size()==0)
1263 pointLocation = getBlobCOG(blobs,0);
1264 fprintf (stdout,
"point is %d %d \n", pointLocation.x, pointLocation.y);
1269 fprintf (stdout,
"I see more than two blobs\n");
1275 mutexResources.lock();
1276 closestBlob=findClosestBlob(blobs,pointLocation);
1277 mutexResources.unlock();
1280 cog.x = closestBlob.get(0).asInt();
1281 cog.y = closestBlob.get(1).asInt();
1286 segmentAndTrack(cog.x, cog.y);
1289 index = closestBlob.get(2).asInt();
1291 fprintf(stdout,
"closest blob is x:%d y:%d with index %d\n\n",cog.x, cog.y, index);
1295 mil=classify(blobs, index);
1298 type=getType(&mil, index);
1300 double actionOrient = 0.0;
1301 int guessAction=(int)Rand::scalar(0,randActions.size());
1302 fprintf(stdout,
"the guess action is %d chosen from size %d\n", guessAction, (
int)randActions.size());
1304 double orient = 0.0;
1305 orient = closestBlob.get(3).asDouble();
1309 fprintf(stdout,
"SHOULD BE IN TEST MODE!\n");
1310 Bottle cmdLearn, cmdReply;
1313 cmdLearn.addString(
"optimize");
1314 cmdLearn.addString(obj);
1320 fprintf(stdout,
"the cmd is: %s \n",cmdLearn.toString().c_str());
1321 rpcKarmaLearn.write(cmdLearn, cmdReply);
1322 fprintf(stdout,
"the reply is: %s \n",cmdReply.toString().c_str());
1324 double optAng = cmdReply.get(1).asDouble();
1325 double optDisp = cmdReply.get(2).asDouble();
1327 fprintf(stdout,
"the optimum angle is %lf with optimum disp %lf\n ",optAng , optDisp);
1331 actionOrient = optAng + orient;
1333 if (actionOrient > 360.0)
1334 actionOrient -= 360.0;
1336 if (actionOrient > 45.0 && actionOrient < 135.0)
1337 actionOrient += 180.0;
1339 fprintf(stdout,
"\n\nthe FINAL angle is %lf \n\n",actionOrient);
1343 fprintf(stdout,
"SHOULD BE IN TRAIN MODE!\n");
1344 if (userTheta >= 0.0)
1345 actionOrient = userTheta;
1347 actionOrient = Rand::scalar(-210.0,30.0);
1352 if (get3DPosition(cog,initPos))
1355 double offset = 0.0;
1356 results = getOffset(closestBlob, actionOrient, initPos);
1357 offset = results.get(0).asDouble();
1358 finalOrient = results.get(1).asDouble();
1360 fprintf(stdout,
"Will now send to karmaMotor:\n");
1361 Bottle karmaMotor,KarmaReply;
1362 karmaMotor.addString(
"push");
1363 karmaMotor.addDouble(initPos[0]);
1364 karmaMotor.addDouble(initPos[1]);
1365 karmaMotor.addDouble(initPos[2] + 0.05);
1366 karmaMotor.addDouble(actionOrient);
1367 karmaMotor.addDouble( offset );
1369 fprintf(stdout,
"%s\n",karmaMotor.toString().c_str());
1370 rpcMotorKarma.write(karmaMotor, KarmaReply);
1371 fprintf(stdout,
"action is %s:\n",KarmaReply.toString().c_str());
1373 cv::Point finalPoint;
1374 if (particleFilter.getTraker(finalPoint))
1376 while(finalPoint.x <1 && finalPoint.x >320 && finalPoint.y <1 && finalPoint.y >240)
1378 fprintf(stdout,
"\n\n\ndid not get a correct final point from particle filter..retrying...\n\n\n");
1379 particleFilter.getTraker(finalPoint);
1382 if (get3DPosition(finalPoint,finalPos))
1384 fprintf(stdout,
"The Final 3Dpos is %lf %lf %lf \n",finalPos[0], finalPos[1], finalPos[2]);
1389 disp = norm(finalPos - initPos);
1393 Bottle cmdLearn, replyLearn;
1396 cmdLearn.addString(
"train");
1397 cmdLearn.addString(obj);
1399 double wrappedAng = 0.0;
1400 wrappedAng = wrapAng ( finalOrient );
1402 cmdLearn.addDouble( wrappedAng );
1403 cmdLearn.addDouble( disp );
1405 fprintf(stdout,
"the cmd is: %s \n",cmdLearn.toString().c_str());
1406 rpcKarmaLearn.write(cmdLearn, replyLearn);
1407 fprintf(stdout,
"the reply is: %s \n",replyLearn.toString().c_str());
1417 double Manager::getBlobLenght(
const Bottle &blobs,
const int i)
1421 if ((i>=0) && (i<blobs.size()))
1424 Bottle *item=blobs.get(i).asList();
1428 tl.x=(int)item->get(0).asDouble();
1429 tl.y=(int)item->get(1).asDouble();
1430 br.x=(int)item->get(2).asDouble();
1431 br.y=(int)item->get(3).asDouble();
1434 int dx = abs(tl.x - br.x);
1435 int dy = abs(tl.y - br.y);
1437 fprintf(stdout,
"%d %d \n", dx, dy);
1439 dist = sqrt ( (
double)((dx*dx) + (dy*dy)) );
1446 double Manager::wrapAng (
const double ang )
1448 if ((ang<0.0) || (ang>=360.0))
1450 double theta=(M_PI/180.0)*ang;
1451 theta=(180.0/M_PI)*atan2(sin(theta),cos(theta));
1460 Bottle Manager::getOffset( Bottle &closestBlob,
double actionOrient, Vector &initPos )
1462 double orient = 0.0;
1463 orient = closestBlob.get(3).asDouble();
1468 cog.x = closestBlob.get(0).asInt();
1469 cog.y = closestBlob.get(1).asInt();
1471 axe1 = closestBlob.get(4).asInt();
1472 axe2 = closestBlob.get(5).asInt();
1473 double finalOrient=actionOrient;
1474 fprintf(stdout ,
"INITIAL orientation is: %lf \n", orient);
1475 if (abs(axe1-axe2) < 5)
1477 fprintf(stdout,
"seem a round object to me...sending theta2\n");
1481 fprintf(stdout,
"sending theta2 - theta1 <-> axis diff is %d\n", abs(axe1-axe2));
1482 finalOrient -= orient;
1487 fprintf(stdout,
"The 3Dpos is %lf %lf %lf \n",initPos[0], initPos[1], initPos[2]);
1489 double alpha = finalOrient * M_PI/180;
1490 pxls.x = int(cog.x + (axe1 * 1.2 ) * cos(alpha));
1491 pxls.y = int(cog.y - (axe2 * 1.2 ) * sin(alpha));
1493 get3DPosition(pxls,offPos);
1494 fprintf(stdout,
"The 3Dpos off point is %lf %lf %lf \n",offPos[0], offPos[1], offPos[2]);
1495 double offset= norm (initPos-offPos);
1496 fprintf(stdout,
"The offset is %lf \n",offset);
1499 ret.addDouble(offset);
1500 ret.addDouble(finalOrient);
1505 Bottle Manager::classify(
const Bottle &blobs,
int index)
1508 mutexResources.lock();
1516 cmd.addVocab(Vocab::encode(
"classify"));
1518 Bottle &options=cmd.addList();
1520 tag<<
"blob_"<<index;
1521 Bottle &item=options.addList();
1522 item.addString(tag.str());
1523 item.addList()=*blobs.get(index).asList();
1525 printf(
"Sending classification request: %s\n",cmd.toString().c_str());
1526 rpcMIL.write(cmd,reply);
1527 printf(
"Received reply: %s\n",reply.toString().c_str());
1528 mutexResources.unlock();
1532 cog=getBlobCOG(blobs,index);
1533 Bottle &tmpLine = gotMils.addList();
1534 Bottle &tmpMils = tmpLine.addList();
1535 tmpMils.addDouble(cog.x);
1536 tmpMils.addDouble(cog.y);
1537 tmpLine.addList()=*reply.get(0).asList()->get(1).asList();
1540 mils.addList()=gotMils;
1545 Bottle Manager::getType(
const yarp::os::Bottle *scores,
int index)
1548 tag<<
"blob_"<<index;
1551 double max_score=0.0;
1552 string max_label=
"unknown";
1553 if (scores->get(0).asList()->size() > 0)
1555 Bottle *tmp_scores=scores->get(0).asList()->get(0).asList()->get(1).asList();
1557 fprintf(stdout,
"bottle is: %s \n",tmp_scores->toString().c_str());
1559 for(
int s=0; s<tmp_scores->size(); s++)
1561 if(tmp_scores->get(s).asList()->get(1).asDouble()>max_score)
1563 max_score=tmp_scores->get(s).asList()->get(1).asDouble();
1564 max_label=tmp_scores->get(s).asList()->get(0).asString();
1575 type.addString(max_label);
1580 type.addString(
"error");
1584 double Manager::getPeriod()
1590 bool Manager::get3DPosition(
const cv::Point &point, Vector &x)
1592 Bottle cmdMotor,replyMotor;
1593 cmdMotor.addVocab(Vocab::encode(
"get"));
1594 cmdMotor.addVocab(Vocab::encode(
"s2c"));
1595 Bottle &options=cmdMotor.addList();
1596 options.addString(camera);
1597 options.addInt(point.x);
1598 options.addInt(point.y);
1599 printf(
"Sending motor query: %s\n",cmdMotor.toString().c_str());
1600 rpcMotorAre.write(cmdMotor,replyMotor);
1601 printf(
"Received blob cartesian coordinates: %s\n",replyMotor.toString().c_str());
1603 if (replyMotor.size()>=3)
1606 x[0]=replyMotor.get(0).asDouble();
1607 x[1]=replyMotor.get(1).asDouble();
1608 x[2]=replyMotor.get(2).asDouble();
1615 Bottle Manager::getBlobs()
1618 mutexResources.lock();
1620 if (Bottle *pBlobs=blobExtractor.read(
false))
1623 printf(
"Received blobs list: %s\n",lastBlobs.toString().c_str());
1625 if (lastBlobs.size()==1)
1627 if (lastBlobs.get(0).asVocab()==Vocab::encode(
"empty"))
1632 mutexResources.unlock();
1637 cv::Point Manager::getBlobCOG(
const Bottle &blobs,
const int i)
1639 cv::Point cog=cv::Point(RET_INVALID,RET_INVALID);
1640 if ((i>=0) && (i<blobs.size()))
1643 Bottle *item=blobs.get(i).asList();
1647 tl.x=(int)item->get(0).asDouble();
1648 tl.y=(int)item->get(1).asDouble();
1649 br.x=(int)item->get(2).asDouble();
1650 br.y=(int)item->get(3).asDouble();
1652 cog.x=(tl.x+br.x)>>1;
1653 cog.y=(tl.y+br.y)>>1;
1658 Bottle Manager::findClosestBlob(
const Bottle &blobs,
const cv::Point &loc)
1660 int ret=RET_INVALID;
1661 double min_d2=std::numeric_limits<double>::max();
1663 pointReturn.clear();
1664 for (
int i=0; i<blobs.size(); i++)
1666 cv::Point cog=getBlobCOG(blobs,i);
1667 if ((cog.x==RET_INVALID) || (cog.y==RET_INVALID))
1670 double dx=loc.x-cog.x;
1671 double dy=loc.y-cog.y;
1672 double d2=dx*dx+dy*dy;
1680 cv::Point cog=getBlobCOG( blobs, ret );
1681 pointReturn.addDouble(cog.x);
1682 pointReturn.addDouble(cog.y);
1683 pointReturn.addInt(ret);
1684 Bottle *item=blobs.get(ret).asList();
1685 double orient = (double)item->get(4).asDouble();
1686 int axe1 = item->get(5).asInt();
1687 int axe2 = item->get(6).asInt();
1688 pointReturn.addDouble(orient);
1689 pointReturn.addInt(axe1);
1690 pointReturn.addInt(axe2);