78#include <yarp/os/all.h>
79#include <yarp/dev/all.h>
80#include <yarp/sig/all.h>
81#include <yarp/math/Math.h>
84using namespace yarp::os;
85using namespace yarp::dev;
86using namespace yarp::sig;
87using namespace yarp::math;
89constexpr double DEG2RAD = M_PI / 180.0;
93class PosturesModule :
public RFModule
96 PolyDriver drvArmL,drvArmR;
97 PolyDriver drvCartL,drvCartR,drvGaze;
98 IEncoders *iencsL,*iencsR;
99 IPositionControl *ipossL,*ipossR;
100 ICartesianControl *icartL,*icartR;
104 map<string,Bottle> emotionsRaw;
107 RpcClient iolPortStatus;
108 RpcClient iolPortHuman;
112 void handHelper(
const string &type,
const Vector &poss)
126 IPositionControl *iposs;
138 int i0=nEncs-poss.length();
139 for (
int i=i0; i<nEncs; i++)
141 imode->setControlMode(i,VOCAB_CM_POSITION);
142 iposs->setRefAcceleration(i,std::numeric_limits<double>::max());
143 iposs->setRefSpeed(i,vels[i-i0]);
144 iposs->positionMove(i,poss[i-i0]);
149 void openHand(
const string &type)
154 printf(
"open hand\n");
155 handHelper(type,poss);
159 void closeHand(
const string &type)
172 printf(
"open hand\n");
173 handHelper(type,poss);
177 void karateHand(
const string &type)
184 printf(
"karate hand\n");
185 handHelper(type,poss);
189 void pointHand(
const string &type)
202 printf(
"point hand\n");
203 handHelper(type,poss);
207 void postureHelper(
const string &priority,
208 const Matrix &targetL,
209 const Matrix &targetR,
210 const Vector &targetG,
211 const Vector &torso=Vector(3,0.0))
213 int ctxtL,ctxtR,ctxtG;
216 if (targetG.length()>=3)
218 printf(
"looking at target\n");
219 igaze->storeContext(&ctxtG);
220 igaze->blockNeckRoll(0.0);
221 igaze->lookAtAbsAngles(targetG);
224 icartL->storeContext(&ctxtL);
225 icartL->getDOF(dof); dof=1.0;
226 icartL->setDOF(dof,dof);
227 icartL->setLimits(0,torso[0],torso[0]);
228 icartL->setLimits(1,torso[1],torso[1]);
229 icartL->setLimits(2,torso[2],torso[2]);
230 icartL->setPosePriority(priority.c_str());
231 icartL->setTrajTime(0.8);
233 printf(
"reaching for left target\n");
234 icartL->goToPoseSync(targetL.getCol(3),dcm2axis(targetL));
236 icartR->storeContext(&ctxtR);
237 icartR->getDOF(dof); dof=1.0;
238 dof[0]=dof[1]=dof[2]=0.0;
239 icartR->setDOF(dof,dof);
240 icartR->setTrackingMode(
true);
241 icartR->setPosePriority(priority.c_str());
242 icartR->setTrajTime(0.8);
244 printf(
"reaching for right target\n");
245 icartR->goToPoseSync(targetR.getCol(3),dcm2axis(targetR));
247 printf(
"waiting for right arm... ");
248 icartR->waitMotionDone();
251 printf(
"waiting for left arm... ");
252 icartL->waitMotionDone();
255 icartL->restoreContext(ctxtL);
256 icartL->deleteContext(ctxtL);
258 icartR->restoreContext(ctxtR);
259 icartR->deleteContext(ctxtR);
261 if (targetG.length()>=3)
263 igaze->restoreContext(ctxtG);
264 igaze->deleteContext(ctxtG);
269 void beSuspicious(
const Vector &lim=Vector(3,0.0))
272 igaze->storeContext(&ctxt);
274 igaze->blockNeckPitch(lim[0]);
275 igaze->blockNeckRoll(lim[1]);
276 igaze->blockNeckYaw(lim[2]);
278 Vector targetG(3,0.0);
281 igaze->lookAtAbsAngles(targetG);
285 igaze->lookAtAbsAngles(targetG);
289 igaze->lookAtAbsAngles(targetG);
290 igaze->waitMotionDone();
292 igaze->restoreContext(ctxt);
293 igaze->deleteContext(ctxt);
297 int beHappy(
const double roll)
300 igaze->storeContext(&ctxt);
302 igaze->blockNeckPitch(-20.0);
303 igaze->blockNeckRoll(roll);
304 igaze->blockNeckYaw(0.0);
305 igaze->blockEyes(0.0);
307 Vector targetG(3,0.0);
308 igaze->lookAtAbsAngles(targetG);
314 bool posture(
const string &part,
316 const Vector &x=Vector(3,0.0))
318 Vector targetG(3,0.0);
319 Matrix targetL=zeros(4,4);
320 Matrix targetR=zeros(4,4);
321 targetL(3,3)=targetR(3,3)=1.0;
325 targetL(0,0)=targetR(0,0)=-1.0;
326 targetL(2,1)=targetR(2,1)=-1.0;
327 targetL(1,2)=targetR(1,2)=-1.0;
329 targetL(0,3)=targetR(0,3)=-0.25;
330 targetL(2,3)=targetR(2,3)=0.05;
338 postureHelper(
"position",targetL,targetR,targetG);
341 else if (type==
"pinch")
346 printf(
"looking at (%s)\n",x.toString(3,3).c_str());
347 igaze->lookAtFixationPoint(x);
349 igaze->stopControl();
358 targetL(2,0)=targetR(2,0)=1.0;
359 targetL(0,1)=targetR(0,1)=-1.0;
360 targetL(1,2)=targetR(1,2)=-1.0;
371 r[1]=-1.0; r[3]=DEG2RAD*30.0;
372 targetL=axis2dcm(r)*targetL;
373 targetR=axis2dcm(r)*targetR;
377 Vector lim(3,0.0); lim[2]=30.0;
378 postureHelper(
"orientation",targetL,targetR,targetG,lim);
379 say(
"go ahead, make my day");
383 say(
"I'm watching you!");
384 posture(part,
"home");
388 else if (type==
"poke")
391 say(
"what was that?");
394 targetL(0,0)=targetR(0,0)=-1.0;
395 targetL(2,1)=targetR(2,1)=-1.0;
396 targetL(1,2)=targetR(1,2)=-1.0;
398 targetL(0,3)=targetR(0,3)=-0.25;
399 targetL(2,3)=targetR(2,3)=0.0;
405 r[2]=1.0; r[3]=DEG2RAD*20.0;
406 targetL=axis2dcm(r)*targetL;
409 targetR=axis2dcm(r)*targetR;
416 Vector lim(3,0.0); lim[0]=-5.0;
417 postureHelper(
"orientation",targetL,targetR,targetG,lim);
421 printf(
"looking at (%s)\n",x.toString(3,3).c_str());
422 igaze->lookAtFixationPoint(x);
424 igaze->stopControl();
427 if (part==
"right_arm")
444 r[2]=-1.0; r[3]=DEG2RAD*50.0;
445 targetL=axis2dcm(r)*targetL;
483 r[2]=1.0; r[3]=DEG2RAD*50.0;
484 targetR=axis2dcm(r)*targetR;
496 postureHelper(
"orientation",targetL,targetR,targetG);
499 posture(part,
"home");
505 targetL(0,0)=targetR(0,0)=-1.0;
506 targetL(2,1)=targetR(2,1)=-1.0;
507 targetL(1,2)=targetR(1,2)=-1.0;
509 targetL(0,3)=targetR(0,3)=-0.25;
510 targetL(2,3)=targetR(2,3)=0.1;
516 r[1]=1.0; r[3]=DEG2RAD*20.0;
517 targetL=axis2dcm(r)*targetL;
518 targetR=axis2dcm(r)*targetR;
524 if (part==
"left_arm")
526 else if (part==
"right_arm")
531 int ctxt=beHappy(roll);
534 postureHelper(
"orientation",targetL,targetR,nolook);
537 igaze->restoreContext(ctxt);
538 igaze->deleteContext(ctxt);
541 posture(part,
"home");
551 if (iolPortHuman.getOutputCount()==0)
555 cmd.addVocab32(
"status");
556 if (iolPortStatus.write(cmd,reply))
560 if ((reply.get(0).asString()==
"ack") &&
561 (reply.get(1).asString()==
"idle"))
564 cmd.addVocab32(
"attention");
565 cmd.addString(
"stop");
566 printf(
"sending: %s\n",cmd.toString().c_str());
567 bool ok=iolPortHuman.write(cmd,reply);
568 printf(
"received: %s\n",reply.toString().c_str());
570 return (reply.get(0).asString()==
"ack");
581 if (iolPortHuman.getOutputCount()==0)
585 cmd.addVocab32(
"attention");
586 cmd.addString(
"start");
587 printf(
"sending: %s\n",cmd.toString().c_str());
588 bool ok=iolPortHuman.write(cmd,reply);
589 printf(
"received: %s\n",reply.toString().c_str());
591 return (reply.get(0).asString()==
"ack");
597 bool say(
const string &sentence)
599 if (iolPortHuman.getOutputCount()>0)
602 cmd.addVocab32(
"say");
603 cmd.addString(sentence.c_str());
604 if (iolPortHuman.write(cmd,reply))
605 return (reply.get(0).asString()==
"ack");
612 bool emotion(
const string &type)
614 if (emotionsPort.getOutputCount()>0)
616 map<string,Bottle>::iterator it=emotionsRaw.find(type);
617 if (it!=emotionsRaw.end())
619 Bottle &b=it->second;
620 for (
int i=0; i<b.size(); i++)
623 cmd.addString(b.get(i).asString());
624 emotionsPort.write(cmd);
636 void configureEmotions(ResourceFinder &rf)
638 int numEmotions=rf.check(
"num_emotions",Value(0)).asInt32();
639 for (
int i=0; i<numEmotions; i++)
644 Bottle &emotion=rf.findGroup(tag.str().c_str());
645 if (!emotion.isNull())
647 if (emotion.check(
"name"))
649 string name=emotion.find(
"name").asString().c_str();
650 if (Bottle *raw=emotion.find(
"raw").asList())
651 emotionsRaw[name]=*raw;
659 bool configure(ResourceFinder &rf)
661 string robot=rf.check(
"robot",Value(
"icub")).asString().c_str();
662 string name=rf.check(
"name",Value(
"funnyPostures")).asString().c_str();
664 rpcPort.open((
"/"+name+
"/rpc").c_str());
665 iolPortStatus.open((
"/"+name+
"/iol/status:rpc").c_str());
666 iolPortHuman.open((
"/"+name+
"/iol/human:rpc").c_str());
667 emotionsPort.open((
"/"+name+
"/emotions/raw").c_str());
670 Property optionArmL(
"(device remote_controlboard)");
671 optionArmL.put(
"remote",(
"/"+robot+
"/left_arm").c_str());
672 optionArmL.put(
"local",(
"/"+name+
"/joint/left").c_str());
673 if (!drvArmL.open(optionArmL))
674 printf(
"Position left_arm controller not available!\n");
676 Property optionArmR(
"(device remote_controlboard)");
677 optionArmR.put(
"remote",(
"/"+robot+
"/right_arm").c_str());
678 optionArmR.put(
"local",(
"/"+name+
"/joint/right").c_str());
679 if (!drvArmR.open(optionArmR))
680 printf(
"Position right_arm controller not available!\n");
682 Property optionCartL(
"(device cartesiancontrollerclient)");
683 optionCartL.put(
"remote",(
"/"+robot+
"/cartesianController/left_arm").c_str());
684 optionCartL.put(
"local",(
"/"+name+
"/cartesian/left").c_str());
685 if (!drvCartL.open(optionCartL))
686 printf(
"Cartesian left_arm controller not available!\n");
688 Property optionCartR(
"(device cartesiancontrollerclient)");
689 optionCartR.put(
"remote",(
"/"+robot+
"/cartesianController/right_arm").c_str());
690 optionCartR.put(
"local",(
"/"+name+
"/cartesian/right").c_str());
691 if (!drvCartR.open(optionCartR))
692 printf(
"Cartesian right_arm controller not available!\n");
694 Property optionGaze(
"(device gazecontrollerclient)");
695 optionGaze.put(
"remote",
"/iKinGazeCtrl");
696 optionGaze.put(
"local",(
"/"+name+
"/gaze").c_str());
697 if (!drvGaze.open(optionGaze))
698 printf(
"Gaze controller not available!\n");
701 if (!drvArmL.isValid() || !drvArmR.isValid() ||
702 !drvCartL.isValid() || !drvCartR.isValid() ||
705 printf(
"Something wrong occured while configuring drivers... quitting!\n");
711 drvArmL.view(iencsL); drvArmL.view(ipossL); drvCartL.view(icartL);
712 drvArmR.view(iencsR); drvArmR.view(ipossR); drvCartR.view(icartR);
714 iencsL->getAxes(&nEncs);
718 configureEmotions(rf);
736 bool respond(
const Bottle &cmd, Bottle &reply)
738 int ack=Vocab32::encode(
"ack");
739 int nack=Vocab32::encode(
"nack");
741 Value item=cmd.get(0);
744 if (item.asString()==
"home")
748 posture(
"foo",
"home");
750 reply.addVocab32(ack);
753 reply.addVocab32(nack);
760 if (Bottle *what=item.asList())
764 string part=what->get(0).asString().c_str();
765 string type=what->get(1).asString().c_str();
769 if (Bottle *where=cmd.get(1).asList())
770 for (
int i=0; i<where->size(); i++)
771 x[i]=where->get(i).asFloat64();
775 ans=posture(part,type,x)?ack:nack;
785 reply.addVocab32(ack);
786 else if (RFModule::respond(cmd,rep))
789 reply.addVocab32(nack);
798 iolPortStatus.close();
799 iolPortHuman.close();
800 emotionsPort.close();
802 if (drvArmL.isValid())
805 if (drvArmR.isValid())
808 if (drvCartL.isValid())
811 if (drvCartR.isValid())
814 if (drvGaze.isValid())
829int main(
int argc,
char *argv[])
832 if (!yarp.checkNetwork())
834 printf(
"YARP server not available!\n");
839 rf.setDefaultContext(
"funny-things");
840 rf.setDefaultConfigFile(
"emotions.ini");
841 rf.configure(argc,argv);
844 return mod.runModule(rf);