Go to the documentation of this file.00001 #include "obstacle_finder/obstacle_finder.h"
00002
00003 namespace obstacle_finder
00004 {
00005 ObstacleFinder::ObstacleFinder(costmap_2d::Costmap2DROS* costmap, double robot_odom_x, double robot_odom_y)
00006 : costmap_(costmap), robot_odom_x_(robot_odom_x), robot_odom_y_(robot_odom_y)
00007 {
00008 }
00009
00010 Obstacle ObstacleFinder::nearestObstacle()
00011 {
00012 return nearestObstacle(costmap_, robot_odom_x_, robot_odom_y_);
00013 }
00014
00015 Obstacle ObstacleFinder::nearestObstacle(double robot_odom_x, double robot_odom_y)
00016 {
00017 robot_odom_x_ = robot_odom_x;
00018 robot_odom_y_ = robot_odom_y;
00019 return nearestObstacle(costmap_, robot_odom_x, robot_odom_y);
00020 }
00021
00022 Obstacle ObstacleFinder::nearestObstacle(costmap_2d::Costmap2DROS* new_costmap, double robot_odom_x,
00023 double robot_odom_y)
00024 {
00025 costmap_ = new_costmap;
00026 robot_odom_x_ = robot_odom_x;
00027 robot_odom_y_ = robot_odom_y;
00028 unsigned int min_x = INT_MAX;
00029 unsigned int min_y = INT_MAX;
00030 double minimum_distance = DBL_MAX;
00031 unsigned int cell_x_idx, cell_y_idx;
00032
00033 costmap_2d::Costmap2D* costmap = costmap_->getCostmap();
00034
00035 unsigned int robot_map_x, robot_map_y;
00036 costmap->worldToMap(robot_odom_x, robot_odom_y, robot_map_x, robot_map_y);
00037
00038 for (cell_x_idx = 0; cell_x_idx < costmap->getSizeInCellsX(); cell_x_idx++)
00039 {
00040 for (cell_y_idx = 0; cell_y_idx < costmap->getSizeInCellsY(); cell_y_idx++)
00041 {
00042 double cost_idx = costmap->getCost(cell_x_idx, cell_y_idx);
00043 int dx = cell_x_idx - robot_map_x;
00044 int dy = cell_y_idx - robot_map_y;
00045
00046 double dist_idx = sqrt((dx * dx) + (dy * dy));
00047
00048
00049 if (cost_idx >= costmap_2d::LETHAL_OBSTACLE)
00050 {
00051 if (dist_idx < minimum_distance)
00052 {
00053 minimum_distance = dist_idx;
00054 min_x = cell_x_idx;
00055 min_y = cell_y_idx;
00056 }
00057 }
00058 }
00059 }
00060
00061 Obstacle co;
00062 costmap->mapToWorld(min_x, min_y, co.x, co.y);
00063 co.dist = minimum_distance * costmap->getResolution();
00064 return co;
00065 }
00066
00067 Obstacle::Obstacle(double x, double y, double dist) : x(x), y(y), dist(dist)
00068 {
00069 }
00070
00071 Obstacle::Obstacle() : x(0), y(0), dist(0)
00072 {
00073 }
00074 }