Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
96129 views
1
# -*- coding: utf-8 -*-
2
"""
3
Created on Sun Feb 15 14:29:23 2015
4
5
@author: rlabbe
6
"""
7
8
# -*- coding: utf-8 -*-
9
"""
10
Created on Sun Feb 8 09:34:36 2015
11
12
@author: rlabbe
13
"""
14
15
import numpy as np
16
import matplotlib.pyplot as plt
17
18
from numpy import array, asarray
19
from numpy.linalg import norm
20
from numpy.random import randn
21
import math
22
from math import sin, cos, atan2, radians, degrees
23
24
25
from filterpy.kalman import UnscentedKalmanFilter as UKF
26
from filterpy.common import Q_discrete_white_noise
27
28
class RadarStation(object):
29
30
def __init__(self, pos, range_std, bearing_std):
31
self.pos = asarray(pos)
32
33
self.range_std = range_std
34
self.bearing_std = bearing_std
35
36
37
def reading_of(self, ac_pos):
38
""" Returns range and bearing to aircraft as tuple. bearing is in
39
radians.
40
"""
41
42
diff = np.subtract(ac_pos, self.pos)
43
rng = norm(diff)
44
brg = atan2(diff[1], diff[0])
45
return rng, brg
46
47
48
def noisy_reading(self, ac_pos):
49
rng, brg = self.reading_of(ac_pos)
50
rng += randn() * self.range_std
51
brg += randn() * self.bearing_std
52
return rng, brg
53
54
55
def z_to_x(self, slant_range, angle):
56
""" given a reading, convert to world coordinates"""
57
58
x = cos(angle)*slant_range
59
z = sin(angle)*slant_range
60
61
return self.pos + (x,z)
62
63
64
65
class ACSim(object):
66
67
def __init__(self, pos, vel, vel_std):
68
self.pos = asarray(pos, dtype=float)
69
self.vel = asarray(vel, dtype=float)
70
self.vel_std = vel_std
71
72
def update(self):
73
vel = self.vel + (randn() * self.vel_std)
74
self.pos += vel
75
76
return self.pos
77
78
79
def two_radar_constvel():
80
dt = 5
81
82
83
def hx(x):
84
r1, b1 = hx.R1.reading_of((x[0], x[2]))
85
r2, b2 = hx.R2.reading_of((x[0], x[2]))
86
87
return array([r1, b1, r2, b2])
88
pass
89
90
91
92
def fx(x, dt):
93
x_est = x.copy()
94
x_est[0] += x[1]*dt
95
x_est[2] += x[3]*dt
96
return x_est
97
98
99
100
101
f = UKF(dim_x=4, dim_z=4, dt=dt, hx=hx, fx=fx, kappa=0)
102
aircraft = ACSim ((100,100), (0.1*dt,0.02*dt), 0.002)
103
104
105
range_std = 0.2
106
bearing_std = radians(0.5)
107
108
R1 = RadarStation ((0,0), range_std, bearing_std)
109
R2 = RadarStation ((200,0), range_std, bearing_std)
110
111
hx.R1 = R1
112
hx.R2 = R2
113
114
f.x = array([100, 0.1, 100, 0.02])
115
f.R = np.diag([range_std**2, bearing_std**2, range_std**2, bearing_std**2])
116
q = Q_discrete_white_noise(2, var=0.002, dt=dt)
117
#q = np.array([[0,0],[0,0.0002]])
118
f.Q[0:2, 0:2] = q
119
f.Q[2:4, 2:4] = q
120
f.P = np.diag([.1, 0.01, .1, 0.01])
121
122
123
track = []
124
zs = []
125
126
127
for i in range(int(300/dt)):
128
129
pos = aircraft.update()
130
131
r1, b1 = R1.noisy_reading(pos)
132
r2, b2 = R2.noisy_reading(pos)
133
134
z = np.array([r1, b1, r2, b2])
135
zs.append(z)
136
track.append(pos.copy())
137
138
zs = asarray(zs)
139
140
141
xs, Ps, Pxz = f.batch_filter(zs)
142
ms, _, _ = f.rts_smoother2(xs, Ps, Pxz)
143
144
track = asarray(track)
145
time = np.arange(0,len(xs)*dt, dt)
146
147
plt.figure()
148
plt.subplot(411)
149
plt.plot(time, track[:,0])
150
plt.plot(time, xs[:,0])
151
plt.legend(loc=4)
152
plt.xlabel('time (sec)')
153
plt.ylabel('x position (m)')
154
155
156
157
plt.subplot(412)
158
plt.plot(time, track[:,1])
159
plt.plot(time, xs[:,2])
160
plt.legend(loc=4)
161
plt.xlabel('time (sec)')
162
plt.ylabel('y position (m)')
163
164
165
plt.subplot(413)
166
plt.plot(time, xs[:,1])
167
plt.plot(time, ms[:,1])
168
plt.legend(loc=4)
169
plt.ylim([0, 0.2])
170
plt.xlabel('time (sec)')
171
plt.ylabel('x velocity (m/s)')
172
173
plt.subplot(414)
174
plt.plot(time, xs[:,3])
175
plt.plot(time, ms[:,3])
176
plt.ylabel('y velocity (m/s)')
177
plt.legend(loc=4)
178
plt.xlabel('time (sec)')
179
180
plt.show()
181
182
183
def two_radar_constalt():
184
dt = .05
185
186
187
def hx(x):
188
r1, b1 = hx.R1.reading_of((x[0], x[2]))
189
r2, b2 = hx.R2.reading_of((x[0], x[2]))
190
191
return array([r1, b1, r2, b2])
192
pass
193
194
195
def fx(x, dt):
196
x_est = x.copy()
197
x_est[0] += x[1]*dt
198
return x_est
199
200
201
202
vx = 100/1000 # meters/sec
203
vz = 0.
204
205
f = UKF(dim_x=3, dim_z=4, dt=dt, hx=hx, fx=fx, kappa=0)
206
aircraft = ACSim ((0, 1), (vx*dt, vz*dt), 0.00)
207
208
209
range_std = 1/1000.
210
bearing_std =1/1000000.
211
212
R1 = RadarStation (( 0, 0), range_std, bearing_std)
213
R2 = RadarStation ((60, 0), range_std, bearing_std)
214
215
hx.R1 = R1
216
hx.R2 = R2
217
218
f.x = array([aircraft.pos[0], vx, aircraft.pos[1]])
219
f.R = np.diag([range_std**2, bearing_std**2, range_std**2, bearing_std**2])
220
q = Q_discrete_white_noise(2, var=0.0002, dt=dt)
221
#q = np.array([[0,0],[0,0.0002]])
222
f.Q[0:2, 0:2] = q
223
f.Q[2,2] = 0.0002
224
f.P = np.diag([.1, 0.01, .1])*0.1
225
226
227
track = []
228
zs = []
229
230
231
for i in range(int(500/dt)):
232
pos = aircraft.update()
233
234
r1, b1 = R1.noisy_reading(pos)
235
r2, b2 = R2.noisy_reading(pos)
236
237
z = np.array([r1, b1, r2, b2])
238
zs.append(z)
239
track.append(pos.copy())
240
241
zs = asarray(zs)
242
243
244
xs, Ps = f.batch_filter(zs)
245
ms, _, _ = f.rts_smoother(xs, Ps)
246
247
track = asarray(track)
248
time = np.arange(0,len(xs)*dt, dt)
249
250
plt.figure()
251
plt.subplot(311)
252
plt.plot(time, track[:,0])
253
plt.plot(time, xs[:,0])
254
plt.legend(loc=4)
255
plt.xlabel('time (sec)')
256
plt.ylabel('x position (m)')
257
258
plt.subplot(312)
259
plt.plot(time, xs[:,1]*1000, label="UKF")
260
plt.plot(time, ms[:,1]*1000, label='RTS')
261
plt.legend(loc=4)
262
plt.xlabel('time (sec)')
263
plt.ylabel('velocity (m/s)')
264
265
plt.subplot(313)
266
plt.plot(time, xs[:,2]*1000, label="UKF")
267
plt.plot(time, ms[:,2]*1000, label='RTS')
268
plt.legend(loc=4)
269
plt.xlabel('time (sec)')
270
plt.ylabel('altitude (m)')
271
plt.ylim([900,1100])
272
273
for z in zs[:10]:
274
p = R1.z_to_x(z[0], z[1])
275
#plt.scatter(p[0], p[1], marker='+', c='k')
276
277
p = R2.z_to_x(z[2], z[3])
278
#plt.scatter(p[0], p[1], marker='+', c='b')
279
280
plt.show()
281
282
283
two_radar_constalt()
284