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
00031
00032
00033
00034
00035
00036
00037
00038 #include<global_planner/astar.h>
00039 #include<costmap_2d/cost_values.h>
00040
00041 namespace global_planner {
00042
00043 AStarExpansion::AStarExpansion(PotentialCalculator* p_calc, int xs, int ys) :
00044 Expander(p_calc, xs, ys) {
00045 }
00046
00047 bool AStarExpansion::calculatePotentials(unsigned char* costs, double start_x, double start_y, double end_x, double end_y,
00048 int cycles, float* potential) {
00049 queue_.clear();
00050 int start_i = toIndex(start_x, start_y);
00051 queue_.push_back(Index(start_i, 0));
00052
00053 std::fill(potential, potential + ns_, POT_HIGH);
00054 potential[start_i] = 0;
00055
00056 int goal_i = toIndex(end_x, end_y);
00057 int cycle = 0;
00058
00059 while (queue_.size() > 0 && cycle < cycles) {
00060 Index top = queue_[0];
00061 std::pop_heap(queue_.begin(), queue_.end(), greater1());
00062 queue_.pop_back();
00063
00064 int i = top.i;
00065 if (i == goal_i)
00066 return true;
00067
00068 add(costs, potential, potential[i], i + 1, end_x, end_y);
00069 add(costs, potential, potential[i], i - 1, end_x, end_y);
00070 add(costs, potential, potential[i], i + nx_, end_x, end_y);
00071 add(costs, potential, potential[i], i - nx_, end_x, end_y);
00072 }
00073
00074 return false;
00075 }
00076
00077 void AStarExpansion::add(unsigned char* costs, float* potential, float prev_potential, int next_i, int end_x,
00078 int end_y) {
00079 if (potential[next_i] < POT_HIGH)
00080 return;
00081
00082 if(costs[next_i]>=lethal_cost_ && !(unknown_ && costs[next_i]==costmap_2d::NO_INFORMATION))
00083 return;
00084
00085 potential[next_i] = p_calc_->calculatePotential(potential, costs[next_i] + neutral_cost_, next_i, prev_potential);
00086 int x = next_i % nx_, y = next_i / nx_;
00087 float distance = abs(end_x - x) + abs(end_y - y);
00088
00089 queue_.push_back(Index(next_i, potential[next_i] + distance * neutral_cost_));
00090 std::push_heap(queue_.begin(), queue_.end(), greater1());
00091 }
00092
00093 }