SLAM for beginners: build a robot that maps a room

How SLAM works, the sensor and compute options, the ROS 2 stack, a seven-step build sequence and why maps come out smeared.

Reference
RE-574
Published
Read
5 min

About the author · Sources, testing and AI policy

Wheeled robot with a 2D lidar building an occupancy map shown on a laptop

SLAM is how a robot answers two questions at once: where am I, and what does the place look like? It is the difference between a robot that follows a scripted path and one that can be dropped into a new building and find its way. The good news is that open-source 2D SLAM is mature and runs on affordable hardware. The hard part is the plumbing around it: odometry, time sync, sensor mounting and expectations.

What SLAM actually does

Simultaneous Localisation and Mapping estimates the robot’s trajectory while building a map of the environment. Each new sensor scan is matched against what the robot already believes, and both the map and the position are corrected together. That is why SLAM drifts less than dead reckoning: it keeps closing loops against physical features.

For most indoor service robots, “SLAM” means 2D lidar SLAM: a horizontal laser scanner produces a floor-plan slice, wheel encoders provide motion, and a package such as slam_toolbox builds an occupancy grid. This is well understood, cheap and adequate for warehouses, hospitals, showrooms and corridors.

Three sensor routes

ApproachSensorsStrengthsWeaknesses
2D lidar SLAM2D lidar plus wheel odometryMature, cheap, reliable indoors, easy ROS 2 supportSingle plane; struggles with glass, ramps and 3D clutter
Visual SLAMOne or two cameras, optionally IMUNo moving part, rich information, works in feature-rich roomsSensitive to lighting, motion blur and textureless walls; more compute
Depth or 3D lidar SLAMRGB-D camera or 3D lidar3D maps, better obstacle understandingHigher cost and compute; heavier calibration

For a first project, 2D lidar plus encoders on a differential-drive base is still the shortest path to a working map. It teaches the concepts without drowning you in calibration.

What it costs in India

PartPlanning rangeComment
Entry 2D lidar, 12 m range₹8,000–15,000Enough for a room-scale map
Mid 2D lidar, 16–25 m₹20,000–45,000Better for corridors and larger halls
Depth camera₹20,000–40,000For visual or RGB-D SLAM
Wheel encoders₹1,500–6,000 per pairOptical or magnetic; resolution matters more than brand
IMU₹300–3,000Cheap MEMS is fine for fusion, not for precision heading
Compute₹8,000–60,000Raspberry Pi 5 for 2D; Jetson class for visual SLAM

A working 2D SLAM prototype can be built for under ₹30,000 in sensors and compute if you already have a chassis. A visual SLAM prototype costs more in compute and time.

The ROS 2 stack that does the work

  • Lidar driver publishes a sensor_msgs/LaserScan.
  • Odometry publishes the transform from odom to base_link, usually from wheel encoders or a motor controller.
  • slam_toolbox consumes scans and odometry, publishes the map and the map to odom correction.
  • Nav2 uses the saved map for localisation and path planning once mapping is done.

The transform tree is where beginners lose days. The chain must be consistent: map to odom comes from SLAM or localisation, odom to base_link comes from your wheel odometry, and base_link to laser comes from the static mounting position. If any link is missing or published twice, the map tears or the robot teleports.

A realistic build sequence

  • Step 1: drive straight. Calibrate wheel diameter and track width until the robot drives a measured 2 m within a few centimetres.
  • Step 2: publish odometry. Confirm odom to base_link moves smoothly when you push the robot by hand.
  • Step 3: mount the lidar level. A tilted scanner produces a map that spirals. Keep it away from the robot’s own body and cables.
  • Step 4: teleop and map one room. Drive slowly, make a loop, and return near the start. Watch whether walls overlap when you close the loop.
  • Step 5: save the map. Export both the occupancy image and the YAML metadata; keep them together.
  • Step 6: localise on the saved map. Start the robot at a known pose and check that the scan aligns with the map before running navigation.
  • Step 7: add Nav2. Tune the costmap inflation radius before tuning planners. Most “bad planner” problems are actually bad costmaps.

Why maps come out bad

SymptomLikely causeFix
Walls doubled or smearedOdometry scale wrong or wheel slipCalibrate wheel diameter and track width; reduce speed
Map curves over a long runGyro drift or poor scan matchingAdd an IMU, slow down, map in smaller loops
Glass or mirrors vanishLidar sees through or reflectsAdd physical landmarks or map those areas manually
Robot loses position after a crowdDynamic obstacles dominate the scanFilter short-lived returns; localise in quieter areas
Map fine, navigation failsCostmap or footprint wrongSet the real robot radius and inflation, check the TF tree

What accuracy to expect

In a clean indoor environment with a decent 2D lidar and calibrated odometry, a small robot can localise within roughly 2–5 cm and a few degrees. Long corridors with no features are harder, and drift grows with distance between loop closures. Outdoors, sunlight interferes with many 2D lidars, and 3D or visual methods become more attractive.

Do not promise centimetre accuracy to a customer until you have measured it in their building, on their floor, at their busiest hour.

Learn SLAM in the right order

Start with a simulated robot in Gazebo to learn the ROS 2 graph without hardware faults. Then move to a real base with encoders and a lidar. Only after 2D mapping works should you attempt visual SLAM. Each step adds one variable; skipping steps adds several at once and makes debugging impossible.

Related: ROS 2 for beginners, choosing distance sensors, wheel encoders and odometry, OpenCV robot vision, AGV vs AMR.

What is SLAM in robotics in simple terms?

SLAM lets a robot build a map of an unknown place while simultaneously working out where it is on that map. It combines sensor scans with motion estimates so the map and the position correct each other.

Can I run SLAM on a Raspberry Pi 5?

Yes for 2D lidar SLAM at modest resolution. Visual SLAM is possible but limited; a Jetson-class computer gives much better frame rates for camera-based methods.

Do I need a lidar for SLAM?

No. Visual and depth-camera SLAM exist. But 2D lidar SLAM is the most reliable and best documented option for indoor wheeled robots, and entry lidars are affordable in India.

Why does my SLAM map look smeared?

Usually because wheel odometry is miscalibrated or the robot is moving too fast for scan matching. Check wheel diameter and track width, slow down, and confirm the lidar is mounted level.

What is the difference between mapping and localisation?

Mapping builds a new map while estimating position. Localisation uses an existing map to work out where the robot is. In ROS 2, slam_toolbox handles mapping and Nav2 handles localisation and planning on the saved map.

Once the map is saved, the Nav2 tutorial shows how to localise and navigate on it.

If the hardware is not ready, simulate the robot in Gazebo and run the same SLAM nodes.

Mapping runs usually start several nodes at once: ROS 2 launch files tutorial.

SLAM publishes the map to odom transform; ROS 2 TF2 tutorial explains the chain.

For the ROS 2 implementation, follow the SLAM Toolbox tutorial.

For a cheap indoor fix, fiducial markers such as ArUco give a known pose without a full SLAM stack.

Sources and further reading

Pass it onWhatsAppLinkedIn

Keep reading

Continue learning

The Build Note

One short email when something is worth your bench time.

A new build sheet, a part price that moved, or a mistake worth avoiding. No daily drip, no recycled news.

One short email when a build or a price is worth your time. Unsubscribe in one click.