CodeTheRobot

ROS 2 Lyrical Luth · Lesson 57 of 86

Map server

Map YAML and image files, map_server, map_saver and the occupancy grid.

Estimated time · 45–60 min

Learning goals

What you will understand

  • Explain map server 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

A map is an occupancy grid: an image where each pixel is free, occupied or unknown, plus a YAML file with the image name, resolution (metres per pixel), origin (pose of the lower-left corner) and thresholds. map_server (a lifecycle node) loads the files and publishes nav_msgs/msg/OccupancyGrid on /map with transient local QoS.

Save a map made by SLAM with nav2_map_server's map_saver_cli -f name. Check that resolution and origin match reality, or every plan will be shifted. Keep one map per floor or area and version it with the world it describes.

Guided lesson

Work through these steps

  1. 1.

    Source the ROS 2 Lyrical environment in a fresh terminal.

  2. 2.

    Read the map server 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

image: warehouse.pgm
# The picture: white is free, black is occupied, grey is unknown.
mode: trinary
# Interpret the image as three states.
resolution: 0.05
# Each pixel is 5 cm.
origin: [-10.0, -10.0, 0.0]
# The lower-left pixel is at (-10, -10) m in the map frame.
occupied_thresh: 0.65
# Darker than this counts as occupied.
free_thresh: 0.25
# Lighter than this counts as free.
negate: 0
# Do not invert the colours.
# ros2 run nav2_map_server map_saver_cli -f ~/maps/warehouse
# Save the map currently on /map to warehouse.yaml and warehouse.pgm.

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 Map server. Expected result: /map shows the warehouse in RViz2

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.

# warehouse.yaml
image: warehouse.pgm
mode: trinary
# TODO: set resolution to 0.05 m per pixel and origin to [-10.0, -10.0, 0.0]
occupied_thresh: 0.65
free_thresh: 0.25
negate: 0

Expected output

/map shows the warehouse in RViz2
Stuck? Show the full solution (try it yourself first)
# warehouse.yaml
image: warehouse.pgm
mode: trinary
resolution: 0.05
origin: [-10.0, -10.0, 0.0]
occupied_thresh: 0.65
free_thresh: 0.25
negate: 0

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

Write a map YAML for a picture with 0.05 m pixels, load it with map_server and lifecycle manager, view it in RViz2, and verify a known wall lies at the right coordinates.

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.