Files
VSAG_SENTRY_SIM_27RMUL/tools/build_gimbal.py
T
mzx f7412ec369 初始化:RMUL 2027 哨兵导航仿真工程
- Gazebo Sim (Jetty 10.6) + ROS 2 Lyrical + Nav2 全栈
- 整车模型:URDF/SDF + 减面网格(SolidWorks 装配体实测尺寸)
- 场地:RMUL 2027 官方包(2D 栅格地图 + 3D 场地模型),放在 field/
- sentry_nav C++ 包:self_filter 点云自滤 / patrol 航点巡逻
- 文档:README(含本机开发环境实测)、MID-360 接入说明

已排除:build/ install/ log/ 缓存目录,以及 115 MB 的原始高模
(仿真实际用的是 meshes/base.stl 减面版,约 25.7 万面)
2026-11-14 21:18:04 +08:00

135 lines
4.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""把 SolidWorks 导出的「云台」STL 转成仿真用网格。
与底盘的关键差异:
* 底盘导出:Y 轴向上(地面在 Y 最小端)
* 云台导出:Y 轴向下(安装板 yaw板/底部固定板 在 Y 最大端,相机/枪管在 Y 最小端)
所以这里用 ros_z = -(sw_y - mount_y) 把模型翻正,并翻转一个水平轴保持右手系
(det=+1,避免镜像)。
输出 meshes/gimbal.stl:原点在「安装面 + yaw 轴」上,模型向 +Z 生长。
用法:
python3 tools/build_gimbal.py --src "/run/media/.../云台" --out src/sentry_sim/meshes
"""
import argparse
import glob
import json
import os
import struct
import sys
import numpy as np
SCALE = 0.001
# 需要剔除的异常件:一个在别处(X≈388),一个是 30 米量级的坏面片
EXCLUDE = ('弹仓底B', '弹仓前1')
# 云台自身的关键点(SW 坐标,毫米):yaw 轴 + 安装面高度
YAW_AXIS_X = 30900.0
YAW_AXIS_Z = 6500.0
MOUNT_Y = 5769.4 # 安装面(yaw板 顶面)
# SW(mm, Y 向下) -> ROS(m, Z 向上),右手系(det=+1)
# ros_x = -(sw_z - YAW_AXIS_Z)
# ros_y = (sw_x - YAW_AXIS_X)
# ros_z = -(sw_y - MOUNT_Y)
ROT = np.array([[0.0, 0.0, -1.0],
[1.0, 0.0, 0.0],
[0.0, -1.0, 0.0]])
ORIGIN_SW = np.array([YAW_AXIS_X, MOUNT_Y, YAW_AXIS_Z])
def read_stl(path):
size = os.path.getsize(path)
with open(path, 'rb') as f:
head = f.read(84)
n = struct.unpack('<I', head[80:84])[0]
if 84 + n * 50 != size:
return None
f.seek(84)
buf = f.read(n * 50)
rec = np.frombuffer(buf, dtype=np.dtype([('n', '<3f4'), ('v', '<9f4'),
('a', '<u2')]))
return rec['v'].reshape(-1, 3, 3).astype(np.float64)
def write_stl(path, tris):
n = len(tris)
v0, v1, v2 = tris[:, 0], tris[:, 1], tris[:, 2]
normals = np.cross(v1 - v0, v2 - v0)
leng = np.linalg.norm(normals, axis=1)
leng[leng == 0] = 1.0
normals = normals / leng[:, None]
rec = np.zeros(n, dtype=np.dtype([('n', '<3f4'), ('v', '<9f4'), ('a', '<u2')]))
rec['n'] = normals
rec['v'] = tris.reshape(n, 9)
with open(path, 'wb') as f:
f.write(b'sentry_sim gimbal'.ljust(80, b' '))
f.write(struct.pack('<I', n))
f.write(rec.tobytes())
def main():
ap = argparse.ArgumentParser()
ap.add_argument('--src', required=True)
ap.add_argument('--out', required=True)
args = ap.parse_args()
files = sorted(glob.glob(os.path.join(args.src, '*.STL')) +
glob.glob(os.path.join(args.src, '*.stl')))
if not files:
sys.exit(f'没找到 STL:{args.src}')
used, skipped, parts = [], [], []
for p in files:
base = os.path.basename(p)
if any(x in base for x in EXCLUDE):
skipped.append(base)
continue
t = read_stl(p)
if t is None:
skipped.append(base + '(无法解析)')
continue
used.append(t)
parts.append(base)
tris = np.concatenate(used)
# 变换到 ROS 系(米),原点在安装面 + yaw 轴
tris = ((tris.reshape(-1, 3) - ORIGIN_SW) @ ROT.T) * SCALE
tris = tris.reshape(-1, 3, 3)
os.makedirs(args.out, exist_ok=True)
out = os.path.join(args.out, 'gimbal.stl')
write_stl(out, tris)
v = tris.reshape(-1, 3)
mn, mx = v.min(axis=0), v.max(axis=0)
meta = {
'source_dir': os.path.abspath(args.src),
'parts_used': len(parts),
'parts_skipped': skipped,
'triangles': int(len(tris)),
'ros_bbox_min_m': [round(float(x), 4) for x in mn],
'ros_bbox_max_m': [round(float(x), 4) for x in mx],
'ros_bbox_size_m': [round(float(mx[i] - mn[i]), 4) for i in range(3)
],
'origin_sw_mm': [float(x) for x in ORIGIN_SW],
'note': '云台导出 Y 轴朝下,已翻正(det=+1,无镜像);原点=安装面+yaw轴',
}
with open(os.path.join(args.out, 'gimbal_transform.json'), 'w') as f:
json.dump(meta, f, ensure_ascii=False, indent=2)
print(f'使用 {len(parts)} 个零件,跳过 {len(skipped)} 个:')
for s in skipped:
print(f' 跳过: {s}')
print(f'\ngimbal.stl: {len(tris):,} 面')
print(f'ROS 包围盒 min={np.round(mn, 3)} max={np.round(mx, 3)}')
print(f'尺寸 (m) = {np.round(mx - mn, 3)}')
if __name__ == '__main__':
main()