111 lines
4.1 KiB
Python
111 lines
4.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Ghep nguon z OFFLINE tu ket qua vslam_vo_bench -- thu cac bo loc z cho G4
|
|
truoc khi viet C++.
|
|
|
|
Vao (cung thu muc, ra cua vslam_vo_bench):
|
|
ref.txt tham chieu: z = tich -ds*sin(pitch), pitch/roll cua GravityAlign
|
|
f2m_rN.txt odometry F2M (base_footprint, frame odom)
|
|
Ra (TUM, cung thu muc):
|
|
ref_clamp.txt tham chieu + KEP MAT BANG: |pitch IMU| < nguong -> dz = 0
|
|
hyb_rN.txt LAI: buoc dich chuyen trong frame THAN tu F2M (met, khong
|
|
truot banh), xoay bang roll/pitch IMU (khong troi):
|
|
dz = -sin(p)*dx + cos(p)*sin(r)*dy + cos(p)*cos(r)*dz
|
|
hyb_clamp_rN.txt lai + kep mat bang
|
|
f2m_clamp_rN.txt dz cua CHINH F2M, kep mat bang theo pitch IMU -- dung
|
|
thiet ke G4 trong ke hoach (dz VO + dz = 0 o node phang)
|
|
|
|
Nguong kep mac dinh 1.5 do: duoi doc thu nho nhat (5 do), tren sai pitch do
|
|
bias IMU muc L2 (~0.9 do). CHOT TRUOC khi cham. Doc that < 1.5 do se bi coi
|
|
la phang -- noi ro trong bao cao.
|
|
|
|
python3 tools/slope_fuse.py DIR [--clamp-deg 1.5]
|
|
"""
|
|
import argparse
|
|
import glob
|
|
import os
|
|
import sys
|
|
|
|
import numpy as np
|
|
|
|
|
|
def load(p):
|
|
d = np.loadtxt(p, comments='#')
|
|
return d[None, :] if d.ndim == 1 else d
|
|
|
|
|
|
def rpy(d):
|
|
qx, qy, qz, qw = d[:, 4], d[:, 5], d[:, 6], d[:, 7]
|
|
roll = np.arctan2(2 * (qw * qx + qy * qz), 1 - 2 * (qx * qx + qy * qy))
|
|
pitch = np.arcsin(np.clip(2 * (qw * qy - qz * qx), -1, 1))
|
|
return roll, pitch
|
|
|
|
|
|
def rot(d):
|
|
qx, qy, qz, qw = d[:, 4], d[:, 5], d[:, 6], d[:, 7]
|
|
R = np.empty((len(d), 3, 3))
|
|
R[:, 0, 0] = 1 - 2 * (qy * qy + qz * qz)
|
|
R[:, 0, 1] = 2 * (qx * qy - qz * qw)
|
|
R[:, 0, 2] = 2 * (qx * qz + qy * qw)
|
|
R[:, 1, 0] = 2 * (qx * qy + qz * qw)
|
|
R[:, 1, 1] = 1 - 2 * (qx * qx + qz * qz)
|
|
R[:, 1, 2] = 2 * (qy * qz - qx * qw)
|
|
R[:, 2, 0] = 2 * (qx * qz - qy * qw)
|
|
R[:, 2, 1] = 2 * (qy * qz + qx * qw)
|
|
R[:, 2, 2] = 1 - 2 * (qx * qx + qy * qy)
|
|
return R
|
|
|
|
|
|
def save(p, d, z, header):
|
|
out = d.copy()
|
|
out[:, 3] = z
|
|
np.savetxt(p, out, fmt='%.9f %.6f %.6f %.6f %.9f %.9f %.9f %.9f', header=header)
|
|
|
|
|
|
def main():
|
|
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
ap.add_argument('dir')
|
|
ap.add_argument('--clamp-deg', type=float, default=1.5)
|
|
a = ap.parse_args()
|
|
th = np.radians(a.clamp_deg)
|
|
|
|
ref_p = os.path.join(a.dir, 'ref.txt')
|
|
if not os.path.exists(ref_p):
|
|
print(f'{a.dir}: thieu ref.txt', file=sys.stderr)
|
|
return 1
|
|
ref = load(ref_p)
|
|
r_roll, r_pitch = rpy(ref)
|
|
|
|
dz = np.diff(ref[:, 3])
|
|
dz[np.abs(r_pitch[1:]) < th] = 0.0
|
|
save(os.path.join(a.dir, 'ref_clamp.txt'), ref, ref[0, 3] + np.concatenate([[0.0], np.cumsum(dz)]),
|
|
f'tham chieu + kep mat bang (|pitch| < {a.clamp_deg} do -> dz = 0)')
|
|
|
|
for f in sorted(glob.glob(os.path.join(a.dir, 'f2m_r*.txt'))):
|
|
tag = os.path.basename(f)[len('f2m_'):-len('.txt')]
|
|
e = load(f)
|
|
if len(e) < 2:
|
|
continue
|
|
R = rot(e)
|
|
dp_w = np.diff(e[:, 1:4], axis=0)
|
|
dp_b = np.einsum('kji,kj->ki', R[:-1], dp_w) # R_prev^T * dp
|
|
roll = np.interp(e[1:, 0], ref[:, 0], r_roll)
|
|
pitch = np.interp(e[1:, 0], ref[:, 0], r_pitch)
|
|
dzh = (-np.sin(pitch) * dp_b[:, 0] + np.cos(pitch) * np.sin(roll) * dp_b[:, 1]
|
|
+ np.cos(pitch) * np.cos(roll) * dp_b[:, 2])
|
|
z0 = e[0, 3]
|
|
save(os.path.join(a.dir, f'hyb_{tag}.txt'), e, z0 + np.concatenate([[0.0], np.cumsum(dzh)]),
|
|
'lai: buoc than F2M xoay bang roll/pitch IMU')
|
|
dzc = dzh.copy()
|
|
dzc[np.abs(pitch) < th] = 0.0
|
|
save(os.path.join(a.dir, f'hyb_clamp_{tag}.txt'), e, z0 + np.concatenate([[0.0], np.cumsum(dzc)]),
|
|
f'lai + kep mat bang (|pitch| < {a.clamp_deg} do)')
|
|
dzf = np.diff(e[:, 3])
|
|
dzf[np.abs(pitch) < th] = 0.0
|
|
save(os.path.join(a.dir, f'f2m_clamp_{tag}.txt'), e, z0 + np.concatenate([[0.0], np.cumsum(dzf)]),
|
|
f'dz F2M + kep mat bang theo pitch IMU (|pitch| < {a.clamp_deg} do)')
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|