28 #include <yarp/math/Math.h>
32 #define CARTCTRL_CLIENT_VER "2.0"
33 #define CARTCTRL_DEFAULT_TMO 0.1
36 using namespace yarp::os;
38 using namespace yarp::sig;
39 using namespace yarp::math;
46 interface->eventHandling(event);
53 this->interface=interface;
81 lastPoseMsgArrivalTime=0.0;
85 portEvents.setInterface(
this);
92 string remote, local, carrier;
94 if (config.check(
"remote"))
95 remote=config.find(
"remote").asString();
99 if (config.check(
"local"))
100 local=config.find(
"local").asString();
105 carrier=config.check(
"carrier",Value(
"udp")).asString();
107 if (config.check(
"timeout"))
108 timeout=config.find(
"timeout").asFloat64();
110 portCmd.open(local+
"/command:o");
111 portState.open(local+
"/state:i");
112 portEvents.open(local+
"/events:i");
113 portRpc.open(local+
"/rpc:o");
116 ok&=Network::connect(portRpc.getName(),remote+
"/rpc:i");
121 if (
info.check(
"server_version"))
123 string server_version=
info.find(
"server_version").asString();
126 yError(
"version mismatch => server(%s) != client(%s); please update accordingly",
133 yWarning(
"unable to retrieve server version; please update the server");
137 yError(
"unable to connect to the server rpc port!");
142 ok&=Network::connect(portCmd.getName(),remote+
"/command:i",carrier);
143 ok&=Network::connect(remote+
"/state:o",portState.getName(),carrier);
144 ok&=Network::connect(remote+
"/events:o",portEvents.getName(),carrier);
149 Bottle command, reply;
154 if (!portRpc.write(command,reply))
156 yError(
"unable to get reply from server!");
164 return connected=
true;
166 yError(
"unable to connect to solver!");
172 yError(
"unable to connect to server!");
187 while (eventsMap.size()>0)
188 unregisterEvent(*eventsMap.begin()->second);
191 portState.interrupt();
192 portEvents.interrupt();
211 Bottle command, reply;
217 if (!portRpc.write(command,reply))
219 yError(
"unable to get reply from server!");
230 if (!connected || (
f==NULL))
233 Bottle command, reply;
237 if (!portRpc.write(command,reply))
239 yError(
"unable to get reply from server!");
245 int mode=reply.get(1).asVocab32();
267 Bottle command, reply;
273 if (!portRpc.write(command,reply))
275 yError(
"unable to get reply from server!");
286 if (!connected || (
f==NULL))
289 Bottle command, reply;
293 if (!portRpc.write(command,reply))
295 yError(
"unable to get reply from server!");
301 int mode=reply.get(1).asVocab32();
323 Bottle command, reply;
326 command.addString(
p);
328 if (!portRpc.write(command,reply))
330 yError(
"unable to get reply from server!");
344 Bottle command, reply;
348 if (!portRpc.write(command,reply))
350 yError(
"unable to get reply from server!");
356 p=reply.get(1).asString();
370 double now=Time::now();
373 if (Vector *v=portState.read(
false))
376 portState.getEnvelope(poseStamp);
377 lastPoseMsgArrivalTime=now;
381 o.resize(pose.length()-
x.length());
383 for (
size_t i=0; i<
x.length(); i++)
386 for (
size_t i=0; i<o.length(); i++)
387 o[i]=pose[
x.length()+i];
392 return (now-lastPoseMsgArrivalTime<timeout);
403 Bottle command, reply;
406 command.addInt32(axis);
408 if (!portRpc.write(command,reply))
410 yError(
"unable to get reply from server!");
416 if (Bottle *posePart=reply.get(1).asList())
419 o.resize(posePart->size()-
x.length());
421 for (
size_t i=0; i<
x.length(); i++)
422 x[i]=posePart->get(i).asFloat64();
424 for (
size_t i=0; i<o.length(); i++)
425 o[i]=posePart->get(
x.length()+i).asFloat64();
427 if ((reply.size()>2) && (stamp!=NULL))
429 if (Bottle *stampPart=reply.get(2).asList())
431 Stamp tmpStamp(stampPart->get(0).asInt32(),
432 stampPart->get(1).asFloat64());
450 if (!connected || (xd.length()<3) || (od.length()<4))
453 Bottle &command=portCmd.prepare();
458 command.addFloat64(t);
459 Bottle &xdesPart=command.addList();
461 for (
int i=0; i<3; i++)
462 xdesPart.addFloat64(xd[i]);
464 for (
int i=0; i<4; i++)
465 xdesPart.addFloat64(od[i]);
468 portCmd.writeStrict();
476 if (!connected || (xd.length()<3))
479 Bottle &command=portCmd.prepare();
484 command.addFloat64(t);
485 Bottle &xdesPart=command.addList();
487 for (
int i=0; i<3; i++)
488 xdesPart.addFloat64(xd[i]);
491 portCmd.writeStrict();
500 if (!connected || (xd.length()<3) || (od.length()<4))
503 Bottle command, reply;
506 command.addFloat64(t);
507 Bottle &xdesPart=command.addList();
509 for (
int i=0; i<3; i++)
510 xdesPart.addFloat64(xd[i]);
512 for (
int i=0; i<4; i++)
513 xdesPart.addFloat64(od[i]);
515 if (!portRpc.write(command,reply))
517 yError(
"unable to get reply from server!");
528 if (!connected || (xd.length()<3))
531 Bottle command, reply;
534 command.addFloat64(t);
535 Bottle &xdesPart=command.addList();
537 for (
int i=0; i<3; i++)
538 xdesPart.addFloat64(xd[i]);
540 if (!portRpc.write(command,reply))
542 yError(
"unable to get reply from server!");
557 Bottle command, reply;
561 if (!portRpc.write(command,reply))
563 yError(
"unable to get reply from server!");
567 return getDesiredOption(reply,xdhat,odhat,qdhat);
573 Vector &xdhat, Vector &odhat,
579 Bottle command, reply;
580 Vector tg(xd.length()+od.length());
581 for (
size_t i=0; i<xd.length(); i++)
584 for (
size_t i=0; i<od.length(); i++)
585 tg[xd.length()+i]=od[i];
591 if (!portRpc.write(command,reply))
593 yError(
"unable to get reply from server!");
597 return getDesiredOption(reply,xdhat,odhat,qdhat);
603 const Vector &od, Vector &xdhat,
604 Vector &odhat, Vector &qdhat)
609 Bottle command, reply;
610 Vector tg(xd.length()+od.length());
611 for (
size_t i=0; i<xd.length(); i++)
614 for (
size_t i=0; i<od.length(); i++)
615 tg[xd.length()+i]=od[i];
622 if (!portRpc.write(command,reply))
624 yError(
"unable to get reply from server!");
628 return getDesiredOption(reply,xdhat,odhat,qdhat);
634 Vector &odhat, Vector &qdhat)
639 Bottle command, reply;
644 if (!portRpc.write(command,reply))
646 yError(
"unable to get reply from server!");
650 return getDesiredOption(reply,xdhat,odhat,qdhat);
656 Vector &xdhat, Vector &odhat,
662 Bottle command, reply;
668 if (!portRpc.write(command,reply))
670 yError(
"unable to get reply from server!");
674 return getDesiredOption(reply,xdhat,odhat,qdhat);
684 Bottle command, reply;
688 if (!portRpc.write(command,reply))
690 yError(
"unable to get reply from server!");
696 if (Bottle *dofPart=reply.get(1).asList())
698 curDof.resize(dofPart->size());
700 for (
size_t i=0; i<curDof.length(); i++)
701 curDof[i]=dofPart->get(i).asFloat64();
717 Bottle command, reply;
720 Bottle &dofPart=command.addList();
722 for (
size_t i=0; i<newDof.length(); i++)
723 dofPart.addInt32((
int)newDof[i]);
725 if (!portRpc.write(command,reply))
727 yError(
"unable to get reply from server!");
733 if (Bottle *dofPart=reply.get(1).asList())
735 curDof.resize(dofPart->size());
737 for (
size_t i=0; i<curDof.length(); i++)
738 curDof[i]=dofPart->get(i).asFloat64();
754 Bottle command, reply;
758 if (!portRpc.write(command,reply))
760 yError(
"unable to get reply from server!");
766 if (Bottle *restPart=reply.get(1).asList())
768 curRestPos.resize(restPart->size());
770 for (
size_t i=0; i<curRestPos.length(); i++)
771 curRestPos[i]=restPart->get(i).asFloat64();
788 Bottle command, reply;
791 command.addList().read(newRestPos);
793 if (!portRpc.write(command,reply))
795 yError(
"unable to get reply from server!");
801 if (Bottle *restPart=reply.get(1).asList())
803 curRestPos.resize(restPart->size());
804 for (
size_t i=0; i<curRestPos.length(); i++)
805 curRestPos[i]=restPart->get(i).asFloat64();
820 Bottle command, reply;
824 if (!portRpc.write(command,reply))
826 yError(
"unable to get reply from server!");
832 if (Bottle *restPart=reply.get(1).asList())
834 curRestWeights.resize(restPart->size());
836 for (
size_t i=0; i<curRestWeights.length(); i++)
837 curRestWeights[i]=restPart->get(i).asFloat64();
849 Vector &curRestWeights)
854 Bottle command, reply;
857 command.addList().read(newRestWeights);
859 if (!portRpc.write(command,reply))
861 yError(
"unable to get reply from server!");
867 if (Bottle *restPart=reply.get(1).asList())
869 curRestWeights.resize(restPart->size());
870 for (
size_t i=0; i<curRestWeights.length(); i++)
871 curRestWeights[i]=restPart->get(i).asFloat64();
884 if (!connected || (
min==NULL) || (
max==NULL))
887 Bottle command, reply;
890 command.addInt32(axis);
892 if (!portRpc.write(command,reply))
894 yError(
"unable to get reply from server!");
902 *
min=reply.get(1).asFloat64();
903 *
max=reply.get(2).asFloat64();
919 Bottle command, reply;
922 command.addInt32(axis);
923 command.addFloat64(
min);
924 command.addFloat64(
max);
926 if (!portRpc.write(command,reply))
928 yError(
"unable to get reply from server!");
939 if (!connected || (t==NULL))
942 Bottle command, reply;
946 if (!portRpc.write(command,reply))
948 yError(
"unable to get reply from server!");
956 *t=reply.get(1).asFloat64();
971 Bottle command, reply;
974 command.addFloat64(t);
976 if (!portRpc.write(command,reply))
978 yError(
"unable to get reply from server!");
989 if (!connected || (tol==NULL))
992 Bottle command, reply;
996 if (!portRpc.write(command,reply))
998 yError(
"unable to get reply from server!");
1006 *tol=reply.get(1).asFloat64();
1021 Bottle command, reply;
1024 command.addFloat64(tol);
1026 if (!portRpc.write(command,reply))
1028 yError(
"unable to get reply from server!");
1042 Bottle command, reply;
1046 if (!portRpc.write(command,reply))
1048 yError(
"unable to get reply from server!");
1054 if (Bottle *qdotPart=reply.get(1).asList())
1056 qdot.resize(qdotPart->size());
1058 for (
size_t i=0; i<qdot.length(); i++)
1059 qdot[i]=qdotPart->get(i).asFloat64();
1075 Bottle command, reply;
1079 if (!portRpc.write(command,reply))
1081 yError(
"unable to get reply from server!");
1087 if (Bottle *xdotPart=reply.get(1).asList())
1090 odot.resize(xdotPart->size()-xdot.length());
1092 for (
size_t i=0; i<xdot.length(); i++)
1093 xdot[i]=xdotPart->get(i).asFloat64();
1095 for (
size_t i=0; i<odot.length(); i++)
1096 odot[i]=xdotPart->get(xdot.length()+i).asFloat64();
1110 if (!connected || (xdot.length()<3) || (odot.length()<4))
1113 Bottle &command=portCmd.prepare();
1117 Bottle &xdotPart=command.addList();
1119 for (
int i=0; i<3; i++)
1120 xdotPart.addFloat64(xdot[i]);
1122 for (
int i=0; i<4; i++)
1123 xdotPart.addFloat64(odot[i]);
1126 portCmd.writeStrict();
1134 if (!connected || (
x.length()<3) || (o.length()<4))
1137 Bottle command, reply;
1140 Bottle &tipPart=command.addList();
1142 for (
int i=0; i<3; i++)
1143 tipPart.addFloat64(
x[i]);
1145 for (
int i=0; i<4; i++)
1146 tipPart.addFloat64(o[i]);
1148 if (!portRpc.write(command,reply))
1150 yError(
"unable to get reply from server!");
1157 if (Vector *v=portState.read(
true))
1160 portState.getEnvelope(poseStamp);
1161 lastPoseMsgArrivalTime=Time::now();
1177 Bottle command, reply;
1181 if (!portRpc.write(command,reply))
1183 yError(
"unable to get reply from server!");
1189 if (Bottle *tipPart=reply.get(1).asList())
1192 o.resize(tipPart->size()-
x.length());
1194 for (
size_t i=0; i<
x.length(); i++)
1195 x[i]=tipPart->get(i).asFloat64();
1197 for (
size_t i=0; i<o.length(); i++)
1198 o[i]=tipPart->get(
x.length()+i).asFloat64();
1211 return attachTipFrame(Vector(3,0.0),Vector(4,0.0));
1218 if (!connected || (
f==NULL))
1221 Bottle command, reply;
1225 if (!portRpc.write(command,reply))
1227 yError(
"unable to get reply from server!");
1235 int flag=reply.get(1).asVocab32();
1252 const double timeout)
1255 double t0=Time::now();
1259 Time::delay(period);
1261 if (!checkMotionDone(&
done) || ((timeout>0.0) && ((Time::now()-t0)>timeout)))
1275 Bottle command, reply;
1278 if (!portRpc.write(command,reply))
1280 yError(
"unable to get reply from server!");
1291 if (!connected || (
id==NULL))
1294 Bottle command, reply;
1297 if (!portRpc.write(command,reply))
1299 yError(
"unable to get reply from server!");
1307 contextIdList.insert(*
id=reply.get(1).asInt32());
1319 if (!connected || ((contextIdList.find(
id)==contextIdList.end()) && (
id!=0)))
1322 Bottle command, reply;
1324 command.addInt32(
id);
1326 if (!portRpc.write(command,reply))
1328 yError(
"unable to get reply from server!");
1339 if (!connected || ((contextIdList.find(
id)==contextIdList.end()) && (
id!=0)))
1342 Bottle command, reply;
1344 command.addList().addInt32(
id);
1346 if (!portRpc.write(command,reply))
1348 yError(
"unable to get reply from server!");
1354 contextIdList.erase(
id);
1368 Bottle command, reply;
1370 Bottle &ids=command.addList();
1371 for (set<int>::iterator itr=contextIdList.begin(); itr!=contextIdList.end(); itr++)
1374 if (!portRpc.write(command,reply))
1376 yError(
"unable to get reply from server!");
1380 contextIdList.clear();
1389 Bottle command, reply;
1393 if (!portRpc.write(command,reply))
1395 yError(
"unable to get reply from server!");
1403 if (Bottle *infoPart=reply.get(1).asList())
1418 return getInfoHelper(
info);
1427 string type=
event.get(0).asString();
1428 double time=
event.get(1).asFloat64();
1429 double checkPoint=(type==
"motion-ongoing")?event.get(2).asFloat64():-1.0;
1430 map<string,CartesianEvent*>::iterator itr;
1433 itr=eventsMap.find(
"*");
1434 if (itr!=eventsMap.end())
1436 if (itr->second!=NULL)
1438 CartesianEvent &Event=*itr->second;
1439 Event.cartesianEventVariables.type=type;
1440 Event.cartesianEventVariables.time=
time;
1442 if (checkPoint>=0.0)
1443 Event.cartesianEventVariables.motionOngoingCheckPoint=checkPoint;
1445 Event.cartesianEventCallback();
1449 string typeExtended=type;
1450 if (checkPoint>=0.0)
1453 ss<<type<<
"-"<<checkPoint;
1454 typeExtended=ss.str();
1458 itr=eventsMap.find(typeExtended);
1459 if (itr!=eventsMap.end())
1461 if (itr->second!=NULL)
1463 CartesianEvent &Event=*itr->second;
1464 Event.cartesianEventVariables.type=type;
1465 Event.cartesianEventVariables.time=
time;
1467 if (checkPoint>=0.0)
1468 Event.cartesianEventVariables.motionOngoingCheckPoint=checkPoint;
1470 Event.cartesianEventCallback();
1482 string type=
event.cartesianEventParameters.type;
1483 if (type==
"motion-ongoing")
1485 double checkPoint=
event.cartesianEventParameters.motionOngoingCheckPoint;
1487 Bottle command, reply;
1491 command.addFloat64(checkPoint);
1493 if (!portRpc.write(command,reply))
1495 yError(
"unable to get reply from server!");
1503 ss<<type<<
"-"<<checkPoint;
1507 eventsMap[type]=&event;
1518 string type=
event.cartesianEventParameters.type;
1519 if (type==
"motion-ongoing")
1521 double checkPoint=
event.cartesianEventParameters.motionOngoingCheckPoint;
1523 Bottle command, reply;
1527 command.addFloat64(checkPoint);
1529 if (!portRpc.write(command,reply))
1531 yError(
"unable to get reply from server!");
1539 ss<<type<<
"-"<<checkPoint;
1543 eventsMap.erase(type);
1554 Bottle command, reply;
1557 command.addList()=options;
1559 if (!portRpc.write(command,reply))
1561 yError(
"unable to get reply from server!");
1575 Bottle command, reply;
1579 if (!portRpc.write(command,reply))
1581 yError(
"unable to get reply from server!");
1589 if (Bottle *optionsPart=reply.get(1).asList())
1590 options=*optionsPart;
#define CARTCTRL_DEFAULT_TMO
#define CARTCTRL_CLIENT_VER
#define IKINCARTCTRL_VOCAB_OPT_Q
#define IKINCARTCTRL_VOCAB_OPT_INFO
#define IKINCARTCTRL_VOCAB_VAL_POSE_XYZ
#define IKINCARTCTRL_VOCAB_OPT_PRIO
#define IKINCARTCTRL_VOCAB_OPT_UNREGISTER
#define IKINCARTCTRL_VOCAB_VAL_TRUE
#define IKINCARTCTRL_VOCAB_CMD_GET
#define IKINCARTCTRL_VOCAB_CMD_GO
#define IKINCARTCTRL_VOCAB_OPT_REST_POS
#define IKINCARTCTRL_VOCAB_OPT_REFERENCE
#define IKINCARTCTRL_VOCAB_OPT_DES
#define IKINCARTCTRL_VOCAB_OPT_LIM
#define IKINCARTCTRL_VOCAB_OPT_QDOT
#define IKINCARTCTRL_VOCAB_OPT_XD
#define IKINCARTCTRL_VOCAB_REP_ACK
#define IKINCARTCTRL_VOCAB_VAL_FALSE
#define IKINCARTCTRL_VOCAB_CMD_STORE
#define IKINCARTCTRL_VOCAB_OPT_TIME
#define IKINCARTCTRL_VOCAB_CMD_DELETE
#define IKINCARTCTRL_VOCAB_OPT_MODE
#define IKINCARTCTRL_VOCAB_CMD_TASKVEL
#define IKINCARTCTRL_VOCAB_VAL_POSE_FULL
#define IKINCARTCTRL_VOCAB_VAL_MODE_TRACK
#define IKINCARTCTRL_VOCAB_OPT_DOF
#define IKINCARTCTRL_VOCAB_OPT_TOL
#define IKINCARTCTRL_VOCAB_VAL_MODE_SINGLE
#define IKINCARTCTRL_VOCAB_CMD_ASK
#define IKINCARTCTRL_VOCAB_CMD_SET
#define IKINCARTCTRL_VOCAB_OPT_REST_WEIGHTS
#define IKINCARTCTRL_VOCAB_CMD_RESTORE
#define IKINCARTCTRL_VOCAB_OPT_ISSOLVERON
#define IKINCARTCTRL_VOCAB_OPT_TIP_FRAME
#define IKINCARTCTRL_VOCAB_CMD_EVENT
#define IKINCARTCTRL_VOCAB_OPT_XDOT
#define IKINCARTCTRL_VOCAB_OPT_TWEAK
#define IKINCARTCTRL_VOCAB_OPT_POSE
#define IKINCARTCTRL_VOCAB_CMD_STOP
#define IKINCARTCTRL_VOCAB_OPT_REGISTER
#define IKINCARTCTRL_VOCAB_VAL_EVENT_ONGOING
#define IKINCARTCTRL_VOCAB_OPT_MOTIONDONE
void setInterface(ClientCartesianController *interface)
void onRead(yarp::os::Bottle &event)
clientcartesiancontroller : implements the client part of the Cartesian Interface.
bool getPosePriority(std::string &p)
bool getTrackingMode(bool *f)
bool goToPosition(const yarp::sig::Vector &xd, const double t=0.0)
virtual ~ClientCartesianController()
bool getJointsVelocities(yarp::sig::Vector &qdot)
bool getRestPos(yarp::sig::Vector &curRestPos)
bool checkMotionDone(bool *f)
bool getRestWeights(yarp::sig::Vector &curRestWeights)
bool tweakGet(yarp::os::Bottle &options)
bool getTipFrame(yarp::sig::Vector &x, yarp::sig::Vector &o)
bool getReferenceMode(bool *f)
bool deleteContext(const int id)
bool getPose(yarp::sig::Vector &x, yarp::sig::Vector &o, yarp::os::Stamp *stamp=NULL)
bool open(yarp::os::Searchable &config)
ClientCartesianController()
bool goToPoseSync(const yarp::sig::Vector &xd, const yarp::sig::Vector &od, const double t=0.0)
bool getInfoHelper(yarp::os::Bottle &info)
bool getInTargetTol(double *tol)
bool getTaskVelocities(yarp::sig::Vector &xdot, yarp::sig::Vector &odot)
bool setPosePriority(const std::string &p)
bool getInfo(yarp::os::Bottle &info)
bool getLimits(const int axis, double *min, double *max)
bool askForPosition(const yarp::sig::Vector &xd, yarp::sig::Vector &xdhat, yarp::sig::Vector &odhat, yarp::sig::Vector &qdhat)
bool attachTipFrame(const yarp::sig::Vector &x, const yarp::sig::Vector &o)
bool setRestPos(const yarp::sig::Vector &newRestPos, yarp::sig::Vector &curRestPos)
bool setInTargetTol(const double tol)
bool setRestWeights(const yarp::sig::Vector &newRestWeights, yarp::sig::Vector &curRestWeights)
void eventHandling(yarp::os::Bottle &event)
bool waitMotionDone(const double period=0.1, const double timeout=0.0)
bool setTaskVelocities(const yarp::sig::Vector &xdot, const yarp::sig::Vector &odot)
bool setDOF(const yarp::sig::Vector &newDof, yarp::sig::Vector &curDof)
bool unregisterEvent(yarp::dev::CartesianEvent &event)
bool getDesired(yarp::sig::Vector &xdhat, yarp::sig::Vector &odhat, yarp::sig::Vector &qdhat)
bool restoreContext(const int id)
bool registerEvent(yarp::dev::CartesianEvent &event)
bool goToPositionSync(const yarp::sig::Vector &xd, const double t=0.0)
bool setLimits(const int axis, const double min, const double max)
bool getTrajTime(double *t)
bool setTrajTime(const double t)
bool goToPose(const yarp::sig::Vector &xd, const yarp::sig::Vector &od, const double t=0.0)
bool getDOF(yarp::sig::Vector &curDof)
bool storeContext(int *id)
bool askForPose(const yarp::sig::Vector &xd, const yarp::sig::Vector &od, yarp::sig::Vector &xdhat, yarp::sig::Vector &odhat, yarp::sig::Vector &qdhat)
bool tweakSet(const yarp::os::Bottle &options)
bool setReferenceMode(const bool f)
bool setTrackingMode(const bool f)
#define IKINCTRL_POSE_FULL
#define IKINCTRL_POSE_XYZ