Skip to content

Latest commit

 

History

History
484 lines (373 loc) · 10.7 KB

File metadata and controls

484 lines (373 loc) · 10.7 KB

Complete SLAM and Nav2 Setup Guide - Squarobot

What Was Fixed

Issue Identified

The original setup had issues with:

  1. RViz Fixed Frame: Was set to base_link instead of map for SLAM
  2. Missing Map Display: RViz config didn't include Map visualization
  3. Manual Configuration Required: User had to manually add displays in RViz

Solution Implemented

Created an all-in-one SLAM launch file that:

  • Launches Gazebo simulation with wall maze world
  • Starts SLAM Toolbox for mapping
  • Opens RViz2 with pre-configured SLAM displays (Map + LaserScan)
  • All ready to use in one command!

Complete File Structure

squarobot_gazebo/
├── config/
│   ├── slam_params.yaml          # SLAM Toolbox configuration
│   ├── nav2_params.yaml           # Nav2 navigation configuration
│   ├── sim.rviz                   # Basic simulation RViz config
│   └── slam.rviz                  # SLAM-specific RViz config (Fixed Frame: map, with Map display)
│
└── launch/
    ├── simulation.launch.py              # Gazebo only
    ├── simulation_with_rviz.launch.py    # Gazebo + RViz
    ├── slam.launch.py                    # SLAM Toolbox only
    ├── navigation.launch.py              # Nav2 only
    └── slam_simulation.launch.py         # ✨ ALL-IN-ONE: Gazebo + SLAM + RViz

Quick Start: SLAM Mapping (Easiest Method)

Method 1: All-in-One Command ⭐ RECOMMENDED

# Single command launches everything!
cd ~/Final
source install/setup.bash
ros2 launch squarobot_gazebo slam_simulation.launch.py

What this launches:

  • ✅ Gazebo with simple_wall world
  • ✅ SLAM Toolbox for mapping
  • ✅ RViz2 with map visualization pre-configured
  • ✅ All bridges and transforms

You should see:

  • Gazebo window with robot in maze
  • RViz window showing:
    • Red laser scan
    • Robot model
    • Blue/gray map building in real-time
    • Fixed Frame set to map (check top-left)

Control the Robot

Open a new terminal:

cd ~/Final
source install/setup.bash
ros2 run teleop_twist_keyboard teleop_twist_keyboard

Drive around the maze:

  • i = forward
  • k = stop
  • j = turn left
  • l = turn right
  • , = backward
  • u, o, m, . = diagonal movements

Watch the map build in RViz as you drive!

Save Your Map

When you've explored the entire maze:

cd ~/Final
ros2 run nav2_map_server map_saver_cli -f my_maze_map

Creates:

  • my_maze_map.yaml - Map metadata
  • my_maze_map.pgm - Map image

Method 2: Manual Launch (Step-by-Step)

If you want to launch components separately for learning:

Terminal 1: Simulation

cd ~/Final
source install/setup.bash
ros2 launch squarobot_gazebo simulation_with_rviz.launch.py

Terminal 2: SLAM

cd ~/Final
source install/setup.bash
ros2 launch squarobot_gazebo slam.launch.py

Terminal 3: Teleoperation

cd ~/Final
source install/setup.bash
ros2 run teleop_twist_keyboard teleop_twist_keyboard

In RViz:

  1. Change Fixed Frame from base_link to map
  2. Click AddBy topic/mapMapOK
  3. Now you'll see the map building

Navigation with Nav2 (After Mapping)

Prerequisites

  1. You must have a saved map (from SLAM above)
  2. Stop the SLAM launch if still running

Launch Navigation Stack

Terminal 1: Simulation

cd ~/Final
source install/setup.bash
ros2 launch squarobot_gazebo simulation_with_rviz.launch.py

Terminal 2: Map Server

