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
00032 #include <time.h>
00033 #include <stdio.h>
00034 #include <sys/types.h>
00035 #include <sys/stat.h>
00036 #include <unistd.h>
00037 #include <fstream>
00038 #include <vector>
00039 #include <string>
00040 #include <list>
00041 #include <algorithm>
00042
00043 #include <angles/angles.h>
00044 #include <sbpl/headers.h>
00045 #include <sbpl_arm_planner/bfs_3d.h>
00046 #include <sbpl_arm_planner/sbpl_collision_space.h>
00047 #include <sbpl_arm_planner/sbpl_arm_planner_params.h>
00048 #include <sbpl_arm_planner/pr2/sbpl_math.h>
00049 #include <sbpl_arm_planner/pr2/orientation_solver.h>
00050 #include <sbpl_arm_planner/pr2/pr2_workspace.h>
00051 #include <boost/thread.hpp>
00052 #include <boost/thread/mutex.hpp>
00053
00054 #ifndef __ENVIRONMENT_ROBARM3D_H_
00055 #define __ENVIRONMENT_ROBARM3D_H_
00056
00057 namespace sbpl_arm_planner
00058 {
00059
00060
00062 typedef struct
00063 {
00064 bool is_6dof_goal;
00065 short unsigned int xyz_tolerance;
00066 int type;
00067 int xyz[3];
00068 double pos[3];
00069 double rpy[3];
00070 double q[4];
00071 double pos_tolerance[3];
00072 double rpy_tolerance[3];
00073 } GoalPos;
00074
00076 typedef struct
00077 {
00078 unsigned char dist;
00079 short unsigned int action;
00080 short unsigned int xyz[3];
00081 int stateID;
00082 int heur;
00083 std::vector<short unsigned int> coord;
00084 double rpy[3];
00085 } EnvROBARM3DHashEntry_t;
00086
00087
00088 typedef struct
00089 {
00090 bool bInitialized;
00091
00092 bool solved_by_ik;
00093 bool solved_by_os;
00094 bool ik_solution_is_valid;
00095
00096 int num_no_ik_solutions;
00097 int num_ik_invalid_joint_limits;
00098 int num_calls_to_ik;
00099 int num_ik_invalid_path;
00100 int num_invalid_ik_solutions;
00101 int num_expands_to_position_constraint;
00102 double goal_to_obstacle_distance;
00103 std::vector<double> ik_solution;
00104
00105 std::vector<double> start_configuration;
00106 std::vector<double> angledelta;
00107 std::vector<int> anglevals;
00108 std::vector<std::vector<double> > obstacles;
00109
00110 std::vector <GoalPos> EndEffGoals;
00111 std::vector <std::vector <double> > ParsedGoals;
00112 std::vector <std::vector <double> > ParsedGoalTolerance;
00113 } EnvROBARM3DConfig_t;
00114
00116 typedef struct
00117 {
00118 EnvROBARM3DHashEntry_t* goalHashEntry;
00119 EnvROBARM3DHashEntry_t* startHashEntry;
00120
00121
00122 int HashTableSize;
00123 std::vector<EnvROBARM3DHashEntry_t*>* Coord2StateIDHashTable;
00124
00125
00126 std::vector<EnvROBARM3DHashEntry_t*> StateID2CoordTable;
00127
00128 }EnvironmentROBARM3D_t;
00129
00130
00132 class EnvironmentROBARM3D: public DiscreteSpaceInformation
00133 {
00134 public:
00135
00136 std::vector<int> expanded_states;
00137 bool save_expanded_states;
00138
00142 EnvironmentROBARM3D();
00143
00147 ~EnvironmentROBARM3D();
00148
00154 bool InitializeEnv(const char* sEnvFile);
00155
00156 bool InitializeEnv(const char* sEnvFile, std::string params_file, std::string arm_file);
00157
00165 bool AreEquivalent(int StateID1, int StateID2);
00166
00175 bool initEnvironment(std::string arm_description_filename, std::string mprims_filename);
00176
00181 bool InitializeMDPCfg(MDPConfig *MDPCfg);
00182
00189 bool setStartConfiguration(std::vector<double> angles);
00190
00197 int GetFromToHeuristic(int FromStateID, int ToStateID);
00198
00204 int GetGoalHeuristic(int stateID);
00205
00211 int GetStartHeuristic(int stateID);
00212
00224 void GetSuccs(int SourceStateID, vector<int>* SuccIDV, vector<int>* CostV);
00225
00227 void GetPreds(int TargetStateID, vector<int>* PredIDV, vector<int>* CostV);
00228
00235 void StateID2Angles(int stateID, std::vector<double> &angles);
00236
00241 int SizeofCreatedEnv();
00242
00249 void PrintState(int stateID, bool bVerbose, FILE* fOut=NULL);
00250
00252 void SetAllActionsandAllOutcomes(CMDPSTATE* state);
00253
00255 void SetAllPreds(CMDPSTATE* state);
00256
00258 void PrintEnv_Config(FILE* fOut);
00259
00268 bool setGoalPosition(const std::vector <std::vector<double> > &goals, const std::vector<std::vector<double> > &tolerances);
00269
00275 bool isPathValid(const std::vector<std::vector<double> > path);
00276
00282 bool isValidJointConfiguration(const std::vector<double> angles);
00283
00290 std::vector<int> debugExpandedStates();
00291
00297 double getEpsilon();
00298
00306 std::vector<std::vector<double> > getShortestPath();
00307
00308
00309
00310
00311
00312
00313 void updateOccupancyGridFromCollisionMap(const mapping_msgs::CollisionMap &collision_map);
00314
00319 void debugAdaptiveMotionPrims();
00320
00328 void getExpandedStates(std::vector<std::vector<double> >* ara_states);
00329
00333 void visualizeOccupancyGrid();
00334
00335 void setReferenceFrameTransform(KDL::Frame f, std::string &name);
00336
00337 SBPLCollisionSpace* getCollisionSpace() const;
00338
00339 OccupancyGrid* getOccupancyGrid() const;
00340
00341 std::vector<double> getPlanningStats();
00342
00343 bool initArmModel(FILE* aCfg, const std::string robot_description);
00344
00351 bool interpolatePathToGoal(std::vector<std::vector<double> > &path, double inc);
00352
00361 bool getElbowCellsAtGoal(std::vector<int> &shoulder, std::vector<double> &goal_m, double rad1, double rad2, std::vector<std::vector<int> > &cells);
00362
00363 void getElbowPoints(std::vector<std::vector<double> > &elbow_points);
00364
00365 void getArmChainRootLinkName(std::string &name);
00366
00367 private:
00368
00369 EnvROBARM3DConfig_t EnvROBARMCfg;
00370 EnvironmentROBARM3D_t EnvROBARM;
00371
00372 boost::thread *heuristic_thread_;
00373 boost::mutex heuristic_mutex_;
00374 bool using_short_mprims_;
00375 bool elbow_heuristic_completed_;
00376 std::vector<std::vector<double> > elbow_poses_;
00377 std::vector<std::vector<int> > elbow_cells_;
00378
00379 OccupancyGrid *grid_;
00380 BFS3D *dijkstra_;
00381 BFS3D *elbow_dijkstra_;
00382 SBPLArmModel *arm_;
00383 RPYSolver* rpysolver_;
00384 SBPLCollisionSpace *cspace_;
00385 SBPLArmPlannerParams prms_;
00386
00387 std::string params_filename_;
00388 std::string arm_desc_filename_;
00389
00390
00391 int (EnvironmentROBARM3D::*getHeuristic_) (int FromStateID, int ToStateID);
00392
00393 std::vector<double> final_joint_config;
00394
00395 std::vector<double> prefinal_joint_config;
00396
00398 unsigned int intHash(unsigned int key);
00399 unsigned int getHashBin(const std::vector<short unsigned int> &coord);
00400 EnvROBARM3DHashEntry_t* getHashEntry(const std::vector<short unsigned int> &coord, short unsigned int action, bool bIsGoal);
00401 EnvROBARM3DHashEntry_t* createHashEntry(const std::vector<short unsigned int> &coord, short unsigned int endeff[3], short unsigned int action);
00402
00404 bool initEnvConfig();
00405 bool initGeneral();
00406 void initDijkstra();
00407 void initElbowDijkstra();
00408 void readConfiguration(FILE* fCfg);
00409
00411 void discretizeAngles();
00412 void coordToAngles(const std::vector<short unsigned int> &coord, std::vector<double> &angles);
00413 void anglesToCoord(const std::vector<double> &angle, std::vector<short unsigned int> &coord);
00414
00416 bool isGoalPosition(const std::vector<double> &pose, const GoalPos &goal, std::vector<double> jnt_angles, int &cost);
00417 bool isGoalStateWithIK(const std::vector<double> &pose, const GoalPos &goal, std::vector<double> jnt_angles);
00418 bool isGoalStateWithOrientationSolver(const GoalPos &goal, std::vector<double> jnt_angles);
00419 bool precomputeHeuristics();
00420 bool precomputeElbowHeuristic();
00421
00423 int cost(EnvROBARM3DHashEntry_t* HashEntry1, EnvROBARM3DHashEntry_t* HashEntry2, bool bState2IsGoal);
00424 int getEdgeCost(int FromStateID, int ToStateID);
00425 void computeCostPerCell();
00426 void computeCostPerRadian();
00427 int getActionCost(const std::vector<double> &from_config, const std::vector<double> &to_config, int dist);
00428
00430 void printHashTableHist();
00431 void printJointArray(FILE* fOut, EnvROBARM3DHashEntry_t* HashEntry, bool bGoal, bool bVerbose);
00432
00434 int getDijkstraDistToGoal(short unsigned int x, short unsigned int y, short unsigned int z) const;
00435 double getDistToClosestGoal(double *xyz,int *goal_num);
00436 int getElbowHeuristic(int FromStateID, int ToStateID);
00437 int getEndEffectorHeuristic(int FromStateID, int ToStateID);
00438 int getCombinedHeuristic(int FromStateID, int ToStateID);
00439 int getEuclideanDistance(int x1, int y1, int z1, int x2, int y2, int z2) const;
00440 void getBresenhamPath(const short unsigned int a[],const short unsigned int b[], std::vector<std::vector<int> > *path);
00441
00442 void clearStats();
00443 };
00444
00445
00446 inline unsigned int EnvironmentROBARM3D::intHash(unsigned int key)
00447 {
00448 key += (key << 12);
00449 key ^= (key >> 22);
00450 key += (key << 4);
00451 key ^= (key >> 9);
00452 key += (key << 10);
00453 key ^= (key >> 2);
00454 key += (key << 7);
00455 key ^= (key >> 12);
00456 return key;
00457 }
00458
00459 inline unsigned int EnvironmentROBARM3D::getHashBin(const std::vector<short unsigned int> &coord)
00460 {
00461 int val = 0;
00462
00463 for(short unsigned int i = 0; i < coord.size(); i++)
00464 val += intHash(coord[i]) << i;
00465
00466 return intHash(val) & (EnvROBARM.HashTableSize-1);
00467 }
00468
00469
00470 inline void EnvironmentROBARM3D::coordToAngles(const std::vector<short unsigned int> &coord, std::vector<double> &angles)
00471 {
00472 for(short unsigned int i = 0; i < coord.size(); i++)
00473 angles[i] = coord[i]*EnvROBARMCfg.angledelta[i];
00474 }
00475
00476 inline void EnvironmentROBARM3D::anglesToCoord(const std::vector<double> &angle, std::vector<short unsigned int> &coord)
00477 {
00478 double pos_angle;
00479
00480 for(int i = 0; i < int(angle.size()); i++)
00481 {
00482
00483 pos_angle = angle[i];
00484 if(pos_angle < 0.0)
00485 pos_angle += 2*M_PI;
00486
00487 coord[i] = (int)((pos_angle + EnvROBARMCfg.angledelta[i]*0.5)/EnvROBARMCfg.angledelta[i]);
00488
00489 if(coord[i] == EnvROBARMCfg.anglevals[i])
00490 coord[i] = 0;
00491 }
00492 }
00493
00494
00495 int EnvironmentROBARM3D::getEuclideanDistance(int x1, int y1, int z1, int x2, int y2, int z2) const
00496 {
00497 return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2));
00498 }
00499
00500 inline SBPLCollisionSpace* EnvironmentROBARM3D::getCollisionSpace() const
00501 {
00502 return cspace_;
00503 }
00504
00505 inline OccupancyGrid* EnvironmentROBARM3D::getOccupancyGrid() const
00506 {
00507 return grid_;
00508 }
00509
00510
00511 }
00512
00513 #endif
00514