60 #include <yarp/os/Network.h>
61 #include <yarp/os/RFModule.h>
62 #include <yarp/sig/Vector.h>
63 #include <yarp/sig/Matrix.h>
64 #include <yarp/math/Math.h>
65 #include <yarp/dev/Drivers.h>
66 #include <yarp/dev/PolyDriver.h>
67 #include <yarp/dev/CartesianControl.h>
69 #include <iCub/d4c/d4c_client.h>
79 class ClientModule:
public RFModule
83 Property targetOptRx,ObsOptRx;
86 yarp::dev::PolyDriver dCtrl;
87 yarp::dev::ICartesianControl *iCtrl;
99 bool configure(ResourceFinder &rf)
101 int verbosity=rf.check(
"verbosity",Value(0)).asInt();
102 string remote=rf.check(
"remote",Value(
"d4c_server")).asString().c_str();
103 string local=rf.check(
"local",Value(
"d4c_client")).asString().c_str();
104 string robot=rf.check(
"robot",Value(
"icub")).asString().c_str();
105 string part=rf.check(
"part",Value(
"left_arm")).asString().c_str();
106 arm=(part==
"left_arm"?
"left":
"right");
109 options.put(
"verbosity",verbosity);
110 options.put(
"remote",(
"/"+remote).c_str());
111 options.put(
"local",(
"/"+local).c_str());
117 optCtrl.put(
"device",
"cartesiancontrollerclient");
118 optCtrl.put(
"remote",(
"/"+robot+
"/cartesianController/"+part).c_str());
119 optCtrl.put(
"local",(
"/"+local+
"/cartesian").c_str());
121 if (dCtrl.open(optCtrl))
126 iCtrl->storeContext(&store_context_id);
134 iCtrl->setDOF(newDof,dof);
135 iCtrl->setLimits(7,-70.0,70.0);
137 return client.open(options);
145 client.disableControl();
146 client.disableField();
148 iCtrl->restoreContext(store_context_id);
165 x[1]=(arm==
"left"?-0.05:0.05);
167 Matrix R(3,3); R=0.0;
168 R(0,0)=-1.0; R(2,1)=-1.0; R(1,2)=-1.0;
170 iCtrl->goToPoseSync(x,o0,2.0);
171 iCtrl->waitMotionDone();
173 iCtrl->setTrajTime(1.0);
174 iCtrl->setInTargetTol(1e-3);
179 Value centerTg; centerTg.fromString((
"("+
string(xTg.toString().c_str())+
")").c_str());
180 Value radiusTg; radiusTg.fromString(
"(0.01 0.01 0.01)");
183 targetOpt.put(
"type",
"target_msd");
184 targetOpt.put(
"active",
"on");
185 targetOpt.put(
"K",2.0);
186 targetOpt.put(
"D",2.5);
187 targetOpt.put(
"name",
"target");
188 targetOpt.put(
"center",centerTg);
189 targetOpt.put(
"radius",radiusTg);
190 client.addItem(targetOpt,target);
193 xOb[0]=(x[0]+xTg[0])/2.0;
194 xOb[1]+=(arm==
"left"?0.05:-0.05);
195 Value centerOb; centerOb.fromString((
"("+
string(xOb.toString().c_str())+
")").c_str());
196 Value radiusOb; radiusOb.fromString(
"(0.1 0.1 0.1)");
198 Property obstacleOpt;
199 obstacleOpt.put(
"type",
"obstacle_gaussian");
200 obstacleOpt.put(
"active",
"on");
201 obstacleOpt.put(
"G",5.0);
202 obstacleOpt.put(
"name",
"obstacle");
203 obstacleOpt.put(
"center",centerOb);
204 obstacleOpt.put(
"radius",radiusOb);
205 obstacleOpt.put(
"cut_tails",
"on");
206 client.addItem(obstacleOpt,obstacle);
208 client.setActiveIF(arm);
209 client.setPointStateToTool();
210 client.enableControl();
211 client.enableField();
218 Vector x,o,xdot,odot;
219 client.getPointState(x,o,xdot,odot);
220 double d1=norm(xTg-x);
223 iCtrl->getPose(xee,oee);
224 double d2=norm(x-xee);
226 Vector zero(4); zero=0.0;
228 o1[0]=(arm==
"left"?-1.0:1.0); o1[3]=M_PI/2.0*(1.0-d1/dist);
229 o=dcm2axis(axis2dcm(o1)*axis2dcm(o0));
230 client.setPointOrientation(o,zero);
232 fprintf(stdout,
"|xTg-x|=%g [m], |x-xee|=%g[m]\n",d1,d2);
233 if ((d1<2e-3) && (d2<2e-3))
235 client.disableControl();
236 client.disableField();
238 fprintf(stdout,
"job accomplished\n");
239 iCtrl->restoreContext(store_context_id);
261 int main(
int argc,
char *argv[])
264 if (!yarp.checkNetwork())
266 fprintf(stdout,
"YARP server not available!\n");
272 rf.configure(argc,argv);
275 return mod.runModule(rf);