17 #ifndef __PRECISION_GRASP_H__ 18 #define __PRECISION_GRASP_H__ 28 #include <yarp/os/RFModule.h> 29 #include <yarp/os/BufferedPort.h> 30 #include <yarp/os/ConnectionReader.h> 31 #include <yarp/sig/Vector.h> 32 #include <yarp/sig/Matrix.h> 33 #include <yarp/math/Math.h> 34 #include <yarp/os/Time.h> 35 #include <yarp/os/Random.h> 36 #include <yarp/os/Semaphore.h> 37 #include <yarp/os/Event.h> 38 #include <yarp/dev/CartesianControl.h> 39 #include <yarp/dev/ControlBoardInterfaces.h> 40 #include <yarp/dev/PolyDriver.h> 41 #include <iCub/iKin/iKinFwd.h> 42 #include <pcl/common/common_headers.h> 43 #include <pcl/features/normal_3d.h> 44 #include <pcl/io/ply_io.h> 45 #include <pcl/filters/statistical_outlier_removal.h> 46 #include <pcl/visualization/pcl_visualizer.h> 47 #include <pcl/filters/voxel_grid.h> 48 #include <pcl/octree/octree.h> 49 #include <boost/thread/thread.hpp> 50 #include <iCub/data3D/SurfaceMeshWithBoundingBox.h> 51 #include <iCub/grasp/forceClosure.h> 52 #include <visualizationThread.h> 53 #include <psoThread.h> 56 #include "custom/dirent.h" 60 #define STATE_ESTIMATE 1 63 #define RIGHT_HAND "right" 64 #define LEFT_HAND "left" 65 #define NO_HAND "no_hand" 67 class PrecisionGrasp:
public yarp::os::RFModule
75 int current_context_right;
78 int current_context_left;
91 bool fromFileFinished;
108 double bestManipulability;
110 std::string filenameTrain;
112 std::string outputFile;
113 ofstream graspFileTrain;
114 std::string winner_hand;
116 std::vector<yarp::sig::Vector> contacts_r1;
117 std::vector<yarp::sig::Vector> normals_r1;
118 std::vector<yarp::sig::Vector> contacts_r2;
119 std::vector<yarp::sig::Vector> normals_r2;
120 std::vector<yarp::sig::Vector> contacts_r3;
121 std::vector<yarp::sig::Vector> normals_r3;
122 std::vector<yarp::sig::Vector> contacts_r4;
123 std::vector<yarp::sig::Vector> normals_r4;
124 yarp::sig::Vector joints_tmp;
125 yarp::sig::Vector winner_joints;
126 yarp::sig::Vector ee_tmp;
127 yarp::sig::Vector winner_ee;
128 yarp::sig::Vector axis_angle_tmp;
129 yarp::sig::Vector winner_axis;
130 yarp::sig::Vector combination_tmp;
131 yarp::sig::Vector winner_combination;
132 yarp::sig::Vector winner_odhat;
133 yarp::sig::Vector winner_xdhat;
134 yarp::sig::Vector center;
135 yarp::sig::Vector dim;
136 yarp::sig::Matrix rotation;
137 yarp::sig::Vector xdhat,odhat;
139 yarp::sig::Vector offsetR;
140 yarp::sig::Vector offsetL;
141 yarp::sig::Vector home_p_l;
142 yarp::sig::Vector home_p_r;
143 yarp::sig::Vector home_o_l;
144 yarp::sig::Vector home_o_r;
146 yarp::dev::PolyDriver dCtrlRight, dCtrlLeft;
147 yarp::dev::ICartesianControl *iCtrlRight, *iCtrlLeft;
148 yarp::dev::IPositionControl *posRight, *posLeft;
149 yarp::dev::IEncoders *encRight, *encLeft;
150 yarp::dev::PolyDriver robotArmRight, robotArmLeft;
151 yarp::dev::PolyDriver robotTorso;
152 yarp::dev::IControlLimits *limTorso, *limArmRight, *limArmLeft;
154 iCub::iKin::iCubArm *armRight, *armLeft;
155 iCub::iKin::iKinChain *chainRight, *chainLeft;
157 yarp::sig::Vector thetaMinRight, thetaMinLeft;
158 yarp::sig::Vector thetaMaxRight, thetaMaxLeft;
160 VisualizationThread *visualizationThread;
161 PsoThread* psoThreadFitness1;
162 PsoThread* psoThreadFitness2;
163 PsoThread* psoThreadFitness3;
164 PsoThread* psoThreadFitness4;
167 pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud;
168 pcl::PointCloud<pcl::PointXYZ>::Ptr cloudxyz;
169 pcl::PointCloud <pcl::Normal>::Ptr normals;
171 yarp::os::Port reconstructionPort;
172 yarp::os::Port ikPort1r;
173 yarp::os::Port ikPort2r;
174 yarp::os::Port ikPort3r;
175 yarp::os::Port ikPort4r;
176 yarp::os::Port ikPort1l;
177 yarp::os::Port ikPort2l;
178 yarp::os::Port ikPort3l;
179 yarp::os::Port ikPort4l;
180 yarp::os::Port areCmdPort;
181 yarp::os::Port depth2kin;
182 yarp::os::BufferedPort<iCub::data3D::SurfaceMeshWithBoundingBox> meshPort;
184 yarp::os::BufferedPort<yarp::os::Bottle> toMatlab;
186 yarp::os::Semaphore mutex;
187 yarp::os::Semaphore mutex_to_write;
188 yarp::os::Event eventRpc;
192 double min(
const double a,
const double b) {
return (a>b)?b:a;};
193 double max(
const double a,
const double b) {
return (a<b)?b:a;};
195 void addPointCloud(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_in);
196 void addPlanePoints();
197 void filter(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_in,pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_in_filtered,
bool second=
false);
198 void write(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_in,
const std::string &fileName);
199 bool normalPointingOut(pcl::Normal &normal, pcl::PointXYZ &point);
200 bool respond(
const yarp::os::Bottle& command, yarp::os::Bottle& reply);
201 void fromSurfaceMesh(
const iCub::data3D::SurfaceMeshWithBoundingBox& msg);
202 bool fillCloudFromFile();
205 std::string extractData(
const yarp::os::Bottle &data,
const int t);
206 void fillVectorFromBottle(
const yarp::os::Bottle* b, yarp::sig::Vector &v);
207 double getZDim(
const yarp::sig::Vector &vx,
const yarp::sig::Vector &vy,
const yarp::sig::Vector &vz);
208 void readData(pcl::PointCloud<pcl::PointXYZ>::Ptr c, pcl::PointCloud <pcl::Normal>::Ptr n);
209 void sampleClouds(pcl::PointCloud<pcl::PointXYZ>::Ptr c, pcl::PointCloud <pcl::Normal>::Ptr n);
210 void writeIKresult(
const std::vector<yarp::sig::Vector> &contacts_r,
const std::vector<yarp::sig::Vector> &normals_r,
const int c,
const std::string &hand,
const int ov_cones);
211 void writeIKBestresult();
212 void write(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_in, pcl::PointCloud <pcl::Normal>::Ptr n,
const std::string &fileName);
215 void writeToMatlab(
const std::vector<yarp::sig::Vector> &contacts_r,
const std::vector<yarp::sig::Vector> &normals_r,
const std::string &hand);
216 void writeBestSolution();
217 void fillBottleFromVector(
const yarp::sig::Vector &vect, yarp::os::Bottle *b);
222 bool configure(yarp::os::ResourceFinder &rf);
224 bool interruptModule();
The Definition of the BoundingBox class.