reloc: --slam-reloc(-auto) tu dinh vi lai trong node (tu xoay/tu di, nguoi luon thang); web ve robot mo o vi tri doan; sieu am cho xoay; LocalMatch gioi han huong

This commit is contained in:
trung09122002
2026-09-29 11:16:04 +07:00
parent 399a46139e
commit 60853531aa
15 changed files with 883 additions and 27 deletions

View File

@@ -34,13 +34,15 @@ def ang(a, b):
return abs(math.degrees(math.atan2(math.sin(a - b), math.cos(a - b))))
def node_yaws(db):
"""Huong (rad) cua moi node theo pose DA TOI UU trong DB."""
def node_poses(db):
"""{id: (x, y, huong rad)} cua moi node theo pose DA TOI UU trong DB."""
c = sqlite3.connect(f"file:{db}?mode=ro", uri=True).cursor()
ids_b, poses_b = c.execute("select opt_ids, opt_poses from Admin").fetchone()
ids = np.frombuffer(zlib.decompress(ids_b[:-12]), dtype=np.int32)
P = np.frombuffer(zlib.decompress(poses_b[:-12]), dtype=np.float32).reshape(-1, 3, 4)
return {int(i): math.atan2(float(P[k][1, 0]), float(P[k][0, 0])) for k, i in enumerate(ids)}
return {int(i): (float(P[k][0, 3]), float(P[k][1, 3]),
math.atan2(float(P[k][1, 0]), float(P[k][0, 0])))
for k, i in enumerate(ids)}
def main():
@@ -54,7 +56,9 @@ def main():
ap.add_argument("--vpr-yaw-deg", type=float, default=45.0,
help="huong chot phai lech huong node anh VPR it hon ngan nay")
a = ap.parse_args()
yaw = node_yaws(a.db)
npose = node_poses(a.db)
yaw = {k: v[2] for k, v in npose.items()}
pos = {k: (v[0], v[1]) for k, v in npose.items()}
q = {}
cands = defaultdict(list)
@@ -70,7 +74,12 @@ def main():
rank=int(r["rank"]), node=int(r["vpr_node"]), sim=float(r["vpr_sim"]),
node_dist=float(r["vpr_node_dist_m"]),
score=float(r["loc_score"]),
l=(float(r["loc_x"]), float(r["loc_y"]), float(r["loc_th"]))))
l=(float(r["loc_x"]), float(r["loc_y"]), float(r["loc_th"])),
# Khop cuc bo GIOI HAN HUONG quanh huong node anh VPR (neu CSV co).
vscore=float(r.get("vloc_score", -1) or -1),
v=(float(r.get("vloc_x", 0) or 0), float(r.get("vloc_y", 0) or 0),
float(r.get("vloc_th", 0) or 0))))
q[qid]["s1"] = float(r["glob_s1"])
def correct(p, t):
return math.hypot(p[0] - t[0], p[1] - t[1]) < a.ok_m and ang(p[2], t[2]) < a.ok_deg
@@ -113,12 +122,15 @@ def main():
# --- Dong thuan CO dieu kien: VPR xac nhan ca HUONG, va du giong ---
def vpr_ok(c, pose, tau):
# Ca HUONG va VI TRI phai khop node anh VPR. Thieu dieu kien vi tri: ca
# VPR dung cho (0.09 m) ma LiDAR ca hai phia cung truot 3 m van lot.
return (c["score"] > 0 and c["sim"] >= tau and c["node"] in yaw
and ang(pose[2], yaw[c["node"]]) < a.vpr_yaw_deg)
and ang(pose[2], yaw[c["node"]]) < a.vpr_yaw_deg
and math.hypot(pose[0] - pos[c["node"]][0], pose[1] - pos[c["node"]][1]) < 2.0)
best = None
for K in (1, 3):
for tau in (0.6, 0.7, 0.8):
for tau in (0.5, 0.6, 0.7):
picks = {}
for k, v in q.items():
if v["glob_ok"] and any(agree(v["g"], c["l"]) and vpr_ok(c, v["g"], tau)
@@ -144,6 +156,45 @@ def main():
for name, acc, ok, bad in rows:
print(f" {name:44s} {acc:5d} {ok:5d} {bad:5d} {100.0 * ok / max(1, n):8.1f}%"
+ (" ⚠️" if bad > 0 else ""))
# --- VPR DAN: anh chon cho + huong, LiDAR xac minh; KHONG can LiDAR toan
# cuc dong y (cuu duoc ca ca LiDAR nhap nhang huong). ---
if not any(c["vscore"] > 0 for cs in cands.values() for c in cs):
print("\n(CSV khong co cot vloc_* -- chay lai bench ban moi de co luat 'VPR dan')")
return 0
lidar = {k: v["g"] for k, v in q.items() if v["glob_ok"] and v["verdict"] == "CHOT"}
print(f"\nVPR DAN: giong >= tau, khop cuc bo GIOI HAN huong (+-45 do quanh node anh) "
f"nam trong 2 m quanh node anh,\n diem LiDAR cua no >= alpha x diem LiDAR tot "
f"nhat TOAN ban do. Chot pose khop cuc bo do.")
print(f" {'luat':30s} {'chot':>5s} {'DUNG':>5s} {'SAI':>4s} | {'HOP cong LiDAR: DUNG':>21s} {'SAI':>4s} {'dung/tong':>9s}")
for K in (1, 3):
for tau in (0.5, 0.6, 0.7):
for alpha in (0.9, 0.95, 1.0):
picks = {}
for k, v in q.items():
best_c = None
for c in cands[k]:
if c["rank"] > K or c["vscore"] <= 0 or c["sim"] < tau:
continue
if c["vscore"] < alpha * v.get("s1", 0.0):
continue
# Pose khop phai o GAN node anh VPR (vi tri doc tu DB,
# khong dung dap an): anh giong nhau chup trong ~2 m.
nxy = pos.get(c["node"])
if nxy is None or math.hypot(c["v"][0] - nxy[0], c["v"][1] - nxy[1]) >= 2.0:
continue
if best_c is None or c["vscore"] > best_c["vscore"]:
best_c = c
if best_c is not None:
picks[k] = best_c["v"]
ok = sum(1 for k, p in picks.items() if correct(p, q[k]["t"]))
u = dict(picks)
u.update(lidar) # cong LiDAR da tin duoc -> uu tien
uok = sum(1 for k, p in u.items() if correct(p, q[k]["t"]))
bad, ubad = len(picks) - ok, len(u) - uok
print(f" top-{K} tau {tau:.1f} alpha {alpha:.2f} {len(picks):5d} {ok:5d} {bad:4d} | "
f"{uok:21d} {ubad:4d} {100.0 * uok / max(1, n):8.1f}%"
+ (" ⚠️" if ubad > 0 else ""))
return 0

View File

@@ -156,6 +156,8 @@ int main(int argc, char** argv) {
// Ung vien VPR (tools/vpr_eval.py --topk-csv): query_id,rank,node_id,sim.
std::string vpr_csv, fusion_csv;
double vpr_radius = 2.5;
// Cua so huong quanh huong node anh VPR cho lan khop cuc bo THU HAI (vloc).
double vpr_yaw_deg = 45.0;
for (int i = 1; i < argc; ++i) {
const std::string a = argv[i];
@@ -173,6 +175,7 @@ int main(int argc, char** argv) {
else if (a == "--vpr-csv") vpr_csv = next();
else if (a == "--fusion-csv") fusion_csv = next();
else if (a == "--vpr-radius") vpr_radius = std::atof(next().c_str());
else if (a == "--vpr-yaw-deg") vpr_yaw_deg = std::atof(next().c_str());
else if (a == "--help") {
std::printf(
"cach dung: %s --map-db A.db [--query-db B.db] [--n 200] [--seq K]\n"
@@ -284,7 +287,7 @@ int main(int argc, char** argv) {
std::fprintf(fus,
"query_id,glob_ok,glob_verdict,glob_ratio,glob_ratio_yaw,glob_s1,glob_x,glob_y,"
"glob_th,truth_x,truth_y,truth_th,rank,vpr_node,vpr_sim,vpr_node_dist_m,"
"loc_score,loc_x,loc_y,loc_th\n");
"loc_score,loc_x,loc_y,loc_th,vloc_score,vloc_x,vloc_y,vloc_th\n");
}
if (!split && query_db != map_db) {
@@ -363,7 +366,7 @@ int main(int argc, char** argv) {
ge.theta, tru.x, tru.y, tru.theta);
const auto it = vpr.find(nodes[idx].id);
if (it == vpr.end()) {
std::fprintf(fus, "%s,0,-1,0,0,-1,0,0,0\n", head);
std::fprintf(fus, "%s,0,-1,0,0,-1,0,0,0,-1,0,0,0\n", head);
} else {
int rank = 0;
for (const auto& cand : it->second) {
@@ -378,9 +381,18 @@ int main(int argc, char** argv) {
const vslam::Pose2D le = h.score > 0.0
? vslam::Pose2DCompose(h.map_from_odom, nodes[last].odom)
: vslam::Pose2D{};
std::fprintf(fus, "%s,%d,%d,%.4f,%.3f,%.2f,%.3f,%.3f,%.4f\n", head, rank, cand.first,
cand.second, std::hypot(center.x - tru.x, center.y - tru.y), h.score,
le.x, le.y, le.theta);
// Lan hai: HUONG do anh VPR chon (+-vpr_yaw_deg quanh huong node
// anh), LiDAR chi chinh vi tri/huong trong cua so do.
const vslam::RelocHypothesis hv = vslam::LocalMatch(
field, pts, nodes[last].odom, center, vpr_radius, cfg, mp->second.theta,
vpr_yaw_deg * M_PI / 180.0);
const vslam::Pose2D ve = hv.score > 0.0
? vslam::Pose2DCompose(hv.map_from_odom, nodes[last].odom)
: vslam::Pose2D{};
std::fprintf(fus, "%s,%d,%d,%.4f,%.3f,%.2f,%.3f,%.3f,%.4f,%.2f,%.3f,%.3f,%.4f\n", head,
rank, cand.first, cand.second,
std::hypot(center.x - tru.x, center.y - tru.y), h.score, le.x, le.y,
le.theta, hv.score, ve.x, ve.y, ve.theta);
}
}
}