-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunvp.m
178 lines (151 loc) · 4.41 KB
/
runvp.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
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
function runvp(nSteps,pauseLen, makeVideo)
global Param;
global State;
global Data;
Param.vp=1;
Param.sim=0;
if ~exist('nSteps','var') || isempty(nSteps)
nSteps = inf;
end
if ~exist('pauseLen','var')
pauseLen = 0; % seconds
end
Data = load_vp_si();
if makeVideo
try
votype = 'avifile';
vo = avifile('video.avi', 'fps', min(5, 1/pauseLen));
catch
votype = 'VideoWriter';
vo = VideoWriter('video', 'MPEG-4');
set(vo, 'FrameRate', min(5, 1/pauseLen));
open(vo);
end
end
% Initalize Params
%===================================================
% vehicle geometry
Param.a = 3.78; % [m]
Param.b = 0.50; % [m]
Param.L = 2.83; % [m]
Param.H = 0.76; % [m]
% 2x2 process noise on control input
sigma.vc = 0.02; % [m/s]
sigma.alpha = 2*pi/180; % [rad]
Param.Qu = diag([sigma.vc, sigma.alpha].^2);
% 3x3 process noise on model error
sigma.x = 0.1; % [m]
sigma.y = 0.1; % [m]
sigma.phi = 0.5*pi/180; % [rad]
Param.Qf = diag([sigma.x, sigma.y, sigma.phi].^2);
% 2x2 observation noise
sigma.r = 0.05; % [m]
sigma.beta = 1*pi/180; % [rad]
Param.R = diag([sigma.r, sigma.beta].^2);
%===================================================
% Initialize State
%===================================================
State.Ekf.mu = [Data.Gps.x(2), Data.Gps.y(2), 36*pi/180]';
State.Ekf.Sigma = zeros(3);
State.Ekf.aug_mu = State.Ekf.mu;
State.Ekf.Observed_landmarks = [];
State.Ekf.data_assoc = [];
State.Ekf.predMu = State.Ekf.mu;
State.Ekf.predSigma = State.Ekf.Sigma;
Param.updateMethod='seq'
State.Ekf.run_time_pred = zeros(nSteps);
State.Ekf.run_time_update = zeros(nSteps);
State.Ekf.run_time_landmarks=zeros(nSteps);
global AAr;
AAr = [0:360]*pi/360;
figure(1); clf;
axis equal;
ci = 1; % control index
t = min(Data.Laser.time(1), Data.Control.time(1));
for k=1:min(nSteps, length(Data.Laser.time))
tic
while (Data.Control.time(ci) < Data.Laser.time(k)) %PREDICT UNTIL YOU GET AN OBSERVATION
% control available
dt = Data.Control.time(ci) - t;
t = Data.Control.time(ci);
u = [Data.Control.ve(ci), Data.Control.alpha(ci)]';
ekfpredict_vp(u, dt); %%PREDICT
ci = ci+1;
end
State.Ekf.run_time_pred(k) = toc;
% observation available
dt = Data.Laser.time(k) - t;
t = Data.Laser.time(k);
z = detectTreesI16(Data.Laser.ranges(k,:));
z_2 = z;
z_2(2,:) = z_2(2,:) - pi/2;
tic
ekfupdate(z_2); %%MAKE AN UPDATE
State.Ekf.run_time_update(k) = toc;
State.Ekf.run_time_landmarks(k) = length(State.Ekf.Observed_landmarks);
State.Ekf.aug_mu = [State.Ekf.aug_mu, State.Ekf.mu(1:3)];
doGraphics(z);
drawnow;
if pauseLen > 0
pause(pauseLen);
end
if makeVideo
F = getframe(gcf);
switch votype
case 'avifile'
vo = addframe(vo, F);
case 'VideoWriter'
writeVideo(vo, F);
otherwise
error('unrecognized votype');
end
end
end
figure;
title('CPU TIME FOR PREDICTION AND UPDATE ');
plot(1:nSteps, State.Ekf.run_time_pred);
hold on;
plot(1:nSteps, State.Ekf.run_time_update);
hold off;
legend('Prediction','Update');
xlabel('nSteps');
ylabel('CPU time');
figure;
title('Number of landmarks with time');
plot(1:nSteps, State.Ekf.run_time_landmarks);
xlabel('Iteration');
ylabel('Number of Landmarks');
%==========================================================================
function doGraphics(z)
% Put whatever graphics you want here for visualization
%
% WARNING: this slows down your process time, so use sparingly when trying
% to crunch the whole data set!
global Param;
global State;
plot(State.Ekf.aug_mu(1,:), State.Ekf.aug_mu(2,:), '--b');
hold on
plotbot(State.Ekf.mu(1), State.Ekf.mu(2), State.Ekf.mu(3), 'black', 1, 'blue', 1);
hold on;
robot_graph =plotcov2d( State.Ekf.mu(1), State.Ekf.mu(2), State.Ekf.Sigma, 'blue', 0, 'blue', 0, 3);
BB=100;
axis([[-BB,BB]+State.Ekf.mu(1), [-BB,BB]+State.Ekf.mu(2)]);
textLabels = false;
N=length(State.Ekf.Observed_landmarks);
if N > 0
for i = 1:N
plotcov2d(State.Ekf.mu(3+2*i-1,1), State.Ekf.mu(3+2*i,1), State.Ekf.Sigma(3+2*i-1:3+2*i,3+2*i-1:3+2*i), 'g', 0, 'y', 0.5, 3);
end
end
title('Victoria Park')
xr = State.Ekf.mu(1);
yr = State.Ekf.mu(2);
tr = State.Ekf.mu(3);
for k=1:size(z,2)
r = z(1,k);
b = z(2,k);
xl = xr + r*cos(b+tr-pi/2);
yl = yr + r*sin(b+tr-pi/2);
plot([xr; xl], [yr; yl],'r',xl,yl,'r*');
end
hold off;