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

+100
View File
@@ -8,6 +8,10 @@
退出码 0 = 一切正常;非 0 = 有东西没起来(逐条原因见输出)。
"""
import math
import os
import re
import subprocess
import sys
import time
@@ -48,6 +52,69 @@ MAP_QOS = QoSProfile(
SENSOR_QOS = QoSProfile(depth=1, reliability=ReliabilityPolicy.BEST_EFFORT)
def parse_gz_pose(out):
"""从 `gz model -m X -p` 的输出里抽出世界位姿,返回 (x,y,z,roll,pitch,yaw)。
真实输出是**多行**的(数值在标签下面两行,各自一个方括号):
Model: [52]
- Name: sentry
- Pose [ XYZ (m) ] [ RPY (rad) ]:
[0.900000 4.000000 0.000000]
[0.000000 -0.000000 0.000000]
注意标签本身就带两个方括号(`[ XYZ (m) ]` `[ RPY (rad) ]`),
所以不能"取第一个 []";要往后几行找真正的数值。
"""
lines = out.splitlines()
for i, line in enumerate(lines):
if 'Pose' not in line or 'XYZ' not in line:
continue
# 数值可能在同一行(冒号后),也可能在随后的两三行里
chunk = ' '.join(lines[i:i + 3])
tail = chunk.split(':', 1)[1] if ':' in chunk else chunk
triples = []
for grp in re.findall(r'\[([^\]]+)\]', tail):
nums = [float(v) for v in re.findall(
r'-?\d+(?:\.\d*)?(?:[eE][-+]?\d+)?', grp)]
if len(nums) == 3:
triples.append(nums)
if len(triples) >= 2:
return tuple(triples[0] + triples[1])
# 兜底:万一某些版本把 6 个数直接裸打在冒号后面,没有方括号
flat = [float(v) for v in re.findall(
r'-?\d+(?:\.\d*)?(?:[eE][-+]?\d+)?', tail)]
if len(flat) >= 6:
return tuple(flat[:6])
return None
def gz_model_pose(model_name):
"""从 Gazebo 读模型的世界位姿,返回 (x, y, z, roll, pitch, yaw) 或 None。
为什么要绕道 gz:ROS 里没有独立的真值来源 ——
`map -> base_link` 是由「静态 map->odom + 轮速里程计」拼出来的,
它无法证明车真的在哪。Gazebo 的服务端才是真值。
坑:sentry_up.sh 把仿真跑在 `GZ_PARTITION`(默认 sentry)分区里,
新开的终端如果不带这个变量,`gz model` 会报
「Service call to [/gazebo/worlds] timed out」,看起来像仿真没跑。
"""
partition = os.environ.get('GZ_PARTITION', 'sentry')
env = dict(os.environ)
env['GZ_PARTITION'] = partition
# 必须用系统版 gz 并清掉 LD_LIBRARY_PATH,否则会加载 ROS vendor 的库,
# 和服务端(系统版 10.6)不是同一套
env.pop('LD_LIBRARY_PATH', None)
cmd = ['/usr/bin/gz', 'model', '-m', model_name, '-p']
try:
out = subprocess.run(cmd, env=env, capture_output=True, text=True,
timeout=10).stdout
except (OSError, subprocess.SubprocessError):
return None
return parse_gz_pose(out)
def main():
rclpy.init()
# 整套栈都是 use_sim_time:=true,探针也必须一致,否则时间戳比较会出错
@@ -140,6 +207,39 @@ def main():
for line in buf.all_frames_as_yaml().splitlines():
print(' ' + line)
# ---------- 4. 定位真值对照 ----------
print('\n=== 4. 定位真值对照 ===')
truth = gz_model_pose('sentry')
tf_pos = None
try:
got = buf.lookup_transform('map', 'base_link', rclpy.time.Time(),
Duration(seconds=1.0))
tf_pos = (got.transform.translation.x, got.transform.translation.y)
except Exception:
pass
if truth is None:
print(' ⚠️ 拿不到 Gazebo 真值(仿真没在跑,或 GZ_PARTITION 对不上)')
print(' 手动试:GZ_PARTITION=%s gz model -m sentry -p'
% os.environ.get('GZ_PARTITION', 'sentry'))
else:
gx, gy = truth[0], truth[1]
print(' [真值] Gazebo 世界坐标 (%.3f, %.3f)' % (gx, gy))
print(' 场地坐标 (map) (%.3f, %.3f)' % (gx, gy))
print(' 中心零点 (field) (%.3f, %.3f)' % (gx - 6.0, gy - 4.0))
if tf_pos is None:
print(' [TF] map -> base_link 拿不到,无法比较')
else:
dev = math.hypot(tf_pos[0] - gx, tf_pos[1] - gy)
flag = '✅' if dev < 0.05 else ('⚠️ ' if dev < 0.5 else '❌')
print(' [TF] map -> base_link (%.3f, %.3f)' % tf_pos)
print(' [偏差] %.3f m %s' % (dev, flag))
if dev >= 0.05:
note = ('现在定位是"作弊"的(静态 map->odom + 轮速里程计),'
'偏差会随时间累积且不自愈')
print(' %s' % note)
problems.append('TF 位置与 Gazebo 真值差 %.2f m' % dev)
# ---------- 汇总 ----------
print('\n=== 汇总 ===')
if problems: