annotate toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/SLAM/Old/slam_kf.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 % This is like robot1, except we only use a Kalman filter.
Daniel@0 2 % The goal is to study how the precision matrix changes.
Daniel@0 3
Daniel@0 4 seed = 0;
Daniel@0 5 rand('state', seed);
Daniel@0 6 randn('state', seed);
Daniel@0 7
Daniel@0 8 if 0
Daniel@0 9 T = 20;
Daniel@0 10 ctrl_signal = [repmat([1 0]', 1, T/4) repmat([0 1]', 1, T/4) ...
Daniel@0 11 repmat([-1 0]', 1, T/4) repmat([0 -1]', 1, T/4)];
Daniel@0 12 else
Daniel@0 13 T = 12;
Daniel@0 14 ctrl_signal = repmat([1 0]', 1, T);
Daniel@0 15 end
Daniel@0 16
Daniel@0 17 nlandmarks = 6;
Daniel@0 18 if 0
Daniel@0 19 true_landmark_pos = [1 1;
Daniel@0 20 4 1;
Daniel@0 21 4 4;
Daniel@0 22 1 4]';
Daniel@0 23 else
Daniel@0 24 true_landmark_pos = 10*rand(2,nlandmarks);
Daniel@0 25 end
Daniel@0 26 figure(1); clf
Daniel@0 27 hold on
Daniel@0 28 for i=1:nlandmarks
Daniel@0 29 %text(true_landmark_pos(1,i), true_landmark_pos(2,i), sprintf('L%d',i));
Daniel@0 30 plot(true_landmark_pos(1,i), true_landmark_pos(2,i), '*')
Daniel@0 31 end
Daniel@0 32 hold off
Daniel@0 33
Daniel@0 34 init_robot_pos = [0 0]';
Daniel@0 35
Daniel@0 36 true_robot_pos = zeros(2, T);
Daniel@0 37 true_data_assoc = zeros(1, T);
Daniel@0 38 true_rel_dist = zeros(2, T);
Daniel@0 39 for t=1:T
Daniel@0 40 if t>1
Daniel@0 41 true_robot_pos(:,t) = true_robot_pos(:,t-1) + ctrl_signal(:,t);
Daniel@0 42 else
Daniel@0 43 true_robot_pos(:,t) = init_robot_pos + ctrl_signal(:,t);
Daniel@0 44 end
Daniel@0 45 %nn = argmin(dist2(true_robot_pos(:,t)', true_landmark_pos'));
Daniel@0 46 nn = wrap(t, nlandmarks); % observe 1, 2, 3, 4, 1, 2, ...
Daniel@0 47 true_data_assoc(t) = nn;
Daniel@0 48 true_rel_dist(:,t) = true_landmark_pos(:, nn) - true_robot_pos(:,t);
Daniel@0 49 end
Daniel@0 50
Daniel@0 51 R = 1e-3*eye(2); % noise added to observation
Daniel@0 52 Q = 1e-3*eye(2); % noise added to robot motion
Daniel@0 53
Daniel@0 54 % Create data set
Daniel@0 55 obs_noise_seq = sample_gaussian([0 0]', R, T)';
Daniel@0 56 obs_rel_pos = true_rel_dist + obs_noise_seq;
Daniel@0 57 %obs_rel_pos = true_rel_dist;
Daniel@0 58
Daniel@0 59
Daniel@0 60 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Daniel@0 61 % Create params for inference
Daniel@0 62
Daniel@0 63 % X(t) = A X(t-1) + B U(t) + noise(Q)
Daniel@0 64
Daniel@0 65 % [L1] = [1 ] * [L1] + [0] * Ut + [0 ]
Daniel@0 66 % [L2] [ 1 ] [L2] [0] [ 0 ]
Daniel@0 67 % [R ]t [ 1] [R ]t-1 [1] [ Q]
Daniel@0 68
Daniel@0 69 % Y(t)|S(t)=s = C(s) X(t) + noise(R)
Daniel@0 70 % Yt|St=1 = [1 0 -1] * [L1] + R
Daniel@0 71 % [L2]
Daniel@0 72 % [R ]
Daniel@0 73
Daniel@0 74 % Create indices into block structure
Daniel@0 75 bs = 2*ones(1, nlandmarks+1); % sizes of blocks in state space
Daniel@0 76 robot_block = block(nlandmarks+1, bs);
Daniel@0 77 for i=1:nlandmarks
Daniel@0 78 landmark_block(:,i) = block(i, bs)';
Daniel@0 79 end
Daniel@0 80 Xsz = 2*(nlandmarks+1); % 2 values for each landmark plus robot
Daniel@0 81 Ysz = 2; % observe relative location
Daniel@0 82 Usz = 2; % input is (dx, dy)
Daniel@0 83
Daniel@0 84
Daniel@0 85 % create block-diagonal trans matrix for each switch
Daniel@0 86 A = zeros(Xsz, Xsz);
Daniel@0 87 for i=1:nlandmarks
Daniel@0 88 bi = landmark_block(:,i);
Daniel@0 89 A(bi, bi) = eye(2);
Daniel@0 90 end
Daniel@0 91 bi = robot_block;
Daniel@0 92 A(bi, bi) = eye(2);
Daniel@0 93 A = repmat(A, [1 1 nlandmarks]); % same for all switch values
Daniel@0 94
Daniel@0 95 % create block-diagonal system cov
Daniel@0 96
Daniel@0 97
Daniel@0 98 Qbig = zeros(Xsz, Xsz);
Daniel@0 99 bi = robot_block;
Daniel@0 100 Qbig(bi,bi) = Q; % only add noise to robot motion
Daniel@0 101 Qbig = repmat(Qbig, [1 1 nlandmarks]);
Daniel@0 102
Daniel@0 103 % create input matrix
Daniel@0 104 B = zeros(Xsz, Usz);
Daniel@0 105 B(robot_block,:) = eye(2); % only add input to robot position
Daniel@0 106 B = repmat(B, [1 1 nlandmarks]);
Daniel@0 107
Daniel@0 108 % create observation matrix for each value of the switch node
Daniel@0 109 % C(:,:,i) = (0 ... I ... -I) where the I is in the i'th posn.
Daniel@0 110 % This computes L(i) - R
Daniel@0 111 C = zeros(Ysz, Xsz, nlandmarks);
Daniel@0 112 for i=1:nlandmarks
Daniel@0 113 C(:, landmark_block(:,i), i) = eye(2);
Daniel@0 114 C(:, robot_block, i) = -eye(2);
Daniel@0 115 end
Daniel@0 116
Daniel@0 117 % create observation cov for each value of the switch node
Daniel@0 118 Rbig = repmat(R, [1 1 nlandmarks]);
Daniel@0 119
Daniel@0 120 % initial conditions
Daniel@0 121 init_x = zeros(Xsz, 1);
Daniel@0 122 init_v = zeros(Xsz, Xsz);
Daniel@0 123 bi = robot_block;
Daniel@0 124 init_x(bi) = init_robot_pos;
Daniel@0 125 init_V(bi, bi) = 1e-5*eye(2); % very sure of robot posn
Daniel@0 126 for i=1:nlandmarks
Daniel@0 127 bi = landmark_block(:,i);
Daniel@0 128 init_V(bi,bi)= 1e5*eye(2); % very uncertain of landmark psosns
Daniel@0 129 %init_x(bi) = true_landmark_pos(:,i);
Daniel@0 130 %init_V(bi,bi)= 1e-5*eye(2); % very sure of landmark psosns
Daniel@0 131 end
Daniel@0 132
Daniel@0 133 [xsmooth, Vsmooth] = kalman_filter(obs_rel_pos, A, C, Qbig, Rbig, init_x, init_V, ...
Daniel@0 134 'model', true_data_assoc, 'u', ctrl_signal, 'B', B);
Daniel@0 135
Daniel@0 136 est_robot_pos = xsmooth(robot_block, :);
Daniel@0 137 est_robot_pos_cov = Vsmooth(robot_block, robot_block, :);
Daniel@0 138
Daniel@0 139 for i=1:nlandmarks
Daniel@0 140 bi = landmark_block(:,i);
Daniel@0 141 est_landmark_pos(:,i) = xsmooth(bi, T);
Daniel@0 142 est_landmark_pos_cov(:,:,i) = Vsmooth(bi, bi, T);
Daniel@0 143 end
Daniel@0 144
Daniel@0 145
Daniel@0 146
Daniel@0 147 P = zeros(size(Vsmooth));
Daniel@0 148 for t=1:T
Daniel@0 149 P(:,:,t) = inv(Vsmooth(:,:,t));
Daniel@0 150 end
Daniel@0 151
Daniel@0 152 figure(1)
Daniel@0 153 for t=1:T
Daniel@0 154 subplot(T/2,2,t)
Daniel@0 155 imagesc(P(1:2:end,1:2:end, t))
Daniel@0 156 colorbar
Daniel@0 157 end
Daniel@0 158
Daniel@0 159 figure(2)
Daniel@0 160 for t=1:T
Daniel@0 161 subplot(T/2,2,t)
Daniel@0 162 imagesc(Vsmooth(1:2:end,1:2:end, t))
Daniel@0 163 colorbar
Daniel@0 164 end
Daniel@0 165
Daniel@0 166
Daniel@0 167
Daniel@0 168 % marginalize out robot position and then check structure
Daniel@0 169 bi = landmark_block(:);
Daniel@0 170 V = Vsmooth(bi,bi,T);
Daniel@0 171 P = inv(V);
Daniel@0 172 P(1:2:end,1:2:end)