2 # -*- coding: iso-8859-15 -*-
12 from datetime import datetime
14 from time import sleep
15 from std_msgs.msg import Float32
16 from nav_msgs.msg import Odometry
18 import matplotlib.pyplot as plt
21 def __init__(self, x_est, P_est, Q, R):
22 self.x_est = x_est # Systemzustand
23 self.P_est = P_est # Fehlerkovarianz
24 self.Q = Q # Systemrauschen
25 self.R = R # Varianz des Messfehlers
28 # Korrektur mit der Messung
29 # (1) Berechnung der Kalman Verstärkung
30 K = self.P_est/(self.R + self.P_est)
31 # (2) Korrektur der Schätzung mit der Messung y
32 x = self.x_est + K*(y - self.x_est)
33 # (3) Korrektur der Fehlerkovarianzmatrix
37 # (1) Prädiziere den Systemzustand
39 # (2) Präzidiere die Fehlerkovarianzmatrix
40 self.P_est = P + self.Q
44 class DW1000(threading.Thread):
45 def __init__(self, name, addr, offset):
46 threading.Thread.__init__(self)
52 self.last_update = datetime.min
54 self.pub = rospy.Publisher(name, Float32, queue_size=16)
60 ret = struct.unpack("f", dev.read(4))
67 # Returns each distance only if current
68 def distance_valid(self):
69 if (datetime.now() - self.last_update).seconds < 1:
76 val = self.get_value()
77 if abs(val - last_val) > 10:
78 print "Ignoring values too far apart %s: %.2f - %.2f" % (self.name, val, last_val)
80 self.dist = val + self.offset
81 self.last_update = datetime.now()
82 self.pub.publish(self.distance())
88 # Varianz des Messfehlers
96 self.filter_x = simple_kalman(1.0, P_est_x, Q, Rx)
97 self.filter_y = simple_kalman(0.0, P_est_y, Q, Ry)
101 self.last_time = rospy.Time.now()
102 rospy.Subscriber("/odom_combined", Odometry, self.odomReceived)
104 def odomReceived(self, msg):
105 self.speed_x = msg.twist.twist.linear.x
106 self.speed_y = msg.twist.twist.linear.y
107 self.speed_z = msg.twist.twist.angular.z
111 - variance of kalman should be dependant on distance
113 def filter(self, x, y):
114 # Correct estimation with speed
115 current_time = rospy.Time.now()
116 dt = (current_time - self.last_time).to_sec()
117 # Subtract vehicle speed
118 pos = np.array([self.filter_x.x_est, self.filter_y.x_est])
120 pos -= np.array([self.speed_x*dt, self.speed_y*dt])
122 rot = np.array([[np.cos(self.speed_z*dt), -np.sin(self.speed_z*dt)],
123 [np.sin(self.speed_z*dt), np.cos(self.speed_z*dt)]])
124 pos = np.dot(pos, rot)
126 self.filter_x.x_est = pos[0]
127 self.filter_y.x_est = pos[1]
129 # run kalman if new measurements are valid
130 if x != None and y != None:
131 x = self.filter_x.run(x)
132 y = self.filter_y.run(y)
134 x = self.filter_x.x_est
135 y = self.filter_y.x_est
137 self.last_time = current_time
141 if __name__ == "__main__":
142 rospy.init_node('DW1000')
143 dwleft = DW1000("uwb_dist_left", 0xc2, +0.02)
144 dwright = DW1000("uwb_dist_right", 0xc0, -0.02)
146 rate = rospy.Rate(10)
148 tf_broadcaster = tf.broadcaster.TransformBroadcaster()
150 while not rospy.is_shutdown() and dwleft.is_alive() and dwright.is_alive():
151 dist_left = dwleft.distance_valid()
152 dist_right = dwright.distance_valid()
153 if dist_left == None or dist_right == None:
154 print "no valid sensor update"
155 # run kalman prediction only
156 pos.filter(None, None)
158 dir = "left" if (dist_left < dist_right) else "right"
160 diff = abs(dist_left - dist_right)
162 # difference to high, correct to maximum
163 off = diff - dist_l_r + 0.01
164 if dist_left > dist_right:
170 print "%.2f %.2f %.2f %.2f %s" % (dwleft.distance(), dwright.distance(), dist_left, dist_right, dir)
172 a_r = (-dist_right**2 + dist_left**2 - dist_l_r**2) / (-2*dist_l_r)
174 t = dist_right**2 - a_r**2
181 x, y = pos.filter(x, y)
182 tf_broadcaster.sendTransform((x, y, 0.0), (0, 0, 0, 1), rospy.Time.now(), "uwb_beacon", "base_footprint")
185 circle_left = plt.Circle((-dist_l_r/2, 0), dwleft.distance, color='red', fill=False)
186 circle_right = plt.Circle((dist_l_r/2, 0), dwright.distance, color='green', fill=False)
187 plt.gca().add_patch(circle_left)
188 plt.gca().add_patch(circle_right)
193 # No current position, still need up update kalman prediction
194 pos.filter(None, None)