From aaa701dfcc39ddc5c47e9d5d88f3f794545c5e3b Mon Sep 17 00:00:00 2001 From: trung09122002 Date: Thu, 3 Sep 2026 09:59:16 +0700 Subject: [PATCH] test(eval): them check_reloc - do relocalization toan cuc Tra loi cau hoi "tha robot xuong cho la, no co tu tim lai duoc khong". Khac eval_ate.py o cho KHONG can chinh Umeyama: relocalization dung nghia la biet minh dang o dau TREN BAN DO CU, chu khong phai co quy dao dung hinh dang. Nen sai so phai la TUYET DOI, va phai biet goc ban do nam dau trong Gazebo. RTAB-Map lay pose node dau tien lam goc = cho robot dung khi bat dau quet, truyen vao bang --map-origin X Y YAW. Phep quy doi da kiem chung ca khi goc quet co xoay. Do: co tim lai duoc khong, mat bao lau va di bao xa, sai so sau khi hoi tu, va dem GlobalClosure (thi giac) so voi Proximity (hinh hoc). De chay duoc bai kiem, phai mo hai cho truoc day bi ep cung trong localization.launch vi ca hai deu MACH nuoc cho he: start_at_origin - khoi tao map->odom ngay tai goc ban do enable_dock_init - dock_init bom /initialpose sau 3 s Va truyen x/y/yaw het chuoi localization -> perception -> odom -> sim -> gazebo de spawn robot o cho khac diem quet. Mac dinh khong doi. --- src/robot_bringup/launch/localization.launch | 40 +++- src/robot_bringup/launch/odom.launch | 10 + src/robot_bringup/launch/perception.launch | 10 + src/slam_eval/CMakeLists.txt | 1 + src/slam_eval/scripts/check_reloc.py | 212 +++++++++++++++++++ 5 files changed, 269 insertions(+), 4 deletions(-) create mode 100755 src/slam_eval/scripts/check_reloc.py diff --git a/src/robot_bringup/launch/localization.launch b/src/robot_bringup/launch/localization.launch index e4d698a..41c66f2 100644 --- a/src/robot_bringup/launch/localization.launch +++ b/src/robot_bringup/launch/localization.launch @@ -25,6 +25,13 @@ --> + + + + + @@ -56,10 +63,34 @@ --> + + + + + + + @@ -73,9 +104,10 @@ - - + + @@ -94,7 +126,7 @@ - + diff --git a/src/robot_bringup/launch/odom.launch b/src/robot_bringup/launch/odom.launch index cf9eb60..3239422 100644 --- a/src/robot_bringup/launch/odom.launch +++ b/src/robot_bringup/launch/odom.launch @@ -21,6 +21,13 @@ --> + + + + + @@ -34,6 +41,9 @@ + + + diff --git a/src/robot_bringup/launch/perception.launch b/src/robot_bringup/launch/perception.launch index 897bcf2..29bc013 100644 --- a/src/robot_bringup/launch/perception.launch +++ b/src/robot_bringup/launch/perception.launch @@ -19,6 +19,13 @@ --> + + + + + @@ -54,6 +61,9 @@ args="0 0 0 0 0 0 map odom"/> + + + diff --git a/src/slam_eval/CMakeLists.txt b/src/slam_eval/CMakeLists.txt index bccb091..484aecf 100644 --- a/src/slam_eval/CMakeLists.txt +++ b/src/slam_eval/CMakeLists.txt @@ -11,6 +11,7 @@ catkin_install_python(PROGRAMS scripts/resource_profile.py scripts/watch_loc.py scripts/check_tilt_tf.py + scripts/check_reloc.py DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} ) diff --git a/src/slam_eval/scripts/check_reloc.py b/src/slam_eval/scripts/check_reloc.py new file mode 100755 index 0000000..b397a61 --- /dev/null +++ b/src/slam_eval/scripts/check_reloc.py @@ -0,0 +1,212 @@ +#!/usr/bin/env python3 +""" +check_reloc - Do RELOCALIZATION TOAN CUC ("kidnapped robot"). + +============ CAU HOI CAN TRA LOI ============ +Tha robot xuong mot cho la tren ban do da co, KHONG cho no biet dang o dau. +No co tu tim lai duoc khong, mat bao lau, va sai bao nhieu sau khi tim duoc? + +Day la kha nang ma ban do khong co dac trung thi giac KHONG THE co: da do +tren ban do house cu -> Word 0, Feature 0, GlobalClosure 0. Khi do he chi con +RGBD/ProximityBySpace, ma cai do can pose khoi tao DA GAN DUNG moi bam duoc. + +Chay bo kiem nay tren CA HAI ban do (co va khong co visual_loop). Chenh lech +giua hai lan chinh la gia tri cua viec bat thi giac. + +============ CACH DO SAI SO ============ +Khong dung Umeyama can chinh nhu eval_ate.py. O day sai so TUYET DOI moi co +nghia - relocalization dung nghia la biet minh dang o dau TREN BAN DO CU, chu +khong phai co mot quy dao dung hinh dang. + +Nen phai biet goc ban do nam o dau trong the gioi Gazebo. RTAB-Map lay pose +cua node dau tien lam goc, tuc la CHO ROBOT DUNG KHI BAT DAU QUET: + + --map-origin X Y YAW pose (trong Gazebo) noi da bat dau quet ban do + +Vi du world house: mapping.launch spawn robot o (-3.0, 1.0, yaw 0), nen + rosrun slam_eval check_reloc.py --map-origin -3.0 1.0 0.0 + +Sai so = || pose SLAM doc tu TF - pose that quy doi ve he ban do || + +============ CACH DUNG ============ + 1. Chay localization.launch, spawn robot o cho KHAC han diem quet: + roslaunch robot_bringup localization.launch world:=house \\ + database_path:=$HOME/projects/vSLAM_ws/maps/map_house.db \\ + visual_loop:=true x:=4.0 y:=-3.0 yaw:=1.57 + ⚠️ start_at_origin dang la true trong localization.launch - voi bai kiem + nay phai de false, neu khong la ta MACH nuoc cho no. + 2. Bo kiem (terminal rieng): + rosrun slam_eval check_reloc.py --map-origin -3.0 1.0 0.0 + 3. Lai robot di quanh. Bo kiem in ra ngay khi tim lai duoc. + 4. Ctrl-C de xem tong ket. +""" + +import argparse +import math +import sys + +import rospy +import tf2_ros +from nav_msgs.msg import Odometry + +try: + from rtabmap_msgs.msg import Info + HAVE_INFO = True +except ImportError: + HAVE_INFO = False + + +def yaw_of(q): + return math.atan2(2.0 * (q.w * q.z + q.x * q.y), + 1.0 - 2.0 * (q.y * q.y + q.z * q.z)) + + +class Reloc(object): + def __init__(self, origin, thresh, hold): + self.ox, self.oy, self.oyaw = origin + self.thresh = thresh # m, duoi nguong nay coi la da tim duoc + self.hold = hold # s, phai giu duoi nguong bao lau + + self.gt = None + self.t0 = None + self.path = 0.0 # quang duong that da di + self.last_gt = None + + self.below_since = None + self.found_at = None # (t, quang duong) luc tim duoc + self.errors = [] # sai so sau khi da tim duoc + self.n_global = 0 + self.n_prox = 0 + + rospy.Subscriber('/ground_truth/odom', Odometry, self.on_gt, queue_size=50) + if HAVE_INFO: + rospy.Subscriber('/rtabmap/info', Info, self.on_info, queue_size=20) + + def on_gt(self, m): + p = m.pose.pose.position + if self.last_gt is not None: + self.path += math.hypot(p.x - self.last_gt[0], p.y - self.last_gt[1]) + self.last_gt = (p.x, p.y) + self.gt = m + + def on_info(self, m): + if m.loopClosureId > 0: + self.n_global += 1 + rospy.loginfo(' GlobalClosure (thi giac) node %d <-> %d', + m.refId, m.loopClosureId) + elif m.proximityDetectionId > 0: + self.n_prox += 1 + + def world_to_map(self, x, y, yaw): + """Quy doi pose that trong Gazebo ve he ban do cua RTAB-Map.""" + dx, dy = x - self.ox, y - self.oy + c, s = math.cos(-self.oyaw), math.sin(-self.oyaw) + return dx * c - dy * s, dx * s + dy * c, yaw - self.oyaw + + def step(self, buf, now): + if self.gt is None: + return None + try: + tr = buf.lookup_transform('map', 'base_footprint', + rospy.Time(0), rospy.Duration(0.5)).transform + except (tf2_ros.LookupException, tf2_ros.ExtrapolationException, + tf2_ros.ConnectivityException): + return None + + p = self.gt.pose.pose + ex, ey, _ = self.world_to_map(p.position.x, p.position.y, + yaw_of(p.orientation)) + err = math.hypot(tr.translation.x - ex, tr.translation.y - ey) + + if self.found_at is not None: + self.errors.append(err) + elif err < self.thresh: + if self.below_since is None: + self.below_since = now + elif (now - self.below_since) >= self.hold: + self.found_at = (now - self.t0, self.path) + rospy.loginfo('*** DA TIM LAI DUOC sau %.1f s / %.2f m ***', + *self.found_at) + else: + self.below_since = None + return err + + def report(self, a): + print('') + print('=' * 64) + print(' RELOCALIZATION TOAN CUC') + print('=' * 64) + print(' Goc ban do (Gazebo) : (%.2f, %.2f, %.3f rad)' + % (self.ox, self.oy, self.oyaw)) + print(' Nguong / giu : %.2f m trong %.1f s' % (self.thresh, self.hold)) + print(' Quang duong da di : %.2f m' % self.path) + print(' ---------------------------------------------') + if self.found_at: + t, d = self.found_at + print(' KET QUA : TIM LAI DUOC') + print(' Mat : %.1f s / %.2f m' % (t, d)) + if self.errors: + e = sorted(self.errors) + print(' Sai so sau khi tim : trung vi %.3f m | p95 %.3f m | max %.3f m' + % (e[len(e) // 2], e[int(len(e) * 0.95)], e[-1])) + else: + print(' KET QUA : KHONG TIM LAI DUOC') + print(' -> he chi con proximity hinh hoc, ma cai do can pose khoi') + print(' tao da gan dung. Kiem tra database co Word khong:') + print(' sqlite3 "SELECT COUNT(*) FROM Word;"') + print(' ---------------------------------------------') + if HAVE_INFO: + print(' GlobalClosure (thi giac) : %d' % self.n_global) + print(' Proximity (hinh hoc) : %d' % self.n_prox) + if self.n_global == 0: + print(' ⚠️ 0 GlobalClosure -> ban do nay khong co tui tu.') + print('=' * 64) + print('') + + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument('--map-origin', nargs=3, type=float, required=True, + metavar=('X', 'Y', 'YAW'), + help='pose trong Gazebo noi DA BAT DAU quet ban do') + ap.add_argument('--thresh', type=float, default=0.30, + help='sai so duoi muc nay thi coi la da tim lai duoc (m)') + ap.add_argument('--hold', type=float, default=3.0, + help='phai giu duoi nguong bao lau moi tinh (s)') + a, _ = ap.parse_known_args() + + rospy.init_node('check_reloc', anonymous=True, disable_signals=True) + if not HAVE_INFO: + rospy.logwarn('check_reloc: khong import duoc rtabmap_msgs/Info ' + '-> khong dem duoc loop closure.') + + r = Reloc(a.map_origin, a.thresh, a.hold) + buf = tf2_ros.Buffer() + tf2_ros.TransformListener(buf) + + while not rospy.is_shutdown() and r.gt is None: + rospy.sleep(0.2) + r.t0 = rospy.Time.now().to_sec() + + print('dang theo doi. Lai robot di quanh. Ctrl-C de xem tong ket.') + print('%8s %10s %10s %s' % ('t(s)', 'sai so(m)', 'da di(m)', 'trang thai')) + try: + while not rospy.is_shutdown(): + now = rospy.Time.now().to_sec() + err = r.step(buf, now) + if err is not None: + state = 'DA TIM DUOC' if r.found_at else 'dang tim...' + print('%8.1f %10.3f %10.2f %s' + % (now - r.t0, err, r.path, state)) + rospy.sleep(2.0) + except KeyboardInterrupt: + pass + r.report(a) + return 0 + + +if __name__ == '__main__': + try: + sys.exit(main()) + except rospy.ROSInterruptException: + pass