-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_constant.m
101 lines (76 loc) · 2.84 KB
/
main_constant.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
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
% Manuel Meraz
% EECS 270 Robot Algorithms
% Simple Particle Filter
inputs = read_files();
% Number of particles
% Minimum is 64 due to cube rooting
M = 64;
% Increase M until weighted mean moves
clear init;
init.condition = false;
init.num_particles = M;
% Set to true to indefinitely pause plot
done = false;
% Uniform importance sampling means that given
% all the fit particles, sample particles from the fit
% set of particles with equal probability
resampling.method = 'uniform';
% Weighted importance sampling samples
% and acquired particles based off of their weights
% i.e. the fittest weights get picked
%resampling.method = 'weighted';
% Sometimes all the weights approach a probability of zero
% Throw some particles outside of weighted mean to not
% get completely lost and diversify the particles
% The ratio of particles not sampled from the importance
% distribution.
resampling.exploratory_ratio = 0.01;
% Initialize continuous state space ranges
% Each row represents a dimension for the state
C = [-4, 6; -3, 10; 0, 2*pi];
% Each particle will be a column vector such that
% particles.poses(:,i) = [x; y; theta]
% particles.weights(:,i) = 0 < w < 1;
% Adjust M for actual number of particles after initialized
weighted_mean = [0; 0];
time = 0;
[M, particles] = initialize_particles(weighted_mean, M, C, time, time);
% For the motion model
dt = 0.5;
for k = 1:length(inputs.commands)
% Action command
u = inputs.commands(k,:).';
% Sensor reading
z = inputs.sensor_readings(k,:).';
% Time since last sensor reading
time = k * dt;
particles.time_since_reading = time - particles.last_sensor_reading;
particles.last_sensor_reading = time;
% Count the number of particles that survive
clear fit_particles;
fit_particles.poses = [];
fit_particles.weights = [];
fit_particles.time_since_reading = particles.time_since_reading;
fit_particles.last_sensor_reading = particles.last_sensor_reading;
for m = 1:length(particles.weights)
particle = particles.poses(:,m);
weight = particles.weights(1,m);
% Predict particle motion
particle = sample_motion_model_velocity(u, particle, dt);
% Weight of the new particle position
weight = weight * beam_range_finder_model(z, particle);
% The particle is useful
if weight > 0
fit_particles.poses = [fit_particles.poses, particle];
fit_particles.weights = [fit_particles.weights, weight];
end
end
% Compute the weighted mean of particles
weighted_mean = compute_mean(particles);
% Normalized and Resmaples particles if necessary
[M, particles, init] = adjust_particles(weighted_mean, fit_particles, resampling, M, C, init);
plot_data(particles, weighted_mean, done);
end
% Final plot
done = true;
plot_data(particles, weighted_mean, done);