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

> The 2D lidar SLAM sample application builds 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.

## Pipeline flow for 2D lidar SLAM

The following figure shows the pipeline flow for 2D lidar simultaneous localization and mapping (SLAM):

<Frame caption="2D lidar SLAM pipeline.">
  <img src="https://mintcdn.com/qualcomm-prod/EDJV-hu6qJhZi4pm/SDKs/QIR-SDK-Ubuntu/images/image32.svg?fit=max&auto=format&n=EDJV-hu6qJhZi4pm&q=85&s=850143248d393e5014f8f7b5ff04ea34" alt="2D lidar scan, robot base odometry, and the QRB ROS SLAM client feeding the Cartographer ROS node and the Cartographer occupancy grid node." width="570" height="330" data-path="SDKs/QIR-SDK-Ubuntu/images/image32.svg" />
</Frame>

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

The following table describes the ROS nodes used in the 2D lidar SLAM pipeline.

| Node name                          | Function                                                                                                                                                                      |
| ---------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `cartographer_node`                | Receives `/scan` and `/odom` topics, runs 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 (`/map`).                                                                        |

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

The following table describes the ROS topics used in the 2D lidar SLAM pipeline.

| ROS topic               | Type                                   | Description                                                             |
| ----------------------- | -------------------------------------- | ----------------------------------------------------------------------- |
| `/scan`                 | `sensor_msgs.msg.LaserScan`            | Receives lidar scan data.                                               |
| `/odom`                 | `nav_msgs.msg.Odometry2`               | Receives odometry data.                                                 |
| `/tracked_pose`         | `geometry_msgs.msg.PoseStamped`        | Publishes the current tracked robot pose.                               |
| `/scan_matched_points2` | `sensor_msgs.msg.PointCloud2`          | Publishes scan-matched point cloud for debugging and visualization.     |
| `/tf`                   | `tf2_msgs.msg.TFMessage`               | Publishes coordinate transform information for multi-frame conversions. |
| `/submap_list`          | `cartographer_ros_msgs.msg.SubmapList` | Publishes submap list for map stitching and visualization.              |
| `/map`                  | `nav_msgs.msg.OccupancyGrid`           | Publishes the occupancy grid map.                                       |

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

The following table describes the ROS services used in the 2D lidar SLAM pipeline.

| ROS service         | Type                                | Description                                                                                                                                                                                  |
| ------------------- | ----------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `/qrb_slam_command` | `qrb_ros_slam_msgs.srv.SlamCommand` | Provides SLAM control services, such as start, stop, and save map. See [qrb\_ros\_slam\_msgs](https://github.com/qualcomm-qrb-ros/qrb_ros_interfaces/tree/main/qrb_ros_slam_msgs) on GitHub. |

## Prerequisites

You have set up the device, installed ROS2 Jazzy and the Qualcomm Intelligent Robotics (QIR) SDK on the device according to [Install the QIR SDK](./install-the-qir-sdk).

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

<Steps>
  <Step title="Install the 2D lidar SLAM packages">
    ```bash theme={null}
    sudo apt install ros-jazzy-qcom-cartographer ros-jazzy-qrb-ros-slam-msgs ros-jazzy-qcom-cartographer-ros
    ```
  </Step>

  <Step title="Set up the robot base">
    * If you have a Qualcomm’s robot base, set it up.

    * If you are using a custom or third-party robot base, apply configurations appropriate to your own robot base. Ensure the robot base can publish odometry data of the type `nav_msgs.msg.Odometry2` on the topic `/odom`.
  </Step>

  <Step title="Set up the 2D lidar">
    Set up the 2D lidar according to [Enable basic RPLIDAR handling with `rplidar-ros2`](./enable-basic-rplidar-handling-with-rplidar-ros2).

    ```bash theme={null}
    source /opt/ros/jazzy/setup.bash export ROS_DOMAIN_ID=124
    ros2 launch rplidar_ros rplidar_a3_launch.py
    ```
  </Step>

  <Step title="Run the 2D lidar SLAM">
    ```bash theme={null}
    source /opt/ros/jazzy/setup.bash
    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">
    ```bash theme={null}
    #Env set up
    source /opt/ros/jazzy/setup.bash
    export ROS_DOMAIN_ID=124
    sh /opt/ros/jazzy/share/cartographer_ros/scripts/qrb_slam_service_request.sh
    ```
  </Step>
</Steps>
