17 #include "precisionGrasp.h" 32 PrecisionGrasp::PrecisionGrasp() : cloud(new
pcl::PointCloud<
pcl::PointXYZRGB>),
33 cloudxyz(new
pcl::PointCloud<
pcl::PointXYZ>),
34 normals (new
pcl::PointCloud <
pcl::Normal>)
46 fromFileFinished=
false;
53 bestManipulability=0.0;
54 offsetL.resize(3,0.0);
55 offsetR.resize(3,0.0);
56 home_p_l.resize(3,0.0); home_p_l[0]=-0.27; home_p_l[2]=0.1;
57 home_p_r.resize(3,0.0); home_p_r[0]=-0.27; home_p_r[2]=0.1;
58 home_o_l.resize(4,0.0);
59 home_o_r.resize(4,0.0);
61 current_state=STATE_WAIT;
62 visualizationThread=
new VisualizationThread(data);
63 psoThreadFitness1=
new PsoThread();
64 psoThreadFitness2=
new PsoThread();
65 psoThreadFitness3=
new PsoThread();
66 psoThreadFitness4=
new PsoThread();
70 bool PrecisionGrasp::configure(ResourceFinder &rf)
72 radiusSearch=rf.check(
"radiusSearch",Value(0.045)).asDouble();
73 handSize=rf.check(
"limit_finger_max",Value(0.08)).asDouble();
74 double limit_finger_min=rf.check(
"limit_finger_min",Value(0.02)).asDouble();
75 string name=rf.check(
"name",Value(
"precision-grasp")).asString().c_str();
76 string useFile=rf.check(
"fromFile",Value(
"false")).asString().c_str();
77 path=rf.find(
"path").asString().c_str();
78 sampling=rf.check(
"sampling",Value(0.01f)).asDouble();
79 alpha=rf.check(
"alpha",Value(0.9)).asDouble();
80 robot=rf.check(
"robot",Value(
"icub")).asString();
81 double phi_p=rf.check(
"phi_p",Value(0.001)).asDouble();
82 double phi_g=rf.check(
"phi_g",Value(0.001)).asDouble();
83 int iterations=rf.check(
"iterations",Value(2000)).asInt();
84 double hand_area=rf.check(
"hand_area",Value(0.00225)).asDouble();
85 outputFile=rf.find(
"outputFile").asString().c_str();
86 posx=rf.check(
"x",Value(0)).asInt();
87 posy=rf.check(
"y",Value(0)).asInt();
88 visualizationThread->setPosition(posx, posy);
89 sizex=rf.check(
"w",Value(320)).asInt();
90 sizey=rf.check(
"h",Value(240)).asInt();
91 visualizationThread->setSize(sizex, sizey);
93 if (rf.check(
"disableRight"))
95 if (rf.check(
"disableLeft"))
101 Property psoProperty;
102 psoProperty.put(
"particles",20);
103 psoProperty.put(
"omega",1.0);
104 psoProperty.put(
"phi_p",phi_p);
105 psoProperty.put(
"phi_g",phi_g);
106 psoProperty.put(
"iterations",iterations);
107 psoProperty.put(
"limit_finger_max",handSize);
108 psoProperty.put(
"limit_finger_min",limit_finger_min);
109 psoProperty.put(
"hand_area",hand_area);
110 psoProperty.put(
"dimension",9);
112 psoThreadFitness1->open(psoProperty);
113 psoThreadFitness2->open(psoProperty);
114 psoThreadFitness3->open(psoProperty);
115 psoThreadFitness4->open(psoProperty);
117 fromFile=(useFile==
"true");
121 path=rf.find(
"path").asString().c_str();
127 rpc.open((
"/"+name+
"/rpc").c_str());
130 depth2kin.open((
"/"+name+
"/depth2kin:o").c_str());
132 toMatlab.open((
"/"+name+
"/matlab").c_str());
134 if (!ikPort1r.open((
"/"+name+
"/ik1r:o").c_str()))
137 if (!ikPort2r.open((
"/"+name+
"/ik2r:o").c_str()))
140 if (!ikPort3r.open((
"/"+name+
"/ik3r:o").c_str()))
143 if (!ikPort4r.open((
"/"+name+
"/ik4r:o").c_str()))
146 if (!ikPort1l.open((
"/"+name+
"/ik1l:o").c_str()))
149 if (!ikPort2l.open((
"/"+name+
"/ik2l:o").c_str()))
152 if (!ikPort3l.open((
"/"+name+
"/ik3l:o").c_str()))
155 if (!ikPort4l.open((
"/"+name+
"/ik4l:o").c_str()))
158 if (!meshPort.open((
"/"+name+
"/mesh:i").c_str()))
161 if (!reconstructionPort.open((
"/"+name+
"/reconstruction").c_str()))
164 if (!areCmdPort.open((
"/"+name+
"/are/cmd:o").c_str()))
171 Network::connect(ikPort1r.getName().c_str(),
"/handIKModule1/right/rpc");
172 Network::connect(ikPort2r.getName().c_str(),
"/handIKModule2/right/rpc");
173 Network::connect(ikPort3r.getName().c_str(),
"/handIKModule3/right/rpc");
174 Network::connect(ikPort4r.getName().c_str(),
"/handIKModule4/right/rpc");
178 Network::connect(ikPort1l.getName().c_str(),
"/handIKModule1/left/rpc");
179 Network::connect(ikPort2l.getName().c_str(),
"/handIKModule2/left/rpc");
180 Network::connect(ikPort3l.getName().c_str(),
"/handIKModule3/left/rpc");
181 Network::connect(ikPort4l.getName().c_str(),
"/handIKModule4/left/rpc");
189 bool PrecisionGrasp::openDevices()
191 Property optArmRight,optArmLeft;
194 string remoteTorsoName=
"/"+robot+
"/torso";
195 optTorso.put(
"device",
"remote_controlboard");
196 optTorso.put(
"remote",remoteTorsoName.c_str());
197 optTorso.put(
"local",
"/localTorso");
199 robotTorso.open(optTorso);
201 if (!robotTorso.isValid())
203 printf(
"Torso not available\n");
207 robotTorso.view(limTorso);
211 Property optCtrlRight;
212 optCtrlRight.put(
"device",
"cartesiancontrollerclient");
213 optCtrlRight.put(
"remote",(
"/"+robot+
"/cartesianController/right_arm").c_str());
214 optCtrlRight.put(
"local",
"/orientation/right/cartesianRight");
216 if (!dCtrlRight.open(optCtrlRight))
218 fprintf(stdout,
"Right Cartesian Interface is not open\n");
222 dCtrlRight.view(iCtrlRight);
223 iCtrlRight->storeContext(&context_in_right);
224 yarp::sig::Vector dof;
225 iCtrlRight->getDOF(dof);
226 yarp::sig::Vector newDof=dof;
230 iCtrlRight->setDOF(newDof,dof);
231 iCtrlRight->storeContext(&context_right);
232 iCtrlRight->restoreContext(context_in_right);
234 string remoteArmNameRight=
"/"+robot+
"/right_arm";
235 optArmRight.put(
"device",
"remote_controlboard");
236 optArmRight.put(
"remote",remoteArmNameRight.c_str());
237 optArmRight.put(
"local",
"/localArm/right");
239 robotArmRight.open(optArmRight);
241 if (!robotArmRight.isValid())
243 printf(
"Right arm not available\n");
247 robotArmRight.view(limArmRight);
248 robotArmRight.view(posRight);
249 robotArmRight.view(encRight);
251 armRight=
new iCubArm(
"right");
252 chainRight=armRight->asChain();
254 chainRight->releaseLink(0);
255 chainRight->releaseLink(1);
256 chainRight->releaseLink(2);
258 deque<IControlLimits*> limRight;
259 limRight.push_back(limTorso);
260 limRight.push_back(limArmRight);
261 armRight->alignJointsBounds(limRight);
263 armRight->setAllConstraints(
false);
265 thetaMinRight.resize(10,0.0);
266 thetaMaxRight.resize(10,0.0);
267 for (
unsigned int i=0; i< chainRight->getDOF(); i++)
269 thetaMinRight[i]=(*chainRight)(i).getMin();
270 thetaMaxRight[i]=(*chainRight)(i).getMax();
276 Property optCtrlLeft;
277 optCtrlLeft.put(
"device",
"cartesiancontrollerclient");
278 optCtrlLeft.put(
"remote",(
"/"+robot+
"/cartesianController/left_arm").c_str());
279 optCtrlLeft.put(
"local",
"/orientation/left/cartesianLeft");
281 if (!dCtrlLeft.open(optCtrlLeft))
283 fprintf(stdout,
"Left Cartesian Interface is not open\n");
287 dCtrlLeft.view(iCtrlLeft);
288 iCtrlLeft->storeContext(&context_in_left);
289 yarp::sig::Vector dof;
290 yarp::sig::Vector newDof;
291 iCtrlLeft->getDOF(dof);
296 iCtrlLeft->setDOF(newDof,dof);
297 iCtrlLeft->storeContext(&context_left);
298 iCtrlLeft->restoreContext(context_in_left);
300 string remoteArmNameLeft=
"/"+robot+
"/left_arm";
301 optArmLeft.put(
"device",
"remote_controlboard");
302 optArmLeft.put(
"remote",remoteArmNameLeft.c_str());
303 optArmLeft.put(
"local",
"/localArm/left");
305 robotArmLeft.open(optArmLeft);
307 if (!robotArmLeft.isValid())
309 printf(
"Left arm not available\n");
313 robotArmLeft.view(limArmLeft);
314 robotArmLeft.view(posLeft);
315 robotArmLeft.view(encLeft);
317 armLeft=
new iCubArm(
"left");
318 chainLeft=armLeft->asChain();
320 chainLeft->releaseLink(0);
321 chainLeft->releaseLink(1);
322 chainLeft->releaseLink(2);
324 deque<IControlLimits*> limLeft;
325 limLeft.push_back(limTorso);
326 limLeft.push_back(limArmLeft);
327 armLeft->alignJointsBounds(limLeft);
329 armLeft->setAllConstraints(
false);
331 thetaMinLeft.resize(10,0.0);
332 thetaMaxLeft.resize(10,0.0);
333 for (
unsigned int i=0; i< chainLeft->getDOF(); i++)
335 thetaMinLeft[i]=(*chainLeft)(i).getMin();
336 thetaMaxLeft[i]=(*chainLeft)(i).getMax();
344 string PrecisionGrasp::extractData(
const yarp::os::Bottle &data,
const int t)
346 Bottle &b=
const_cast<Bottle&
>(data);
347 string hand=b.find(
"hand").asString().c_str();
348 double cost=b.find(
"cost").asDouble();
349 Bottle *eeB=b.find(
"ee").asList();
350 fillVectorFromBottle(eeB,ee_tmp);
351 Bottle *oright=b.find(
"or").asList();
352 fillVectorFromBottle(oright,axis_angle_tmp);
353 Bottle *j=b.find(
"joints").asList();
354 fillVectorFromBottle(j,joints_tmp);
355 Bottle* combination=b.find(
"combination").asList();
356 fillVectorFromBottle(combination,combination_tmp);
358 yarp::dev::ICartesianControl *iCtrl;
359 iCub::iKin::iCubArm *arm;
360 yarp::sig::Vector *thetaMin=&thetaMinRight;
361 yarp::sig::Vector *thetaMax=&thetaMaxRight;
368 thetaMin=&thetaMinRight;
369 thetaMax=&thetaMaxRight;
375 thetaMin=&thetaMinLeft;
376 thetaMax=&thetaMaxLeft;
379 double manipulability=0.0;
381 iCtrl->askForPose(ee_tmp,axis_angle_tmp,xdhat,odhat,q);
386 yarp::sig::Vector od(3);
387 od[0]=axis_angle_tmp[0]*axis_angle_tmp[3];
388 od[1]=axis_angle_tmp[1]*axis_angle_tmp[3];
389 od[2]=axis_angle_tmp[2]*axis_angle_tmp[3];
391 yarp::sig::Vector odhattmp(3);
392 odhattmp[0]=odhat[0]*odhat[3];
393 odhattmp[1]=odhat[1]*odhat[3];
394 odhattmp[2]=odhat[2]*odhat[3];
396 double xdist=norm(ee_tmp-xdhat);
397 double odist=norm(od-odhattmp);
399 printf(
"\n\nodist %g xdist %g\n", odist, xdist);
401 if (xdist>0.01 || odist>0.05)
407 Matrix jacobian=arm->GeoJacobian();
408 Matrix mulJac=jacobian*(jacobian.transposed());
410 manipulability=sqrt(det(mulJac));
413 for (
unsigned int k=0; k<thetaMin->size(); k++)
415 limits+=(q[k]-((*thetaMin)[k]))*((*thetaMax)[k]-q[k])/(((*thetaMax)[k]-(*thetaMin)[k])*((*thetaMax)[k]-(*thetaMin)[k]));
418 manipulability*=(1-exp(-limits));
421 printf(
"manipulability %g\n", manipulability);
422 printf(
"cost %g\n\n", cost);
425 if (cost<bestCost+toll && manipulability>(bestManipulability-toll))
427 string tag_0=b.get(0).asString().c_str();
429 winner_ov_cones=ov_cones1;
430 else if (tag_0==
"IK2")
431 winner_ov_cones=ov_cones2;
432 else if (tag_0==
"IK3")
433 winner_ov_cones=ov_cones3;
435 winner_ov_cones=ov_cones4;
436 winner_joints=joints_tmp;
438 winner_axis=axis_angle_tmp;
439 winner_combination=combination_tmp;
445 bestManipulability=manipulability;
452 void PrecisionGrasp::fillVectorFromBottle(
const yarp::os::Bottle* b, yarp::sig::Vector &v)
455 for (
int i=0; i<b->size(); i++)
456 v[i]=b->get(i).asDouble();
460 bool PrecisionGrasp::interruptModule()
463 Bottle &out=toMatlab.prepare();
465 out.addString(
"quit");
467 toMatlab.interrupt();
468 ikPort1r.interrupt();
469 ikPort2r.interrupt();
470 ikPort3r.interrupt();
471 ikPort4r.interrupt();
472 ikPort1l.interrupt();
473 ikPort2l.interrupt();
474 ikPort3l.interrupt();
475 ikPort4l.interrupt();
476 depth2kin.interrupt();
477 areCmdPort.interrupt();
478 reconstructionPort.interrupt();
479 meshPort.interrupt();
482 if (psoThreadFitness1->isRunning())
483 psoThreadFitness1->stop();
485 if (psoThreadFitness2->isRunning())
486 psoThreadFitness2->stop();
488 if (psoThreadFitness3->isRunning())
489 psoThreadFitness3->stop();
491 if (psoThreadFitness4->isRunning())
492 psoThreadFitness4->stop();
498 bool PrecisionGrasp::close()
510 reconstructionPort.close();
515 if (graspFileTrain.is_open())
516 graspFileTrain.close();
518 if (visualizationThread->isRunning())
519 visualizationThread->stop();
520 delete visualizationThread;
522 if (psoThreadFitness1->isRunning())
523 psoThreadFitness1->stop();
524 delete psoThreadFitness1;
526 if (psoThreadFitness2->isRunning())
527 psoThreadFitness2->stop();
528 delete psoThreadFitness2;
530 if (psoThreadFitness3->isRunning())
531 psoThreadFitness3->stop();
532 delete psoThreadFitness3;
534 if (psoThreadFitness4->isRunning())
535 psoThreadFitness4->stop();
536 delete psoThreadFitness4;
538 if (dCtrlRight.isValid())
540 iCtrlRight->restoreContext(context_in_right);
543 if (dCtrlLeft.isValid())
545 iCtrlLeft->restoreContext(context_in_left);
548 if (robotArmRight.isValid())
549 robotArmRight.close();
550 if (robotArmLeft.isValid())
551 robotArmLeft.close();
552 if (robotTorso.isValid())
559 void PrecisionGrasp::filter(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_in,pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_in_filtered,
bool second)
561 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_tmp (
new pcl::PointCloud<pcl::PointXYZRGB>);
562 pcl::StatisticalOutlierRemoval<pcl::PointXYZRGB> sor;
563 sor.setInputCloud (cloud_in);
564 sor.setMeanK (cloud_in->size()/2);
565 sor.setStddevMulThresh (1.0);
566 sor.filter (*cloud_in_filtered);
570 void PrecisionGrasp::write(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_in,
const string &fileName)
573 myfile.open (fileName.c_str());
585 for (
int i=0; i<cloud_in->size(); i++)
587 int r=cloud_in->at(i).r;
588 int g=cloud_in->at(i).g;
589 int b=cloud_in->at(i).b;
590 myfile << cloud_in->at(i).x <<
" " << cloud_in->at(i).y <<
" " << cloud_in->at(i).z <<
" " << r <<
" " << g <<
" " << b <<
"\n";
596 void PrecisionGrasp::write(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in, pcl::PointCloud <pcl::Normal>::Ptr n,
const string &fileName)
599 myfile.open (fileName.c_str());
600 for (
int i=0; i<cloud_in->size(); i++)
602 myfile << cloud_in->at(i).x <<
" " << cloud_in->at(i).y <<
" " << cloud_in->at(i).z <<
" " << n->at(i).normal_x <<
" " << n->at(i).normal_y <<
" " << n->at(i).normal_z <<
"\n";
608 void PrecisionGrasp::fromSurfaceMesh (
const SurfaceMeshWithBoundingBox& msg)
613 for (
size_t i = 0; i<msg.mesh.points.size(); ++i)
615 PointXYZRGB pointrgb;
616 pointrgb.x=msg.mesh.points.at(i).x;
617 pointrgb.y=msg.mesh.points.at(i).y;
618 pointrgb.z=msg.mesh.points.at(i).z;
619 if (i<msg.mesh.rgbColour.size())
621 int32_t rgb= msg.mesh.rgbColour.at(i).rgba;
623 pointrgb.r = (rgb >> 16) & 0x0000ff;
624 pointrgb.g = (rgb >> 8) & 0x0000ff;
625 pointrgb.b = (rgb) & 0x0000ff;
634 cloudxyz->push_back(point);
635 cloud->push_back(pointrgb);
638 boundingBox.setBoundingBox(msg.boundingBox);
642 bool PrecisionGrasp::fillCloudFromFile()
644 struct dirent *entry;
647 dp = opendir(path.c_str());
654 while((entry = readdir(dp)))
656 if (strcmp(entry->d_name,
".") == 0 || strcmp(entry->d_name,
"..") == 0)
662 if (entry->d_name!=NULL)
664 pcl::PointCloud<PointXYZRGB>::Ptr cloud_in_rgb (
new pcl::PointCloud<PointXYZRGB>);
666 string root=path+
"/";
667 string name=entry->d_name;
668 string file=root+name;
670 if (loadPLYFile(file.c_str(), *cloud_in_rgb) == -1)
672 cout <<
"cannot read file \n";
678 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_in_filtered (
new pcl::PointCloud<pcl::PointXYZRGB>);
679 filter(cloud_in_rgb,cloud_in_filtered);
680 addPointCloud(cloud_in_filtered);
683 addPointCloud(cloud_in_rgb);
690 void PrecisionGrasp::sampleClouds(pcl::PointCloud<pcl::PointXYZ>::Ptr c, pcl::PointCloud <pcl::Normal>::Ptr n)
692 pcl::octree::OctreePointCloudSearch<pcl::PointXYZ> octree (128.0f);
693 octree.setInputCloud(cloudxyz);
694 octree.addPointsFromInputCloud();
696 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_downsampled (
new pcl::PointCloud<pcl::PointXYZ>);
698 if (cloudxyz->size()<100)
701 pcl::VoxelGrid<pcl::PointXYZ> voxfilter;
702 voxfilter.setInputCloud (cloudxyz);
703 voxfilter.setLeafSize (sampling,sampling,sampling);
704 voxfilter.filter(*cloud_downsampled);
706 yarp::sig::Vector vx,vy,vz; boundingBox.getAxis(vx,vy,vz);
707 double zdim=getZDim(vx,vy,vz);
709 double threshold=center[2]-(zdim/scale);
710 if (zdim<(handSize/2))
711 threshold=center[2]-zdim;
715 for (
int i=0; i<cloud_downsampled->size(); i++)
718 std::vector<int> pointIdxNKNSearch;
719 std::vector<float> pointNKNSquaredDistance;
722 if (octree.nearestKSearch (cloud_downsampled->at(i), k, pointIdxNKNSearch, pointNKNSquaredDistance) > 0)
724 id=pointIdxNKNSearch[0];
725 if (normalPointingOut(normals->at(
id),cloudxyz->at(
id)) && cloudxyz->points.at(
id).z>threshold)
727 c->push_back(cloudxyz->at(
id));
728 n->push_back(normals->at(
id));
732 printf(
"points %d\n", (
int)c->size());
736 bool PrecisionGrasp::updateModule()
738 if ((fromFile && !fromFileFinished) || (current_state==STATE_ESTIMATE))
740 double totTime=Time::now();
744 iCtrlRight->deleteContext(current_context_right);
745 iCtrlRight->storeContext(¤t_context_right);
746 iCtrlRight->restoreContext(context_right);
750 iCtrlLeft->deleteContext(current_context_left);
751 iCtrlLeft->storeContext(¤t_context_left);
752 iCtrlLeft->restoreContext(context_left);
756 if (!fillCloudFromFile())
760 else if (current_state==STATE_ESTIMATE)
762 SurfaceMeshWithBoundingBox *receivedMesh=meshPort.read(
false);
763 if (receivedMesh!=NULL)
765 if (receivedMesh->mesh.points.size()==0)
767 current_state=STATE_WAIT;
770 fromSurfaceMesh(*receivedMesh);
777 write(cloud,outputFile);
779 center=boundingBox.getCenter();
780 dim=boundingBox.getDim();
781 rotation=boundingBox.getOrientation();
786 double timeNormals=Time::now();
787 pcl::search::Search<pcl::PointXYZ>::Ptr tree = boost::shared_ptr<pcl::search::Search<pcl::PointXYZ> > (
new pcl::search::KdTree<pcl::PointXYZ>);
788 pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> normal_estimator;
789 normal_estimator.setSearchMethod (tree);
790 normal_estimator.setRadiusSearch (radiusSearch);
791 normal_estimator.setInputCloud (cloudxyz);
792 normal_estimator.compute (*normals);
793 printf(
"Time for normal estimation %g\n", Time::now()-timeNormals);
795 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_toEvaluate (
new pcl::PointCloud<pcl::PointXYZ>);
796 pcl::PointCloud <pcl::Normal>::Ptr normals_toEvaluate (
new pcl::PointCloud <pcl::Normal>);
798 sampleClouds(cloud_toEvaluate,normals_toEvaluate);
805 if (!fromFile || (fromFile && !fromFileFinished))
808 data.normals=*normals;
809 data.sampled_cloud=*cloud_toEvaluate;
810 data.boundingBox=boundingBox;
811 visualizationThread->start();
817 double alphaToUse=0.8;
818 double t=Time::now();
819 psoThreadFitness1->setData(cloud_toEvaluate,normals_toEvaluate,alphaToUse,0);
820 psoThreadFitness2->setData(cloud_toEvaluate,normals_toEvaluate,alphaToUse,1);
821 psoThreadFitness3->setData(cloud_toEvaluate,normals_toEvaluate,alphaToUse,2);
822 psoThreadFitness4->setData(cloud_toEvaluate,normals_toEvaluate,alphaToUse,3);
825 while(!psoThreadFitness1->checkDone() || !psoThreadFitness2->checkDone() || !psoThreadFitness3->checkDone() || !psoThreadFitness4->checkDone())
828 Bottle req; req.clear();
829 if (psoThreadFitness1->isSuccessful())
834 contacts_r1.push_back(triplet1.c1); contacts_r1.push_back(triplet1.c2); contacts_r1.push_back(triplet1.c3);
836 normals_r1.push_back(triplet1.n1); normals_r1.push_back(triplet1.n2); normals_r1.push_back(triplet1.n3);
838 ov_cones1=triplet1.ov_cones;
840 req=prepareData(triplet1,1);
841 if (ikPort1r.getOutputCount()>0 && !rightBlocked)
846 if (ikPort1l.getOutputCount()>0 && !leftBlocked)
852 if (psoThreadFitness2->isSuccessful())
857 contacts_r2.push_back(triplet2.c1); contacts_r2.push_back(triplet2.c2); contacts_r2.push_back(triplet2.c3);
859 normals_r2.push_back(triplet2.n1); normals_r2.push_back(triplet2.n2); normals_r2.push_back(triplet2.n3);
861 ov_cones2=triplet2.ov_cones;
864 req=prepareData(triplet2,2);
865 if (ikPort2r.getOutputCount()>0 && !rightBlocked)
870 if (ikPort2l.getOutputCount()>0 && !leftBlocked)
876 if (psoThreadFitness3->isSuccessful())
881 contacts_r3.push_back(triplet3.c1); contacts_r3.push_back(triplet3.c2); contacts_r3.push_back(triplet3.c3);
883 normals_r3.push_back(triplet3.n1); normals_r3.push_back(triplet3.n2); normals_r3.push_back(triplet3.n3);
885 ov_cones3=triplet3.ov_cones;
888 req=prepareData(triplet3,3);
889 if (ikPort3r.getOutputCount()>0 && !rightBlocked)
894 if (ikPort3l.getOutputCount()>0 && !leftBlocked)
900 if (psoThreadFitness4->isSuccessful())
906 contacts_r4.push_back(triplet4.c1); contacts_r4.push_back(triplet4.c2); contacts_r4.push_back(triplet4.c3);
908 normals_r4.push_back(triplet4.n1); normals_r4.push_back(triplet4.n2); normals_r4.push_back(triplet4.n3);
910 ov_cones4=triplet4.ov_cones;
913 req=prepareData(triplet4,4);
914 if (ikPort4r.getOutputCount()>0 && !rightBlocked)
919 if (ikPort4l.getOutputCount()>0 && !leftBlocked)
926 if ((!psoThreadFitness1->isSuccessful()) && (!psoThreadFitness2->isSuccessful()) && (!psoThreadFitness3->isSuccessful()) && (!psoThreadFitness4->isSuccessful()) && alphaToUse<=0.9)
929 psoThreadFitness1->setData(cloud_toEvaluate,normals_toEvaluate,alphaToUse,0);
930 psoThreadFitness2->setData(cloud_toEvaluate,normals_toEvaluate,alphaToUse,1);
931 psoThreadFitness3->setData(cloud_toEvaluate,normals_toEvaluate,alphaToUse,2);
932 psoThreadFitness4->setData(cloud_toEvaluate,normals_toEvaluate,alphaToUse,3);
934 else if ((!psoThreadFitness1->isSuccessful()) && (!psoThreadFitness2->isSuccessful()) && (!psoThreadFitness3->isSuccessful()) && (!psoThreadFitness4->isSuccessful()) && alphaToUse>0.9)
938 printf(
"Didn't find any triplet\n");
940 current_state=STATE_WAIT;
942 fromFileFinished=
true;
947 printf(
"Time %g\n", Time::now()-t);
949 current_state=STATE_IK;
955 fromFileFinished=
true;
958 if (current_state==STATE_GRASP)
964 current_state=STATE_WAIT;
971 void PrecisionGrasp::associateDim()
973 yarp::sig::Vector x,y,z;
974 boundingBox.getAxis(x,y,z);
976 yarp::sig::Vector dim_tmp(3,0.0);
978 yarp::sig::Vector tmpx(3,0.0);
981 yarp::sig::Vector tmpy(3,0.0);
984 yarp::sig::Vector tmpz(3,0.0);
987 double dotx1=abs(dot(x,tmpx));
double doty1=abs(dot(x,tmpy));
988 double dotx2=abs(dot(y,tmpx));
double doty2=abs(dot(y,tmpy));
989 double dotx3=abs(dot(z,tmpx));
double doty3=abs(dot(z,tmpy));
991 if (dotx1<dotx2 && dotx1<dotx3)
999 else if (doty2>doty3)
1005 else if (dotx2<dotx1 && dotx2<dotx3)
1013 else if (doty1>doty3)
1027 else if (doty1>doty2)
1038 void PrecisionGrasp::readData(pcl::PointCloud<pcl::PointXYZ>::Ptr c, pcl::PointCloud <pcl::Normal>::Ptr n)
1040 ifstream infile(
"C:\\Users\\Utente\\MATLABdata\\points\\points1.txt");
1041 double c1,c2,c3,n1,n2,n3;
1042 while(infile >> c1 >> c2 >> c3 >> n1 >> n2 >> n3)
1044 pcl::PointXYZ p(c1,c2,c3);
1046 pcl::Normal normal(n1,n2,n3);
1047 n->push_back(normal);
1052 double PrecisionGrasp::getZDim(
const yarp::sig::Vector &vx,
const yarp::sig::Vector &vy,
const yarp::sig::Vector &vz)
1054 yarp::sig::Vector z(3,0.0); z[2]=1.0;
1056 double cx=abs(dot(vx,z));
1057 double cy=abs(dot(vy,z));
1058 double cz=abs(dot(vz,z));
1074 res.addString(
"IK1");
1076 res.addString(
"IK2");
1078 res.addString(
"IK3");
1080 res.addString(
"IK4");
1081 Bottle ¢erB=res.addList();
1082 centerB.addString(
"center");
1083 Bottle ¢er_coord=centerB.addList();
1084 for (
int i=0; i<center.size(); i++)
1085 center_coord.addDouble(center[i]);
1086 Bottle &dimB=res.addList();
1087 dimB.addString(
"dim");
1088 Bottle &dim_coord=dimB.addList();
1089 for (
int i=0; i<dim.size(); i++)
1090 dim_coord.addDouble(dim[i]);
1091 Bottle &c1=res.addList();
1093 Bottle &c1_coord=c1.addList();
1094 for (
int i=0; i<triplet.c1.size(); i++)
1095 c1_coord.addDouble(triplet.c1[i]);
1096 Bottle &c2=res.addList();
1098 Bottle &c2_coord=c2.addList();
1099 for (
int i=0; i<triplet.c2.size(); i++)
1100 c2_coord.addDouble(triplet.c2[i]);
1101 Bottle &c3=res.addList();
1103 Bottle &c3_coord=c3.addList();
1104 for (
int i=0; i<triplet.c3.size(); i++)
1105 c3_coord.addDouble(triplet.c3[i]);
1106 Bottle &n1=res.addList();
1108 Bottle &n1_coord=n1.addList();
1109 for (
int i=0; i<triplet.n1.size(); i++)
1110 n1_coord.addDouble(triplet.n1[i]);
1111 Bottle &n2=res.addList();
1113 Bottle &n2_coord=n2.addList();
1114 for (
int i=0; i<triplet.n2.size(); i++)
1115 n2_coord.addDouble(triplet.n2[i]);
1116 Bottle &n3=res.addList();
1118 Bottle &n3_coord=n3.addList();
1119 for (
int i=0; i<triplet.n3.size(); i++)
1120 n3_coord.addDouble(triplet.n3[i]);
1121 Bottle &rot=res.addList();
1122 rot.addString(
"rot");
1123 Bottle &rot_mat=rot.addList();
1124 for (
int i=0; i<rotation.rows(); i++)
1125 for (
int j=0; j<rotation.cols(); j++)
1126 rot_mat.addDouble(rotation(i,j));
1131 void PrecisionGrasp::askToGrasp()
1134 b.clear(); reply.clear();
1135 b.addString(
"getPoint");
1136 b.addString(winner_hand.c_str());
1137 b.addDouble(winner_ee[0]);
1138 b.addDouble(winner_ee[1]);
1139 b.addDouble(winner_ee[2]);
1141 depth2kin.write(b,reply);
1143 winner_ee[0]=reply.get(1).asDouble();
1144 winner_ee[1]=reply.get(2).asDouble();
1145 winner_ee[2]=reply.get(3).asDouble();
1147 if (winner_hand==
"right")
1149 printf(
"winner %s\n", winner_ee.toString().c_str());
1150 printf(
"offset %s\n", offsetR.toString().c_str());
1155 printf(
"winner %s\n", winner_ee.toString().c_str());
1156 printf(
"offset %s\n", offsetL.toString().c_str());
1162 cmd.addString(
"pgrasp");
1163 Bottle &point=cmd.addList();
1164 point.addDouble(winner_ee[0]);
1165 point.addDouble(winner_ee[1]);
1166 point.addDouble(winner_ee[2]);
1167 point.addDouble(winner_axis[0]);
1168 point.addDouble(winner_axis[1]);
1169 point.addDouble(winner_axis[2]);
1170 point.addDouble(winner_axis[3]);
1171 cmd.addString(winner_hand.c_str());
1173 Bottle &bJoints=cmd.addList();
1174 bJoints.addString(
"joints");
1175 Bottle &pos=bJoints.addList();
1177 yarp::sig::Vector joints(9);
1178 joints[0]=winner_joints[3]*180.0/M_PI;
1179 joints[1]=winner_joints[0]*180.0/M_PI;
1180 joints[2]=winner_joints[1]*180.0/M_PI;
1181 joints[3]=winner_joints[2]*180.0/M_PI;
1182 joints[4]=winner_joints[4]*180.0/M_PI;
1183 joints[5]=winner_joints[5]*180.0/M_PI;
1184 joints[6]=winner_joints[6]*180.0/M_PI;
1185 joints[7]=winner_joints[7]*180.0/M_PI;
1188 for (
size_t i=0; i<joints.size(); i++)
1189 pos.addDouble(joints[i]);
1191 yarp::sig::Vector speed(joints.size(),40.0);
1193 Bottle &bVels=cmd.addList();
1194 bVels.addString(
"vels");
1195 Bottle &v=bVels.addList();
1196 for (
size_t i=0; i<speed.size(); i++)
1197 v.addDouble(speed[i]);
1199 Bottle &bTols=cmd.addList();
1200 bTols.addString(
"tols");
1201 Bottle &t=bTols.addList();
1203 for (
int i=0; i<7; i++)
1207 Bottle &bThresh=cmd.addList();
1208 bThresh.addString(
"thres");
1209 Bottle &th=bThresh.addList();
1210 for (
int i=0; i<5; i++)
1211 th.addDouble(120.0);
1213 Bottle &tmo=cmd.addList();
1214 tmo.addString(
"tmo");
1217 if (areCmdPort.getOutputCount()>0)
1219 areCmdPort.write(cmd,reply);
1220 if (reply.get(0).asString()==
"ack")
1295 void PrecisionGrasp::fillBottleFromVector(
const yarp::sig::Vector &vect, yarp::os::Bottle *b)
1297 for (
unsigned int i=0; i<vect.size(); i++)
1298 b->addDouble(vect[i]);
1302 void PrecisionGrasp::writeBestSolution()
1304 std::vector<yarp::sig::Vector> contacts_r;
1305 std::vector<yarp::sig::Vector> normals_r;
1307 if (winner_triplet==1)
1309 contacts_r=contacts_r1;
1310 normals_r=normals_r1;
1312 else if (winner_triplet==2)
1314 contacts_r=contacts_r2;
1315 normals_r=normals_r2;
1317 else if (winner_triplet==3)
1319 contacts_r=contacts_r3;
1320 normals_r=normals_r3;
1324 contacts_r=contacts_r4;
1325 normals_r=normals_r4;
1328 Bottle &out=toMatlab.prepare();
1330 out.addString(
"best");
1331 Bottle &res=out.addList();
1332 Bottle &joints=res.addList();
1333 joints.addString(
"joints");
1334 Bottle &jointsVal=joints.addList();
1335 fillBottleFromVector(winner_joints, &jointsVal);
1337 Bottle &ee=res.addList();
1339 Bottle &eeVal=ee.addList();
1340 fillBottleFromVector(winner_ee, &eeVal);
1342 Bottle &xdes=res.addList();
1343 xdes.addString(
"xdhat");
1344 Bottle &xdesVal=xdes.addList();
1345 fillBottleFromVector(winner_xdhat, &xdesVal);
1347 Bottle &axisangle=res.addList();
1348 axisangle.addString(
"axisangle");
1349 Bottle &axisangleVal=axisangle.addList();
1350 fillBottleFromVector(winner_axis, &axisangleVal);
1352 Bottle &odes=res.addList();
1353 odes.addString(
"odhat");
1354 Bottle &odesVal=odes.addList();
1355 fillBottleFromVector(winner_odhat, &odesVal);
1357 Bottle &c=res.addList();
1358 c.addString(
"center");
1359 Bottle ¢erValue=c.addList();
1360 fillBottleFromVector(center, ¢erValue);
1362 Bottle &d=res.addList();
1364 Bottle &dimValue=d.addList();
1365 fillBottleFromVector(dim, &dimValue);
1367 Bottle &c1=res.addList();
1369 Bottle &c1Value=c1.addList();
1370 c1Value.addDouble(contacts_r.at(winner_combination[0])[0]);
1371 c1Value.addDouble(contacts_r.at(winner_combination[0])[1]);
1372 c1Value.addDouble(contacts_r.at(winner_combination[0])[2]);
1374 Bottle &c2=res.addList();
1376 Bottle &c2Value=c2.addList();
1377 c2Value.addDouble(contacts_r.at(winner_combination[1])[0]);
1378 c2Value.addDouble(contacts_r.at(winner_combination[1])[1]);
1379 c2Value.addDouble(contacts_r.at(winner_combination[1])[2]);
1381 Bottle &c3=res.addList();
1383 Bottle &c3Value=c3.addList();
1384 c3Value.addDouble(contacts_r.at(winner_combination[2])[0]);
1385 c3Value.addDouble(contacts_r.at(winner_combination[2])[1]);
1386 c3Value.addDouble(contacts_r.at(winner_combination[2])[2]);
1388 Bottle &n1=res.addList();
1390 Bottle &n1Value=n1.addList();
1391 n1Value.addDouble(normals_r.at(winner_combination[0])[0]);
1392 n1Value.addDouble(normals_r.at(winner_combination[0])[1]);
1393 n1Value.addDouble(normals_r.at(winner_combination[0])[2]);
1395 Bottle &n2=res.addList();
1397 Bottle &n2Value=n2.addList();
1398 n2Value.addDouble(normals_r.at(winner_combination[1])[0]);
1399 n2Value.addDouble(normals_r.at(winner_combination[1])[1]);
1400 n2Value.addDouble(normals_r.at(winner_combination[1])[2]);
1402 Bottle &n3=res.addList();
1404 Bottle &n3Value=n3.addList();
1405 n3Value.addDouble(normals_r.at(winner_combination[2])[0]);
1406 n3Value.addDouble(normals_r.at(winner_combination[2])[1]);
1407 n3Value.addDouble(normals_r.at(winner_combination[2])[2]);
1409 Bottle &r=res.addList();
1410 r.addString(
"rotmat");
1411 Bottle &rotValue=r.addList();
1412 for (
int i=0; i<rotation.rows(); i++)
1414 for (
int j=0; j<rotation.cols(); j++)
1416 rotValue.addDouble(rotation(i,j));
1420 Bottle &h=res.addList();
1421 h.addString(
"hand");
1422 Bottle &hValue=h.addList();
1423 if (winner_hand==
"right")
1424 hValue.addString(
"r");
1426 hValue.addString(
"l");
1428 Bottle &cl=res.addList();
1429 cl.addString(
"cloud");
1430 Bottle &clValue=cl.addList();
1431 for (
int i=0; i<cloud->size(); i++)
1433 Bottle &point=clValue.addList();
1434 point.addDouble(cloud->at(i).x);
1435 point.addDouble(cloud->at(i).y);
1436 point.addDouble(cloud->at(i).z);
1437 point.addDouble(cloud->at(i).r);
1438 point.addDouble(cloud->at(i).g);
1439 point.addDouble(cloud->at(i).b);
1441 if (toMatlab.getOutputCount()>0)
1442 toMatlab.writeStrict();
1446 void PrecisionGrasp::writeToMatlab(
const std::vector<yarp::sig::Vector> &contacts_r,
const std::vector<yarp::sig::Vector> &normals_r,
const std::string &hand)
1448 Bottle &out=toMatlab.prepare();
1450 Bottle &res=out.addList();
1451 Bottle &joints=res.addList();
1452 joints.addString(
"joints");
1453 Bottle &jointsVal=joints.addList();
1454 fillBottleFromVector(joints_tmp, &jointsVal);
1456 Bottle &ee=res.addList();
1458 Bottle &eeVal=ee.addList();
1459 fillBottleFromVector(ee_tmp, &eeVal);
1461 Bottle &axisangle=res.addList();
1462 axisangle.addString(
"axisangle");
1463 Bottle &axisangleVal=axisangle.addList();
1464 fillBottleFromVector(axis_angle_tmp, &axisangleVal);
1466 Bottle &c=res.addList();
1467 c.addString(
"center");
1468 Bottle ¢erValue=c.addList();
1469 fillBottleFromVector(center, ¢erValue);
1471 Bottle &d=res.addList();
1473 Bottle &dimValue=d.addList();
1474 fillBottleFromVector(dim, &dimValue);
1476 Bottle &c1=res.addList();
1478 Bottle &c1Value=c1.addList();
1479 c1Value.addDouble(contacts_r.at(combination_tmp[0])[0]);
1480 c1Value.addDouble(contacts_r.at(combination_tmp[0])[1]);
1481 c1Value.addDouble(contacts_r.at(combination_tmp[0])[2]);
1483 Bottle &c2=res.addList();
1485 Bottle &c2Value=c2.addList();
1486 c2Value.addDouble(contacts_r.at(combination_tmp[1])[0]);
1487 c2Value.addDouble(contacts_r.at(combination_tmp[1])[1]);
1488 c2Value.addDouble(contacts_r.at(combination_tmp[1])[2]);
1490 Bottle &c3=res.addList();
1492 Bottle &c3Value=c3.addList();
1493 c3Value.addDouble(contacts_r.at(combination_tmp[2])[0]);
1494 c3Value.addDouble(contacts_r.at(combination_tmp[2])[1]);
1495 c3Value.addDouble(contacts_r.at(combination_tmp[2])[2]);
1497 Bottle &n1=res.addList();
1499 Bottle &n1Value=n1.addList();
1500 n1Value.addDouble(normals_r.at(combination_tmp[0])[0]);
1501 n1Value.addDouble(normals_r.at(combination_tmp[0])[1]);
1502 n1Value.addDouble(normals_r.at(combination_tmp[0])[2]);
1504 Bottle &n2=res.addList();
1506 Bottle &n2Value=n2.addList();
1507 n2Value.addDouble(normals_r.at(combination_tmp[1])[0]);
1508 n2Value.addDouble(normals_r.at(combination_tmp[1])[1]);
1509 n2Value.addDouble(normals_r.at(combination_tmp[1])[2]);
1511 Bottle &n3=res.addList();
1513 Bottle &n3Value=n3.addList();
1514 n3Value.addDouble(normals_r.at(combination_tmp[2])[0]);
1515 n3Value.addDouble(normals_r.at(combination_tmp[2])[1]);
1516 n3Value.addDouble(normals_r.at(combination_tmp[2])[2]);
1518 Bottle &r=res.addList();
1519 r.addString(
"rotmat");
1520 Bottle &rotValue=r.addList();
1521 for (
int i=0; i<rotation.rows(); i++)
1523 for (
int j=0; j<rotation.cols(); j++)
1525 rotValue.addDouble(rotation(i,j));
1529 Bottle &h=res.addList();
1530 h.addString(
"hand");
1531 Bottle &hValue=h.addList();
1533 hValue.addString(
"r");
1535 hValue.addString(
"l");
1537 Bottle &cl=res.addList();
1538 cl.addString(
"cloud");
1539 Bottle &clValue=cl.addList();
1540 for (
int i=0; i<cloud->size(); i++)
1542 Bottle &point=clValue.addList();
1543 point.addDouble(cloud->at(i).x);
1544 point.addDouble(cloud->at(i).y);
1545 point.addDouble(cloud->at(i).z);
1546 point.addDouble(cloud->at(i).r);
1547 point.addDouble(cloud->at(i).g);
1548 point.addDouble(cloud->at(i).b);
1550 if (toMatlab.getOutputCount()>0)
1551 toMatlab.writeStrict();
1555 void PrecisionGrasp::writeIKBestresult()
1557 std::vector<yarp::sig::Vector> contacts_r;
1558 std::vector<yarp::sig::Vector> normals_r;
1560 if (winner_triplet==1)
1562 contacts_r=contacts_r1;
1563 normals_r=normals_r1;
1565 else if (winner_triplet==2)
1567 contacts_r=contacts_r2;
1568 normals_r=normals_r2;
1570 else if (winner_triplet==3)
1572 contacts_r=contacts_r3;
1573 normals_r=normals_r3;
1577 contacts_r=contacts_r4;
1578 normals_r=normals_r4;
1581 std::string filename=
"C:/Users/Utente/Desktop/IKResults/BestIKresult.txt";
1583 file.open(filename.c_str());
1585 for (
unsigned int i=0; i<winner_joints.size(); i++)
1586 file << winner_joints[i] <<
" ";
1588 file <<
"ee=[" << winner_ee[0] <<
" " << winner_ee[1] <<
" " << winner_ee[2] <<
"];\n";
1589 file <<
"xdhat=[" << winner_xdhat[0] <<
" " << winner_xdhat[1] <<
" " << winner_xdhat[2] <<
"];\n";
1590 file <<
"axisangle=[" << winner_axis[0] <<
" " << winner_axis[1] <<
" " << winner_axis[2] <<
" " << winner_axis[3] <<
"];\n";
1591 file <<
"odhat=[" << winner_odhat[0] <<
" " << winner_odhat[1] <<
" " << winner_odhat[2] <<
" " << winner_odhat[3] <<
"];\n";
1592 file <<
"center=[" << center[0] <<
" " << center[1] <<
" " << center[2] <<
"];\n";
1593 file <<
"dim=[" << dim[0] <<
" " << dim[1] <<
" " << dim[2] <<
"];\n";
1594 file <<
"c1=[" << contacts_r.at(winner_combination[0])[0] <<
" " << contacts_r.at(winner_combination[0])[1] <<
" " << contacts_r.at(winner_combination[0])[2] <<
"];\n";
1595 file <<
"c2=[" << contacts_r.at(winner_combination[1])[0] <<
" " << contacts_r.at(winner_combination[1])[1] <<
" " << contacts_r.at(winner_combination[1])[2] <<
"];\n";
1596 file <<
"c3=[" << contacts_r.at(winner_combination[2])[0] <<
" " << contacts_r.at(winner_combination[2])[1] <<
" " << contacts_r.at(winner_combination[2])[2] <<
"];\n";
1597 file <<
"n1=[" << normals_r.at(winner_combination[0])[0] <<
" " << normals_r.at(winner_combination[0])[1] <<
" " << normals_r.at(winner_combination[0])[2] <<
"];\n";
1598 file <<
"n2=[" << normals_r.at(winner_combination[1])[0] <<
" " << normals_r.at(winner_combination[1])[1] <<
" " << normals_r.at(winner_combination[1])[2] <<
"];\n";
1599 file <<
"n3=[" << normals_r.at(winner_combination[2])[0] <<
" " << normals_r.at(winner_combination[2])[1] <<
" " << normals_r.at(winner_combination[2])[2] <<
"];\n";
1601 for (
int i=0; i<rotation.rows(); i++)
1603 for (
int j=0; j<rotation.cols(); j++)
1605 file << rotation(i,j);
1606 if (j!=rotation.cols()-1)
1609 if (i!=rotation.rows()-1)
1614 if (winner_hand==
"right")
1615 file <<
"hand='r';\n";
1617 file <<
"hand='l';\n";
1619 file <<
"ov_cones " << winner_ov_cones <<
";\n";
1621 file <<
"center=(" << center[0] <<
" " << center[1] <<
" " << center[2] <<
"];\n";
1622 file <<
"dim=(" << dim[0] <<
" " << dim[1] <<
" " << dim[2] <<
"];\n";
1623 file <<
"c1=(" << contacts_r.at(winner_combination[0])[0] <<
" " << contacts_r.at(winner_combination[0])[1] <<
" " << contacts_r.at(winner_combination[0])[2] <<
")\n";
1624 file <<
"c2=(" << contacts_r.at(winner_combination[1])[0] <<
" " << contacts_r.at(winner_combination[1])[1] <<
" " << contacts_r.at(winner_combination[1])[2] <<
")\n";
1625 file <<
"c3=(" << contacts_r.at(winner_combination[2])[0] <<
" " << contacts_r.at(winner_combination[2])[1] <<
" " << contacts_r.at(winner_combination[2])[2] <<
")\n";
1626 file <<
"n1=(" << normals_r.at(winner_combination[0])[0] <<
" " << normals_r.at(winner_combination[0])[1] <<
" " << normals_r.at(winner_combination[0])[2] <<
")\n";
1627 file <<
"n2=(" << normals_r.at(winner_combination[1])[0] <<
" " << normals_r.at(winner_combination[1])[1] <<
" " << normals_r.at(winner_combination[1])[2] <<
")\n";
1628 file <<
"n3=(" << normals_r.at(winner_combination[2])[0] <<
" " << normals_r.at(winner_combination[2])[1] <<
" " << normals_r.at(winner_combination[2])[2] <<
")\n";
1635 void PrecisionGrasp::writeIKresult(
const std::vector<yarp::sig::Vector> &contacts_r,
const std::vector<yarp::sig::Vector> &normals_r,
const int c,
const string &hand,
const int ov_cones)
1639 std::string filename=
"C:/Users/Utente/Desktop/IKResults/IKresult_"+hand+ss.str()+
".txt";
1641 file.open(filename.c_str());
1643 for (
unsigned int i=0; i<joints_tmp.size(); i++)
1644 file << joints_tmp[i] <<
" ";
1646 file <<
"ee=[" << ee_tmp[0] <<
" " << ee_tmp[1] <<
" " << ee_tmp[2] <<
"];\n";
1647 file <<
"axisangle=[" << axis_angle_tmp[0] <<
" " << axis_angle_tmp[1] <<
" " << axis_angle_tmp[2] <<
" " << axis_angle_tmp[3] <<
"];\n";
1648 file <<
"center=[" << center[0] <<
" " << center[1] <<
" " << center[2] <<
"];\n";
1649 file <<
"dim=[" << dim[0] <<
" " << dim[1] <<
" " << dim[2] <<
"];\n";
1650 file <<
"c1=[" << contacts_r.at(combination_tmp[0])[0] <<
" " << contacts_r.at(combination_tmp[0])[1] <<
" " << contacts_r.at(combination_tmp[0])[2] <<
"];\n";
1651 file <<
"c2=[" << contacts_r.at(combination_tmp[1])[0] <<
" " << contacts_r.at(combination_tmp[1])[1] <<
" " << contacts_r.at(combination_tmp[1])[2] <<
"];\n";
1652 file <<
"c3=[" << contacts_r.at(combination_tmp[2])[0] <<
" " << contacts_r.at(combination_tmp[2])[1] <<
" " << contacts_r.at(combination_tmp[2])[2] <<
"];\n";
1653 file <<
"n1=[" << normals_r.at(combination_tmp[0])[0] <<
" " << normals_r.at(combination_tmp[0])[1] <<
" " << normals_r.at(combination_tmp[0])[2] <<
"];\n";
1654 file <<
"n2=[" << normals_r.at(combination_tmp[1])[0] <<
" " << normals_r.at(combination_tmp[1])[1] <<
" " << normals_r.at(combination_tmp[1])[2] <<
"];\n";
1655 file <<
"n3=[" << normals_r.at(combination_tmp[2])[0] <<
" " << normals_r.at(combination_tmp[2])[1] <<
" " << normals_r.at(combination_tmp[2])[2] <<
"];\n";
1657 for (
int i=0; i<rotation.rows(); i++)
1659 for (
int j=0; j<rotation.cols(); j++)
1661 file << rotation(i,j);
1662 if (j!=rotation.cols()-1)
1665 if (i!=rotation.rows()-1)
1671 file <<
"hand='r';\n";
1673 file <<
"hand='l';\n";
1675 file <<
"ov_cones " << ov_cones <<
";\n";
1677 file <<
"center=(" << center[0] <<
" " << center[1] <<
" " << center[2] <<
")\n";
1678 file <<
"dim=(" << dim[0] <<
" " << dim[1] <<
" " << dim[2] <<
")\n";
1679 file <<
"c1=(" << contacts_r.at(combination_tmp[0])[0] <<
" " << contacts_r.at(combination_tmp[0])[1] <<
" " << contacts_r.at(combination_tmp[0])[2] <<
")\n";
1680 file <<
"c2=(" << contacts_r.at(combination_tmp[1])[0] <<
" " << contacts_r.at(combination_tmp[1])[1] <<
" " << contacts_r.at(combination_tmp[1])[2] <<
")\n";
1681 file <<
"c3=(" << contacts_r.at(combination_tmp[2])[0] <<
" " << contacts_r.at(combination_tmp[2])[1] <<
" " << contacts_r.at(combination_tmp[2])[2] <<
")\n";
1682 file <<
"n1=(" << normals_r.at(combination_tmp[0])[0] <<
" " << normals_r.at(combination_tmp[0])[1] <<
" " << normals_r.at(combination_tmp[0])[2] <<
")\n";
1683 file <<
"n2=(" << normals_r.at(combination_tmp[1])[0] <<
" " << normals_r.at(combination_tmp[1])[1] <<
" " << normals_r.at(combination_tmp[1])[2] <<
")\n";
1684 file <<
"n3=(" << normals_r.at(combination_tmp[2])[0] <<
" " << normals_r.at(combination_tmp[2])[1] <<
" " << normals_r.at(combination_tmp[2])[2] <<
")\n";
1691 bool PrecisionGrasp::respond(
const Bottle& command, Bottle& reply)
1693 string tag_0=command.get(0).asString().c_str();
1696 if (command.size()<3)
1698 reply.addString(
"nack, command not recognized");
1701 string tag_1=command.get(1).asString().c_str();
1702 if (tag_1==
"visualization")
1704 if (command.get(2).asString()==
"on")
1708 reply.addString(
"ack");
1711 else if (tag_1==
"filter")
1713 if (command.get(2).asString()==
"on")
1717 reply.addString(
"ack");
1720 else if (tag_1==
"x")
1722 if (command.size()>1)
1724 posx=command.get(1).asInt();
1725 visualizationThread->setPosition(posx,posy);
1726 reply.addString(
"ack");
1729 reply.addString(
"nack");
1732 else if (tag_1==
"y")
1734 if (command.size()>1)
1736 posy=command.get(1).asInt();
1737 visualizationThread->setPosition(posx,posy);
1738 reply.addString(
"ack");
1741 reply.addString(
"nack");
1744 else if (tag_1==
"w")
1746 if (command.size()>1)
1748 sizex=command.get(1).asInt();
1749 visualizationThread->setSize(sizex,sizey);
1750 reply.addString(
"ack");
1753 reply.addString(
"nack");
1756 else if (tag_1==
"h")
1758 if (command.size()>1)
1760 sizey=command.get(1).asInt();
1761 visualizationThread->setSize(sizex,sizey);
1762 reply.addString(
"ack");
1765 reply.addString(
"nack");
1768 else if (tag_1==
"write")
1770 if (command.get(2).asString()==
"on")
1774 reply.addString(
"ack");
1777 else if (tag_1==
"offsetR")
1779 offsetR[0]=command.get(2).asDouble();
1780 offsetR[1]=command.get(3).asDouble();
1781 offsetR[2]=command.get(4).asDouble();
1782 reply.addString(
"ack");
1785 else if (tag_1==
"offsetL")
1787 offsetL[0]=command.get(2).asDouble();
1788 offsetL[1]=command.get(3).asDouble();
1789 offsetL[2]=command.get(4).asDouble();
1790 reply.addString(
"ack");
1794 if (tag_0==
"IK1" || tag_0==
"IK2" || tag_0==
"IK3" || tag_0==
"IK4")
1796 if (current_state==STATE_IK)
1799 reply.addString(
"ack");
1802 mutex_to_write.wait();
1803 string hand=extractData(command,1);
1805 writeIKresult(contacts_r1,normals_r1,1,hand,ov_cones1);
1806 mutex_to_write.post();
1808 else if (tag_0==
"IK2")
1810 mutex_to_write.wait();
1811 string hand=extractData(command,2);
1813 writeIKresult(contacts_r2,normals_r2,2,hand,ov_cones2);
1814 mutex_to_write.post();
1816 else if (tag_0==
"IK3")
1818 mutex_to_write.wait();
1819 string hand=extractData(command,3);
1821 writeIKresult(contacts_r3,normals_r3,3,hand,ov_cones3);
1822 mutex_to_write.post();
1826 mutex_to_write.wait();
1827 string hand=extractData(command,4);
1829 writeIKresult(contacts_r4,normals_r4,4,hand,ov_cones4);
1830 mutex_to_write.post();
1836 writeBestSolution();
1837 writeIKBestresult();
1842 bestManipulability=0.0;
1844 iCtrlRight->restoreContext(current_context_right);
1845 iCtrlLeft->restoreContext(current_context_left);
1848 if (!dont && straight)
1850 current_state=STATE_GRASP;
1854 current_state=STATE_WAIT;
1859 reply.addString(
"nack");
1866 current_state=STATE_GRASP;
1869 reply.addString(
"ack");
1872 reply.addString(
"nack");
1877 reply.addString(
"set visualization on/off");
1878 reply.addString(
"set x");
1879 reply.addString(
"set y");
1880 reply.addString(
"set w");
1881 reply.addString(
"set h");
1882 reply.addString(
"set offsetL x y z");
1883 reply.addString(
"set offsetR x y z");
1884 reply.addString(
"set filter on/off");
1885 reply.addString(
"set write on/off");
1886 reply.addString(
"block right/left");
1887 reply.addString(
"unblock right/left");
1888 reply.addString(
"grasp (x y) [wait]");
1889 reply.addString(
"go");
1890 reply.addString(
"dont");
1891 reply.addString(
"isGrasped");
1896 if (command.size()>=2)
1898 if (command.get(1).asString()==
"right")
1900 Network::disconnect(ikPort1r.getName().c_str(),
"/handIKModule1/right/rpc");
1901 Network::disconnect(ikPort2r.getName().c_str(),
"/handIKModule2/right/rpc");
1902 Network::disconnect(ikPort3r.getName().c_str(),
"/handIKModule3/right/rpc");
1903 Network::disconnect(ikPort4r.getName().c_str(),
"/handIKModule4/right/rpc");
1908 Network::disconnect(ikPort1l.getName().c_str(),
"/handIKModule1/left/rpc");
1909 Network::disconnect(ikPort2l.getName().c_str(),
"/handIKModule2/left/rpc");
1910 Network::disconnect(ikPort3l.getName().c_str(),
"/handIKModule3/left/rpc");
1911 Network::disconnect(ikPort4l.getName().c_str(),
"/handIKModule4/left/rpc");
1914 reply.addString(
"ack");
1919 reply.addString(
"nack");
1923 if (tag_0==
"unblock")
1925 if (command.size()>=2)
1927 if (command.get(1).asString()==
"right")
1929 Network::connect(ikPort1r.getName().c_str(),
"/handIKModule1/right/rpc");
1930 Network::connect(ikPort2r.getName().c_str(),
"/handIKModule2/right/rpc");
1931 Network::connect(ikPort3r.getName().c_str(),
"/handIKModule3/right/rpc");
1932 Network::connect(ikPort4r.getName().c_str(),
"/handIKModule4/right/rpc");
1937 Network::connect(ikPort1l.getName().c_str(),
"/handIKModule1/left/rpc");
1938 Network::connect(ikPort2l.getName().c_str(),
"/handIKModule2/left/rpc");
1939 Network::connect(ikPort3l.getName().c_str(),
"/handIKModule3/left/rpc");
1940 Network::connect(ikPort4l.getName().c_str(),
"/handIKModule4/left/rpc");
1943 reply.addString(
"ack");
1948 reply.addString(
"nack");
1952 if (tag_0==
"isGrasped")
1954 string r=grasped?
"true":
"false";
1955 reply.addString(r.c_str());
1962 reply.addString(
"ack");
1963 current_state=STATE_WAIT;
1968 if (current_state==STATE_ESTIMATE || current_state==STATE_IK || current_state==STATE_GRASP)
1970 reply.addString(
"nack");
1974 if (visualizationThread->isRunning())
1975 visualizationThread->stop();
1981 Bottle *pos=command.get(1).asList();
1985 cmd1.addInt(pos->get(0).asInt());
1986 cmd1.addInt(pos->get(1).asInt());
1988 if (reconstructionPort.getOutputCount()>0)
1989 reconstructionPort.write(cmd1,reply);
1993 cmd2.addString(
"3Drec");
1995 if (reconstructionPort.getOutputCount()>0)
1996 reconstructionPort.write(cmd2,reply);
1998 if (reply.size()>0 && reply.get(0).asString()==
"ack")
2001 current_state=STATE_ESTIMATE;
2003 if (command.size()>=2)
2004 straight=(command.get(2).asString()!=
"wait");
2011 reply.addString(
"ack");
2013 reply.addString(
"nack");
2016 reply.addString(
"nack");
2020 reply.addString(
"nack");
2025 void PrecisionGrasp::addPointCloud(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_in)
2030 for (
size_t j = 0; j < cloud_in->points.size (); ++j)
2032 PointXYZRGB pointrgb=cloud_in->points[j];
2033 pcl::PointXYZ point;
2037 cloudxyz->push_back(point);
2038 cloud->push_back(pointrgb);
2043 double PrecisionGrasp::getPeriod()
2049 bool PrecisionGrasp::normalPointingOut(pcl::Normal &normal, pcl::PointXYZ &point)
2051 yarp::sig::Vector p(3);
2052 p[0]=point.x-(normal.normal_x*0.003);
2053 p[1]=point.y-(normal.normal_y*0.003);
2054 p[2]=point.z-(normal.normal_z*0.003);
2056 yarp::sig::Vector fromCenter=(center-p)/norm(center-p);
2058 yarp::sig::Vector n(3);
2059 n[0]=normal.normal_x;
2060 n[1]=normal.normal_y;
2061 n[2]=normal.normal_z;
2064 return (dot(n,fromCenter)<0);
iCub::data3D::BoundingBox getMinimumBoundingBox(pcl::PointCloud< pcl::PointXYZRGB >::Ptr cloud)
Given a point cloud (defined in the Point Cloud Library), it computes the minimum enclosing bounding ...