17 #include "handIKModule.h" 27 HandIKModule::HandIKModule()
34 bool HandIKModule::configure(ResourceFinder &rf)
36 string name=rf.check(
"name",Value(
"handIKModule1")).asString().c_str();
37 hand=rf.check(
"hand",Value(
"right")).asString().c_str();
41 portName=
"/"+name+
"/"+hand+
"/out";
42 outputPort.open(portName.c_str());
43 string rpcPortName=
"/"+name+
"/"+hand+
"/rpc";
44 rpc.open(rpcPortName.c_str());
46 createCombinationVector();
52 bool HandIKModule::extractData(
const Bottle &data)
54 Bottle &b=
const_cast<Bottle&
>(data);
55 Bottle* c=b.find(
"center").asList();
56 fillVectorFromBottle(c,center);
57 Bottle* d=b.find(
"dim").asList();
58 fillVectorFromBottle(d,dim);
59 Bottle* c1B=b.find(
"c1").asList();
61 fillVectorFromBottle(c1B,c1);
63 Bottle* c2B=b.find(
"c2").asList();
65 fillVectorFromBottle(c2B,c2);
67 Bottle* c3B=b.find(
"c3").asList();
69 fillVectorFromBottle(c3B,c3);
71 Bottle* n1B=b.find(
"n1").asList();
73 fillVectorFromBottle(n1B,n1);
74 Bottle* n2B=b.find(
"n2").asList();
76 fillVectorFromBottle(n2B,n2);
77 Bottle* n3B=b.find(
"n3").asList();
79 fillVectorFromBottle(n3B,n3);
80 Bottle* rot=b.find(
"rot").asList();
81 fillMatrixFromBottle(rot,rotation,3,3);
82 contacts_r.push_back(c1);
83 contacts_r.push_back(c2);
84 contacts_r.push_back(c3);
85 normals_r.push_back(n1);
86 normals_r.push_back(n2);
87 normals_r.push_back(n3);
92 void HandIKModule::fillMatrixFromBottle(
const Bottle* b, yarp::sig::Matrix &m,
int rows,
int cols)
96 for (
int i=0; i<rows; i++)
98 for (
int j=0; j<cols; j++)
100 m(i,j)=b->get(k).asDouble();
107 void HandIKModule::fillVectorFromBottle(
const Bottle* b, yarp::sig::Vector &v)
110 for (
int i=0; i<b->size(); i++)
111 v[i]=b->get(i).asDouble();
115 bool HandIKModule::interruptModule()
117 printf(
"interrupting\n");
118 outputPort.interrupt();
119 printf(
"interrupted out\n");
121 printf(
"interrupted rpc\n");
126 bool HandIKModule::close()
130 printf(
"closed out\n");
132 printf(
"closed rpc\n");
137 bool HandIKModule::updateModule()
144 for (
unsigned int i=0; i<combinations.size(); i++)
146 HandIK_Problem problem(hand,3);
148 problem.dimensions[0]=dim[1];
149 problem.dimensions[1]=dim[0];
150 problem.dimensions[2]=dim[2];
152 problem.contactPoints.push_back(contacts_o.at((
unsigned int)combinations.at(i)[0]));
153 problem.contactPoints.push_back(contacts_o.at((
unsigned int)combinations.at(i)[1]));
154 problem.contactPoints.push_back(contacts_o.at((
unsigned int)combinations.at(i)[2]));
156 problem.normalDirs.push_back(normals_o.at((
unsigned int)combinations.at(i)[0]));
157 problem.normalDirs.push_back(normals_o.at((
unsigned int)combinations.at(i)[1]));
158 problem.normalDirs.push_back(normals_o.at((
unsigned int)combinations.at(i)[2]));
160 HandIK_Solver solver(problem);
163 HandIK_Variables guess(problem.nFingers);
173 guess.rpy_ee[0]=-M_PI;
175 guess.rpy_ee[2]=M_PI/2.0;
181 guess.rpy_ee[2]=M_PI;
184 solver.setInitialGuess(guess);
188 HandIK_Variables solution;
189 double t0=Time::now();
190 bool found=solver.solve(solution);
191 double t1=Time::now();
194 printf(
"#### %d solution found in %g [s] ...\n", i, t1-t0);
196 double objFunc=solution.cost_fun;
197 printf(
"Cost %g\n", objFunc);
198 objFunc+=evaluateFingers(solution,i);
199 printf(
"Global cost %g\n", objFunc);
200 if (objFunc<bestObjValue)
202 bestObjValue=objFunc;
204 bestSolution=solution;
208 bestSolution.print();
213 outputPort.write(out);
222 double HandIKModule::evaluateFingers(
const HandIK_Variables &solution,
const int id)
224 yarp::sig::Vector encoders(9,0.0);
225 encoders[0]=solution.joints[3];
226 encoders[1]=solution.joints[0];
227 encoders[2]=solution.joints[1];
228 encoders[3]=solution.joints[2];
229 encoders[4]=solution.joints[4];
230 encoders[5]=solution.joints[5];
231 encoders[6]=solution.joints[6];
232 encoders[7]=solution.joints[7];
234 iCub::iKin::iCubFinger thumb(hand+
"_thumb");
235 iCub::iKin::iCubFinger index(hand+
"_index");
236 iCub::iKin::iCubFinger middle(hand+
"_middle");
238 yarp::sig::Vector joints_thumb;
239 thumb.getChainJoints(encoders,joints_thumb);
240 yarp::sig::Matrix thumbH=thumb.getH(joints_thumb);
241 yarp::sig::Vector thumbH_col3=thumbH.getCol(3);
243 yarp::sig::Vector joints_index;
244 index.getChainJoints(encoders,joints_index);
245 yarp::sig::Matrix indexH=index.getH(joints_index);
246 yarp::sig::Vector indexH_col3=indexH.getCol(3);
248 yarp::sig::Vector joints_middle;
249 middle.getChainJoints(encoders,joints_middle);
250 yarp::sig::Matrix middleH=middle.getH(joints_middle);
251 yarp::sig::Vector middleH_col3=middleH.getCol(3);
253 yarp::sig::Matrix Hc=rpy2dcm(solution.rpy_ee);
254 Hc(0,3)=solution.xyz_ee[0];
255 Hc(1,3)=solution.xyz_ee[1];
256 Hc(2,3)=solution.xyz_ee[2];
259 res+=norm2(contacts_o.at(combinations.at(
id)[0])-(Hc*thumbH_col3));
260 res+=norm2(contacts_o.at(combinations.at(
id)[1])-(Hc*indexH_col3));
261 res+=norm2(contacts_o.at(combinations.at(
id)[2])-(Hc*middleH_col3));
263 printf(
"res %g\n", res);
269 void HandIKModule::fromRootToObject()
286 rot_tran(0,3)=center[0];
287 rot_tran(1,3)=center[1];
288 rot_tran(2,3)=center[2];
289 Matrix rotmat=SE3inv(rot_tran);
291 Vector c1=rotmat*contacts_r.at(0);
292 Vector c2=rotmat*contacts_r.at(1);
293 Vector c3=rotmat*contacts_r.at(2);
294 Vector n1=rotmat.submatrix(0,2,0,2)*normals_r.at(0).subVector(0,2);
296 Vector n2=rotmat.submatrix(0,2,0,2)*normals_r.at(1).subVector(0,2);
298 Vector n3=rotmat.submatrix(0,2,0,2)*normals_r.at(2).subVector(0,2);
300 contacts_o.push_back(c1);
301 contacts_o.push_back(c2);
302 contacts_o.push_back(c3);
303 normals_o.push_back(n1);
304 normals_o.push_back(n2);
305 normals_o.push_back(n3);
309 void HandIKModule::createCombinationVector()
311 Vector v1(3); v1[0]=0; v1[1]=1; v1[2]=2;
312 Vector v2(3); v2[0]=0; v2[1]=2; v2[2]=1;
313 Vector v3(3); v3[0]=1; v3[1]=0; v3[2]=2;
314 Vector v4(3); v4[0]=1; v4[1]=2; v4[2]=0;
315 Vector v5(3); v5[0]=2; v5[1]=1; v5[2]=0;
316 Vector v6(3); v6[0]=2; v6[1]=0; v6[2]=1;
317 combinations.push_back(v1);
318 combinations.push_back(v2);
319 combinations.push_back(v3);
320 combinations.push_back(v4);
321 combinations.push_back(v5);
322 combinations.push_back(v6);
326 bool HandIKModule::respond(
const Bottle& command, Bottle& reply)
328 tag=command.get(0).asString();
329 if (tag==
"IK1" || tag==
"IK2" || tag==
"IK3" || tag==
"IK4")
331 Network::connect(portName.c_str(),
"/precision-grasp/rpc");
336 bool f=extractData(command);
341 reply.addString(
"ack");
345 reply.addString(
"nack");
350 void HandIKModule::prepareData(yarp::os::Bottle &data)
352 data.addString(tag.c_str());
353 Bottle &handB=data.addList();
354 handB.addString(
"hand");
355 handB.addString(hand.c_str());
356 Bottle &cost=data.addList();
357 cost.addString(
"cost");
358 cost.addDouble(bestObjValue);
366 rot_tran(0,3)=center[0];
367 rot_tran(1,3)=center[1];
368 rot_tran(2,3)=center[2];
370 yarp::sig::Vector ee_ob=bestSolution.xyz_ee;
371 ee_ob.push_back(1.0);
373 yarp::sig::Vector ee_root=rot_tran*ee_ob;
374 yarp::sig::Matrix tmp=rpy2dcm(bestSolution.rpy_ee);
375 yarp::sig::Matrix tmp2=rot_tran*tmp;
376 yarp::sig::Vector or_root=dcm2axis(tmp2);
378 Bottle &ee=data.addList();
380 Bottle &ee_coord=ee.addList();
381 ee_coord.addDouble(ee_root[0]);
382 ee_coord.addDouble(ee_root[1]);
383 ee_coord.addDouble(ee_root[2]);
384 Bottle &orientation=data.addList();
385 orientation.addString(
"or");
386 Bottle &or_coord=orientation.addList();
387 yarp::sig::Vector or_axisangle=dcm2axis(rpy2dcm(bestSolution.rpy_ee));
388 or_coord.addDouble(or_root[0]);
389 or_coord.addDouble(or_root[1]);
390 or_coord.addDouble(or_root[2]);
391 or_coord.addDouble(or_root[3]);
392 Bottle &j=data.addList();
393 j.addString(
"joints");
394 Bottle &joints=j.addList();
395 for (
unsigned int i=0; i<bestSolution.joints.size(); i++)
396 joints.addDouble(bestSolution.joints[i]);
397 Bottle &order=data.addList();
398 order.addString(
"combination");
399 Bottle &combination=order.addList();
400 combination.addInt((
int)combinations.at(winnerIndex)[0]);
401 combination.addInt((
int)combinations.at(winnerIndex)[1]);
402 combination.addInt((
int)combinations.at(winnerIndex)[2]);
406 double HandIKModule::getPeriod()