feat(localization): integrate LIO ROS TF, replay and validated AMCL replacement

GCC 15.2 C++23 clean rebuild: zero warnings. All 11 xmake test suites pass. Independent 120 s Gazebo replay: ATE 0.005279 m / 0.191335 deg; 1 s RPE 0.005358 m / 0.099086 deg. Both startup entry points validate Nav2 activation, no AMCL, and a single map-to-odom publisher.
This commit is contained in:
mzx committed 2026-11-22 01:44:36 +08:00
1 parent 55e9456e4a
commit b833e6f658
30 files changed
+1314 -329

No files matched your search

+68 -38
View File
@@ -34,12 +34,9 @@
坐标系
------
同时发布两条静态变换:
map → odom 平移 (-5.0, 3.0) 仿真里用里程计当真值,替代 AMCL
map → field 恒等变换 两者都已是「场地中心 = 零点」
`map` 是官方栅格图坐标系(西南内角 = (0,0)),`field` 是给决策代码用的直觉坐标系。
map → odom 由显式选择的 LIO / AMCL 定位器发布,不能同时启动。
map → field 为恒等变换。map / field 都以场地中心为原点。
Nav2 等待仿真时钟和 odom/base/lidar TF 就绪后启动。
"""
import os
@@ -47,17 +44,19 @@ import subprocess
import sys
from pathlib import Path
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import (
DeclareLaunchArgument,
ExecuteProcess,
IncludeLaunchDescription,
EmitEvent,
RegisterEventHandler,
LogInfo,
OpaqueFunction,
SetEnvironmentVariable,
)
from launch.conditions import IfCondition
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.event_handlers import OnProcessExit
from launch.events import Shutdown
from launch.substitutions import (LaunchConfiguration,
PathJoinSubstitution, PythonExpression)
from launch_ros.actions import Node
@@ -120,12 +119,25 @@ def _gz_resource_path():
return parts
def _validate_lio(context):
if LaunchConfiguration('localization').perform(context) == 'lio':
mode = LaunchConfiguration('lio_mode').perform(context)
path = Path(LaunchConfiguration('lio_map').perform(context))
if mode == 'localization' and not path.is_file():
raise RuntimeError(f'Missing LIO map: {path}; first select lio_mode:=mapping')
return []
def generate_launch_description():
headless = LaunchConfiguration('headless')
nav2 = LaunchConfiguration('nav2')
rviz = LaunchConfiguration('rviz')
world = LaunchConfiguration('world')
map_yaml = LaunchConfiguration('map')
localization = LaunchConfiguration('localization')
lio_mode = LaunchConfiguration('lio_mode')
lio_map = LaunchConfiguration('lio_map')
lio_export = LaunchConfiguration('lio_export_map')
# 布尔参数统一用 PythonExpression 判,避免 launch 把 "false" 当真值
is_headless = PythonExpression(["'", headless, "'.lower() in ('true', '1')"])
@@ -137,7 +149,33 @@ def generate_launch_description():
robot_description = _robot_description()
navigation_ready = ExecuteProcess(
cmd=['python3', str(WS / 'tools' / 'wait_navigation_ready.py')],
name='wait_navigation_ready', output='screen', condition=IfCondition(nav2_on),
)
# Use the same process boundary and options as the shell entry point.
# The nested include path reproduced a Wait action discovery timeout on this setup.
navigation = ExecuteProcess(
cmd=['ros2', 'launch', 'nav2_bringup', 'bringup_launch.py',
['map:=', map_yaml], 'use_sim_time:=true',
['params_file:=', str(PKG_SHARE / 'config' / 'nav2_params.yaml')],
'use_composition:=False',
['use_localization:=', PythonExpression(["'", localization, "' == 'amcl'"])],
'serve_static_map:=True', 'use_keepout_zones:=False',
'use_speed_zones:=False', 'slam:=False'],
name='nav2', output='screen', condition=IfCondition(nav2_on),
)
actions = [
DeclareLaunchArgument('localization', default_value='lio', choices=['lio', 'amcl']),
DeclareLaunchArgument('lio_mode', default_value='localization', choices=['mapping', 'localization']),
DeclareLaunchArgument('lio_map', default_value=str(WS / 'generated' / 'field.flmap')),
DeclareLaunchArgument('lio_export_map', default_value=lio_map),
DeclareLaunchArgument('lio_initial_x', default_value='-5.0'),
DeclareLaunchArgument('lio_initial_y', default_value='3.0'),
DeclareLaunchArgument('lio_initial_z', default_value='0.0'),
DeclareLaunchArgument('lio_initial_yaw', default_value='0.0'),
OpaqueFunction(function=_validate_lio),
# ---------------- 参数 ----------------
DeclareLaunchArgument(
'headless', default_value='false',
@@ -247,15 +285,20 @@ def generate_launch_description():
output='screen',
),
# ---------------- 7. 静态变换 ----------------
# map -> odom 由 AMCL 发布(use_localization:=True),这里**不能**再发一份,
# 否则两个发布者打架。要回到"作弊定位"就把 use_localization 改 False 并放开下面:
# Node(package='tf2_ros', executable='static_transform_publisher',
# name='map_to_odom',
# arguments=['--x','1.0','--y','1.0','--z','0.0',
# '--yaw','0.0','--pitch','0.0','--roll','0.0',
# '--frame-id','map','--child-frame-id','odom'],
# parameters=[{'use_sim_time': True}], output='screen'),
ExecuteProcess(
cmd=[str(BIN_DIR / 'field_localization_node'), '--ros-args',
'-p', 'use_sim_time:=true', '-p', 'point_cloud_topic:=/scan/points_filtered',
'-p', ['mode:=', lio_mode], '-p', ['map_path:=', lio_map],
'-p', ['export_map_path:=', lio_export], '-p', 'publish_map_to_odom:=true',
'-p', ['initial_x:=', LaunchConfiguration('lio_initial_x')],
'-p', ['initial_y:=', LaunchConfiguration('lio_initial_y')],
'-p', ['initial_z:=', LaunchConfiguration('lio_initial_z')],
'-p', ['initial_yaw:=', LaunchConfiguration('lio_initial_yaw')]],
name='field_localization', output='screen',
condition=IfCondition(PythonExpression(["'", localization, "' == 'lio'"])),
),
# ---------------- 7. map -> field 恒等别名 ----------------
# map -> field:恒等变换(map 已以场地中心为原点)
Node(
package='tf2_ros',
@@ -269,29 +312,16 @@ def generate_launch_description():
),
# ---------------- 8. 静态地图服务 ----------------
# 由 bringup 提供:serve_static_map 默认跟随 use_localization,
# 所以 use_localization:=True 时 bringup 自己起 map_server + amcl。
# 两个分支均由 bringup 提供 map_server,只有 AMCL 分支启动 amcl。
# 这里再起一份会**同名冲突**(lifecycle_manager 崩在 createLifecycleServiceServers)。
# ---------------- 9. Nav2 ----------------
IncludeLaunchDescription(
PythonLaunchDescriptionSource(os.path.join(
get_package_share_directory('nav2_bringup'),
'launch', 'bringup_launch.py')),
launch_arguments={
'map': map_yaml,
'use_sim_time': 'true',
'params_file': str(PKG_SHARE / 'config' / 'nav2_params.yaml'),
# 组合模式在本机起不来(容器一个组件都不加载),分进程更稳
'use_composition': 'False',
'use_localization': 'True',
# 这两个子系统本工程用不上,少两个 lifecycle 节点少两处启动风险
'use_keepout_zones': 'False',
'use_speed_zones': 'False',
'slam': 'False',
}.items(),
condition=IfCondition(nav2_on),
),
RegisterEventHandler(OnProcessExit(
target_action=navigation_ready,
on_exit=lambda event, context: [navigation] if event.returncode == 0 else [
EmitEvent(event=Shutdown(reason='simulation clock/TF readiness failed'))],
)),
navigation_ready,
# ---------------- 10. RViz ----------------
Node(