> ## 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.

# 使用 cartographer_ros 在模拟器中启用 2D 激光雷达 SLAM

[QRB ROS Simulation](https://github.com/qualcomm-qrb-ros/qrb_ros_simulation) 软件包提供了仿真环境。它允许您控制 QRB AMR 的运动，并通过 ROS 话题获取激光雷达的扫描数据。

<Note>
  **注意**

  更多信息，请参阅 GitHub 上的 [simulation\_2d\_lidar\_slam](https://github.com/qualcomm-qrb-ros/qrb_ros_samples/tree/main/robotics/simulation_2d_lidar_slam)。
</Note>

<video
  autoPlay
  muted
  loop
  playsInline
  width={1016}
  height={480}
  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-2d-lidar-slam-result.mp4"
  style={{
width: "100%",
display: "block",
margin: "0 auto" }}
/>

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

## `simulation_2d_lidar_slam` 的管道流程

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

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

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

| **ROS 节点**                         | **描述**                                                                                                                                     |
| :--------------------------------- | :----------------------------------------------------------------------------------------------------------------------------------------- |
| `cartographer_node`                | 该节点接收 `/scan` 和 `/odom` 话题，执行建图、定位，并保存地图或查询重定位状态。该节点基于 ROS 服务 `qrb_slam_command`。                                                          |
| `cartographer_occupancy_grid_node` | 该节点接收 `cartographer_node` 发送的地图相关话题，并发布 ROS 标准地图（`/static_map`）。                                                                           |
| `qrb_ros_simulation`               | `qrb ros simulation` 是一个用于搭建 Qualcomm 机器人仿真环境的 ROS 软件包。详情请参阅 [QRB ROS Simulation](https://github.com/qualcomm-qrb-ros/qrb_ros_simulation)。 |

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

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

  <tbody>
    <tr>
      <td>`/tracked_pose`</td>
      <td>`geometry_msgs.msg.PoseStamped`</td>
      <td>`cartographer_node`</td>
    </tr>

    <tr>
      <td>`/tf`</td>
      <td>`tf2_msgs.msg.TFMessage`</td>
      <td>`cartographer_node`</td>
    </tr>

    <tr>
      <td>`/submap_list`</td>
      <td>`cartographer_ros_msgs.msg.SubmapList`</td>
      <td>`cartographer_node`</td>
    </tr>

    <tr>
      <td>`/scan_matched_points2`</td>
      <td>`sensor_msgs.msg.PointCloud2`</td>
      <td>`cartographer_node`</td>
    </tr>

    <tr>
      <td>`/map`</td>
      <td>`nav_msgs.msg.OccupancyGrid`</td>
      <td>`cartographer_occupancy_grid_node`</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。
   ```bash theme={null}
   # Set the ROS_DOMAIN_ID
   export ROS_DOMAIN_ID=123
   ros2 launch qrb_ros_sim_gazebo gazebo_robot_base.launch.py world_model:=ionic
   ```

## 在模拟器中运行开箱即用的 2D 激光雷达 SLAM

<Accordion title="试一试">
  <Steps>
    <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="开始建图">
      使用键盘发送控制指令，例如 <kbd>0</kbd>：开始建图。
    </Step>
  </Steps>
</Accordion>
