grasp
All Data Structures Namespaces Functions Modules
powerGrasp.h
1 /* Copyright: (C) 2014 iCub Facility - Istituto Italiano di Tecnologia
2  * Authors: Ilaria Gori
3  * email: ilaria.gori@iit.it
4  * Permission is granted to copy, distribute, and/or modify this program
5  * under the terms of the GNU General Public License, version 2 or any
6  * later version published by the Free Software Foundation.
7  *
8  * A copy of the license can be found in the file LICENSE located in the
9  * root directory.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14  * Public License for more details
15 */
16 
17 #ifndef __POWER_GRASP_MODULE_H__
18 #define __POWER_GRASP_MODULE_H__
19 
20 #include <cstdlib>
21 #include <string>
22 #include <vector>
23 #include <fstream>
24 
25 #include <pcl/io/ply_io.h>
26 #include <pcl/filters/statistical_outlier_removal.h>
27 
28 #include <yarp/os/all.h>
29 
30 #include <iCub/learningMachine/FixedRangeScaler.h>
31 #include <iCub/learningMachine/LSSVMLearner.h>
32 #include <iCub/data3D/SurfaceMeshWithBoundingBox.h>
33 
34 #include "visualizationThread.h"
35 #include "orientationThread.h"
36 
37 #ifdef _WIN32
38  #include "custom/dirent.h"
39 #endif
40 
41 #define similarity 0.005
42 #define STATE_WAIT 0
43 #define STATE_ESTIMATE 1
44 #define STATE_GRASP 2
45 #define RIGHT_HAND "right"
46 #define LEFT_HAND "left"
47 #define NO_HAND "no_hand"
48 #define ACK "ack"
49 #define NACK "nack"
50 #define MODALITY_RIGHT 0
51 #define MODALITY_LEFT 1
52 #define MODALITY_TOP 2
53 #define MODALITY_CENTER 3
54 #define MODALITY_AUTO 4
55 
56 class PowerGrasp: public yarp::os::RFModule
57 {
58 private:
59  int currentState;
60  int currentModality;
61  int numberOfBestPoints;
62  int winnerIndex;
63  int modality;
64  int nFile;
65  int posx;
66  int posy;
67  int sizex;
68  int sizey;
69  bool fromFile;
70  bool fromFileFinished;
71  bool grasped;
72  bool visualize;
73  bool straight;
74  bool train;
75  bool testWithLearning;
76  bool readyToGrasp;
77  bool rightBlocked;
78  bool leftBlocked;
79  bool rightDisabled;
80  bool leftDisabled;
81  bool filterCloud;
82  bool graspSpecificPoint;
83  bool blockRightTmp;
84  bool blockLeftTmp;
85  bool testWithLearningEnabled;
86  bool writeCloud;
87  bool noResult;
88  bool tooFar;
89  double handSize;
90  double fingerSize;
91  double radiusSearch;
92  double maxCurvature;
93  double maxy;
94  double maxz;
95  double dimy;
96  double dimz;
97  float bestCurvature;
98  float currentCurvature;
99  std::string chosenHand;
100  std::string path;
101  std::string outputDir;
102 
103  VisualizationThread *visualizationThread;
104  OrientationThread* orientationThreadRight;
105  OrientationThread* orientationThreadLeft;
106  DataToShow data;
107 
108  pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud;
109  pcl::PointCloud<pcl::PointXYZ>::Ptr cloudxyz;
110  pcl::PointCloud <pcl::Normal>::Ptr normals;
111 
112  yarp::sig::Vector offsetR;
113  yarp::sig::Vector offsetL;
114  yarp::sig::Vector chosenPoint;
115  yarp::sig::Vector chosenNormal;
116  yarp::sig::Vector chosenPixel;
117  yarp::sig::Matrix chosenOrientation;
118 
119  yarp::os::Port reconstructionPort;
120  yarp::os::Port areCmdPort;
121  yarp::os::Port depth2kin;
122  yarp::os::BufferedPort<iCub::data3D::SurfaceMeshWithBoundingBox> meshPort;
123  yarp::os::Port rpc;
124 
125  yarp::os::Mutex mutex;
126  yarp::os::Event eventRpc;
127 
128  std::vector<double> rankScores;
129  std::vector<int> rankIndices;
130 
131  iCub::data3D::BoundingBox boundingBox;
132 
133  iCub::learningmachine::FixedRangeScaler scalerIn,scalerOut;
134  iCub::learningmachine::LSSVMLearner machine;
135 
136  double min(const double a, const double b) {return (a>b)?b:a;};
137  double max(const double a, const double b) {return (a<b)?b:a;};
138 
139  void addPointCloud(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_in);
140  void addPlanePoints();
141  void configureSVM(yarp::os::Bottle &bottleSVM);
142  void configureGeneralInfo(yarp::os::ResourceFinder &rf);
143  void normalEstimation();
144  void filter(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_in,pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_in_filtered);
145  void write(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_in, const int nFile);
146  void insertElement(const double score, const int index);
147  void fromSurfaceMesh(const iCub::data3D::SurfaceMeshWithBoundingBox& msg);
148  void askToGrasp();
149  void resetBools();
150  void manageModality();
151  void chooseCandidatePoints();
152  void rankPoints();
153  void startVisualization();
154  void computeDim();
155  int findIndexFromCloud(const yarp::sig::Vector &point);
156  double scoreFunction(const int index);
157  bool normalPointingOut(const int index, const yarp::sig::Vector &center);
158  bool respond(const yarp::os::Bottle& command, yarp::os::Bottle& reply);
159  bool fillCloudFromFile();
160  bool get3DPoint(const yarp::sig::Vector &point2D, yarp::sig::Vector &point3D);
161  std::string chooseBestPointAndOrientation(int &winnerIndex, yarp::sig::Matrix &designedOrientation);
162  yarp::sig::Vector vectorFromNormal(const int index);
163  yarp::sig::Vector vectorFromCloud(const int index);
164  yarp::sig::Vector pointFromBottle(const yarp::os::Bottle &bot, const int index);
165  yarp::sig::Vector assignIndexToAxes(double &anglez);
166  yarp::sig::Vector findBiggestAxis(int &ind);
167  yarp::sig::Vector computeApproachVector(const yarp::sig::Vector &chosenPoint);
168  std::vector<pcl::PointIndices> selectBigClusters(const std::vector<pcl::PointIndices> &clusters);
169 
170 public:
171 
172  PowerGrasp();
173  bool configure(yarp::os::ResourceFinder &rf);
174  bool close();
175  bool interruptModule();
176  bool updateModule();
177  double getPeriod();
178 };
179 
180 #endif
181 
The Definition of the BoundingBox class.
Definition: boundingBox.h:61