ROS Navigation Stack之dwa_local_planner源碼分析

DWA和base_local_planner的關(guān)系

在base_local_planner包中有兩個文件叫trajectory_planner.cpp 以及對應的ros實現(xiàn),其和DWA是同一層的。
由于nav_core提供了統(tǒng)一的接口,因此我們可以先看看統(tǒng)一的接口有哪些,那我們便知道每一個算法里比較重要的函數(shù)有哪些。

nav_core包里的base_local_planner.h文件
//最為關(guān)鍵的地方,計算機器人下一刻的速度
virtual bool computeVelocityCommands(geometry_msgs::Twist& cmd_vel) = 0;
//判斷是否到達目標點
virtual bool isGoalReached() = 0;
//加載全局路徑
virtual bool setPlan(const std::vector<geometry_msgs::PoseStamped>& plan) = 0;
//初始化
virtual void initialize(std::string name, tf::TransformListener* tf, costmap_2d::Costmap2DROS* costmap_ros) = 0;

下面我們就先看看base_local_planner的computeVelocityCommands的主要實現(xiàn)框架

bool TrajectoryPlannerROS::computeVelocityCommands(geometry_msgs::Twist& cmd_vel)
{
    //檢查初始化、檢查是否已經(jīng)到達目標點...略
    transformGlobalPlan(*tf_, global_plan_, global_pose, *costmap_, global_frame_, transformed_plan);
    //如果已經(jīng)到達目標點,姿態(tài)還沒到
    if (xy_tolerance_latch_ || (getGoalPositionDistance(global_pose, goal_x, goal_y) <= xy_goal_tolerance_)) 
    {
        tc_->updatePlan(transformed_plan);
        //所以這個函數(shù)里最關(guān)鍵的子函數(shù)是findBestPath
        Trajectory path = tc_->findBestPath(global_pose, robot_vel, drive_cmds);
        return true;
    }
    tc_->updatePlan(transformed_plan);
    Trajectory path = tc_->findBestPath(global_pose, robot_vel, drive_cmds);
    //然后又是轉(zhuǎn)換,然后就發(fā)布出速度了...
}

接下來我們看一下TrajectoryPlanner的findBestPath的實現(xiàn)框架,Come on~

Trajectory TrajectoryPlanner::findBestPath(tf::Stamped<tf::Pose> global_pose, tf::Stamped<tf::Pose> global_vel,
      tf::Stamped<tf::Pose>& drive_velocities)
{
    //...
    Trajectory best = createTrajectories(pos[0], pos[1], pos[2], vel[0], vel[1], vel[2],
        acc_lim_x_, acc_lim_y_, acc_lim_theta_);
    
    //...
}

順藤摸瓜,一睹createTrajectories的內(nèi)部實現(xiàn),這個函數(shù)是軌跡采樣算法,可以說是一個非常關(guān)鍵的函數(shù)。

Trajectory TrajectoryPlanner::createTrajectories(double x, double y, double theta,
      double vx, double vy, double vtheta,
      double acc_x, double acc_y, double acc_theta) 
{
    //檢查最終點是否是有效的,判斷變量在updatePlan中被賦值
    if( final_goal_position_valid_ )
    {
      double final_goal_dist = hypot( final_goal_x_ - x, final_goal_y_ - y );
      max_vel_x = min( max_vel_x, final_goal_dist / sim_time_ );
    }
    
    //是否使用dwa算法, sim_peroid_是1/controller_frequency_,暫時不清楚sim_period_和sim_time_的區(qū)別
    if (dwa_)
    {
      max_vel_x = max(min(max_vel_x, vx + acc_x * sim_period_), min_vel_x_);
      min_vel_x = max(min_vel_x_, vx - acc_x * sim_period_);

      max_vel_theta = min(max_vel_th_, vtheta + acc_theta * sim_period_);
      min_vel_theta = max(min_vel_th_, vtheta - acc_theta * sim_period_);
    }
    else
    {
      max_vel_x = max(min(max_vel_x, vx + acc_x * sim_time_), min_vel_x_);
      min_vel_x = max(min_vel_x_, vx - acc_x * sim_time_);

      max_vel_theta = min(max_vel_th_, vtheta + acc_theta * sim_time_);
      min_vel_theta = max(min_vel_th_, vtheta - acc_theta * sim_time_);
    }
    //...先忽略其中的邏輯,只要知道按照不同的規(guī)則生成路徑,調(diào)用的子函數(shù)是generateTrajectory
}

這個子函數(shù)的作用就是生成路徑,并且評分

void TrajectoryPlanner::generateTrajectory
{
    //主要有兩大作用:
    //生成路徑和速度
    vx_i = computeNewVelocity(vx_samp, vx_i, acc_x, dt);
    vy_i = computeNewVelocity(vy_samp, vy_i, acc_y, dt);
    vtheta_i = computeNewVelocity(vtheta_samp, vtheta_i, acc_theta, dt);
    //計算位置
    x_i = computeNewXPosition(x_i, vx_i, vy_i, theta_i, dt);
    y_i = computeNewYPosition(y_i, vx_i, vy_i, theta_i, dt);
    theta_i = computeNewThetaPosition(theta_i, vtheta_i, dt);
    //對路徑進行評分
    if (!heading_scoring_) 
    {
      //
      cost = pdist_scale_ * path_dist + goal_dist * gdist_scale_ + occdist_scale_ * occ_cost;
    } 
    else 
    {
      cost = occdist_scale_ * occ_cost + pdist_scale_ * path_dist + 0.3 * heading_diff + goal_dist * gdist_scale_;
    }
    //這里的順序與源碼不同,我覺得總分來看更有組織性
    
    //該軌跡與全局路徑的相對距離
    path_dist = path_map_(cell_x, cell_y).target_dist;
    //距離目標點距離
    goal_dist = goal_map_(cell_x, cell_y).target_dist;
    //離障礙物距離
    double footprint_cost = footprintCost(x_i, y_i, theta_i);
    occ_cost = std::max(std::max(occ_cost, footprint_cost), double(costmap_.getCost(cell_x, cell_y)));
}

綜上所述,其整一個邏輯順序就是computeVelocityCommands->findBestTrajectory --> createTrajectories --> generateTrajectory

最終,選擇分數(shù)最低的軌跡,發(fā)布出去。這便是整個局部規(guī)劃器的實現(xiàn)思路和邏輯。下一篇,談談DWA。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • Navigation Stack概述 [圖片上傳失敗...(image-855943-1544240929262)...
    茶色少年閱讀 2,052評論 0 1
  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,205評論 3 119
  • 昨天看到某個故人的問候,回想起了這兩年經(jīng)歷的一切,大家都有錯誤,分開已經(jīng)是必然,雖然很難過。 失去了很多,失去了篤...
    林中之鯨閱讀 210評論 0 0
  • 作品解說:這是我參加文魁大腦俱樂部思維導圖武林計劃的第41副作品,主題為卓越公司的戰(zhàn)略管理。 我繪制這幅導圖的目的...
    AFL李政閱讀 393評論 0 1
  • 午夜的燈光很是迷離,深夜的思念徜徉在昏黃的燈角。閉上雙眼,我想起盛夏的某個午后,我拿著羅蘭,一步一步,走向她...
    香徹閱讀 402評論 0 2

友情鏈接更多精彩內(nèi)容