costmap_2d详解5:static_layer.cpp分析

本文详细解析了ROS中Costmap2D的静态地图层工作原理,包括关键变量的作用,初始化过程,地图数据的接收与处理,以及如何更新成本地图和边界。介绍了如何通过配置参数调整地图的致命成本阈值、未知区域的成本值等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关键变量

  std::string global_frame_;  //global_costmap时为map, local_costmap时为odom_combined
  std::string map_frame_; 
  bool subscribe_to_updates_;
  bool map_received_;
  bool has_updated_data_;

/*
*在接收到地图时,x_, y_被置为0,width_, height_ 被设为地图宽高
*/
  unsigned int x_, y_, width_, height_;
  bool track_unknown_space_; //默认没有配置,使用true
  bool use_maximum_;  //false
  bool first_map_only_;      //false
  bool trinary_costmap_; //true
  ros::Subscriber map_sub_, map_update_sub_;
  unsigned char* costmap_; //这个变量是从Costmap2D 类中继承来的,是一个指针
   //致命cost 阈值,使用默认的为100,未知的区域,cost 值为255
  unsigned char lethal_threshold_, unknown_cost_value_;
  dynamic_reconfigure::Server<costmap_2d::GenericPluginConfig> *dsrv_;

关键代码

/*该函数是虚函数,是从Layer 类中继承来的,是在costmap_2d_ros.cpp 中

*plugin->initialize(layered_costmap_, name + "/" + pname, &tf_); 调用的

*/