cd ~/Final
source install/setup.bash
ros2 run nav2_map_server map_server --ros-args \
  -p yaml_filename:=$PWD/my_maze_map.yaml \
  -p use_sim_time:=true

Terminal 3: Lifecycle Manager (for map server)

source install/setup.bash
ros2 run nav2_lifecycle_manager lifecycle_manager --ros-args \
  -p use_sim_time:=true \
  -p autostart:=true \
  -p node_names:="['map_server']"

Terminal 4: Navigation

cd ~/Final
source install/setup.bash
ros2 launch squarobot_gazebo navigation.launch.py

Using Navigation in RViz

  1. Set Initial Pose:

    • Click "2D Pose Estimate" button (top toolbar)
    • Click on map where robot actually is
    • Drag to set orientation
  2. Set Navigation Goal:

    • Click "2D Goal Pose" button
    • Click where you want robot to go
    • Drag to set desired final orientation
  3. Watch autonomous navigation!


Verification & Troubleshooting

Check if SLAM is Working

# Check map topic is publishing
ros2 topic hz /map
# Should show updates every few seconds

# Check SLAM node is running
ros2 node list | grep slam
# Should show: /slam_toolbox

# Echo map to see data
ros2 topic echo /map --once

Check if Map Display is Visible in RViz

In RViz, verify:

  1. Fixed Frame: Top-left should show map (not base_link)
  2. Displays Panel: Should show "Map" with checkmark
  3. Map Topic: Should be /map
  4. Map Alpha: Set to 0.7 for semi-transparent

Common Issues & Fixes

Issue: Map not showing in RViz

Solution:

# Check Fixed Frame
# In RViz: Global Options → Fixed Frame → Change to "map"

# Verify map topic exists
ros2 topic list | grep map
# Should show: /map

# Check SLAM is publishing
ros2 topic hz /map

Issue: "No map received" in SLAM

Solution:

# SLAM needs time to initialize
# Drive robot a bit (at least 0.5m) to trigger first map update

# Verify scan is working
ros2 topic hz /scan
# Should show ~10 Hz

Issue: Robot location jumps around

Solution:

  • Drive slower with teleopincrease SLAM minimum_travel_distance in slam_params.yaml
  • Ensure good lidar visibility (not too close to walls)

Issue: TF errors "map to odom"

Solution:

# SLAM creates map→odom transform
# Verify SLAM is running
ros2 run tf2_tools view_frames
# Should show: map → odom → base_link

# Check TF tree
ros2 run tf2_ros tf2_echo map odom

Parameters Tuning

SLAM Parameters (config/slam_params.yaml)

For better mapping in tight spaces:

minimum_travel_distance: 0.1  # Default: 0.2
minimum_travel_heading: 0.1   # Default: 0.2

For faster map updates:

map_update_interval: 2.0  # Default: 5.0

For loop closure (recognizing visited places):

do_loop_closing: true  # Already enabled
loop_search_maximum_distance: 5.0  # Increase for larger maps

Nav2 Parameters (config/nav2_params.yaml)

Adjust robot speed:

controller_server:
  FollowPath:
    max_vel_x: 0.5  # Default: 0.3 (increase for faster)
    max_vel_theta: 1.5  # Default: 1.0 (faster turning)

Adjust obstacle avoidance:

local_costmap:
  robot_radius: 0.2  # Increase if robot hits obstacles
  inflation_radius: 0.7  # Increase for wider berth

Adjust path planning tolerance:

planner_server:
  GridBased:
    tolerance: 0.3  # Default: 0.5 (smaller = closer to goal)

Advanced Usage

Save Multiple Maps

# Map 1: Exploration
ros2 run nav2_map_server map_saver_cli -f exploration_map

# Map 2: After optimization
ros2 run nav2_map_server map_saver_cli -f optimized_map

# Map 3: Different area
ros2 run nav2_map_server map_saver_cli -f area2_map

Record SLAM Session for Replay

