00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <ros/ros.h>
00031 #include <vector>
00032 #include <resource_retriever/retriever.h>
00033 #include <sbpl_arm_planner/bresenham.h>
00034 #include <sbpl_arm_planner/sbpl_arm_model.h>
00035 #include <sbpl_arm_planner/occupancy_grid.h>
00036 #include <geometric_shapes/shapes.h>
00037 #include <geometric_shapes/bodies.h>
00038 #include <geometric_shapes/shape_operations.h>
00039 #include <tf_conversions/tf_kdl.h>
00040
00041 #include <tf/tf.h>
00042 #include <mapping_msgs/CollisionObject.h>
00043
00044 using namespace std;
00045
00046 #ifndef _SBPL_COLLISION_SPACE_
00047 #define _SBPL_COLLISION_SPACE_
00048
00050 typedef struct
00051 {
00052 short unsigned int x;
00053 short unsigned int y;
00054 short unsigned int z;
00055 bool bIsObstacle;
00056 } CELL3V;
00057
00058 namespace sbpl_arm_planner
00059 {
00060
00061 class SBPLCollisionSpace
00062 {
00063 public:
00068 SBPLCollisionSpace(SBPLArmModel* arm, OccupancyGrid* grid);
00069
00071 ~SBPLCollisionSpace(){};
00072
00074 void setDebugFile(FILE* file_ptr);
00075
00077 bool checkCollision(const std::vector<double> &angles, bool verbose, bool check_mesh, unsigned char &dist);
00078
00080 bool checkLinkForCollision(const std::vector<double> &angles, int link_num, bool verbose, unsigned char &dist);
00081
00083 inline bool isValidCell(const int x, const int y, const int z, const int radius);
00084
00085 void addArmCuboidsToGrid();
00086
00087 bool getCollisionCylinders(const std::vector<double> &angles, std::vector<std::vector<double> > &cylinders);
00088
00090 void getLineSegment(const std::vector<int> a,const std::vector<int> b,std::vector<std::vector<int> > &points);
00091
00092 void getInterpolatedPath(const std::vector<double> &start, const std::vector<double> &end, double inc, std::vector<std::vector<double> > &path);
00093
00094 void getInterpolatedPath(const std::vector<double> &start, const std::vector<double> &end, std::vector<double> &inc, std::vector<std::vector<double> > &path);
00095
00097 bool checkPathForCollision(const std::vector<double> &start, const std::vector<double> &end, bool verbose, unsigned char &dist);
00098
00100 bool checkLinkPathForCollision(const std::vector<double> &start, const std::vector<double> &end, int link_num, bool verbose, unsigned char &dist);
00101
00103 unsigned char isValidLineSegment(const std::vector<int> a,const std::vector<int> b,const short unsigned int radius);
00104
00105
00106 void removeAttachedObject();
00107
00108
00109 void attachSphereToGripper(std::string frame, geometry_msgs::Pose pose, double radius);
00110
00111
00112 void attachCylinderToGripper(std::string frame, geometry_msgs::Pose pose, double radius, double length);
00113
00114
00115 void attachMeshToGripper(const std::string frame, const geometry_msgs::Pose pose, const bodies::BoundingCylinder &cyl);
00116
00117
00118 void attachMeshToGripper(const std::string frame, const geometry_msgs::Pose pose, const std::vector<int32_t> &triangles, const std::vector<geometry_msgs::Point> &vertices);
00119
00121 bool getAttachedObject(const std::vector<double> &angles, std::vector<std::vector<double> > &xyz);
00122
00124 double getAttachedObjectRadius();
00125
00127 void addCollisionObject(const mapping_msgs::CollisionObject &object);
00128
00130 void transformPose(std::string current_frame, std::string desired_frame, geometry_msgs::Pose &pose_in, geometry_msgs::Pose &pose_out);
00131
00132 void transformPose(const std::string ¤t_frame, const std::string &desired_frame, const geometry_msgs::Pose &pose_in, geometry_msgs::Pose &pose_out);
00133
00135 void processCollisionObjectMsg(const mapping_msgs::CollisionObject &object);
00136
00137 void removeCollisionObject(const mapping_msgs::CollisionObject &object);
00138
00139 void removeAllCollisionObjects();
00140
00141 void putCollisionObjectsInGrid();
00142
00143 void getCollisionObjectVoxelPoses(std::vector<geometry_msgs::Pose> &points);
00144
00146 bool getJointPosesInGrid(std::vector<double> angles, std::vector<std::vector<int> > &jnts, KDL::Frame &f_out, bool verbose);
00147
00148
00149 private:
00150
00151
00153 SBPLArmModel* arm_;
00154
00156 OccupancyGrid* grid_;
00157
00159 FILE* fOut_;
00160
00162 std::map<std::string, mapping_msgs::CollisionObject> object_map_;
00163
00165 std::map<std::string, std::vector<btVector3> > object_voxel_map_;
00166
00167 std::vector<std::string> known_objects_;
00168
00169 tf::TransformListener tf_;
00170
00171 std::vector<double> inc_;
00172
00173 bool object_attached_;
00174 int attached_object_frame_num_;
00175 short unsigned int attached_object_radius_;
00176 std::vector<KDL::Frame> object_points_;
00177
00178
00180 double distanceBetween3DLineSegments(std::vector<int> l1a, std::vector<int> l1b,std::vector<int> l2a,std::vector<int> l2b);
00181
00182 bool getBoundingCylinderOfMesh(std::string mesh_file, shapes::Shape *mesh, bodies::BoundingCylinder &cyl);
00183 void getBoundingCylinderOfMesh(const std::vector<int32_t> &triangles, const std::vector<geometry_msgs::Point> &vertices, bodies::BoundingCylinder &cyl);
00184
00185 bool isValidPoint(double &x, double &y, double &z, short unsigned int &radius, unsigned char &dist);
00186 bool isValidAttachedObject(const std::vector<double> &angles, unsigned char &dist);
00187 bool isValidAttachedObject(const std::vector<double> &angles, unsigned char &dist, std::vector<int> j1, std::vector<int> j2);
00188
00189 };
00190
00191 inline bool SBPLCollisionSpace::isValidCell(const int x, const int y, const int z, const int radius)
00192 {
00193 if(grid_->getCell(x,y,z) <= radius)
00194 return false;
00195 return true;
00196 }
00197
00198 }
00199 #endif
00200