-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculateWeightsLF.m
58 lines (51 loc) · 1.75 KB
/
calculateWeightsLF.m
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
function [ weights, avgWeight, bestIndex ] = calculateWeightsLF( particles, distances, lf )
% This function returns a vector of normalized real weights, the average un-normalized weight,
% and the index of the particle with highest weight. It accepts botSim[], a vector of distances,
% and a likelihood field of the map.
count = length(particles);
totalWeight = 0;
bestWeight = 0;
bestIndex = 1;
outOfMapFactor = 1;
anglePerScan = 2*pi/length(distances);
weights(count,1) = 0;
% Calculate weights for each particle
for i = 1:count
% Calculate weight here
weights(i) = 1;
if ~particles(i).insideMap()
weights(i) = outOfMapFactor;
end
for j = 1:length(distances)
if isnan(distances(j))
weight = lf.minLikelihood;
else
scanAngle = particles(i).getBotAng() + ((j-1) * anglePerScan);
scanVector = distances(j) * [cos(scanAngle) sin(scanAngle)];
projectedPoint = particles(i).getBotPos() + scanVector;
weight = lf.getPointLikelihood(projectedPoint);
end
weights(i) = weights(i) * weight;
end
if weights(i) > bestWeight
bestWeight = weights(i);
bestIndex = i;
end
totalWeight = totalWeight + weights(i);
if isnan(weights(i))
fprintf('Weight %d is NaN', i)
distances
partDists
end
end
% Set average (un-normalized) weight
avgWeight = totalWeight / count;
if isnan(avgWeight)
disp('Average weight is NaN')
[totalWeight, count]
end
% Normalize weights
for i = 1:count
weights(i) = weights(i) / totalWeight;
end
end