From b0d14d7206830847b776965e26d1449e4370e59f Mon Sep 17 00:00:00 2001 From: trung09122002 Date: Thu, 3 Sep 2026 09:54:04 +0700 Subject: [PATCH] feat(2.5d): dua roll/pitch do tu IMU vao TF tren phan cung that MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Nguyen tac 2.5D la pose SE(2) con roll/pitch thi DO. Nhung do nghieng do duoc truoc gio chi den duoc octomap/costmap qua gravity_align_nodelet; RTAB-Map dat anh depth bang TF phang nen ban do 3D khong duoc bu nghieng. Cach lam: khai hai khop revolute base_roll_joint / base_pitch_joint, va base_tilt_tf.py publish /joint_states. robot_state_publisher lo phan TF nen van la NGUON DUY NHAT cua moi canh - khong tranh chap. Nghiem thu tren nhanh phan cung (description.launch use_sim:=false): base_footprint -> base_roll_link /tf 392 ban tin base_roll_link -> base_link /tf 392 ban tin -> 2 canh, moi canh mot nguon TF roll 5.057 pitch -8.637 vs IMU bom vao 5.000 / -8.670 -> lech 0.057 ⚠️ Chi bat khi use_sim=false. Trong Gazebo giu khop fixed nhu cu: san phang nen pitch do duoc chi 0.0006 do, khop revolute khong mang lai gi ma Gazebo lai mo phong vat ly no. Da kiem: URDF sinh ra khi use_sim=true GIONG HET TUNG BYTE ban truoc thay doi. Da thu type="floating" truoc do va that bai, hai ly do doc lap: 1. urdf2sdf huy luon link goc -> gazebo_ros_p3d FATAL, mat /scan va odom 2. libkdl_parser doi floating thanh fixed -> RSP van phat, thanh hai nguon Ca hai deu ghi lai trong robot.urdf.xacro de khong ai thu lai. gravity_align_nodelet them param tilt_from_tf de KHONG bu hai lan; no buoc theo use_sim trong perception.launch nen hai ben khong the lech nhau. --- .../launch/perception_nodelets.launch | 15 ++ .../src/gravity_align_nodelet.cpp | 41 +++- src/robot_bringup/CMakeLists.txt | 1 + src/robot_bringup/launch/description.launch | 60 ++++++ src/robot_bringup/launch/odom.launch | 7 + src/robot_bringup/launch/perception.launch | 10 + src/robot_bringup/launch/sim.launch | 6 + src/robot_bringup/scripts/base_tilt_tf.py | 194 ++++++++++++++++++ src/robot_bringup/urdf/robot.urdf.xacro | 126 +++++++++++- 9 files changed, 448 insertions(+), 12 deletions(-) create mode 100755 src/robot_bringup/scripts/base_tilt_tf.py diff --git a/src/perception_3d/launch/perception_nodelets.launch b/src/perception_3d/launch/perception_nodelets.launch index 904348c..046a4bf 100644 --- a/src/perception_3d/launch/perception_nodelets.launch +++ b/src/perception_3d/launch/perception_nodelets.launch @@ -51,9 +51,24 @@ + + + + diff --git a/src/perception_3d/src/gravity_align_nodelet.cpp b/src/perception_3d/src/gravity_align_nodelet.cpp index 6ea41f2..0ddfa9c 100644 --- a/src/perception_3d/src/gravity_align_nodelet.cpp +++ b/src/perception_3d/src/gravity_align_nodelet.cpp @@ -75,6 +75,20 @@ private: pnh_.param("output_frame", output_frame_, "base_footprint"); pnh_.param("wheel_radius", wheel_radius_, 0.075); + // ============ ⚠️ TRANH BU NGHIENG HAI LAN ============ + // false (mac dinh): canh base_footprint -> base_link trong TF la PHANG + // (base_joint la khop fixed rpy="0 0 0"), nen node nay phai tu ap + // Lift(0,0,r) * Tilt(roll,pitch). + // + // true: chay kem robot_bringup/scripts/base_tilt_tf.py, canh do TRONG TF + // DA co san do nghieng. Node nay chi con tra thang + // output_frame <- camera va ap mot phep duy nhat. + // Van tu ap nua la NGHIENG HAI LAN - loi im lang, cloud lech gap doi + // goc nghieng that ma khong co gi bao. + // + // PHAI dat bang description.launch:tilt_in_tf. Hai co lech nhau la sai. + pnh_.param("tilt_from_tf", tilt_from_tf_, false); + // He so complementary filter: ty trong cua gyro. // 0.98 @200Hz -> hang so thoi gian ~0.25s. Gyro chiem uu the ngan han, // accel keo ve trong dai han. @@ -211,17 +225,23 @@ private: if (msg->width == 0 || msg->height == 0) return; - // --- Static transform camera -> base_link (tu URDF) --- + // --- Transform camera -> khung dich --- + // tilt_from_tf_=false: tra ve base_link (canh TF phang), phai tu ap + // Lift*Tilt o duoi. + // tilt_from_tf_=true : tra thang ve base_footprint - canh do da mang san + // Lift va Tilt do base_tilt_tf.py phat. + const std::string& target = tilt_from_tf_ ? output_frame_ : base_frame_; + geometry_msgs::TransformStamped tf_bl_cam; try { - tf_bl_cam = tf_buffer_->lookupTransform(base_frame_, msg->header.frame_id, + tf_bl_cam = tf_buffer_->lookupTransform(target, msg->header.frame_id, ros::Time(0), ros::Duration(0.1)); } catch (const tf2::TransformException& ex) { NODELET_WARN_THROTTLE(5.0, "gravity_align: khong lookup duoc TF %s <- %s: %s", - base_frame_.c_str(), msg->header.frame_id.c_str(), ex.what()); + target.c_str(), msg->header.frame_id.c_str(), ex.what()); return; } @@ -255,12 +275,16 @@ private: tf_bl_cam.transform.rotation.y, tf_bl_cam.transform.rotation.z).toRotationMatrix(); + // Khi tilt_from_tf_=true, t_bl_cam DA la base_footprint <- camera va da + // chua ca Lift lan Tilt. Nhan them nua la bu hai lan. Eigen::Affine3d tilt = Eigen::Affine3d::Identity(); - tilt.linear() = (Eigen::AngleAxisd(pitch, Eigen::Vector3d::UnitY()) * - Eigen::AngleAxisd(roll, Eigen::Vector3d::UnitX())).toRotationMatrix(); - Eigen::Affine3d lift = Eigen::Affine3d::Identity(); - lift.translation() << 0.0, 0.0, wheel_radius_; + if (!tilt_from_tf_) + { + tilt.linear() = (Eigen::AngleAxisd(pitch, Eigen::Vector3d::UnitY()) * + Eigen::AngleAxisd(roll, Eigen::Vector3d::UnitX())).toRotationMatrix(); + lift.translation() << 0.0, 0.0, wheel_radius_; + } const Eigen::Affine3f total = (lift * tilt * t_bl_cam).cast(); @@ -308,6 +332,9 @@ private: std::shared_ptr tf_listener_; std::string base_frame_, output_frame_; + // true = canh base_footprint -> base_link trong TF DA co do nghieng + // (base_tilt_tf.py phat) -> node nay KHONG duoc tu ap nua. + bool tilt_from_tf_ = false; double wheel_radius_ = 0.075; double alpha_ = 0.98; double accel_gate_ = 1.0; diff --git a/src/robot_bringup/CMakeLists.txt b/src/robot_bringup/CMakeLists.txt index fa26d19..8c4e7b0 100644 --- a/src/robot_bringup/CMakeLists.txt +++ b/src/robot_bringup/CMakeLists.txt @@ -9,6 +9,7 @@ catkin_package() # phai build. Cai dat de dung duoc bang `rosrun robot_bringup teleop.py`. catkin_install_python(PROGRAMS scripts/teleop.py + scripts/base_tilt_tf.py scripts/virtual_joystick.py # teleop_model.py la MODULE, khong phai node. Phai cai vao CUNG thu muc voi # virtual_joystick.py vi node import no theo kieu anh em (sys.path[0] la thu diff --git a/src/robot_bringup/launch/description.launch b/src/robot_bringup/launch/description.launch index c250b46..6077677 100644 --- a/src/robot_bringup/launch/description.launch +++ b/src/robot_bringup/launch/description.launch @@ -22,6 +22,35 @@ publish joint_states cho banh xe -> bat fake de TF khong dut. --> + + + + @@ -31,6 +60,37 @@ + + + + + + + + + + + + + + + + + + @@ -29,6 +34,7 @@ + @@ -39,6 +45,7 @@ + diff --git a/src/robot_bringup/launch/perception.launch b/src/robot_bringup/launch/perception.launch index 8b902e8..897bcf2 100644 --- a/src/robot_bringup/launch/perception.launch +++ b/src/robot_bringup/launch/perception.launch @@ -19,6 +19,11 @@ --> + + + + @@ -49,6 +54,7 @@ args="0 0 0 0 0 0 map odom"/> + @@ -56,6 +62,10 @@ + + diff --git a/src/robot_bringup/launch/sim.launch b/src/robot_bringup/launch/sim.launch index b5d65c2..11a714d 100644 --- a/src/robot_bringup/launch/sim.launch +++ b/src/robot_bringup/launch/sim.launch @@ -17,6 +17,11 @@ --> + + + + @@ -43,6 +48,7 @@ + diff --git a/src/robot_bringup/scripts/base_tilt_tf.py b/src/robot_bringup/scripts/base_tilt_tf.py new file mode 100755 index 0000000..9e169ca --- /dev/null +++ b/src/robot_bringup/scripts/base_tilt_tf.py @@ -0,0 +1,194 @@ +#!/usr/bin/env python3 +""" +base_tilt_tf - Dua do nghieng DO tu IMU vao TF, qua /joint_states. + +============ VAI TRO TRONG KIEN TRUC 2.5D ============ +Nguyen tac cua 2.5D: pose la SE(2), con roll/pitch thi DO chu khong uoc luong. +Cho dung de dat do nghieng do duoc la TF, khong phai pose: + + map -> odom SLAM sua drift (RTAB-Map) + odom -> base_footprint SE(2) phang tuyet doi (EKF, two_d_mode=true) + base_footprint -> base_roll_link -> base_link <- NGHIENG NAM O DAY + base_link -> laser/camera/imu tinh, tu URDF + +Nho vay pose van la 3 bac tu do, ma moi thu doc TF - RTAB-Map, octomap, +costmap, RViz - deu nhan duoc do nghieng that ma khong phai tu bu. + +============ ⚠️ VI SAO PHAT /joint_states CHU KHONG PHAT TF ============ +Mot canh TF chi duoc co MOT nguon. robot_state_publisher phat MOI khop `fixed`, +nen khong the vua de khop fixed vua tu phat canh do - se thanh hai nguon, +va TF KHONG BAO LOI, no chi cho ket qua nhay lung tung. + +Da thu de khop thanh `floating` cho RSP bo qua. THAT BAI, hai ly do: + 1. urdf2sdf cua Gazebo huy luon link goc: + "link[base_footprint] is not modeled in sdf" + [FATAL] gazebo_ros_p3d: bodyName: base_footprint does not exist + 2. libkdl_parser.so doi nguoc lai: + "Converting unknown joint type of joint '%s' into a fixed joint" + -> RSP van phat canh do tren phan cung that. + +Cach dung: khai hai khop REVOLUTE trong URDF va publish /joint_states. Do la +co che RSP ho tro san, nen RSP van la nguon duy nhat cua moi canh TF. + +============ ⚠️ NODE NAY GIU MOT CANH TF SONG CON ============ +base_footprint -> base_link noi phan dinh vi voi toan bo phan cam bien. Node +chet la RSP khong con goc khop -> canh dung yen o gia tri cuoi (khong dut han +nhu truong hop tu phat TF, nhung cloud se lech dan neu robot doi do nghieng). + +Node duoc viet de KHONG BAO GIO ngung phat: + - Mat IMU -> phat roll=pitch=0 (coi nhu phang) va canh bao, VAN phat + - IMU cu -> nhu tren + - Chua co IMU lan nao -> phat 0 ngay tu dau +Va nen dat respawn="true" trong launch. + +============ CACH DO ROLL/PITCH ============ +Bo loc bu (complementary filter), giong het gravity_align_nodelet de hai ben +khong lech nhau: + + tich phan gyro -> nhanh, muot, nhung TROI (da do: 5.2 do sau ~10 s) + goc tu accel -> khong troi, nhung nhieu va sai khi robot tang toc + tron: rp = alpha * rp_gyro + (1-alpha) * rp_accel + +⚠️ Chi lay goc tu accel khi | |a| - g | < accel_gate, tuc robot khong tang toc + dang ke. Khong co cong nay thi luc tang toc accel do ca gia toc chuyen dong + va bo loc keo pitch di sai. +⚠️ Loc thong thap accel TRUOC khi so voi cong. Da mac loi cong tren mau tho: + pitch troi toi 5.2 do; loc xong con 0.02 do. +""" + +import math + +import rospy +from sensor_msgs.msg import Imu, JointState +from std_msgs.msg import Float64MultiArray + +GRAVITY = 9.80665 + + +class BaseTiltTF(object): + def __init__(self): + # ⚠️ Ten PHAI khop robot.urdf.xacro. Sai ten thi RSP im lang bo qua, + # khong bao loi, va canh TF dung nguyen o 0. + self.roll_joint = rospy.get_param('~roll_joint', 'base_roll_joint') + self.pitch_joint = rospy.get_param('~pitch_joint', 'base_pitch_joint') + + self.rate_hz = rospy.get_param('~rate', 50.0) + self.alpha = rospy.get_param('~alpha', 0.98) + self.accel_lpf_alpha = rospy.get_param('~accel_lpf_alpha', 0.05) + self.accel_gate = rospy.get_param('~accel_gate', 0.5) + self.imu_timeout = rospy.get_param('~imu_timeout', 0.5) + # Chan do nghieng vo ly: IMU loi hoac dat sai chieu thi thà coi la phang + # con hon lam lech ca ban do. + self.max_tilt = rospy.get_param('~max_tilt', math.radians(25.0)) + + self.roll = 0.0 + self.pitch = 0.0 + self.ax_f = 0.0 + self.ay_f = 0.0 + self.az_f = GRAVITY + self.have_imu = False + self.last_imu = None + self.warned = False + + # /joint_states chap nhan NHIEU nguon publish: robot_state_publisher + # gom theo TEN KHOP. Node nay chi phat hai khop nghieng; banh xe do + # firmware MCU phat. Khong dam nhau. + self.js_pub = rospy.Publisher('/joint_states', JointState, queue_size=10) + self.rp_pub = rospy.Publisher('~roll_pitch', Float64MultiArray, + queue_size=10) + rospy.Subscriber(rospy.get_param('~imu_topic', '/imu/data_raw'), + Imu, self.on_imu, queue_size=100) + + rospy.loginfo('base_tilt_tf: publish /joint_states cho [%s, %s] @ %.0f Hz', + self.roll_joint, self.pitch_joint, self.rate_hz) + rospy.loginfo('base_tilt_tf: roll/pitch DO tu IMU, KHONG uoc luong. ' + 'Mat IMU -> phat 0 va canh bao, KHONG ngung phat.') + + # ---------------- IMU ---------------- + + def on_imu(self, msg): + now = msg.header.stamp + if self.last_imu is None: + self.last_imu = now + self.have_imu = True + return + + dt = (now - self.last_imu).to_sec() + self.last_imu = now + # Bo mau nhay lui hoac cach qua xa (bag tua lai, /clock nhay) + if dt <= 0.0 or dt > 0.5: + return + + # 1. Tich phan gyro - muot nhung troi + self.roll += msg.angular_velocity.x * dt + self.pitch += msg.angular_velocity.y * dt + + # 2. Loc thong thap accel TRUOC khi so voi cong + a = msg.linear_acceleration + k = self.accel_lpf_alpha + self.ax_f = k * a.x + (1.0 - k) * self.ax_f + self.ay_f = k * a.y + (1.0 - k) * self.ay_f + self.az_f = k * a.z + (1.0 - k) * self.az_f + + norm = math.sqrt(self.ax_f ** 2 + self.ay_f ** 2 + self.az_f ** 2) + if norm < 1e-3 or abs(norm - GRAVITY) >= self.accel_gate: + return # dang tang toc -> accel khong dang tin, chi giu gyro + + # 3. Keo ve goc do tu trong luc + roll_a = math.atan2(self.ay_f, self.az_f) + pitch_a = math.atan2(-self.ax_f, + math.sqrt(self.ay_f ** 2 + self.az_f ** 2)) + self.roll = self.alpha * self.roll + (1.0 - self.alpha) * roll_a + self.pitch = self.alpha * self.pitch + (1.0 - self.alpha) * pitch_a + + # ---------------- TF ---------------- + + def tilt_now(self, stamp): + """Tra ve (roll, pitch). Luon tra duoc gia tri, khong bao gio raise.""" + fresh = (self.have_imu and self.last_imu is not None and + (stamp - self.last_imu).to_sec() < self.imu_timeout) + if not fresh: + if not self.warned: + rospy.logwarn('base_tilt_tf: KHONG co IMU moi -> phat roll=pitch=0. ' + 'Ban do 3D se SAI neu robot thuc su nghieng.') + self.warned = True + return 0.0, 0.0 + + if self.warned: + rospy.loginfo('base_tilt_tf: IMU tro lai.') + self.warned = False + + r = max(-self.max_tilt, min(self.max_tilt, self.roll)) + p = max(-self.max_tilt, min(self.max_tilt, self.pitch)) + if r != self.roll or p != self.pitch: + rospy.logwarn_throttle( + 5.0, 'base_tilt_tf: nghieng vuot nguong %.1f do -> da chan. ' + 'Kiem tra huong lap IMU.', math.degrees(self.max_tilt)) + return r, p + + def publish(self, _evt=None): + stamp = rospy.Time.now() + r, p = self.tilt_now(stamp) + + # Chi phat GOC KHOP. robot_state_publisher dung URDF de dung TF + # base_footprint -> base_roll_link -> base_link, gom ca do cao + # wheel_radius trong cua base_roll_joint. + # => khong con phai tu tinh quaternion, va khong the lech voi URDF. + js = JointState() + js.header.stamp = stamp + js.name = [self.roll_joint, self.pitch_joint] + js.position = [r, p] + js.velocity = [] + js.effort = [] + self.js_pub.publish(js) + + m = Float64MultiArray() + m.data = [r, p, 1.0 if not self.warned else 0.0] + self.rp_pub.publish(m) + + +if __name__ == '__main__': + rospy.init_node('base_tilt_tf') + node = BaseTiltTF() + rospy.Timer(rospy.Duration(1.0 / node.rate_hz), node.publish) + rospy.spin() diff --git a/src/robot_bringup/urdf/robot.urdf.xacro b/src/robot_bringup/urdf/robot.urdf.xacro index db99072..c27ca0a 100644 --- a/src/robot_bringup/urdf/robot.urdf.xacro +++ b/src/robot_bringup/urdf/robot.urdf.xacro @@ -72,11 +72,127 @@ - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +