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:
1 parent
55e9456e4a
commit
b833e6f658
30 files changed
+1314
-329
No files matched your search
@@ -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(
|
||||
|
||||
@@ -387,5 +387,14 @@
|
||||
<frame name='lidar_link' attached_to='lidar_joint'>
|
||||
<pose>0 0 0 0 0 0</pose>
|
||||
</frame>
|
||||
<!-- Evaluation only: independent world pose, never consumed by localization. -->
|
||||
<plugin filename="gz-sim-pose-publisher-system" name="gz::sim::systems::PosePublisher">
|
||||
<publish_model_pose>true</publish_model_pose>
|
||||
<publish_link_pose>false</publish_link_pose>
|
||||
<publish_nested_model_pose>false</publish_nested_model_pose>
|
||||
<use_pose_vector_msg>false</use_pose_vector_msg>
|
||||
<update_frequency>100</update_frequency>
|
||||
<topic>/evaluation/ground_truth</topic>
|
||||
</plugin>
|
||||
</model>
|
||||
</sdf>
|
||||
@@ -24,4 +24,5 @@ exec ros2 run ros_gz_bridge parameter_bridge \
|
||||
'/scan@sensor_msgs/msg/LaserScan[gz.msgs.LaserScan' \
|
||||
'/imu@sensor_msgs/msg/Imu[gz.msgs.IMU' \
|
||||
'/scan/points@sensor_msgs/msg/PointCloud2[gz.msgs.PointCloudPacked' \
|
||||
'/evaluation/ground_truth@geometry_msgs/msg/PoseStamped[gz.msgs.Pose' \
|
||||
--ros-args -p use_sim_time:=true
|
||||
@@ -160,7 +160,7 @@ env_bin_dir() {
|
||||
|
||||
# ① 现行布局优先
|
||||
local name p
|
||||
for name in self_filter nav_client patrol odom_tf; do
|
||||
for name in self_filter nav_client patrol odom_tf field_localization_node; do
|
||||
if [ -x "${ws}/build/bin/${name}" ]; then
|
||||
printf '%s\n' "${ws}/build/bin"
|
||||
return 0
|
||||
@@ -168,7 +168,7 @@ env_bin_dir() {
|
||||
done
|
||||
|
||||
# ② 老布局:去 build/ 里找产物实际所在的那一层
|
||||
for name in self_filter nav_client patrol odom_tf; do
|
||||
for name in self_filter nav_client patrol odom_tf field_localization_node; do
|
||||
p=$(find "$ws/build" -maxdepth 4 -type f -name "$name" 2>/dev/null | head -1 || true)
|
||||
if [ -n "$p" ]; then
|
||||
dirname "$p"
|
||||
|
||||
@@ -115,6 +115,16 @@ export GZ_SIM_RESOURCE_PATH="${PKG_SHARE}/models:${FIELD_ROOT}/src/guodong_sentr
|
||||
export GZ_PARTITION="${GZ_PARTITION:-sentry}"
|
||||
export ROS_DOMAIN_ID="${ROS_DOMAIN_ID:-0}" # 默认用 0,跟普通终端一致;要隔离时自己设
|
||||
|
||||
LOCALIZATION="${LOCALIZATION:-lio}"
|
||||
LIO_MODE="${LIO_MODE:-localization}"
|
||||
LIO_MAP="${LIO_MAP:-${WS}/generated/field.flmap}"
|
||||
LIO_EXPORT_MAP="${LIO_EXPORT_MAP:-${LIO_MAP}}"
|
||||
case "$LOCALIZATION" in lio) NAV2_LOCALIZATION=False ;; amcl) NAV2_LOCALIZATION=True ;; *) echo "LOCALIZATION must be lio or amcl" >&2; exit 1 ;; esac
|
||||
case "$LIO_MODE" in mapping|localization) ;; *) echo "LIO_MODE must be mapping or localization" >&2; exit 1 ;; esac
|
||||
if [ "$LOCALIZATION" = lio ] && [ "$LIO_MODE" = localization ] && [ ! -r "$LIO_MAP" ]; then
|
||||
echo "[sentry_up] missing LIO map: $LIO_MAP; first run LIO_MODE=mapping or choose LOCALIZATION=amcl" >&2
|
||||
exit 1
|
||||
fi
|
||||
WORLD="${PKG_SHARE}/worlds/sentry_arena.sdf"
|
||||
PIDS=()
|
||||
CLEANED=0
|
||||
@@ -228,14 +238,24 @@ ros2 run ros_gz_bridge parameter_bridge \
|
||||
'/scan@sensor_msgs/msg/LaserScan[gz.msgs.LaserScan' \
|
||||
'/imu@sensor_msgs/msg/Imu[gz.msgs.IMU' \
|
||||
'/scan/points@sensor_msgs/msg/PointCloud2[gz.msgs.PointCloudPacked' \
|
||||
'/evaluation/ground_truth@geometry_msgs/msg/PoseStamped[gz.msgs.Pose' \
|
||||
--ros-args -p use_sim_time:=true &
|
||||
PIDS+=($!)
|
||||
|
||||
# ---------- 4. robot_state_publisher(TF 树)----------
|
||||
echo "[sentry_up] 启动 robot_state_publisher"
|
||||
# XML must be passed as a typed string; CLI YAML parsing breaks on XML comments.
|
||||
mkdir -p "${WS}/generated"
|
||||
xacro "${PKG_SHARE}/urdf/sentry.urdf.xacro" > "${WS}/generated/robot_description.urdf"
|
||||
python3 - "${WS}/generated/robot_description.urdf" "${WS}/generated/robot_state_publisher.json" <<'PY_PARAMS'
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
text = Path(sys.argv[1]).read_text()
|
||||
Path(sys.argv[2]).write_text(json.dumps({'robot_state_publisher': {'ros__parameters': {'robot_description': text, 'use_sim_time': True}}}))
|
||||
PY_PARAMS
|
||||
ros2 run robot_state_publisher robot_state_publisher --ros-args \
|
||||
-p robot_description:="$(xacro "${PKG_SHARE}/urdf/sentry.urdf.xacro")" \
|
||||
-p use_sim_time:=true &
|
||||
--params-file "${WS}/generated/robot_state_publisher.json" &
|
||||
PIDS+=($!)
|
||||
|
||||
# ---------- 5. odom -> base_link ----------
|
||||
@@ -255,15 +275,17 @@ echo "[sentry_up] 启动点云自滤(${NAV_BIN_DIR}/self_filter)"
|
||||
$(nav_cmd self_filter) --ros-args -p use_sim_time:=true &
|
||||
PIDS+=($!)
|
||||
|
||||
# ---------- 5.6 map->odom:由 AMCL 发布,不要在这里再发一份 ----------
|
||||
# 2026-11-18 起启用 AMCL(use_localization:=True):
|
||||
# · map->odom 由 nav2_amcl 根据 /scan2d 对栅格图做粒子滤波后发布
|
||||
# · 如果这里再发一个静态 map->odom,两个发布者会打架(TF 跳变/重复)
|
||||
# 想回到旧的「作弊定位」,把下面这行注释放开、并把 use_localization 改回 False:
|
||||
#
|
||||
# ros2 run tf2_ros static_transform_publisher \
|
||||
# --x 0.9 --y 4.0 --z 0.0 --yaw 0.0 --pitch 0.0 --roll 0.0 \
|
||||
# --frame-id map --child-frame-id odom --ros-args -p use_sim_time:=true &
|
||||
# ---------- 5.6 唯一 map->odom 发布者:LIO 或 AMCL ----------
|
||||
if [ "$LOCALIZATION" = lio ]; then
|
||||
echo "[sentry_up] LIO mode=$LIO_MODE (AMCL disabled)"
|
||||
$(nav_cmd field_localization_node) --ros-args -p use_sim_time:=true \
|
||||
-p point_cloud_topic:=/scan/points_filtered -p imu_topic:=/imu \
|
||||
-p mode:="$LIO_MODE" -p map_path:="$LIO_MAP" -p export_map_path:="$LIO_EXPORT_MAP" \
|
||||
-p publish_map_to_odom:=true \
|
||||
-p initial_x:="${LIO_INITIAL_X:--5.0}" -p initial_y:="${LIO_INITIAL_Y:-3.0}" \
|
||||
-p initial_z:="${LIO_INITIAL_Z:-0.0}" -p initial_yaw:="${LIO_INITIAL_YAW:-0.0}" &
|
||||
PIDS+=($!)
|
||||
fi
|
||||
|
||||
# ---------- 5.65 map->field 恒等变换(两者都是「场地中心 = 零点」)----------
|
||||
# 世界与地图都已整体平移 (-6,-4)(官方包原定义是西南内角 = (0,0)),
|
||||
@@ -275,26 +297,11 @@ ros2 run tf2_ros static_transform_publisher \
|
||||
--frame-id map --child-frame-id field --ros-args -p use_sim_time:=true &
|
||||
PIDS+=($!)
|
||||
|
||||
# ---------- 5.7 静态地图服务:由 bringup 提供,不要自己再起 ----------
|
||||
# nav2_bringup 的 serve_static_map 默认跟随 use_localization,
|
||||
# 所以 use_localization:=True 时 bringup 会自己起 map_server + amcl + 一个
|
||||
# lifecycle_manager_localization。这里再起一份就会**同名节点/服务冲突**,
|
||||
# 典型症状是 lifecycle_manager 崩在 createLifecycleServiceServers()
|
||||
# 报 "could not create service"。
|
||||
#
|
||||
# 只有回到 use_localization:=False 时,才需要在这里手工补 map_server:
|
||||
#
|
||||
# echo "[sentry_up] 启动 map_server($MAP_YAML)"
|
||||
# ros2 run nav2_map_server map_server --ros-args \
|
||||
# -p yaml_filename:="${MAP_YAML}" -p use_sim_time:=true -p frame_id:=map &
|
||||
# PIDS+=($!)
|
||||
# ros2 run nav2_lifecycle_manager lifecycle_manager --ros-args \
|
||||
# -p node_names:="['map_server']" -p autostart:=true -p bond_timeout:=30.0 &
|
||||
# PIDS+=($!)
|
||||
# Nav2 manages the existing 2D map_server in both localization branches.
|
||||
|
||||
# ---------- 6. Nav2 ----------
|
||||
if [ "$NAV2" = "1" ]; then
|
||||
echo "[sentry_up] 启动 Nav2(AMCL + 栅格图)"
|
||||
echo "[sentry_up] 启动 Nav2($LOCALIZATION + 栅格图)"
|
||||
# 三个参数都是必需的,各有原因(踩过的坑):
|
||||
# use_composition:=False 组合模式在本机起不来 —— /nav2_container 起来后一个组件都不加载,
|
||||
# 日志停在 process started 就没了。分进程模式正常。
|
||||
@@ -309,7 +316,8 @@ if [ "$NAV2" = "1" ]; then
|
||||
use_sim_time:=true \
|
||||
params_file:="${PKG_SHARE}/config/nav2_params.yaml" \
|
||||
use_composition:=False \
|
||||
use_localization:=True \
|
||||
use_localization:="$NAV2_LOCALIZATION" \
|
||||
serve_static_map:=True \
|
||||
use_keepout_zones:=False \
|
||||
use_speed_zones:=False \
|
||||
slam:=False &
|
||||
|
||||
@@ -174,6 +174,15 @@
|
||||
<gazebo reference="wheel_br"><mu1>1.2</mu1><mu2>1.2</mu2></gazebo>
|
||||
|
||||
<gazebo>
|
||||
<!-- Evaluation only: independent world pose, never consumed by localization. -->
|
||||
<plugin filename="gz-sim-pose-publisher-system" name="gz::sim::systems::PosePublisher">
|
||||
<publish_model_pose>true</publish_model_pose>
|
||||
<publish_link_pose>false</publish_link_pose>
|
||||
<publish_nested_model_pose>false</publish_nested_model_pose>
|
||||
<use_pose_vector_msg>false</use_pose_vector_msg>
|
||||
<update_frequency>100</update_frequency>
|
||||
<topic>/evaluation/ground_truth</topic>
|
||||
</plugin>
|
||||
<plugin filename="gz-sim-mecanum-drive-system" name="gz::sim::systems::MecanumDrive">
|
||||
<front_left_joint>wheel_fl_joint</front_left_joint>
|
||||
<front_right_joint>wheel_fr_joint</front_right_joint>
|
||||
|
||||
Reference in new issue
Block a user