tuw_marker_noise.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import rospy
4 import tf
5 import numpy
6 import math
7 import matplotlib.pyplot as mplot
8 
9 from mpl_toolkits.mplot3d import Axes3D
10 from marker_msgs.msg import MarkerDetection
11 
13  def __init__(self, fig):
14  # get and store parameters
15  self.fig = fig
16  self.plot_data = rospy.get_param('~plot_data')
17  self.beta_1 = rospy.get_param('~beta_1')
18  self.beta_2 = rospy.get_param('~beta_2')
19  self.beta_3 = rospy.get_param('~beta_3')
20  self.beta_4 = rospy.get_param('~beta_4')
21  self.beta_5 = rospy.get_param('~beta_5')
22  self.beta_6 = rospy.get_param('~beta_6')
23  self.beta_7 = rospy.get_param('~beta_7')
24  self.beta_8 = rospy.get_param('~beta_8')
25  self.beta_9 = rospy.get_param('~beta_9')
26  self.beta_10 = rospy.get_param('~beta_10')
27  self.beta_11 = rospy.get_param('~beta_11')
28  self.beta_12 = rospy.get_param('~beta_12')
29  self.beta_13 = rospy.get_param('~beta_13')
30  self.beta_14 = rospy.get_param('~beta_14')
31  self.beta_15 = rospy.get_param('~beta_15')
32  self.beta_16 = rospy.get_param('~beta_16')
33  self.beta_17 = rospy.get_param('~beta_17')
34  self.beta_18 = rospy.get_param('~beta_18')
35 
36  # initialize axes
37  if self.plot_data:
38  self.ax = fig.add_subplot(111, projection='3d')
39  self.ax.set_title("Detected markers")
40  self.ax.set_xlim(-8, 8, False, False)
41  self.ax.set_ylim(-8, 8, False, False)
42  self.ax.set_zlim( 0, 8, False, False)
43 
44  # subscribe and advertise
45  rospy.Subscriber('marker', MarkerDetection, self.cb_marker)
46  self.pub = rospy.Publisher('marker_noise', MarkerDetection, queue_size=10)
47 
48  def cb_marker(self, data):
49  # copy found markers and set standard deviation of noise
50  noisy_data = data
51 
52  # renew axes
53  if self.plot_data:
54  self.ax.clear()
55  #self.ax.set_autoscale_on(False)
56  #self.ax.set_autoscalez_on(False)
57  self.ax.autoscale(False)
58  self.ax.set_xlabel("X axis")
59  self.ax.set_ylabel("Y axis")
60  self.ax.set_zlabel("Z axis")
61 
62  # add and visualize gaussian noise
63  for marker in noisy_data.markers:
64  # store original pose in cartesian coordinate system
65  p_o = (marker.pose.position.x, marker.pose.position.y, marker.pose.position.z)
66  q_o = (marker.pose.orientation.x, marker.pose.orientation.y, marker.pose.orientation.z, marker.pose.orientation.w)
67 
68  # calculate original pose in spherical coordinate system
69  radial = math.sqrt(p_o[0]**2 + p_o[1]**2 + p_o[2]**2)
70  polar = math.acos(p_o[2]/radial)
71  azimuthal = math.atan2(p_o[1], p_o[0])
72  (roll, pitch, yaw) = tf.transformations.euler_from_quaternion(q_o)
73 
74  # calculate noise
75  sigma_radial = math.sqrt(max(self.beta_1 * radial**2 + self.beta_2, 0) + max(min(self.beta_3 * azimuthal**2 + self.beta_4, math.pi**2), 0) + max(min(self.beta_5 * yaw**2 + self.beta_6, math.pi**2), 0))
76  sigma_azimuthal = min(math.sqrt(max(self.beta_7 / radial**2 + self.beta_8, 0) + max(min(self.beta_9 * azimuthal**2 + self.beta_10, math.pi**2), 0) + max(min(self.beta_11 * yaw**2 + self.beta_12, math.pi**2), 0)), math.pi)
77  sigma_yaw = min(math.sqrt(max(self.beta_13 * radial**2 + self.beta_14, 0) + max(min(self.beta_15 * azimuthal**2 + self.beta_16, math.pi**2), 0) + max(min(self.beta_17 * yaw**2 + self.beta_18, math.pi**2), 0)), math.pi)
78 
79  # add gaussian noise in spherical coordinate system
80  radial += numpy.random.normal(0, sigma_radial)
81  azimuthal += numpy.random.normal(0, sigma_azimuthal)
82  yaw += numpy.random.normal(0, sigma_yaw)
83 
84  # calculate noisy pose in cartesian coordinate system
85  tmp = math.fabs(radial * math.sin(polar))
86  marker.pose.position.x = tmp * math.cos(azimuthal)
87  marker.pose.position.y = tmp * math.sin(azimuthal)
88  marker.pose.position.z = radial * math.cos(polar)
89  tmp = tf.transformations.quaternion_from_euler(roll, pitch, yaw)
90  marker.pose.orientation.x = tmp[0]
91  marker.pose.orientation.y = tmp[1]
92  marker.pose.orientation.z = tmp[2]
93  marker.pose.orientation.w = tmp[3]
94 
95  # store noise pose in spherical coordinate system
96  p_n = (marker.pose.position.x, marker.pose.position.y, marker.pose.position.z)
97  q_n = (marker.pose.orientation.x, marker.pose.orientation.y, marker.pose.orientation.z, marker.pose.orientation.w)
98 
99  if self.plot_data:
100  # vector indicating the orientation
101  o = numpy.array((1,1,1,1))
102 
103  # transform orientation vector accordingly
104  R_o = tf.transformations.quaternion_matrix(q_o)
105  R_n = tf.transformations.quaternion_matrix(q_n)
106  o_o = numpy.dot(R_o, o)
107  o_n = numpy.dot(R_n, o)
108 
109  # plot ids and arrows indicating the pose of original and noisy marker
110  if len(marker.ids) > 0:
111  id = marker.ids[0]
112  else:
113  id = '{}'
114  self.ax.text(p_o[0], p_o[1], p_o[2], id, None)
115  self.ax.quiver(p_o[0], p_o[1], p_o[2], o_o[0], o_o[1], o_o[2])
116  self.ax.quiver(p_n[0], p_n[1], p_n[2], o_n[0], o_n[1], o_n[2])
117 
118  # publish and draw (noisy) markers
119  self.pub.publish(noisy_data)
120  if self.plot_data:
121  self.fig.canvas.draw()
122 
123 if __name__ == '__main__':
124  # initialize ros node
125  rospy.init_node('tuw_marker_noise', anonymous=True)
126 
127  # get parameters
128  plot_data = rospy.get_param('~plot_data')
129 
130  # create tuw_marker_noise object
131  fig = mplot.figure()
132  noise = tuw_marker_noise(fig)
133 
134  # keep python from exiting until this node is stopped
135  if plot_data:
136  mplot.show()
137  else:
138  rospy.spin()
139 


tuw_marker_noise
Author(s): Markus Macsek
autogenerated on Mon Jun 10 2019 15:39:06