ROS-NAV2- practice 3- drive a robot to build a map

By ihsumlee , 28 February 2026
content

Right now you’re just not exploring enough (or not moving the robot correctly), so SLAM only sees a small area and can’t complete the map.

Below is the clean way to build a full map in Stage + SLAM Toolbox.


1) Important concept: SLAM needs

motion + new viewpoints

A laser only “draws” what it can see from the robot’s current pose. To fill the map you must:

  • move around obstacles,

  • change heading frequently,

  • revisit areas to close loops.

If robot stays near the start, map stays partial.


2) Don’t “drag” the robot with mouse (usually not recommended)

In Stage, moving the robot by mouse can “teleport” it. SLAM hates teleporting because it breaks motion continuity.

Instead, drive the robot using cmd_vel (teleop).


3) Drive robot_0 properly (keyboard teleop)

3.1 Find the cmd_vel topic

ros2 topic list | grep cmd_vel

You’ll likely see:

  • /robot_0/cmd_vel

3.2 Install keyboard teleop (if needed)

sudo apt update
sudo apt install -y ros-jazzy-teleop-twist-keyboard

3.3 Drive robot_0

Open a new terminal:

source /opt/ros/jazzy/setup.bash
source ~/nav_class_ws/install/setup.bash
ros2 run teleop_twist_keyboard teleop_twist_keyboard --ros-args -r cmd_vel:=/robot_0/cmd_vel

Use keys:

  • i forward

  • j left turn

  • l right turn

  • k stop


4) A “map-completion route” that works (do this pattern)

Do this in order (takes ~3–5 minutes):

  1. 360° scan at start

    Rotate slowly in place one full circle.

  2. Wall-following loop

    Move forward along the boundary of the free space, turning at corners.

  3. Center sweep

    Do a “lawnmower” pattern through open areas.

  4. Loop closure

    Return near the start and rotate again.

This helps SLAM “stitch” the map and reduce gray unknown areas.


5) RViz settings that help mapping (keep it lightweight)

  • Fixed Frame: map

  • Displays:

    • Map

    • LaserScan

  • Turn TF display OFF (you already know)

  • Lower Frame Rate to 10 if VM is slow


6) If map still won’t grow: 3 quick checks

Check A: Are scans flowing into SLAM?

ros2 topic echo /robot_0/base_scan --once

Check B: Is SLAM publishing continuously?

ros2 topic echo /map_metadata --once

If this prints, SLAM is actively updating the map.

Check C: Is robot actually moving (odom changes)?

ros2 topic echo /robot_0/odom --once

If odom stays constant, your teleop isn’t reaching the robot (wrong cmd_vel topic).


7) When you’re satisfied: save the map

mkdir -p ~/nav_class_assets/maps
ros2 run nav2_map_server map_saver_cli -f ~/nav_class_assets/maps/classroom_map

Tags