CodeTheRobot

Ubuntu Linux for Robotics · Lesson 75 of 96

SLAM Toolbox

Live mapping, driving to map, saving the map and the pose graph.

Estimated time · 60–90 min

Learning goals

What you will understand

  • Explain slam toolbox in your own words
  • Use it in a small workstation example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

SLAM Toolbox builds a 2D map while estimating the robot's pose. Install it with sudo apt install ros-lyrical-slam-toolbox and start it with the online_async launch file. It uses /scan and odometry and publishes /map and the map → odom transform.

Drive slowly, avoid fast rotations, and close loops by returning to known places. Save the map with map_saver_cli when finished. Use simulated time when mapping in Gazebo (use_sim_time:=true).

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the slam toolbox commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

sudo apt install ros-lyrical-slam-toolbox
# Install SLAM Toolbox.
ros2 launch slam_toolbox online_async_launch.py use_sim_time:=true
# Start live mapping.
ros2 run teleop_twist_keyboard teleop_twist_keyboard
# Drive the robot slowly around the space.
rviz2
# Watch the map grow on the /map topic.
ros2 run nav2_map_server map_saver_cli -f ~/maps/office
# Save the finished map (office.yaml and office.pgm).

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

Watch it run · Mobile robot

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

goalMOBILE ROBOT

Demo for SLAM Toolbox. Expected result: office.yaml and office.pgm are written

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

# TODO: save the current map to ~/maps/office with map_saver_cli

Expected output

office.yaml and office.pgm are written
Stuck? Show the full solution (try it yourself first)
ros2 run nav2_map_server map_saver_cli -f ~/maps/office

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

Map a simulated room: drive slowly, close a loop, save the map, and repeat while driving too fast to see the map distort.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.