|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# This node takes a ground truth pose publishing system and converts it to the |
| 4 | +# noisy "odom -> base_link" + "map -> odom" corrections when localisation |
| 5 | +# approach typical in robotics. |
| 6 | +# |
| 7 | +# We assume that "map -> odom" is not currently defined, but is instead |
| 8 | +# provided by the "start_pose" param. The provided "odom -> base_link" is then |
| 9 | +# pulled from the odometry on "in_topic_odom". |
| 10 | +# |
| 11 | +# The "map -> base_link" transform is left unchanged, but it now consists of a |
| 12 | +# noisy "odom -> base_link" link plus ground truth "map -> odom" localisation |
| 13 | +# link. |
| 14 | +# |
| 15 | +# Noise model is basic zero-mean Gaussian noise on velocity, but scaled by the |
| 16 | +# velocity. This leads to desirable results like 0 velocity having no noise / |
| 17 | +# drift, and is tolerable within our velocity limits. May look at this making |
| 18 | +# something more mature in the future. Current: |
| 19 | +# Linear vel noise = N(0, linear_vel * noise_linear) |
| 20 | +# Angular vel noise = N(0, angular_vel * noise_angular) |
| 21 | + |
| 22 | +import benchbot_spatial as sp |
| 23 | +import numpy as np |
| 24 | +import rospy |
| 25 | +import threading |
| 26 | +import tf2_ros |
| 27 | +import transforms3d as t3 |
| 28 | +from geometry_msgs.msg import (Point, Pose, Quaternion, Transform, |
| 29 | + TransformStamped, Twist, Vector3) |
| 30 | +from nav_msgs.msg import Odometry |
| 31 | +from std_msgs.msg import String |
| 32 | + |
| 33 | + |
| 34 | +def _noisy(std_dev): |
| 35 | + return std_dev * np.random.randn() |
| 36 | + |
| 37 | + |
| 38 | +def _SE3_to_pose_msg(pose): |
| 39 | + return Pose( |
| 40 | + position=Point(*pose[0:3, 3]), |
| 41 | + orientation=Quaternion( |
| 42 | + *np.roll(t3.quaternions.mat2quat(pose[0:3, 0:3]), -1)), |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +def _SE3_to_tf_msg(pose): |
| 47 | + return Transform( |
| 48 | + translation=Vector3(*pose[0:3, 3]), |
| 49 | + rotation=Quaternion(*np.roll(t3.quaternions.mat2quat(pose[0:3, |
| 50 | + 0:3]), -1)), |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +class PoseNoisifier(object): |
| 55 | + |
| 56 | + def __init__(self): |
| 57 | + self.in_topic_odom = rospy.get_param("in_topic_odom", "/odom_raw") |
| 58 | + self.in_topic_start_pose = rospy.get_param("in_topic_start_pose", |
| 59 | + "/odom_start_pose") |
| 60 | + self.in_topic_vel = rospy.get_param("in_topic_vel", "/cmd_vel") |
| 61 | + |
| 62 | + self.out_topic_odom = rospy.get_param("out_topic_odom", "/odom") |
| 63 | + self.out_frame_map = rospy.get_param("out_frame_map", "map") |
| 64 | + self.out_frame_odom = rospy.get_param("out_frame_odom", "odom") |
| 65 | + self.out_frame_robot = rospy.get_param("out_frame_robot", "base_link") |
| 66 | + |
| 67 | + self.noise_angular = rospy.get_param("noise_angular", 0.1) |
| 68 | + self.noise_linear = rospy.get_param("noise_linear", 0.1) |
| 69 | + |
| 70 | + self._lock = threading.Lock() |
| 71 | + |
| 72 | + self._start_pose = None |
| 73 | + |
| 74 | + self._accumulated_error = np.eye(4) |
| 75 | + self._last_vel = Twist() |
| 76 | + |
| 77 | + self._tf = tf2_ros.TransformBroadcaster() |
| 78 | + |
| 79 | + self._pub_odom = rospy.Publisher(self.out_topic_odom, |
| 80 | + Odometry, |
| 81 | + queue_size=1) |
| 82 | + self._sub_odom = rospy.Subscriber(self.in_topic_odom, Odometry, |
| 83 | + self.pub) |
| 84 | + self._sub_start_pose = rospy.Subscriber(self.in_topic_start_pose, |
| 85 | + String, self.update_start_pose) |
| 86 | + self._sub_vel = rospy.Subscriber(self.in_topic_vel, Twist, |
| 87 | + self.update_vel) |
| 88 | + |
| 89 | + def pub(self, odom_msg): |
| 90 | + with self._lock: |
| 91 | + # Bail if we don't have valid data to work with |
| 92 | + if self._start_pose is None: |
| 93 | + return |
| 94 | + |
| 95 | + # Translate our clean odom into a noisy odom |
| 96 | + noisy_odom = np.matmul(self._accumulated_error, |
| 97 | + sp.pose_msg_to_SE3(odom_msg.pose)) |
| 98 | + |
| 99 | + # Figure out "localisation" transform from map to odom frame |
| 100 | + localisation = np.matmul(self._start_pose, |
| 101 | + np.linalg.inv(self._accumulated_error)) |
| 102 | + |
| 103 | + # Publish our messages |
| 104 | + o = Odometry() |
| 105 | + o.header.stamp = odom_msg.header.stamp |
| 106 | + o.header.frame_id = self.out_frame_odom |
| 107 | + o.child_frame_id = self.out_frame_robot |
| 108 | + o.pose.pose = _SE3_to_pose_msg(noisy_odom) |
| 109 | + o.pose.covariance = odom_msg.pose.covariance |
| 110 | + o.twist = self._last_vel |
| 111 | + self._pub_odom.publish(o) |
| 112 | + |
| 113 | + t = TransformStamped() |
| 114 | + t.header.stamp = odom_msg.header.stamp |
| 115 | + t.header.frame_id = self.out_frame_map |
| 116 | + t.child_frame_id = self.out_frame_odom |
| 117 | + t.transform = _SE3_to_tf_msg(localisation) |
| 118 | + self._tf.sendTransform(t) |
| 119 | + |
| 120 | + t = TransformStamped() |
| 121 | + t.header.stamp = odom_msg.header.stamp |
| 122 | + t.header.frame_id = self.out_frame_odom |
| 123 | + t.child_frame_id = self.out_frame_robot |
| 124 | + t.transform = _SE3_to_tf_msg(noisy_odom) |
| 125 | + self._tf.sendTransform(t) |
| 126 | + |
| 127 | + # Update accumulated noise |
| 128 | + self._accumulated_error = np.matmul( |
| 129 | + self._accumulated_error, |
| 130 | + np.matmul( |
| 131 | + sp.SE3_from_translation(self._last_vel.linear.x * |
| 132 | + _noisy(self.noise_linear)), |
| 133 | + sp.SE3_from_yaw(self._last_vel.angular.z * |
| 134 | + _noisy(self.noise_angular))), |
| 135 | + ) |
| 136 | + |
| 137 | + def update_start_pose(self, start_pose_msg): |
| 138 | + with self._lock: |
| 139 | + self._accumulated_error = np.eye(4) |
| 140 | + self._start_pose = sp.wxyzXYZ_to_SE3(*np.array([ |
| 141 | + float(x.strip()) for x in start_pose_msg.data.replace( |
| 142 | + '[', '').replace(']', '').split(',') |
| 143 | + ])) |
| 144 | + |
| 145 | + def update_vel(self, vel_msg): |
| 146 | + with self._lock: |
| 147 | + self._last_vel = vel_msg |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == '__main__': |
| 151 | + rospy.init_node("noisify_odom") |
| 152 | + |
| 153 | + pn = PoseNoisifier() |
| 154 | + |
| 155 | + rospy.spin() |
0 commit comments