feat: AMCL 定位接入、场地定位工具与文档更新

This commit is contained in:
mzx committed 2026-11-19 12:54:02 +08:00
1 parent 0c6b49e276
commit 0e4e4c6e18
45 files changed
+3173 -151

No files matched your search

+165
View File
@@ -0,0 +1,165 @@
#!/usr/bin/env python3
"""AMCL 效果演示:让里程计「说谎」,看 AMCL 是不是在纠正它。
对比三列:
真值 Gazebo 世界坐标(物理真相)
原始里程计 odom -> base_link(被 odom_scale 打了比例误差,会越走越偏)
AMCL 定位 map -> base_link(AMCL 用扫描 + 地图纠正后的结果)
期望:原始里程计的偏差随行进距离增长,而 AMCL 一直贴在真值附近。
用法:
# 终端 1:启动仿真,并给里程计加 15% 比例误差
SENTRY_ODOM_SCALE=1.15 HEADLESS=1 scripts/sentry_up.sh
# 终端 2:等 AMCL 激活后
python3 tools/amcl_drift_demo.py
不设 `SENTRY_ODOM_SCALE` 也能跑(等于 1.0 真实里程计),但那样看不出 AMCL 的价值
—— 因为仿真里 odom 本来就是真值。
"""
import math
import os
import sys
import time
import rclpy
import rclpy.time
from geometry_msgs.msg import Twist
from rclpy.duration import Duration
from rclpy.node import Node
from tf2_ros import Buffer, TransformListener
# 复用 tools/check_stack.py 的 gz_model_pose():它已经处理好了
# · GZ_PARTITION(默认 sentry,和 sentry_up.sh 一致)
# · 清掉 LD_LIBRARY_PATH 用系统版 gz
# 自己再写一遍就会踩「分区写死」的坑(本脚本第一版就是这个 bug)。
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import check_stack as cs # noqa: E402
SCALE = float(os.environ.get('SENTRY_ODOM_SCALE', '1.0'))
def gz_truth():
"""Gazebo 里的真实位姿(世界坐标 == 场地坐标)。"""
pose = cs.gz_model_pose('sentry')
return (pose[0], pose[1]) if pose else None
rclpy.init()
node = Node('amcl_drift_demo')
node.set_parameters([rclpy.parameter.Parameter(
'use_sim_time', rclpy.Parameter.Type.BOOL, True)])
buf = Buffer()
TransformListener(buf, node)
pub = node.create_publisher(Twist, '/cmd_vel', 10)
end = time.time() + 20
while time.time() < end:
rclpy.spin_once(node, timeout_sec=0.2)
try:
buf.lookup_transform('map', 'base_link', rclpy.time.Time(),
Duration(seconds=0.5))
break
except Exception:
pass
def tf_xy(target, source):
try:
t = buf.lookup_transform(target, source, rclpy.time.Time(),
Duration(seconds=0.5)).transform.translation
return t.x, t.y
except Exception:
return None
# ---------- 启动自检:读不到就别往下跑 ----------
origin = gz_truth()
if origin is None:
print('❌ 读不到 Gazebo 真值,无法做对照。可能原因:')
print(' ① 仿真没在跑')
print(' ② GZ_PARTITION 对不上(sentry_up.sh 默认用 sentry)')
print(' 先确认:GZ_PARTITION=%s gz model -m sentry -p'
% os.environ.get('GZ_PARTITION', 'sentry'))
print(' 或者直接跑:python3 tools/check_stack.py')
rclpy.try_shutdown()
sys.exit(1)
if tf_xy('map', 'base_link') is None:
print('❌ 读不到 TF map -> base_link(AMCL 可能还没激活)')
print(' 等 30~60 秒再试,或先跑 python3 tools/check_stack.py')
rclpy.try_shutdown()
sys.exit(1)
print('\n⚠️ 本演示**直接发 /cmd_vel,绕过了整个 Nav2** —— 所以没有避障。')
print(' 车撞到东西是预期的:这里要的是「里程计和真实位置不一致」,')
print(' 走 Nav2 的话代价地图会把它绕开,就制造不出漂移了。')
print(' Nav2 自己的避障要另外验证(用 nav_client.py 发目标)。')
print()
print('odom_scale = %.2f (1.00 = 真实里程计;>1 = 报告的距离偏大)' % SCALE)
if abs(SCALE - 1.0) < 1e-6:
print('⚠️ 现在是 1.00 —— 仿真里 odom 本来就是真值,看不出 AMCL 的价值。')
print(' 想看效果:SENTRY_ODOM_SCALE=1.15 HEADLESS=1 scripts/sentry_up.sh')
print('出发点 = (%.3f, %.3f)\n' % origin)
def sample(tag):
rclpy.spin_once(node, timeout_sec=0.1)
tr = gz_truth()
amcl = tf_xy('map', 'base_link')
raw = tf_xy('odom', 'base_link') # 原始里程计(含比例误差)
if tr is None or amcl is None or raw is None:
print(' %-8s 读不到' % tag)
return None, None
d_amcl = math.hypot(amcl[0] - origin[0], amcl[1] - origin[1])
d_raw = math.hypot(raw[0], raw[1])
d_true = math.hypot(tr[0] - origin[0], tr[1] - origin[1])
print(' %-8s 真值走了%5.2fm 原始里程计说%5.2fm AMCL 说%5.2fm '
'里程计误差%+5.2fm AMCL 误差%+5.2fm'
% (tag, d_true, d_raw, d_amcl, d_raw - d_true, d_amcl - d_true))
return d_raw - d_true, d_amcl - d_true
print('【静止】三列应该一致:')
for i in range(2):
sample('t=%ds' % i)
time.sleep(1)
print('\n【直线前进】(0.25 m/s × 12 s)')
rows = []
tw = Twist()
tw.linear.x = 0.25
t0 = time.time()
next_sample = 0.0
while time.time() - t0 < 12.0:
pub.publish(tw)
rclpy.spin_once(node, timeout_sec=0.05)
if time.time() - t0 >= next_sample:
r = sample('%4.1fs' % (time.time() - t0))
if r[0] is not None:
rows.append(r)
next_sample += 2.0
time.sleep(0.05)
pub.publish(Twist())
time.sleep(1.0)
sample('停')
print('\n【结论】')
if rows:
raw_err = rows[-1][0]
amcl_err = rows[-1][1]
print(' 里程计累计误差:%+.2f m' % raw_err)
print(' AMCL 累计误差:%+.2f m' % amcl_err)
if abs(raw_err) <= 0.15:
print(' ~ 里程计误差太小,看不出差别'
'(odom_scale 接近 1.0?或者车没怎么动?)')
elif abs(amcl_err) < abs(raw_err) * 0.8:
print(' ✅ AMCL 把漂移压住了 —— 这正是它的作用')
else:
print(' ❌ AMCL 没能纠正')
rclpy.try_shutdown()