Welcome to ROSMASTER M3 repository
13.slam_toolbox localization and navigation
slam_toolbox Localization and Navigation
slam_toolbox Localization and Navigation1. Course Content2. Principle Introduction2.1 Introduction2.2 Difference Between the Two Localization Methods3. Start the Agent4. Run the Example4.1 Preparations4.2 Start the Navigation Node5. Rapid Relocalization5.1 Odometry Cumulative Error5.2 Robot Kidnapping6. Source Code Analysis6.1 View TF Tree6.2 Launch File
1. Course Content
- Run the sample program to perform navigation using slam_toolbox in localization mode and quickly recover the robot's precise position after a kidnapping event.
2. Principle Introduction
2.1 Introduction
- The default localization algorithm in Navigation 2 is AMCL (Adaptive Monte Carlo Localization). This involves replacing the default AMCL localization method with the pure localization mode of slam_toolbox, while other navigation functions remain unchanged.
2.2 Difference Between the Two Localization Methods
- AMCL: Suitable for scenarios with a known map, such as when a robot performs repetitive tasks or navigation in an indoor environment with a pre-built map. AMCL can be used to determine its own position.
- slam_toolbox: Compared to AMCL's particle filter localization, which must estimate position through probability distribution while moving, slam_toolbox's pure localization can use a pose graph for rapid scan-to-map matching to solve for a precise global position.
3. Start the Agent
Note: If it is already running, you do not need to start it again.
Enter the command in the vehicle's terminal:
start_agent
The terminal will print the following information, indicating a successful connection:

4. Run the Example
4.1 Preparations
- A grid map and a pose graph map in
posegraphformat are required. To build a pose graph map, refer to the09-Lifelong Mappingchapter.
4.2 Start the Navigation Node
For Raspberry Pi 5 users, if the ROS container is not started, you need to start it first,
xxxxxxxxxxbringup_m3
Open a terminal inside the container,
xxxxxxxxxxexec_m3
(ORIN board)
xxxxxxxxxxros2 launch m3_bringup nav2.launch.py map:=map.yaml pose_map:=map localization_mode:=slam_toolbox
(RDK & PI5 boards) Run in separate steps, with visualization launched on the accompanying virtual machine Rosmaster-M3-Ubuntu22.04,
xxxxxxxxxx# On the virtual machinerviz2 -d /home/yahboom/yahboomM3_ws/user_ws/src/m3_bringup/rviz/nav2.rviz# On the host machineros2 launch m3_bringup nav2.launch.py map:=map.yaml pose_map:=map localization_mode:=slam_toolbox gui:=False[!TIP]
Launch Parameter Descriptions
localization_mode
- The global localization method. Here, slam_toolbox's localization method is selected, which can achieve higher-precision global localization using a pose graph.
pose_map
- The name of the pose graph map built by slam_toolbox. If slam_toolbox is chosen as the localization method, this must be provided. The default is
map.
map
- The name of the grid map, default is
map.yaml.
- Near the map's origin, localization can be completed without providing an initial pose.

- In the left toolbar, you can uncheck the global and local costmaps to get a clearer view of the laser point cloud and the environment's contours.


- Navigation is still performed by specifying a target pose with
Nav2 Goal.

5. Rapid Relocalization
5.1 Odometry Cumulative Error
- Odometry can accumulate errors or experience errors in local positioning due to wheel slippage. At this point, the laser point cloud and the environment's contours will no longer match, as shown in the figure below:

- With a pose graph, you only need to provide an approximate pose using the
2D Pose Estimatetool to achieve precise localization.


5.2 Robot Kidnapping
- When the robot is manually moved by an external force and loses its global position, this localization problem is known as the robot kidnapping problem. Here, we recover the global position by providing an approximate pose using the pose graph.

- Similarly, you only need to provide an approximate pose using the
2D Pose Estimatetool to achieve precise localization.

