upload
This commit is contained in:
114
src/robot_sim/scripts/sim_degradation.py
Normal file
114
src/robot_sim/scripts/sim_degradation.py
Normal file
@@ -0,0 +1,114 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Bom nhieu THUC TE vao topic mo phong.
|
||||
|
||||
============ VI SAO CAN NODE NAY ============
|
||||
Gazebo dung /clock dong bo hoan hao: moi message deu co timestamp chinh xac
|
||||
tuyet doi, khong bao gio drop, khong bao gio lech.
|
||||
|
||||
Tren robot that:
|
||||
- Camera cam USB, LiDAR cam Ethernet, IMU qua UART -> BA dong ho khac nhau
|
||||
- Khong co hardware sync (D410 khong co IMU tich hop)
|
||||
- Jitter timestamp 10-50ms la binh thuong
|
||||
- Drop frame khi CPU ban
|
||||
|
||||
Kinh nghiem nganh: ~80% loi SLAM thuc te den tu HIEU CHUAN SAI hoac DONG BO SAI,
|
||||
khong phai tu thuat toan. Neu chi test tren sim sach, ban se tin nham rang
|
||||
he thong da chay tot.
|
||||
|
||||
Node nay cho phep bat dan tung muc do te di, de biet stack chiu duoc den dau
|
||||
TRUOC KHI gap phan cung that.
|
||||
|
||||
Vi du:
|
||||
rosrun robot_sim sim_degradation.py \
|
||||
_topics:="[{in: /scan, out: /scan_degraded, type: LaserScan, jitter_ms: 30, drop: 0.02}]"
|
||||
"""
|
||||
|
||||
import random
|
||||
|
||||
import rospy
|
||||
from sensor_msgs.msg import LaserScan, Imu, Image, PointCloud2, FluidPressure
|
||||
from nav_msgs.msg import Odometry
|
||||
|
||||
TYPE_MAP = {
|
||||
'LaserScan': LaserScan,
|
||||
'Imu': Imu,
|
||||
'Image': Image,
|
||||
'PointCloud2': PointCloud2,
|
||||
'FluidPressure': FluidPressure,
|
||||
'Odometry': Odometry,
|
||||
}
|
||||
|
||||
|
||||
class Degrader(object):
|
||||
"""Mot kenh: nhan topic sach -> publish topic da lam te di."""
|
||||
|
||||
def __init__(self, cfg):
|
||||
self.name = cfg['in']
|
||||
msg_type = TYPE_MAP[cfg['type']]
|
||||
|
||||
# Lech co he thong (bias) - mo phong offset dong ho chua hieu chuan
|
||||
self.offset_ms = float(cfg.get('offset_ms', 0.0))
|
||||
# Jitter ngau nhien quanh offset
|
||||
self.jitter_ms = float(cfg.get('jitter_ms', 0.0))
|
||||
# Ty le drop message [0..1]
|
||||
self.drop = float(cfg.get('drop', 0.0))
|
||||
|
||||
self.n_in = 0
|
||||
self.n_dropped = 0
|
||||
|
||||
self.pub = rospy.Publisher(cfg['out'], msg_type, queue_size=10)
|
||||
self.sub = rospy.Subscriber(cfg['in'], msg_type, self.cb, queue_size=10)
|
||||
|
||||
rospy.loginfo('sim_degradation: %s -> %s (offset %.1fms, jitter %.1fms, drop %.1f%%)',
|
||||
cfg['in'], cfg['out'], self.offset_ms, self.jitter_ms, self.drop * 100)
|
||||
|
||||
def cb(self, msg):
|
||||
self.n_in += 1
|
||||
|
||||
if self.drop > 0.0 and random.random() < self.drop:
|
||||
self.n_dropped += 1
|
||||
return
|
||||
|
||||
if self.offset_ms != 0.0 or self.jitter_ms != 0.0:
|
||||
shift = self.offset_ms
|
||||
if self.jitter_ms > 0.0:
|
||||
shift += random.gauss(0.0, self.jitter_ms)
|
||||
try:
|
||||
msg.header.stamp += rospy.Duration.from_sec(shift / 1000.0)
|
||||
except AttributeError:
|
||||
rospy.logwarn_once('sim_degradation: %s khong co header, bo qua jitter', self.name)
|
||||
|
||||
self.pub.publish(msg)
|
||||
|
||||
def report(self):
|
||||
if self.n_in == 0:
|
||||
return
|
||||
rospy.loginfo('sim_degradation: %s - nhan %d, drop %d (%.1f%%)',
|
||||
self.name, self.n_in, self.n_dropped,
|
||||
100.0 * self.n_dropped / self.n_in)
|
||||
|
||||
|
||||
def main():
|
||||
rospy.init_node('sim_degradation')
|
||||
|
||||
topics = rospy.get_param('~topics', [])
|
||||
if not topics:
|
||||
rospy.logwarn('sim_degradation: chua cau hinh ~topics, node khong lam gi.')
|
||||
rospy.logwarn('Xem robot_sim/config/sim_sensors.yaml de biet vi du.')
|
||||
|
||||
degraders = [Degrader(t) for t in topics]
|
||||
|
||||
def on_shutdown():
|
||||
for d in degraders:
|
||||
d.report()
|
||||
|
||||
rospy.on_shutdown(on_shutdown)
|
||||
rospy.spin()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
except rospy.ROSInterruptException:
|
||||
pass
|
||||
Reference in New Issue
Block a user