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
00039 #include <explore_2d/exploration.h>
00040 #include <occupancy_grid_utils/shortest_path.h>
00041 #include <occupancy_grid_utils/coordinate_conversions.h>
00042 #include <cstdlib>
00043 #include <ctime>
00044
00045 namespace explore_2d
00046 {
00047
00048 namespace nm=nav_msgs;
00049 namespace gu=occupancy_grid_utils;
00050 namespace gm=geometry_msgs;
00051
00052
00053 Explorer::Explorer (const ExplorerParams& params) :
00054 params_(params)
00055 {
00056 srand(time(NULL));
00057 }
00058
00059
00060 void Explorer::updateOccupancyGrid (const nm::OccupancyGrid& grid)
00061 {
00062 inflated_grid_ = gu::inflateObstacles(grid, params_.robot_radius);
00063 }
00064
00065
00066 gm::Pose Explorer::nextNavGoal ()
00067 {
00068 ROS_ASSERT_MSG (inflated_grid_, "Can't get a nav goal until the grid is set");
00069 const nm::MapMetaData& info = inflated_grid_->info;
00070 const unsigned NUM_TRIES_UNKNOWN_SPACE=5;
00071
00072 gu::Cell c;
00073 unsigned n=0;
00074
00075
00076
00077 while (true) {
00078
00079 n++;
00080
00081
00082 const unsigned num_cells = info.width*info.height;
00083 unsigned i = -1;
00084 do {
00085 i = rand() % num_cells;
00086 } while (inflated_grid_->data[i] != gu::UNOCCUPIED);
00087 c = gu::indexCell(info, i);
00088
00089
00090 const int dx = c.x > (int) info.width/2 ? 1 : -1;
00091 const int dy = c.y > (int) info.height/2 ? 1 : -1;
00092
00093 gu::Cell c2;
00094 while (true) {
00095 c2 = gu::Cell(c.x+dx, c.y+dy);
00096 if (!gu::withinBounds(info, c2))
00097 break;
00098 if (inflated_grid_->data[cellIndex(info, c2)] != gu::UNOCCUPIED)
00099 break;
00100 c = c2;
00101 }
00102 if (!gu::withinBounds(info, c2) || inflated_grid_->data[cellIndex(info, c2)] == gu::UNKNOWN ||
00103 n > NUM_TRIES_UNKNOWN_SPACE)
00104 break;
00105 }
00106
00107
00108 gm::Pose p;
00109 p.position = gu::cellCenter(info, c);
00110 p.orientation.w = 1.0;
00111
00112 return p;
00113 }
00114
00115
00116 }