6. Source Code Analysis
6.1 View TF Tree
In the terminal, enter:
xxxxxxxxxxros2 run rqt_tf_tree rqt_tf_tree- Here, the TF transformation from the
mapcoordinate frame to theodomcoordinate frame is provided by slam_toolbox.

6.2 Launch File
- Launch file path:
xxxxxxxxxx~/yahboomM3_ws/user_ws/src/m3_bringup/launch/navigation/nav2.launch.py
- The key code snippet below starts the Navigation2 stack node and the slam_toolbox pure localization node.
xxxxxxxxxx # slamtoolbox positioning navigation slamtoolbox_localization_nav = GroupAction( condition=IfCondition(PythonExpression(["'", localization_mode, "' == 'slam_toolbox' and '", slam, "' == 'False' "])), actions=[ PushRosNamespace(namespace=namespace), # Start navigation2 navigation file (no global positioning) IncludeLaunchDescription( PythonLaunchDescriptionSource(nav2_launch_mod), launch_arguments={ "map": map_dir, "params_file": params_file, "use_sim_time":use_sim_time, }.items()), # Start slamtoolbox positioning node IncludeLaunchDescription( PythonLaunchDescriptionSource(slamtoolbox_localization_launch_file), launch_arguments={ "map": pose_map, "use_sim_time":use_sim_time, }.items(), ), # Log Printing LogInfo(msg=[Fore.GREEN+'====================Robot-M3 Navigation Start with SlamToolbox localization====================',Fore.RESET]) ] )
- Parameter file path. If you need to configure the slam_toolbox parameters for pure localization mode, the path is as follows:
xxxxxxxxxx~/yahboomM3_ws/user_ws/src/m3_bringup/config/mapper_params_localization.yaml
xslam_toolbox ros__parameters solver_pluginsolver_pluginsCeresSolver ceres_linear_solverSPARSE_NORMAL_CHOLESKY ceres_preconditionerSCHUR_JACOBI ceres_trust_strategyLEVENBERG_MARQUARDT ceres_dogleg_typeTRADITIONAL_DOGLEG ceres_loss_functionNone # ROS Parameters odom_frameodom map_framemap base_framebase_footprint scan_topic/scan modelocalization #localization # if you'd like to start localizing on bringup in a map and pose map_file_name"" #map_start_pose: [5.0, 1.0, 0.0] map_start_at_docktrue debug_loggingfalse throttle_scans1 transform_publish_period0.02 #if 0 never publishes odometry map_update_interval5.0 resolution0.05 min_laser_range0.05 #for rastering images max_laser_range12.0 #for rastering images minimum_time_interval0.5 transform_timeout0.2 tf_buffer_duration30. stack_size_to_use40000000 #// program needs a larger stack size to serialize large maps # General Parameters use_scan_matchingtrue use_scan_barycentertrue minimum_travel_distance0.5 minimum_travel_heading0.5 scan_buffer_size3 scan_buffer_maximum_scan_distance10.0 link_match_minimum_response_fine0.1 link_scan_maximum_distance1.5 do_loop_closingtrue loop_match_minimum_chain_size3 loop_match_maximum_variance_coarse3.0 loop_match_minimum_response_coarse0.35 loop_match_minimum_response_fine0.45 # Correlation Parameters - Correlation Parameters correlation_search_space_dimension0.5 correlation_search_space_resolution0.01 correlation_search_space_smear_deviation0.1 # Correlation Parameters - Loop Closure Parameters loop_search_space_dimension8.0 loop_search_space_resolution0.05 loop_search_space_smear_deviation0.03 loop_search_maximum_distance3.0 # Scan Matcher Parameters distance_variance_penalty0.5 angle_variance_penalty1.0 fine_search_angle_offset0.00349 coarse_search_angle_offset0.349 coarse_angle_resolution0.0349 minimum_angle_penalty0.9 minimum_distance_penalty0.5 use_response_expansiontrue min_pass_through2 occupancy_threshold0.1