Welcome to ROSMASTER A1 PRO repository
11.Cartographer Relocalization Navigation
📍 Cartographer Relocalization and Pure Localization Autonomous Navigation
1. Course Content
Master using Google Cartographer to load previously built pose state file (
.pbstream) for high-precision global pure localization (Pure Localization).Combine Cartographer pure localization pose output with Nav2 path planner to achieve autonomous obstacle-avoiding single-point/multi-point navigation in known map environments.
2. Cartographer Pure Localization Navigation Principle Analysis
In actual industrial and research applications, after map building is complete, there is usually no need to continue incrementally expanding the map during navigation. Cartographer pure localization mode has the following characteristics and advantages:
Pose Graph Freeze (Freeze State): After loading existing
.pbstreamoffline pose graph, Cartographer sets all historical submaps to read-only frozen state, no longer inserting new pose nodes and constraint edges, greatly reducing memory usage and CPU computational load.Real-time CSM Scan Matching and Kidnap Resistance: The front-end uses Correlative Scan Matching to quickly perform graph matching between real-time laser scans and historical submaps; even if the robot's starting position deviates or it is short-distance transported (robot kidnapping), the algorithm can quickly re-converge localization based on surrounding geometric features at millisecond level.
Seamless Nav2 Navigation Protocol Stack Integration: Cartographer outputs authoritative
map -> odomcoordinate transformation, while Nav2 focuses on costmap (Costmap 2D) updates, A* global path planning, and local obstacle avoidance control.
3. Preparation and Running
3.1 Start Chassis + Lidar
Run in Terminal 1 (one-click start of A1 Pro chassis driver, IMU attitude solution, and lidar):
xxxxxxxxxxros2 launch yahboomcar_nav laser_bringup_launch.py
3.2 Start RViz Navigation Visualization
Run in Terminal 2 (it is recommended to open RViz on the virtual machine if the VM and robot are on the same LAN):
xxxxxxxxxxros2 launch yahboomcar_nav display_nav_launch.py
3.3 Start Cartographer Pure Localization Node
Run in Terminal 3 to load .pbstream pose graph and start pure localization core:
xxxxxxxxxxros2 launch yahboomcar_nav localization_imu_odom.launch.py use_rviz:=false load_state_filename:=$HOME/yahboomcar_ros2_ws/yahboomcar_ws/src/yahboomcar_nav/maps/map.pbstream
Tip
After pure localization starts, the robot will automatically match the surrounding environment for alignment; if in a feature-sparse area, you can also use the 2D Pose Estimate tool in RViz top toolbar to click and drag the heading arrow to assist acceleration convergence:

3.4 Start Nav2 Navigation Controller
Run in Terminal 4 to start navigation planning and controller (loads corresponding static raster map):
xxxxxxxxxxros2 launch yahboomcar_nav navigation_cartodwb_launch.py maps:=$HOME/yahboomcar_ros2_ws/yahboomcar_ws/src/yahboomcar_nav/maps/map.yaml

Tip
In the Views panel on the right side of RViz, you can switch views or adjust observation height:

4. Relocalization Navigation Operation Flow and Effect Description
4.1 Automatic Relocalization and Status Check
After Cartographer pure localization starts, the system will automatically match the current laser point cloud with the loaded historical submaps, outputting stable map -> odom coordinate transformation. Observe that the laser point cloud in RViz is completely attached to the map contours, representing successful relocalization.
4.2 Single-Point Autonomous Obstacle-Avoiding Navigation
Click the
2D Goal Pose(or Nav2 Goal) tool at the top of RViz2.Click in the free traversable area on the map and drag out the expected goal heading arrow.
The system will immediately generate a global planned path, the robot autonomously starts front-wheel steering and smoothly moves forward, real-time avoiding dynamic obstacles during travel and heading to the destination.

