> ## Documentation Index
> Fetch the complete documentation index at: https://dragonwingdocs.qualcomm.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 使用 navigation2 在模拟器中启用导航

`simulation_amr_navigation` 是一个仿真示例应用程序，演示如何在模拟器中的 Qualcomm 机器人平台上运行 `navigation2`。

[QRB ROS Simulation](https://github.com/qualcomm-qrb-ros/qrb_ros_simulation) 是一个用于搭建 Qualcomm 机器人仿真环境的 ROS 项目。

<video
  autoPlay
  muted
  loop
  playsInline
  width={1280}
  height={720}
  src="https://mintlify.s3.us-west-1.amazonaws.com/qualcomm-prod/zh/SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/simulation-amr-navigation-result.mp4"
  style={{
width: "100%",
display: "block",
margin: "0 auto" }}
/>

<p style={{ textAlign: 'center', fontWeight: 'bold' }}>
  图：`simulation_amr_navigation` 的示例结果
</p>

## `simulation_amr_navigation` 的管道流程

<img
  src="https://mintcdn.com/qualcomm-prod/IP0o0G5fE3MFmCJ8/SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/simulation-amr-navigation-pipeline.svg?fit=max&auto=format&n=IP0o0G5fE3MFmCJ8&q=85&s=a28881244d2b5b86dbed059605cc7835"
  style={{
width: "90%",
display: "block",
margin: "0 auto" }}
  width="522"
  height="402"
  data-path="SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/simulation-amr-navigation-pipeline.svg"
/>

<p style={{ textAlign: 'center', fontWeight: 'bold' }}>
  图：`simulation_amr_navigation` 的管道流程
</p>

## `simulation_amr_navigation `管道中使用的 ROS 节点

| **ROS 节点**           | **描述**                                                                                                                                     |
| :------------------- | :----------------------------------------------------------------------------------------------------------------------------------------- |
| `qrb_ros_simulation` | `qrb ros simulation` 是一个用于搭建 Qualcomm 机器人仿真环境的 ROS 软件包。详情请参阅 [QRB ROS Simulation](https://github.com/qualcomm-qrb-ros/qrb_ros_simulation)。 |
| `navigation2 nodes`  | `navigation2` 是 ROS Navigation Stack 的专业支持继任者。它使移动机器人能够在复杂环境中导航，以完成用户定义的应用任务，几乎适用于任何类型的机器人运动学。                                             |

## `simulation_amr_navigation `管道中使用的 ROS 话题

<table>
  <thead>
    <tr>
      <th style={{ width: '20%' }}>ROS 话题</th>
      <th style={{ width: '30%' }}>类型</th>
      <th style={{ width: '20%' }}>发布者</th>
      <th style={{ width: '30%' }}>描述</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`/map`</td>
      <td>`nav_msgs::msg::OccupancyGrid`</td>
      <td>`cartographer_occupancy_grid_node`</td>
      <td>环境的 2D 占据栅格地图。</td>
    </tr>

    <tr>
      <td>`/tf`</td>
      <td>`tf2_msgs::msg::TFMessage`</td>
      <td>`cartographer_occupancy_grid_node`、`qrb_ros_simulation`</td>
      <td>不同坐标系之间的空间关系。</td>
    </tr>

    <tr>
      <td>`/scan`</td>
      <td>`sensor_msg::msg::LaserScan`</td>
      <td>`qrb_ros_simulation`</td>
      <td>2D 激光扫描数据。</td>
    </tr>

    <tr>
      <td>`/goal_pose`</td>
      <td>`geometry_msgs::msg::PoseStamped`</td>
      <td>`Application to call navigation2`</td>
      <td>导航的目标位姿，包括在地图坐标系中的位置和朝向。</td>
    </tr>
  </tbody>
</table>

## 前提条件

您已完成[为运行示例应用程序设置环境](./set-up-env-for-sample-app)中的以下设置（跳过 **Set up host docker** 的设置）。

* 设置设备
* 设置主机

<Note>
  **注意**

  确保开发板通过以太网线连接到仿真主机。否则，示例的性能可能会因数据传输问题而下降。
</Note>

### 设置仿真环境

1. 在您的主机上，根据 [QRB ROS Simulation](https://github.com/qualcomm-qrb-ros/qrb_ros_simulation) 的 **README** 完成以下操作，搭建 Qualcomm 机器人仿真环境。确保设备和主机位于同一本地网络中，并且可以通过 ROS 通信相互通信。
   1. 在 Ubuntu24.04 主机上设置开发环境。
   2. 从源代码构建。
2. 在主机上运行 QRB Robot Base AMR Mini。
   ```bash theme={null}
   # Set the ROS_DOMAIN_ID
   export ROS_DOMAIN_ID=123
   ros2 launch qrb_ros_sim_gazebo gazebo_robot_base_mini.launch.py world_model:=ionic
   ```

## 运行开箱即用的 `navigation2`

<Accordion title="试一试">
  <Steps>
    <Step title="配置 navigation2 参数">
      配置 `navigation2` 参数以使用 GracefulController。

      ```bash theme={null}
      sed -i.bak -E 's/^([[:space:]]*)plugin: "nav2_mppi_controller::MPPIController"/\1#plugin: "nav2_mppi_controller::MPPIController"\n\1plugin: "nav2_graceful_controller::GracefulController"/' /usr/ros/jazzy/share/nav2_bringup/params/nav2_params.yaml
      ```
    </Step>

    <Step title="运行 2D 激光雷达 SLAM">
      ```bash theme={null}
      source /usr/share/qirp-setup.sh
      export ROS_DOMAIN_ID=123
      ros2 launch cartographer_ros qrb_2d_lidar_slam_gazebo_sim.launch.py
      ```
    </Step>

    <Step title="运行 qrb_slam_service_request 脚本">
      在新终端中，运行以下命令。

      ```bash theme={null}
      ssh root@[IP address]
      source /usr/share/qirp-setup.sh
      export ROS_DOMAIN_ID=123
      chmod 777 /usr/ros/jazzy/share/cartographer_ros/scripts/qrb_slam_service_request.sh
      bash /usr/ros/jazzy/share/cartographer_ros/scripts/qrb_slam_service_request.sh
      ```
    </Step>

    <Step title="开始建图">
      使用键盘发送 `0` 控制指令以开始建图。
    </Step>

    <Step title="运行 navigation2">
      在新终端中，运行以下命令。

      ```bash theme={null}
      source /usr/share/qirp-setup.sh
      export ROS_DOMAIN_ID=123
      ros2 launch nav2_bringup navigation_launch.py use_sim_time:=true
      ```
    </Step>

    <Step title="发送导航目标">
      在新终端中，运行以下命令以发送导航目标并开始导航。

      ```bash theme={null}
      export ROS_DOMAIN_ID=123
      ros2 action send_goal /navigate_to_pose nav2_msgs/action/NavigateToPose "{pose: {header: {stamp: {sec: 0, nanosec: 0},frame_id: 'map'},pose: {position: {x: 1.0,y: 1.0,z: 0.0},orientation: {x: 0.0,y: 0.0,z: 0.0,w: 1.0}}}}"
      ```
    </Step>
  </Steps>
</Accordion>
