对tools进行了整理,Xmake也改为相对路径,在线 LiDAR-IMU 定位器实现

This commit is contained in:
mzx committed 2026-11-23 14:32:42 +08:00
1 parent 0602144ac5
commit 4502296915
70 files changed
+2652 -1298

No files matched your search

+19 -47
View File
@@ -11,7 +11,6 @@
import math
import os
import re
import shutil
import subprocess
import sys
import time
@@ -29,6 +28,8 @@ from sensor_msgs.msg import PointCloud2, LaserScan
from nav_msgs.msg import Odometry, OccupancyGrid
from tf2_ros import Buffer, TransformListener
import project_env
# 期望处于 ACTIVE 的节点(缺了哪个,导航就会缺一块能力)
#
@@ -96,53 +97,17 @@ def parse_gz_pose(out):
return None
def _gz_bin():
"""返回 gz 可执行文件路径(系统安装的那个),找不到返回 None。
不写死 `/usr/bin/gz`:换台机器 gz 可能装在别处。
优先用 tools/project_env.py 的统一探测逻辑(同样支持 GZ_BIN 覆盖)。
"""
try:
import project_env # noqa: PLC0415 (同目录模块,按需导入)
gz = project_env.find_gz_bin()
if gz is not None:
return gz
except ImportError:
pass
for cand in ('/usr/bin/gz', '/usr/local/bin/gz'):
if os.access(cand, os.X_OK):
return cand
return shutil.which('gz')
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 的库,
# 和服务端不是同一套。
env.pop('LD_LIBRARY_PATH', None)
gz_bin = _gz_bin()
if gz_bin is None:
return None
cmd = [str(gz_bin), 'model', '-m', model_name, '-p']
"""Read the model pose from Gazebo in the simulation partition."""
gz = project_env.find_gz_bin()
env_partition = os.environ.get('GZ_PARTITION') or 'sentry'
try:
out = subprocess.run(cmd, env=env, capture_output=True, text=True,
timeout=10).stdout
except (OSError, subprocess.SubprocessError):
result = project_env.run_gz(
gz, 'model', '-m', model_name, '-p', timeout=10,
partition=env_partition)
except (OSError, subprocess.TimeoutExpired):
return None
return parse_gz_pose(out)
return parse_gz_pose(result.stdout) if result.returncode == 0 else None
def main():
@@ -280,7 +245,14 @@ def main():
# ---------- 4. 定位真值对照 ----------
print('\n=== 4. 定位真值对照 ===')
truth = gz_model_pose('sentry')
try:
truth = gz_model_pose('sentry')
except project_env.ProbeError as error:
truth = None
if os.environ.get('GZ_BIN'):
problems.append(str(error))
else:
print(f' ⚠️ {error}')
tf_pos = None
try:
got = buf.lookup_transform('map', 'base_link', rclpy.time.Time(),
@@ -292,7 +264,7 @@ def main():
if truth is None:
print(' ⚠️ 拿不到 Gazebo 真值(仿真没在跑,或 GZ_PARTITION 对不上)')
print(' 手动试:GZ_PARTITION=%s gz model -m sentry -p'
% os.environ.get('GZ_PARTITION', 'sentry'))
% (os.environ.get('GZ_PARTITION') or 'sentry'))
else:
gx, gy = truth[0], truth[1]
print(' [真值] Gazebo 世界坐标 (%.3f, %.3f)' % (gx, gy))