void StaticLayer::onInitialize()
{
  ros::NodeHandle nh("~/" + name_), g_nh;
  current_ = true;
  //global_costmap时为map, local_costmap时为odom_combined
  global_frame_ = layered_costmap_->getGlobalFrameID();

  std::string map_topic;
  nh.param("map_topic", map_topic, std::string("map"));
  nh.param("first_map_only", first_map_only_, false);
  nh.param("subscribe_to_updates", subscribe_to_updates_, false);

  //静态地图层中没有配置,使用默认的
  nh.param("track_unknown_space", track_unknown_space_, true);
  nh.param("use_maximum", use_maximum_, false);

  int temp_lethal_threshold, temp_unknown_cost_value;

  //致命cost 阈值,使用默认的为100
  nh.param("lethal_cost_threshold", temp_lethal_threshold, int(100));

  //未知的区域,cost 值为255
  nh.param("unknown_cost_value", temp_unknown_cost_value, int(-1));

 //三倍的costmap??默认没有配置,为true
  nh.param("trinary_costmap", trinary_costmap_, true);

  lethal_threshold_ = std::max(std::min(temp_lethal_threshold, 100), 0);
  unknown_cost_value_ = temp_unknown_cost_value;

  // Only resubscribe if topic has changed
  if (map_sub_.getTopic() != ros::names::resolve(map_topic))
  {
//从/map中订阅地图数据,并在回调函数中使用
    map_sub_ = g_nh.subscribe(map_topic, 1, &StaticLayer::incomingMap, this);
    map_received_ = false;
    has_updated_data_ = false;


  dsrv_ = new dynamic_reconfigure::Server<costmap_2d::GenericPluginConfig>(nh);
  dynamic_reconfigure::Server<costmap_2d::GenericPluginConfig>::CallbackType cb = boost::bind(&StaticLayer::reconfigureCB, this, _1, _2);
  dsrv_->setCallback(cb);

}

void StaticLayer::incomingMap(const nav_msgs::OccupancyGridConstPtr& new_map)
{
//从地图消息中获取地图的宽和高
  unsigned int size_x = new_map->info.width, size_y = new_map->info.height;

  //判断master costmap地图是否发生变化
  Costmap2D* master = layered_costmap_->getCostmap();
  if (!layered_costmap_->isRolling() && (master->getSizeInCellsX() != size_x ||
      master->getSizeInCellsY() != size_y ||
      master->getResolution() != new_map->info.resolution ||
      master->getOriginX() != new_map->info.origin.position.x ||
      master->getOriginY() != new_map->info.origin.position.y ||
      !layered_costmap_->isSizeLocked()))
  {

// 如果master costmap 层发生变化,就更新master costmap 范围
    layered_costmap_->resizeMap(size_x, size_y, new_map->info.resolution, new_map->info.origin.position.x,new_map->info.origin.position.y, true);

  }
 //一开始size_x_为0,所以会进入并仅仅更新static costmap 层
  else if (size_x_ != size_x || size_y_ != size_y ||
           resolution_ != new_map->info.resolution ||
           origin_x_ != new_map->info.origin.position.x ||
           origin_y_ != new_map->info.origin.position.y)
  {
   //仅更新static costmap 层信息
    resizeMap(size_x, size_y, new_map->info.resolution,
              new_map->info.origin.position.x, new_map->info.origin.position.y);
  }
  /*size_x_,size_y_是从Costmap2D类中继承过来的
  * 此时size_x_,size_y_,resolution_,origin_x_,origin_y_
  * 都已经变成map 的地图的范围等信息了
  */
  unsigned int index = 0;

  //根据新地图中的数据来更新costmap 的值,
  //将图片的像素值转换成代价值了,具体转换可以看interpretValue函数
  for (unsigned int i = 0; i < size_y; ++i)
  {
    for (unsigned int j = 0; j < size_x; ++j)
    {
      //地图传来的值为-1(即255),0 和100
      unsigned char value = new_map->data[index];
      //这个变量是从Costmap2D 类中继承来的,是一个指针,具体指向哪里呢?
      costmap_[index] = interpretValue(value);
      ++index;
    }
  }
  map_frame_ = new_map->header.frame_id;
  x_ = y_ = 0;
  width_ = size_x_;  //这个是以像素坐标系(地图左下角)为参考的坐标,是int类型
  height_ = size_y_;
  map_received_ = true;
  has_updated_data_ = true;

  // shutdown the map subscrber if firt_map_only_ flag is on
  if (first_map_only_)
  {
    ROS_INFO("Shutting down the map subscriber. first_map_only flag is on");
    map_sub_.shutdown();
  }
}


 

unsigned char StaticLayer::interpretValue(unsigned char value)
{
  // check if the static value is above the unknown or lethal thresholds
  // 实际上就是高于一个阈值lethal_threshold那么就认为是障碍物
  // 没有信息的就认为是未探测的区域
  // 否则为自由空间,true, -1,
  if (track_unknown_space_ && value == unknown_cost_value_)
  {
    //map数据为-1 时会进入这里
    return NO_INFORMATION;
  }
  else if (!track_unknown_space_ && value == unknown_cost_value_)
    return FREE_SPACE;
  // map数据为100 时,>=100 就是致命的障碍物,会进入这里
  else if (value >= lethal_threshold_)
    return LETHAL_OBSTACLE;
  else if (trinary_costmap_)
    return FREE_SPACE;
  // map数据为0 时进入这里
  double scale = (double) value / lethal_threshold_;
  return scale * LETHAL_OBSTACLE;
}

 

/*作用:

 * 参数: robot_x, robot_y, robot_yaw

*min_x, min_y,max_x, max_y 初始值min_x=min_y=1e30,max_x=max_y=-1e30

*返回:min_x, min_y,max_x, max_y为地图的范围,是以世界坐标系(map)为参考的坐标,double类型

*/

void StaticLayer::updateBounds(double robot_x, double robot_y, double robot_yaw, double* min_x, double* min_y,double* max_x, double* max_y)
{
  //传过来的参数 min_x=min_y=1e30,max_x=max_y=-1e30
  if( !layered_costmap_->isRolling() ){
    if (!map_received_ || !(has_updated_data_ || has_extra_bounds_))
      return;
  }
  //初始化边界
  useExtraBounds(min_x, min_y, max_x, max_y);
  double wx, wy;

  /*计算静态costmap地图左下角像素的坐标*/
  mapToWorld(x_, y_, wx, wy);
  *min_x = std::min(wx, *min_x);
  *min_y = std::min(wy, *min_y);

  /*计算静态costmap地图右上角像素的坐标*/
  mapToWorld(x_ + width_, y_ + height_, wx, wy);
  *max_x = std::max(wx, *max_x);
  *max_y = std::max(wy, *max_y);

  has_updated_data_ = false;

}

/*作用:用static costmap 的值更新master costmap 的值

*参数:master_grid: 是master costmap 地图

* min_i,min_j,max_i,max_j 是该master costmap 地图的范围,以像素坐标表示

*/
 

void StaticLayer::updateCosts(costmap_2d::Costmap2D& master_grid, int min_i, int min_j, int max_i, int max_j)
{

  if (!layered_costmap_->isRolling())
  {
    if (!use_maximum_)
/*
 *静态costmap 会进入这里面,会获取static costmap 的cost 值,然后更新master costmap
 *static costmap 的cost 值是在incomingMap函数中处理初始化的,该函数在costmap_2d 中实现
*/
      updateWithTrueOverwrite(master_grid, min_i, min_j, max_i, max_j);
    else
      updateWithMax(master_grid, min_i, min_j, max_i, max_j);

  }

}

 

<think>嗯,用户想了解ROS中的代价地图的实现与使用方法。首先,我需要回顾一下ROS中代价地图的基本概念,确保自己理解正确。根据之前提供的引用资料,代价地图分为全局和局部两种,用于导航时动态更新障碍物信息。比如引用[2]提到,代价地图在静态地图基础上添加辅助信息,实时更新,这对机器人导航至关重要。 接下来,用户可能需要知道具体的实现结构。引用[1]中提到了Costmap2DROS类,它是与ROS其他模块的接口,负责更新和发布代价地图。里面有一个重要的成员layered_costmap_,这说明代价地图是分层的。需要解释这些层次结构,比如静态层、障碍物层、膨胀层等,引用[3]也提到三层地图组成,所以这部分要详细说明。 然后,关于如何使用,用户可能需要配置参数,比如在ROS的配置文件中设置各层的参数。例如,引用[2]中的案例提到了prohibition_layer插件,这说明用户可以自定义层次。需要给出具体的配置示例,比如YAML文件的参数设置,以及如何加载这些参数到节点中。 代码示例部分,用户可能想看到如何启动代价地图,比如初始化Costmap2DROS对象,并设置各个层。引用[1]中的代码片段显示layered_costmap_是protected成员,所以可能需要通过构造函数或其他方法来配置。此外,发布地图到rviz的步骤也很重要,用户需要知道如何可视化结果。 关于自定义层,用户可能对如何编写插件感兴趣。引用[2]提到了prohibition_layer的例子,可以简要说明如何继承CostmapLayer类,并实现必要的函数,如updateBounds和updateCosts。这部分需要指导用户如何编译和注册插件,以便ROS能够识别。 最后,用户可能需要常见问题的解决方法,比如性能优化或地图更新问题。引用[3]提到恢复行为,可能与代价地图的更新策略有关。需要给出一些实际建议,如调整更新频率或裁剪区域,以优化系统性能。 总结起来,回答应该分结构实现、配置方法、使用步骤、自定义开发和常见问题几个部分,每个部分结合引用资料中的具体信息,确保准确性和实用性。同时,生成的相关问题需要引导用户深入理解,比如实现原理、优化策略和应用场景等。</think>### ROS代价地图实现与使用详解 #### 一、核心实现结构 1. **分层架构**:代价地图通过$layered\_costmap\_$实现多层叠加,包含静态层、障碍物层、膨胀层等[^1][^2] ```cpp // 典型层次结构初始化代码 layered_costmap_->addLayer(new StaticLayer()); layered_costmap_->addLayer(new ObstacleLayer()); layered_costmap_->addLayer(new InflationLayer()); ``` 2. **双重地图模式**: - **全局代价地图**:用于全局路径规划,分辨率较低(默认0.05m) - **局部代价地图**:用于局部避障,分辨率较高(默认0.01m)[^3] 3. **动态更新机制**: ```python # 更新循环伪代码 while ros::ok(): updateMap() publishCostmap() sleep(update_frequency) ``` #### 二、基础配置方法 1. **参数文件配置**(以局部代价地图为例): ```yaml local_costmap: plugins: - {name: static_layer, type: "costmap_2d::StaticLayer"} - {name: obstacle_layer, type: "costmap_2d::ObstacleLayer"} - {name: inflation_layer, type: "costmap_2d::InflationLayer"} update_frequency: 5.0 publish_frequency: 2.0 ``` 2. **可视化调试**: ```bash roslaunch turtlebot3_navigation turtlebot3_navigation.launch # 在Rviz中添加Costmap显示 ``` #### 三、核心使用流程 1. **初始化代价地图**: ```cpp costmap_2d::Costmap2DROS local_costmap("local_costmap", tf_); costmap_2d::Costmap2DROS global_costmap("global_costmap", tf_); ``` 2. **访问代价数据**: ```cpp unsigned char cost = local_costmap.getCostmap()->getCost(mx, my); ``` 3. **坐标转换示例**: ```cpp geometry_msgs::PointStamped world_point; costmap->getCostmap()->mapToWorld(mx, my, world_point.point.x, world_point.point.y); ``` #### 四、自定义层开发 1. **创建插件模板**: ```cpp class ProhibitionLayer : public costmap_2d::CostmapLayer { public: virtual void updateBounds(double robot_x, double robot_y, double* min_x, double* min_y, double* max_x, double* max_y); virtual void updateCosts(costmap_2d::Costmap2D& master_grid, int min_i, int min_j, int max_i, int max_j); }; ``` 2. **注册插件**: ```xml <library path="lib/libprohibition_layer"> <class name="costmap_2d/ProhibitionLayer" type="costmap_2d::ProhibitionLayer" base_class_type="costmap_2d::Layer"/> </library> ``` #### 五、典型问题处理 1. **地图更新延迟**: - 检查传感器数据频率是否匹配$update\_frequency$参数 - 验证tf树的时间同步性 2. **性能优化**: ```yaml obstacle_layer: combination_method: 1 # 使用最大值覆盖策略 obstacle_keep_time: 2.0 # 减少障碍物保留时间 inflation_layer: inflation_radius: 0.5 # 适当缩小膨胀半径 ``` 3. **特殊区域处理**: ```cpp // 设置禁止区域示例 addExtraBounds(prohibition_area.min_x, prohibition_area.min_y, prohibition_area.max_x, prohibition_area.max_y); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值