CodeTheRobot

Ubuntu Linux for Robotics · Lesson 76 of 96

AMCL localization

Localizing on a saved map, setting the initial pose and checking convergence.

Estimated time · 45–75 min

Learning goals

What you will understand

  • Explain amcl localization 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

AMCL localizes the robot on a known map using a particle filter: it compares the current scan with the map and publishes the map → odom transform. Provide the map through the map server, set the initial pose (RViz2 '2D Pose Estimate' or the /initialpose topic), then drive a little so the particles converge.

If the pose jumps, check the map resolution and origin, the lidar frame and the quality of odometry. Verify with ros2 run tf2_ros tf2_echo map base_link, which is the robot's pose in the map.

Guided lesson

Work through these steps

  1. 1.

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

  2. 2.

    Read the amcl localization 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

ros2 launch nav2_bringup localization_launch.py map:=$HOME/maps/office.yaml use_sim_time:=true
# Start the map server and AMCL.
ros2 topic pub --once /initialpose geometry_msgs/msg/PoseWithCovarianceStamped "{header: {frame_id: map}, pose: {pose: {position: {x: 0.0, y: 0.0}, orientation: {w: 1.0}}}}"
# Give an initial pose from the terminal.
ros2 run tf2_ros tf2_echo map base_link
# Read the robot's pose in the map.
ros2 topic echo /amcl_pose --once
# See the estimate and its covariance.

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 AMCL localization. Expected result: The pose of the robot in the map frame

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: print one /amcl_pose message

Expected output

The pose of the robot in the map frame
Stuck? Show the full solution (try it yourself first)
ros2 topic echo /amcl_pose --once

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

Localize a simulated robot on a saved map, give a wrong initial pose and watch whether the particles recover as you drive, and compare the estimate with the simulator's true pose.

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.