CodeTheRobot

ROS 2 Lyrical Luth · Lesson 60 of 86

AMCL: localization

Particle filter localization on a known map, initial pose and tuning.

Estimated time · 60–90 min

Learning goals

What you will understand

  • Explain amcl: localization in your own words
  • Use it in a small robotics example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

AMCL (Adaptive Monte Carlo Localization) estimates the robot's pose on a known map with a particle filter: many pose guesses are weighted by how well the current scan matches the map and resampled. It publishes the map → odom transform, so map → base_link is the estimated robot pose.

It needs an initial pose (RViz2 '2D Pose Estimate' or the /initialpose topic or set_initial_pose parameters). Tune min/max_particles, the laser model and the odometry noise (alpha1–alpha5). If localization jumps, check the map, the lidar frame and the odometry quality first.

Guided lesson

Work through these steps

  1. 1.

    Source the ROS 2 Lyrical environment in a fresh terminal.

  2. 2.

    Read the amcl: localization example and identify what each line does.

  3. 3.

    Build only the affected package, source the workspace overlay, and run the example.

  4. 4.

    Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.

Worked example

Read it before you run it

amcl:
# Node section.
  ros__parameters:
# ROS parameters.
    base_frame_id: "base_footprint"
# The robot's base frame.
    odom_frame_id: "odom"
# The odometry frame it corrects against.
    global_frame_id: "map"
# The map frame it publishes into.
    scan_topic: scan
# Which lidar topic to use.
    min_particles: 500
# Fewest pose guesses.
    max_particles: 2000
# Most pose guesses.
    set_initial_pose: true
# Start localized without a manual click...
    initial_pose: {x: 0.0, y: 0.0, yaw: 0.0}
# ...at this pose in the map.

What the example is doing

  1. 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
  2. 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
  3. 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.

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 particle cloud tightens around the robot

Practice code

Now type it yourself

Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.

amcl:
  ros__parameters:
    global_frame_id: "map"
    odom_frame_id: "odom"
# TODO: set base_frame_id to "base_footprint", min_particles to 500 and max_particles to 2000

Expected output

The particle cloud tightens around the robot
Stuck? Show the full solution (try it yourself first)
amcl:
  ros__parameters:
    global_frame_id: "map"
    odom_frame_id: "odom"
    base_frame_id: "base_footprint"
    min_particles: 500
    max_particles: 2000

Expected result

How you know it worked

  • ROS_DISTRO is lyrical in every terminal used for the exercise.
  • The complete package from the exercise builds and the expected graph entity appears in a second terminal.

Practical exercise

Apply the lesson

Localize a simulated robot on a saved map, give a wrong initial pose and watch the particles converge (or not) as you drive, then plot map → base_link against the simulator's true pose.

Before you continue

  • ROS_DISTRO reports lyrical in every terminal you used
  • The example builds and runs without errors
  • You checked the result with a ros2 command, RViz2 or a test, not just by reading the code

If something goes wrong

ros2 cannot find a package or executable.

Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.

Nodes run but cannot discover each other.

Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.