comparison toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/SLAM/Old/paskin1.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 % This is like robot1, except we only use a Kalman filter.
2 % The goal is to study how the precision matrix changes.
3
4 seed = 1;
5 rand('state', seed);
6 randn('state', seed);
7
8 if 0
9 T = 20;
10 ctrl_signal = [repmat([1 0]', 1, T/4) repmat([0 1]', 1, T/4) ...
11 repmat([-1 0]', 1, T/4) repmat([0 -1]', 1, T/4)];
12 else
13 T = 60;
14 ctrl_signal = repmat([1 0]', 1, T);
15 end
16
17 nlandmarks = 6;
18 if 0
19 true_landmark_pos = [1 1;
20 4 1;
21 4 4;
22 1 4]';
23 else
24 true_landmark_pos = 10*rand(2,nlandmarks);
25 end
26 if 0
27 figure(1); clf
28 hold on
29 for i=1:nlandmarks
30 %text(true_landmark_pos(1,i), true_landmark_pos(2,i), sprintf('L%d',i));
31 plot(true_landmark_pos(1,i), true_landmark_pos(2,i), '*')
32 end
33 hold off
34 end
35
36 init_robot_pos = [0 0]';
37
38 true_robot_pos = zeros(2, T);
39 true_data_assoc = zeros(1, T);
40 true_rel_dist = zeros(2, T);
41 for t=1:T
42 if t>1
43 true_robot_pos(:,t) = true_robot_pos(:,t-1) + ctrl_signal(:,t);
44 else
45 true_robot_pos(:,t) = init_robot_pos + ctrl_signal(:,t);
46 end
47 nn = argmin(dist2(true_robot_pos(:,t)', true_landmark_pos'));
48 %true_data_assoc(t) = nn;
49 %true_data_assoc = wrap(t, nlandmarks); % observe 1, 2, 3, 4, 1, 2, ...
50 true_data_assoc = sample_discrete(normalise(ones(1,nlandmarks)),1,T);
51 true_rel_dist(:,t) = true_landmark_pos(:, nn) - true_robot_pos(:,t);
52 end
53
54 R = 1e-3*eye(2); % noise added to observation
55 Q = 1e-3*eye(2); % noise added to robot motion
56
57 % Create data set
58 obs_noise_seq = sample_gaussian([0 0]', R, T)';
59 obs_rel_pos = true_rel_dist + obs_noise_seq;
60 %obs_rel_pos = true_rel_dist;
61
62
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 % Create params for inference
65
66 % X(t) = A X(t-1) + B U(t) + noise(Q)
67
68 % [L1] = [1 ] * [L1] + [0] * Ut + [0 ]
69 % [L2] [ 1 ] [L2] [0] [ 0 ]
70 % [R ]t [ 1] [R ]t-1 [1] [ Q]
71
72 % Y(t)|S(t)=s = C(s) X(t) + noise(R)
73 % Yt|St=1 = [1 0 -1] * [L1] + R
74 % [L2]
75 % [R ]
76
77 % Create indices into block structure
78 bs = 2*ones(1, nlandmarks+1); % sizes of blocks in state space
79 robot_block = block(nlandmarks+1, bs);
80 for i=1:nlandmarks
81 landmark_block(:,i) = block(i, bs)';
82 end
83 Xsz = 2*(nlandmarks+1); % 2 values for each landmark plus robot
84 Ysz = 2; % observe relative location
85 Usz = 2; % input is (dx, dy)
86
87
88 % create block-diagonal trans matrix for each switch
89 A = zeros(Xsz, Xsz);
90 for i=1:nlandmarks
91 bi = landmark_block(:,i);
92 A(bi, bi) = eye(2);
93 end
94 bi = robot_block;
95 A(bi, bi) = eye(2);
96 A = repmat(A, [1 1 nlandmarks]); % same for all switch values
97
98 % create block-diagonal system cov
99
100
101 Qbig = zeros(Xsz, Xsz);
102 bi = robot_block;
103 Qbig(bi,bi) = Q; % only add noise to robot motion
104 Qbig = repmat(Qbig, [1 1 nlandmarks]);
105
106 % create input matrix
107 B = zeros(Xsz, Usz);
108 B(robot_block,:) = eye(2); % only add input to robot position
109 B = repmat(B, [1 1 nlandmarks]);
110
111 % create observation matrix for each value of the switch node
112 % C(:,:,i) = (0 ... I ... -I) where the I is in the i'th posn.
113 % This computes L(i) - R
114 C = zeros(Ysz, Xsz, nlandmarks);
115 for i=1:nlandmarks
116 C(:, landmark_block(:,i), i) = eye(2);
117 C(:, robot_block, i) = -eye(2);
118 end
119
120 % create observation cov for each value of the switch node
121 Rbig = repmat(R, [1 1 nlandmarks]);
122
123 % initial conditions
124 init_x = zeros(Xsz, 1);
125 init_v = zeros(Xsz, Xsz);
126 bi = robot_block;
127 init_x(bi) = init_robot_pos;
128 %init_V(bi, bi) = 1e-5*eye(2); % very sure of robot posn
129 init_V(bi, bi) = Q; % simualate uncertainty due to 1 motion step
130 for i=1:nlandmarks
131 bi = landmark_block(:,i);
132 init_V(bi,bi)= 1e5*eye(2); % very uncertain of landmark psosns
133 %init_x(bi) = true_landmark_pos(:,i);
134 %init_V(bi,bi)= 1e-5*eye(2); % very sure of landmark psosns
135 end
136
137 %k = nlandmarks-1; % exact
138 k = 3;
139 ndx = {};
140 for t=1:T
141 landmarks = unique(true_data_assoc(t:-1:max(t-k,1)));
142 tmp = [landmark_block(:, landmarks) robot_block'];
143 ndx{t} = tmp(:);
144 end
145
146 [xa, Va] = kalman_filter(obs_rel_pos, A, C, Qbig, Rbig, init_x, init_V, ...
147 'model', true_data_assoc, 'u', ctrl_signal, 'B', B, ...
148 'ndx', ndx);
149
150 [xe, Ve] = kalman_filter(obs_rel_pos, A, C, Qbig, Rbig, init_x, init_V, ...
151 'model', true_data_assoc, 'u', ctrl_signal, 'B', B);
152
153
154 if 0
155 est_robot_pos = x(robot_block, :);
156 est_robot_pos_cov = V(robot_block, robot_block, :);
157
158 for i=1:nlandmarks
159 bi = landmark_block(:,i);
160 est_landmark_pos(:,i) = x(bi, T);
161 est_landmark_pos_cov(:,:,i) = V(bi, bi, T);
162 end
163 end
164
165
166
167 nrows = 10;
168 stepsize = T/(2*nrows);
169 ts = 1:stepsize:T;
170
171 if 1 % plot
172
173 clim = [0 max(max(Va(:,:,end)))];
174
175 figure(2)
176 if 0
177 imagesc(Ve(1:2:end,1:2:end, T))
178 clim = get(gca,'clim');
179 else
180 i = 1;
181 for t=ts(:)'
182 subplot(nrows,2,i)
183 i = i + 1;
184 imagesc(Ve(1:2:end,1:2:end, t))
185 set(gca, 'clim', clim)
186 colorbar
187 end
188 end
189 suptitle('exact')
190
191
192 figure(3)
193 if 0
194 imagesc(Va(1:2:end,1:2:end, T))
195 set(gca,'clim', clim)
196 else
197 i = 1;
198 for t=ts(:)'
199 subplot(nrows,2,i)
200 i = i+1;
201 imagesc(Va(1:2:end,1:2:end, t))
202 set(gca, 'clim', clim)
203 colorbar
204 end
205 end
206 suptitle('approx')
207
208
209 figure(4)
210 i = 1;
211 for t=ts(:)'
212 subplot(nrows,2,i)
213 i = i+1;
214 Vd = Va(1:2:end,1:2:end, t) - Ve(1:2:end,1:2:end,t);
215 imagesc(Vd)
216 set(gca, 'clim', clim)
217 colorbar
218 end
219 suptitle('diff')
220
221 end % all plot
222
223
224 for t=1:T
225 i = 1:2*nlandmarks;
226 denom = Ve(i,i,t) + (Ve(i,i,t)==0);
227 Vd =(Va(i,i,t)-Ve(i,i,t)) ./ denom;
228 Verr(t) = max(Vd(:));
229 end
230 figure(6); plot(Verr)
231 title('max relative Verr')
232
233 for t=1:T
234 %err(t)=rms(xa(:,t), xe(:,t));
235 err(t)=rms(xa(1:end-2,t), xe(1:end-2,t)); % exclude robot
236 end
237 figure(5);plot(err)
238 title('rms mean pos')