> ## 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 with cartographer_ros

The 2D lidar SLAM sample application is based on `Cartographer`, which is capable of completing indoor map construction and localization based on 2D lidar sensors. It's suitable for indoor navigation of robots.

<img
  src="https://mintcdn.com/qualcomm-prod/IP0o0G5fE3MFmCJ8/SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/result_2d_slam.png?fit=max&auto=format&n=IP0o0G5fE3MFmCJ8&q=85&s=9ed54294730f6c0846be1883c703aeec"
  style={{
width: "50%",
display: "block",
margin: "0 auto" }}
  width="883"
  height="1040"
  data-path="SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/result_2d_slam.png"
/>

<p style={{ textAlign: 'center', fontWeight: 'bold' }}>
  Figure: Sample results of 2D lidar SLAM
</p>

## Pipeline flow for `2D lidar SLAM`

<img
  src="https://mintcdn.com/qualcomm-prod/Wc1juBQkT-boIg_Z/SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/2d-lidar-slam-ga1.6.svg?fit=max&auto=format&n=Wc1juBQkT-boIg_Z&q=85&s=ec3606245d54b4cedbd8a11278ec33df"
  style={{
width: "100%",
display: "block",
margin: "0 auto" }}
  width="570"
  height="330"
  data-path="SDKs/QIR-SDK-2.0/media/80-65220-2-qirp-sdk-qsg/2d-lidar-slam-ga1.6.svg"
/>

<p style={{ textAlign: 'center', fontWeight: 'bold' }}>
  Figure: Pipeline flow for 2D lidar SLAM
</p>

## ROS nodes used in the 2D lidar SLAM pipeline

| **ROS node**                       | **Description**                                                                                                                                                                   |
| :--------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `cartographer_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` | Receives map-related topics sent by the `cartographer_node` and publishes a ROS standard map (`/static_map`).                                                                     |

## ROS topics/services used in 2D lidar SLAM

<p style={{ textAlign: 'center', fontWeight: 'bold' }}>
  Table: ROS topics used in 2D lidar SLAM
</p>

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

<p style={{ textAlign: 'center', fontWeight: 'bold' }}>
  Table: ROS services used in in 2D lidar SLAM
</p>

<table>
  <thead>
    <tr>
      <th style={{ width: '30%' }}>ROS service</th>
      <th style={{ width: '40%' }}>Type</th>
      <th style={{ width: '30%' }}>Provided by</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>`/qrb_slam_command`</td>
      <td>`qrb_ros_slam_msgs.srv.SlamCommand`</td>
      <td>`cartographer_node`</td>
    </tr>
  </tbody>
</table>

## Prerequisites

* You have **Set up the device** according to [Set up the environment for running sample applications](./set-up-env-for-sample-app).
* Connect the power supply and USB port of RPLIDAR A3M1.

## Run out-of-the-box `2D lidar SLAM`

<Accordion title="Try me">
  <Steps>
    <Step title="Set up the robot base">
      <Note>
        **Note** The following configuration applies exclusively to Qualcomm’s robot base. If you are using a custom or third-party robot base, skip this configuration step and apply configurations appropriate to your own robot base.
      </Note>

      1. Open a new terminal connect to the device.
         ```bash theme={null}
         source /usr/share/qirp-setup.sh
         export ROS_DOMAIN_ID=124
         ```
      2. Select the robot base model.
         * 0604 AMR: `robot_base` (default)
         * Mini AMR: `robot_base_mini`
         ```bash theme={null}
         export ROBOT_BASE_MODEL=robot_base_mini
         ```
      3. (Optional) If you don't connect the ultrasonic sensor to the MCB, disable ultrasound emergency stop.
         ```bash theme={null}
         vi /usr/ros/jazzy/share/qrb_ros_robot_base/config/[robot base model file]
         # modify ultra_enable: false
         ```
      4. Launch the robot base AMR.
         ```bash theme={null}
         ros2 launch qrb_ros_robot_base robot_base.launch.py
         ```
    </Step>

    <Step title="Set up the 2D lidar">
      In a new terminal, run the following commands.

      ```bash theme={null}
      source /usr/share/qirp-setup.sh
      export ROS_DOMAIN_ID=124
      ros2 launch rplidar_ros rplidar_a3_launch.py
      ```
    </Step>

    <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=124
      ros2 launch cartographer_ros qrb_2d_lidar_slam.launch.py
      ```
    </Step>

    <Step title="Run the qrb_slam_service_request script">
      In a new terminal, run the following commands.

      ```bash theme={null}
      source /usr/share/qirp-setup.sh
      export ROS_DOMAIN_ID=124
      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">
      In the terminal opened in the preceding step, use the keyboard to send control commands, such as starting the mapping process.
    </Step>
  </Steps>
</Accordion>
