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
17 from wild_thumper.srv import DWM1000Center, DWM1000CenterResponse
19 import matplotlib.pyplot as plt
22 def __init__(self, x_est, P_est, Q, R):
23 self.x_est = x_est # Systemzustand
24 self.P_est = P_est # Fehlerkovarianz
25 self.Q = Q # Systemrauschen
26 self.R = R # Varianz des Messfehlers
29 # Korrektur mit der Messung
30 # (1) Berechnung der Kalman Verstärkung
31 K = self.P_est/(self.R + self.P_est)
32 # (2) Korrektur der Schätzung mit der Messung y
33 x = self.x_est + K*(y - self.x_est)
34 # (3) Korrektur der Fehlerkovarianzmatrix
38 # (1) Prädiziere den Systemzustand
40 # (2) Präzidiere die Fehlerkovarianzmatrix
41 self.P_est = P + self.Q
45 def set_measure_cov(self, R):
49 class DW1000(threading.Thread):
50 def __init__(self, name, addr, offset):
51 threading.Thread.__init__(self)
57 self.last_update = datetime.min
59 self.pub = rospy.Publisher(name, Float32, queue_size=16)
65 ret = struct.unpack("f", dev.read(4))
72 # Returns each distance only if current
73 def distance_valid(self):
74 if (datetime.now() - self.last_update).seconds < 1:
80 while not rospy.is_shutdown():
81 val = self.get_value()
82 if abs(val - last_val) > 10:
83 rospy.logwarn("Ignoring values too far apart %s: %.2f - %.2f", self.name, val, last_val)
85 self.dist = val + self.offset
86 self.last_update = datetime.now()
87 self.pub.publish(self.distance())
93 # Varianz des Messfehlers
101 self.filter_x = simple_kalman(1.0, P_est_x, Q, Rx)
102 self.filter_y = simple_kalman(0.0, P_est_y, Q, Ry)
106 self.last_time = rospy.Time.now()
107 rospy.Subscriber("/odom_combined", Odometry, self.odomReceived)
109 def odomReceived(self, msg):
110 self.speed_x = msg.twist.twist.linear.x
111 self.speed_y = msg.twist.twist.linear.y
112 self.speed_z = msg.twist.twist.angular.z
114 def filter(self, x, y):
115 # Correct estimation with speed
116 current_time = rospy.Time.now()
117 dt = (current_time - self.last_time).to_sec()
118 # Subtract vehicle speed
119 pos = np.array([self.filter_x.x_est, self.filter_y.x_est])
121 pos -= np.array([self.speed_x*dt, self.speed_y*dt])
123 rot = np.array([[np.cos(self.speed_z*dt), -np.sin(self.speed_z*dt)],
124 [np.sin(self.speed_z*dt), np.cos(self.speed_z*dt)]])
125 pos = np.dot(pos, rot)
127 self.filter_x.x_est = pos[0]
128 self.filter_y.x_est = pos[1]
130 # run kalman if new measurements are valid
131 if x != None and y != None:
132 x = self.filter_x.run(x)
133 y = self.filter_y.run(y)
136 dist = np.linalg.norm([x, y])
137 self.filter_x.set_measure_cov(np.polyval([0.017795, -0.021832, 0.010968], dist))
138 self.filter_y.set_measure_cov(np.polyval([0.0060314, -0.013387, 0.0065049], dist))
140 x = self.filter_x.x_est
141 y = self.filter_y.x_est
143 self.last_time = current_time
147 def handle_center_call(req):
148 diff = dwleft.distance_valid() - dwright.distance_valid()
149 dwleft.offset -= diff/2
150 dwright.offset += diff/2
151 rospy.loginfo("Centering to %.2f %.2f", dwleft.offset, dwright.offset)
152 return DWM1000CenterResponse()
154 if __name__ == "__main__":
155 rospy.init_node('DWM1000', log_level=rospy.DEBUG)
156 dwleft = DW1000("uwb_dist_left", 0xc2, +0.00)
157 dwright = DW1000("uwb_dist_right", 0xc0, -0.00)
158 dist_l_r = 0.285 # Distance between both DWM1000
159 rate = rospy.Rate(10)
161 tf_broadcaster = tf.broadcaster.TransformBroadcaster()
162 rospy.Service('/DWM1000/center', DWM1000Center, handle_center_call)
164 while not rospy.is_shutdown() and dwleft.is_alive() and dwright.is_alive():
165 dist_left = dwleft.distance_valid()
166 dist_right = dwright.distance_valid()
167 if dist_left == None or dist_right == None:
168 rospy.logerr_throttle(10, "no valid sensor update")
169 # run kalman prediction only
170 pos.filter(None, None)
172 dir = "left" if (dist_left < dist_right) else "right"
174 diff = abs(dist_left - dist_right)
176 # difference to high, correct to maximum
177 off = diff - dist_l_r + 0.01
178 if dist_left > dist_right:
184 rospy.logdebug("%.2f %.2f %.2f %.2f %s", dwleft.distance(), dwright.distance(), dist_left, dist_right, dir)
186 a_r = (-dist_right**2 + dist_left**2 - dist_l_r**2) / (-2*dist_l_r)
188 t = dist_right**2 - a_r**2
191 rospy.logdebug("x=%.2f, y=%.2f", x, y)
195 x, y = pos.filter(x, y)
196 tf_broadcaster.sendTransform((x, y, 0.0), (0, 0, 0, 1), rospy.Time.now(), "uwb_beacon", "base_footprint")
199 circle_left = plt.Circle((-dist_l_r/2, 0), dwleft.distance, color='red', fill=False)
200 circle_right = plt.Circle((dist_l_r/2, 0), dwright.distance, color='green', fill=False)
201 plt.gca().add_patch(circle_left)
202 plt.gca().add_patch(circle_right)
207 # No current position, still need up update kalman prediction
208 pos.filter(None, None)