-
Notifications
You must be signed in to change notification settings - Fork 0
/
particleFilter.py
345 lines (272 loc) · 11.1 KB
/
particleFilter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
from common import *
from numpy.random import randn, uniform
from numpy import *
from numpy.linalg import *
from coordinateFrames import *
from joy import *
import pdb
class Particle:
def __init__(self, d=0, yaw=0, prob=0):
self.d = d
self.yaw = yaw
self.prob = prob
class ParticleFilter:
"""
Particle Filter that estimates
(r, d, theta) with respect to the current waypoint line
Particles consist of (d, theta)
"""
def __init__(self, core):
# noise constants
self.xNoise = 0.1 # cm
self.yNoise = 0.1 # cm
self.yawNoise = 0.002 # radians
self.alpha = 0.4
self.coordinateFrames = core.coordinateFrames
# particles
self.numChosenParticles = 100
self.numRandomParticles = 40 # must be multiple of 4!
self.numTotalParticles = self.numChosenParticles + self.numRandomParticles
self.particles = []
self.equalProbability = 1.0 / self.numTotalParticles
for i in range(self.numTotalParticles):
initialPos = 0
initialYaw = 0
self.particles.append(Particle(initialPos, initialYaw, self.equalProbability))
self.mostProbable = self.particles[0]
self.sensor = []
self.waypoints = []
# state
self.r = 0
def setState(self, pos, yaw):
"""
input is position and yaw in real coordinates
"""
[r, d] = self.coordinateFrames.convertRealToWaypoint(pos)
self.r = r
for particle in self.particles:
randPos = randn() * 5
randYaw = randn() * 0.05
particle.prob = self.equalProbability
particle.d = d + randPos
particle.yaw = -self.coordinateFrames.getRealToWaypointYaw() + yaw + randYaw
self.particles[0].d = d
self.particles[0].yaw = -self.coordinateFrames.getRealToWaypointYaw() + yaw
self.mostProbable = self.particles[0]
def printParticles(self):
for particle in self.particles:
print("pos: " + str(particle.d) + "\tyaw: " + str(particle.yaw) + "\tprob: " + str(particle.prob))
def setSensorAndWaypoints(self, sensor, waypoints):
self.sensor = sensor
if (len(waypoints) != len(self.waypoints)):
self.coordinateFrames.calculateRealToWaypointTransformation(waypoints[0], waypoints[1])
self.setState(waypoints[0], 0)
self.waypoints = waypoints
def correct(self):
totalProb = 0
sensorReal = array(ParticleFilter.convertSensor(self.sensor))
# try to localize R
self.correctR(sensorReal, self.sensor)
for particle in self.particles:
rotatedLength = CoordinateFrames.rotateCCW([Constants.tagLength, 0], particle.yaw)
frontDist = abs(particle.d + rotatedLength[1])
backDist = abs(particle.d - rotatedLength[1])
self.sensorModel(particle, sensorReal, array([frontDist, backDist]))
totalProb += particle.prob
self.particles.sort(key=lambda x: x.prob, reverse=True)
# self.mostProbable.d = self.mostProbable.d + self.alpha * (self.particles[0].d - self.mostProbable.d)
# self.mostProbable.yaw = self.mostProbable.yaw + self.alpha * (self.particles[0].yaw - self.mostProbable.yaw)
self.mostProbable = self.particles[0]
# normalize probabilities so they add to one
scalar = 1.0 / totalProb
for particle in self.particles:
particle.prob *= scalar
# draw new samples
newParticles = []
for i in range(self.numChosenParticles):
randNum = uniform()
cumulativeProb = 0
drewParticle = False
for particle in self.particles:
cumulativeProb += particle.prob
if (cumulativeProb > randNum):
newParticle = Particle(particle.d, particle.yaw, self.equalProbability)
newParticles.append(newParticle)
drewParticle = True
break
if (not drewParticle):
newParticles.append(self.mostProbable)
# draw scattering of particles around each solution
solutions = ParticleFilter.generateSolutions(sensorReal)
numParticlesPerSolution = self.numRandomParticles / len(solutions)
for i in range(self.numRandomParticles):
randPos = randn() * 1.5
randYaw = randn() * 0.03
solutionNum = int(floor(i / numParticlesPerSolution))
solution = solutions[solutionNum]
newParticles.append(Particle(solution[0] + randPos, solution[1] + randYaw, self.equalProbability))
self.particles = newParticles
def correctR(self, sensorReal, sensor):
# modify self.r if necessary
rotatedLength = CoordinateFrames.rotateCCW([Constants.tagLength, 0], -self.coordinateFrames.getRealToWaypointYaw())
# locations of the front and back sensor of most probable particle
frontR = self.r + rotatedLength[0]
backR = self.r - rotatedLength[0]
waypointDist = norm(array(self.waypoints[1]) - array(self.waypoints[0]))
print("Sensor: " + str(sensor) + " estimated: " + str([frontR, backR]))
if (sensorReal[1] == -1 and sensorReal[0] == -1):
# we are completely not between waypoints
if ((backR > 0 or backR < waypointDist) or
(frontR > 0 or frontR < waypointDist)):
# our estimate is that at least one of our sensors is still
# in range of waypoints
# determine which end we are off of by looking at our current estimate
if (self.r < waypointDist / 2):
# closest to start
# decrease r
print("Case 1 start")
self.r = 0
else:
# closest to end
# increase r
print("Case 1 end")
self.r = waypointDist
elif (sensorReal[1] == -1 or sensorReal[0] == -1):
# one sensor is no longer in range of waypoints
if (backR > 0 and frontR > 0 and backR < waypointDist and frontR < waypointDist):
# if we think we are in range of waypoints
# find which end we are closest to
if (self.r < waypointDist / 2):
# we are close to start
# move r to within range [0, rotatedLength[1]]
print("Case 2 a start")
print("actual f and b: " + str(sensorReal) + "\t" + str(sensor))
print("estimated f and b: " + str([frontR, backR]))
print("before r: " + str(self.r))
self.r = self.r - abs(rotatedLength[0]) * 0.2
print("after r: " + str(self.r))
else:
# we are close to end
# move r to within range
print("Case 2 a end")
self.r = self.r + abs(rotatedLength[0]) * 0.2
if ((backR < 0 or backR > waypointDist) and
(frontR < 0 or frontR > waypointDist)):
# our estimate is completely off
# find which end we are closest to
if (self.r < waypointDist / 2):
# we are close to start
# move r to within range [0, rotatedLength[1]]
print("Case 2 b start")
self.r = uniform() * abs(rotatedLength[0])
else:
# we are close to end
# move r to within range
print("Case 2 b end")
self.r = waypointDist - (uniform() * abs(rotatedLength[0]))
elif (backR < 0 or backR > waypointDist or
frontR < 0 or frontR > waypointDist):
# we estimate that our sensors are not between waypoints
# however we are getting measurements
# find which end we are closest to
if (self.r < waypointDist / 2):
# we are close to start
# move r to within range [0, rotatedLength[1]]
print("Case 3 start")
print("actual f and b: " + str(sensorReal) + "\t" + str(sensor))
print("estimated f and b: " + str([frontR, backR]))
print("before r: " + str(self.r))
self.r = abs(rotatedLength[0])
print("after r: " + str(self.r))
else:
# we are close to end
# move r to within range
print("Case 3 end")
print("actual f and b: " + str(sensorReal) + "\t" + str(sensor))
print("estimated f and b: " + str([frontR, backR]))
print("before r: " + str(self.r))
self.r = waypointDist - abs(rotatedLength[0])
print("after r: " + str(self.r))
@staticmethod
def generateSolutions(sensor):
solutions = []
f = sensor[0]
b = sensor[1]
d1 = (f+b)/2
theta1 = atan2((f-b), 2 * Constants.tagLength)
d2 = (f-b)/2
theta2 = atan2((f+b), 2 * Constants.tagLength)
solutions.append([d1, theta1])
solutions.append([-d1, -theta1])
solutions.append([d2, theta2])
solutions.append([-d2, -theta2])
return solutions
def sensorModel(self, particle, sensorDists, particleDists):
# distsDiff measures how close the sensor values are
# to what the distance of this particle to the line
if (sensorDists[0] == -1):
distsDiff = sqrt(2) * abs(sensorDists[1] - particleDists[1])
elif (sensorDists[1] == -1):
distsDiff = sqrt(2) * abs(sensorDists[0] - particleDists[0])
elif (sensorDists[0] == -1 and sensorDists[1] == -1):
distsDiff = 0
else:
distsDiff = norm(sensorDists - particleDists)
# Yawdiff measures how close the yaw of the particle is to our
# estimated yaw
yawScalar = 50
# yawDiff = yawScalar * abs(particle.yaw - self.mostProbable.yaw)
yawDiff = yawScalar * abs(particle.yaw + self.coordinateFrames.getRealToWaypointYaw())
# scalar = 0.2231
scalar = 0.11
# the probability we multiply our particle prob by,
# p = P(sensor value | particle location), will be
# e^(-(distsDiff + yawDiff) * scalar)
# so that when distsDiff+yawDiff is 0, p=1
# and for distsDiff+yawDiff > 0, p < 1 and drops off rapidly
# how rapid the drop depends on the scalar
sensorProb = exp(-(distsDiff + yawDiff) * scalar)
particle.prob *= sensorProb
def actionModel(self, direction):
travelDist = array([0., 0.])
noiseVar = 0
if direction == (Directions.PosX):
travelDist[0] += Constants.wheelSideLength
noiseVar = self.xNoise
elif direction == (Directions.NegX):
travelDist[0] -= Constants.wheelSideLength
noiseVar = self.xNoise
elif direction == (Directions.PosY):
travelDist[1] += Constants.wheelSideLength
noiseVar = self.yNoise
elif direction == (Directions.NegY):
travelDist[1] -= Constants.wheelSideLength
noiseVar = self.yNoise
travelDir = travelDist / Constants.wheelSideLength
rotatedTravelDist = CoordinateFrames.rotateCCW(travelDist, -self.coordinateFrames.getRealToWaypointYaw())
self.r += rotatedTravelDist[0]
# move each particle with noise
for particle in self.particles:
particleTravelDist = travelDist + (travelDir * noiseVar * randn())
rotatedTravelDist = CoordinateFrames.rotateCCW(particleTravelDist, particle.yaw)
particle.d += rotatedTravelDist[1]
particle.yaw += randn() * self.yawNoise
def getState(self):
realPos = self.coordinateFrames.convertWaypointToReal([self.r, self.mostProbable.d])
return RobotState(realPos, self.mostProbable.yaw)
def getWaypointState(self):
return RobotState([self.r, self.mostProbable.d], self.mostProbable.yaw)
@staticmethod
def convertSensor(sensor):
# converts sensor values into real distances
# model for sensor is
# y: real dist
# x: sensor dist
# y = t * sqrt((255 / x) - 1)
t = 6.5 # cm
ret = [-1, -1]
if (sensor[0] >= 10):
ret[0] = t * sqrt((255. / sensor[0]) - 1)
if (sensor[1] >= 10):
ret[1] = t * sqrt((255. / sensor[1]) - 1)
return ret