4.3 Multi-Point Autonomous Continuous Cruising
If multi-point cruising task is needed:
Click
Panels / navigation2 /Waypoint / Nav Through Poses Modein the Navigation2 control panel in the lower-left corner of RViz2.Use
Nav2 Goalto sequentially click and calibrate multiple target waypoints on the map.Click
Start Waypoint Following, and the robot will sequentially pass through each waypoint in the preset order.

4.4 Goal Arrival and Terminal Feedback
After the robot arrives at the preset goal and completes pose alignment, it automatically stops smoothly, and the terminal will output goal successful completion prompt:

Note
To end navigation task, press Ctrl+C in each terminal window to stop the program.
5. Core Source Code and Localization Parameter Analysis
5.1 Pure Localization and Navigation Launch Analysis
Pure Localization Launch Path:
xxxxxxxxxx$HOME/yahboomcar_ros2_ws/yahboomcar_ws/src/yahboomcar_nav/launch/nav2/localization_imu_odom.launch.py
Loads
cartographer_node, loads.pbstreamformat pose graph through-load_state_filenameparameter.Starts
cartographer_occupancy_grid_nodeto output static raster map layer.Starts
set_initpose_from_rviznode to receive/initialposetopic from RViz, achieving interactive relocalization initialization.
Navigation Controller Launch Path:
xxxxxxxxxx$HOME/yahboomcar_ros2_ws/yahboomcar_ws/src/yahboomcar_nav/launch/nav2/navigation_cartodwb_launch.py
Introduces
cartographer_bringup_launch.py, shields AMCL node, with Cartographer providing global localization.Loads
cartoteb_nav_params.yamlnavigation parameter file, collaborating Planner and Controller to complete motion planning.
5.2 Core Algorithm Parameter Interpretation
Parameter Configuration File Path:
xxxxxxxxxx$HOME/yahboomcar_ros2_ws/yahboomcar_ws/src/yahboomcar_nav/params/cartoteb_nav_params.yaml
Core Parameter Description Table
| Parameter Module | Parameter Name | Current Config Value | Core Function and Physical Meaning |
|---|---|---|---|
| Cartographer Pure Localization | load_state_filename | yahboomcar.pbstream | Complete path of loaded offline pose graph state file |
| Cartographer Optimization | pure_localization_trimmer | max_submaps: 3 | Pure localization trimmer only retains the 3 most recent local submaps, avoiding additional memory usage |
| Local Controller | FollowPath.plugin | TebLocalPlannerROS | Local trajectory optimization planner plugin |
| Local Controller | max_vel_x | 0.30 | Maximum allowed cruising speed during navigation (m/s) |
| Local Controller | xy_goal_tolerance | 0.20 | Goal point planar distance arrival tolerance (meters) |
| Local Controller | yaw_goal_tolerance | 0.25 | Goal point heading angle arrival tolerance (radians ≈ 14.3°) |
| Local Costmap | inflation_radius | 0.21 | Local obstacle dynamic inflation radius (meters) |
| Local Costmap | cost_scaling_factor | 8.0 | Scaling factor for obstacle cost decay with distance |
Parameter File Core Configuration Snippet
# Controller server configuration (TEB Local Planner)controller_server ros__parameters use_sim_timeFalse controller_frequency20.0 FollowPath plugin"teb_local_planner::TebLocalPlannerROS" max_vel_x0.30 # Maximum cruising speed (m/s) min_vel_x-0.05 # Limit reverse speed max_vel_theta1.0 # Maximum turning angular velocity (rad/s) acc_lim_x1.0 # Linear acceleration limit acc_lim_theta1.0 # Angular acceleration limit min_obstacle_dist0.1 # Minimum obstacle hard distance (m) xy_goal_tolerance0.20 # Arrival distance accuracy (m) yaw_goal_tolerance0.25 # Arrival angle accuracy (rad)
# Local costmap inflation configurationlocal_costmap local_costmap ros__parameters inflation_layer plugin"nav2_costmap_2d::InflationLayer" cost_scaling_factor8.0 inflation_radius0.21 # Inflation radius (m)