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

# Enable 2D lidar SLAM in the simulator with cartographer_ros

The [QRB ROS Simulation](https://github.com/qualcomm-qrb-ros/qrb_ros_simulation) package provides the simulation environment. It allows you to control the movement of QRB AMRs and get the scanning data of lidar through a ROS topic.

<Note>
  **Note**

  For more information, see [simulation\_2d\_lidar\_slam](https://github.com/qualcomm-qrb-ros/qrb_ros_samples/tree/main/robotics/simulation_2d_lidar_slam) on GitHub.
</Note>

<video
  autoPlay
  muted
  loop
  playsInline
  width={1016}
  height={480}
  src="https://mintcdn.com/qualcomm-prod/EDJV-hu6qJhZi4pm/SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/simulation-2d-lidar-slam-result.mp4?fit=max&auto=format&n=EDJV-hu6qJhZi4pm&q=85&s=d20522c7b5316b6d0dc1df99394653c4"
  style={{
width: "100%",
display: "block",
margin: "0 auto" }}
  data-path="SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/simulation-2d-lidar-slam-result.mp4"
/>

<p style={{ textAlign: 'center', fontWeight: 'bold' }}>
  Figure: Sample effects of `simulation_2d_lidar_slam`
</p>

## Pipeline flow for `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' }}>
  Figure: Pipeline flow for `simulation_2d_lidar_slam`
</p>

## ROS nodes used in the `simulation_2d_lidar_slam `pipeline

| **ROS node**                       | **Description**                                                                                                                                                                                |
| :--------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `cartographer_node`                | This node receives `/scan` and `/odom` topics, executes mapping, localization, and saves the map or queries the relocation status. The node is based on the ROS service `qrb_slam_command`.    |
| `cartographer_occupancy_grid_node` | This node receives map-related topics sent by the `cartographer_node` and publishes a ROS standard map (`/static_map`).                                                                        |
| `qrb_ros_simulation`               | `qrb ros simulation` is a ROS package that sets up the Qualcomm robotic simulation environment. For details, see [QRB ROS Simulation](https://github.com/qualcomm-qrb-ros/qrb_ros_simulation). |

## ROS topics used in the `simulation_2d_lidar_slam `pipeline

<table>
  <thead>
    <tr>
      <th style={{ width: '25%' }}>ROS topic</th>
      <th style={{ width: '45%' }}>Type</th>
      <th style={{ width: '30%' }}>Published by</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>

## Prerequisites

You have completed the following settings in [Set up the environment for running sample applications](./set-up-env-for-sample-app) (Skip the settings of **Set up host docker**).

* Set up the device
* Set up the host computer

<Note>
  **Note**

  Ensure that the board is connected to the simulation host using an Ethernet cable. Otherwise, the sample's performance may degrade due to data transmission issues.
</Note>

### Set up the simulation environment

1. On your host computer, complete the following actions to build the Qualcomm robotic simulation environment according to the **README** of [QRB ROS Simulation](https://github.com/qualcomm-qrb-ros/qrb_ros_simulation). Ensure that the device and the host are in the same local network and can communicate with each other through ROS communication.
   1. Set up development environment on Ubuntu24.04 host.
   2. Build from source.
2. Run the QRB Robot Base AMR on the host.
   ```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
   ```

## Run out-of-the-box 2d lidar SLAM in the simulator

<Accordion title="Try me">
  <Steps>
    <Step title="Run the 2D lidar SLAM">
      In a new terminal, run the following commands.

      ```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="Run the qrb_slam_service_request script">
      In a new terminal, run the following commands.

      ```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="Start mapping">
      Use the keyboard to send control commands, such as <kbd>0</kbd>: start mapping.
    </Step>
  </Steps>
</Accordion>
