Nav2笔记

1、源码安装

git clone https://github.com/ros-planning/navigation2.git -b humble

1.1 一键安装依赖

wget http://fishros.com/install -O fishros && . fishros
rosdepc install -r --from-paths src --ignore-src --rosdistro $ROS_DISTRO -y

1.2 编译

colcon build --packages-up-to navigation2

 测试,安装是否成功

ros2 pkg list | grep navigation2

 成功显示

navigation2

------------------------------------------------------------------------------------------------------------------------------

1.3 二进制安装 

 sudo apt install ros-humble-nav2-* -y

2、源码目录功能

#==============控制器及其实现相关功能包======================#
nav2_controller | 控制器
nav2_dwb_controller | DWB控制器,Nav2控制器的一个实现
nav2_regulated_pure_pursuit_controller | 纯追踪控制器,Nav2控制器的一个实现
nav2_constrained_smoother

#==============规划器及其实现相关功能包======================#
nav2_planner | Nav2规划器
nav2_navfn_planner | navfn规划器,Nav2规划器的一个实现
nav2_smac_planner | smac规划器,Nav2规划器的一个实现

#=====================恢复器==============================#
nav2_recoveries | Nav2恢复器

#=====================行为树节点及其定义====================#
nav2_bt_navigator | 导航行为树
nav2_behavior_tree | 行为树节点插件定义

#=====================地图和定位===========================#
nav2_map_server | 地图服务器
nav2_costmap_2d | 2D代价地图
nav2_voxel_grid | 体素栅格
nav2_amcl | 自适应蒙特卡洛定位。  状态估计,输入地图、激光、里程计数据,输出机器人map和odom之间的位资关系。

#=====================通用插件系统管理等====================#
nav2_bringup | 启动入口
nav2_common | 公共功能包
nav2_msgs | 通信相关消息定义
nav2_util | 常用工具
nav2_lifecycle_manager |节点生命周期管理器 
nav2_rviz_plugins | RVIZ插件

#=====================核心定义============================#
nav2_core | Nav2核心包
navigation2 | nav2导航汇总配置

#=====================应用================================#
nav2_waypoint_follower | 路点跟踪

#=====================测试=================================#
nav2_system_tests | 系统测试

3、参数配置

复制下面的路径:

navigation2/nav2_bringup/bringup/params/nav2_params.yaml

每组参数解析:

编号 配置项 用途 对应模块与参数详解
1 amcl 机器人定位 nav2_amcl
2 bt_navigator 导航行为树(用于加载行为树节点并根据xml配置进行调度) nav2_bt_navigator,nav2_behavior_tree
3 controller_server 控制器服务器 nav2_controller,nav2_dwb_controller,nav2_regulated_pure_pursuit_controller
4 planner_server 规划服务器 nav2_planner,nav2_navfn_planner,smac_planner
5 recoveries_server 恢复服务器 nav2_recoveries
6 local_costmap 局部代价地图 nav2_costmap_2d,static_layer,inflation_layer
7 global_costmap 全局代价地图 nav2_costmap_2d,nav2_map_server

根据自己机器人情况配置:

旋转半径(robot_radius)
local_costmap:
  local_costmap:
    ros__parameters:
      robot_radius: 0.12
      
global_costmap:
  global_costmap:
    ros__parameters:
      robot_radius: 0.12
碰撞半径(inflation_radius) 
global_costmap:
  global_costmap:
    ros__parameters:
      plugins: ["static_layer", "obstacle_layer", "inflation_layer"]
      inflation_layer:
        plugin: "nav2_costmap_2d::InflationLayer"
        cost_scaling_factor: 3.0
        inflation_radius: 0.35
 frame_id配置:
  • 默认全局的坐标系:map
  • 默认里程计坐标系:odom
  • 默认雷达话题:scan
  • 默认机器人基坐标系:base_link
  • 默认地图话题:map

4、编写launch文件

在一个launch文件中包裹另一个功能包中的luanch文件采用的是IncludeLaunchDescriptionPythonLaunchDescriptionSource

'''
作者: 小鱼
公众号: 鱼香ROS
QQ交流群: 2642868461
描述: Nav2 launch启动文件
'''
import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node


def generate_launch_description():
    #=============================1.定位到包的地址=============================================================
    fishbot_navigation2_dir = get_package_share_directory('fishbot_navigation2')
    nav2_bringup_dir = get_package_share_directory('nav2_bringup')
    
    
    #=============================2.声明参数,获取配置文件路径===================================================
    # use_sim_time 这里要设置成true,因为gazebo是仿真环境,其时间是通过/clock话题获取,而不是系统时间
    use_sim_time = LaunchConfiguration('use_sim_time', default='true') 
    map_yaml_path = LaunchConfiguration('map',default=os.path.join(fishbot_navigation2_dir,'maps','fishbot_map.yaml'))
    nav2_param_path = LaunchConfiguration('params_file',default=os.path.join(fishbot_navigation2_dir,'param','fishbot_nav2.yaml'))
    rviz_config_dir = os.path.join(nav2_bringup_dir,'rviz','nav2_default_view.rviz')

    #=============================3.声明启动launch文件,传入:地图路径、是否使用仿真时间以及nav2参数文件==============
    nav2_bringup_launch = IncludeLaunchDescription(
            PythonLaunchDescriptionSource([nav2_bringup_dir,'/launch','/bringup_launch.py']),
            launch_arguments={
                'map': map_yaml_path,
                'use_sim_time': use_sim_time,
                'params_file': nav2_param_path}.items(),
        )
    rviz_node =  Node(
            package='rviz2',
            executable='rviz2',
            name='rviz2',
            arguments=['-d', rviz_config_dir],
            parameters=[{'use_sim_time': use_sim_time}],
            output='screen')
    
    return LaunchDescription([nav2_bringup_launch,rviz_node])

5、修改CmakeList.txt,添加路径:

install(
  DIRECTORY launch param maps
  DESTINATION share/${PROJECT_NAME}
)

7、构建

colcon build --packages-up-to  fishbot_navigation2

8、输出机器人在地图中的位置

ros2 run tf2_ros  tf2_echo map base_link

你可能感兴趣的:(笔记)