# Record all SLAM data
ros2 bag record /scan /odom /tf /tf_static /map

# Replay later
ros2 bag play my_recording.bag

Localization Only (Using Existing Map)

Modify slam_params.yaml:

mode: localization  # Change from "mapping"

Then launch:

ros2 launch squarobot_gazebo slam_simulation.launch.py
# Robot will localize on existing map without creating new one

What Each File Does

Configuration Files

slam_params.yaml:

  • SLAM algorithm settings
  • Map frame names (map, odom, base_link)
  • Loop closure thresholds
  • Mapping frequency and quality

nav2_params.yaml:

  • Navigation behavior configuration
  • Costmap settings (obstacle detection)
  • Path planning algorithms
  • Recovery behaviors

slam.rviz:

  • RViz visualization for SLAM
  • Fixed Frame: map (required for SLAM)
  • Displays: Grid, Robot, LaserScan, TF, Map
  • Pre-configured for immediate use

Launch Files

slam_simulation.launch.py ⭐:

  • All-in-one SLAM solution
  • Launches: Gazebo + SLAM + RViz
  • Uses slam.rviz config automatically
  • Recommended for SLAM mapping

slam.launch.py:

  • SLAM Toolbox node only
  • Use with existing simulation
  • For manual control

navigation.launch.py:

  • Nav2 navigation stack
  • Requires separate map server
  • Use after mapping is complete

Learning Path

Beginner: Start Here

  1. Run slam_simulation.launch.py
  2. Drive robot with teleop
  3. Watch map build in RViz
  4. Save map when done
  5. Success! You've created a map

Intermediate: Add Navigation

  1. Load saved map with map_server
  2. Launch navigation.launch.py
  3. Set initial pose in RViz
  4. Set goal pose
  5. Watch autonomous navigation

Advanced: Tune Parameters

  1. Adjust SLAM sensitivity
  2. Modify Nav2 costmaps
  3. Change robot speeds
  4. Optimize for your environment

Quick Command Reference

# SLAM Mapping (All-in-One)
ros2 launch squarobot_gazebo slam_simulation.launch.py

# Teleoperation
ros2 run teleop_twist_keyboard teleop_twist_keyboard

# Save Map
ros2 run nav2_map_server map_saver_cli -f my_map

# Check Topics
ros2 topic list
ros2 topic hz /map
ros2 topic hz /scan

# Check Nodes
ros2 node list
ros2 node info /slam_toolbox

# Check TF Tree
ros2 run tf2_tools view_frames
ros2 run tf2_ros tf2_echo map odom

# Launch Navigation (separate terminals)
ros2 launch squarobot_gazebo simulation_with_rviz.launch.py
ros2 run nav2_map_server map_server --ros-args -p yaml_filename:=./my_map.yaml
ros2 launch squarobot_gazebo navigation.launch.py

Success Indicators

SLAM Working Correctly:

  • Map appears in RViz (blue/gray)
  • Map updates as robot moves
  • /map topic publishing (~5 Hz)
  • No TF errors in console
  • Robot tracks correctly on map

Navigation Working Correctly:

  • Robot localizes on map (green arrow)
  • Path appears when goal is set (green line)
  • Robot follows path smoothly
  • Avoids obstacles
  • Reaches goal within tolerance

Summary

What was created:

  1. ✅ Complete SLAM configuration
  2. ✅ Complete Nav2 configuration
  3. ✅ SLAM-specific RViz config with Map display
  4. ✅ All-in-one launch file for easy SLAM
  5. ✅ Separate launch files for flexibility

Ready to use:

cd ~/Final
source install/setup.bash
ros2 launch squarobot_gazebo slam_simulation.launch.py
# Everything works out of the box!

For navigation:

  1. First create map with SLAM
  2. Save it with map_saver_cli
  3. Launch navigation stack
  4. Set goals in RViz

You now have a complete SLAM and navigation system ready to use! 🎉