ROS globalplanner A*算法的改进

一直想对ros中的a*的改进,看源码astar.cpp中有四个add(),只有上下左右,没有斜方向。自己加了之后感觉还不如原来规划的,不知道为什么。

#include<global_planner/astar.h>
#include<costmap_2d/cost_values.h>

namespace global_planner {

AStarExpansion::AStarExpansion(PotentialCalculator* p_calc, int xs, int ys) :
        Expander(p_calc, xs, ys) {
}

bool AStarExpansion::calculatePotentials(unsigned char* costs, double start_x, double start_y, double end_x, double end_y,
                                        int cycles, float* potential) {
    queue_.clear();
    int start_i = toIndex(start_x, start_y);
    queue_.push_back(Index(start_i, 0));

    std::fill(potential, potential + ns_, POT_HIGH);
    potential[start_i] = 0;

    int goal_i = toIndex(end_x, end_y);
    int cycle = 0;

    while (queue_.size() > 0 && cycle < cycles) {
        Index top = queue_[0];//min
        std::pop_heap(queue_.begin(), queue_.end(), greater1()); //把旧的最小的放最后,新的放上面
        queue_.pop_back(); //旧的删除

        int i = top.i;
        if (i == goal_i)
            return true;

        add(costs, potential, potential[i], i + 1, end_x, end_y, i);
        add(costs, potential, potential[i], i - 1, end_x, end_y, i);
        add(costs, potential, potential[i], i + nx_, end_x, end_y, i);
        add(costs, potential, potential[i], i - nx_, end_x, end_y, i);
        add(costs, potential, potential[i], i + 1 -nx_, end_x, end_y, i);
        add(costs, potential, potential[i], i - 1 +nx_, end_x, end_y, i);
        add(costs, potential, potential[i], i + nx_ +1, end_x, end_y, i);
        add(costs, potential, potential[i], i - nx_ -1, end_x, end_y, i);
        cycle++;
    }

    return false;
}

void AStarExpansion::add(unsigned char* costs, float* potential, float prev_potential, int next_i, int end_x,
                         int end_y, int i) {
    if (next_i < 0 || next_i >= ns_)
        return;

    if (potential[next_i] < POT_HIGH)
        return;

    if(costs[next_i]>=lethal_cost_ && !(unknown_ && costs[next_i]==costmap_2d::NO_INFORMATION))
        return;
    
    if (next_i == i + 1 || next_i == i-1 || next_i == i+nx_ || next_i == i-nx_)
    potential[next_i] = p_calc_->calculatePotential(potential, costs[next_i] + neutral_cost_, next_i, prev_potential);
    else
    potential[next_i] = p_calc_->calculatePotential(potential, costs[next_i] + 1.4 * neutral_cost_, next_i, prev_potential);
    int x = next_i % nx_, y = next_i / nx_;
    float distance = abs(end_x - x) + abs(end_y - y);

    queue_.push_back(Index(next_i, potential[next_i] + distance * neutral_cost_));
    std::push_heap(queue_.begin(), queue_.end(), greater1());
}

} //end namespace global_planner

挖坑

更多推荐

ROS globalplanner A*算法的改进