Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/bnt/examples/dynamic/SLAM/mk_linear_slam.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 function [A,B,C,Q,R,Qbig,Rbig,init_x,init_V,robot_block,landmark_block,... | |
2 true_landmark_pos, true_robot_pos, true_data_assoc, ... | |
3 obs_rel_pos, ctrl_signal] = mk_linear_slam(varargin) | |
4 | |
5 % We create data from a linear system for testing SLAM algorithms. | |
6 % i.e. , new robot pos = old robot pos + ctrl_signal, which is just a displacement vector. | |
7 % and observation = landmark_pos - robot_pos, which is just a displacement vector. | |
8 % | |
9 % The behavior is determined by the following optional arguments: | |
10 % | |
11 % 'nlandmarks' - num. landmarks | |
12 % 'landmarks' - 'rnd' means random locations in the unit sqyare | |
13 % 'square' means at [1 1], [4 1], [4 4] and [1 4] | |
14 % 'T' - num steps to run | |
15 % 'ctrl' - 'stationary' means the robot remains at [0 0], | |
16 % 'leftright' means the robot receives a constant contol of [1 0], | |
17 % 'square' means we navigate the robot around the square | |
18 % 'data-assoc' - 'rnd' means we observe landmarks at random | |
19 % 'nn' means we observe the nearest neighbor landmark | |
20 % 'cycle' means we observe landmarks in order 1,2,.., 1, 2, ... | |
21 | |
22 args = varargin; | |
23 % get mandatory params | |
24 for i=1:2:length(args) | |
25 switch args{i}, | |
26 case 'nlandmarks', nlandmarks = args{i+1}; | |
27 case 'T', T = args{i+1}; | |
28 end | |
29 end | |
30 | |
31 % set defaults | |
32 true_landmark_pos = rand(2,nlandmarks); | |
33 true_data_assoc = []; | |
34 | |
35 % get args | |
36 for i=1:2:length(args) | |
37 switch args{i}, | |
38 case 'landmarks', | |
39 switch args{i+1}, | |
40 case 'rnd', true_landmark_pos = rand(2,nlandmarks); | |
41 case 'square', true_landmark_pos = [1 1; 4 1; 4 4; 1 4]'; | |
42 end | |
43 case 'ctrl', | |
44 switch args{i+1}, | |
45 case 'stationary', ctrl_signal = repmat([0 0]', 1, T); | |
46 case 'leftright', ctrl_signal = repmat([1 0]', 1, T); | |
47 case 'square', ctrl_signal = [repmat([1 0]', 1, T/4) repmat([0 1]', 1, T/4) ... | |
48 repmat([-1 0]', 1, T/4) repmat([0 -1]', 1, T/4)]; | |
49 end | |
50 case 'data-assoc', | |
51 switch args{i+1}, | |
52 case 'rnd', true_data_assoc = sample_discrete(normalise(ones(1,nlandmarks)),1,T); | |
53 case 'cycle', true_data_assoc = wrap(1:T, nlandmarks); | |
54 end | |
55 end | |
56 end | |
57 if isempty(true_data_assoc) | |
58 use_nn = 1; | |
59 else | |
60 use_nn = 0; | |
61 end | |
62 | |
63 %%%%%%%%%%%%%%%%%%%%%%%% | |
64 % generate data | |
65 | |
66 init_robot_pos = [0 0]'; | |
67 true_robot_pos = zeros(2, T); | |
68 true_rel_dist = zeros(2, T); | |
69 for t=1:T | |
70 if t>1 | |
71 true_robot_pos(:,t) = true_robot_pos(:,t-1) + ctrl_signal(:,t); | |
72 else | |
73 true_robot_pos(:,t) = init_robot_pos + ctrl_signal(:,t); | |
74 end | |
75 nn = argmin(dist2(true_robot_pos(:,t)', true_landmark_pos')); | |
76 if use_nn | |
77 true_data_assoc(t) = nn; | |
78 end | |
79 true_rel_dist(:,t) = true_landmark_pos(:, nn) - true_robot_pos(:,t); | |
80 end | |
81 | |
82 | |
83 R = 1e-3*eye(2); % noise added to observation | |
84 Q = 1e-3*eye(2); % noise added to robot motion | |
85 | |
86 % Create data set | |
87 obs_noise_seq = sample_gaussian([0 0]', R, T)'; | |
88 obs_rel_pos = true_rel_dist + obs_noise_seq; | |
89 %obs_rel_pos = true_rel_dist; | |
90 | |
91 %%%%%%%%%%%%%%%%%% | |
92 % Create params | |
93 | |
94 | |
95 % X(t) = A X(t-1) + B U(t) + noise(Q) | |
96 | |
97 % [L1] = [1 ] * [L1] + [0] * Ut + [0 ] | |
98 % [L2] [ 1 ] [L2] [0] [ 0 ] | |
99 % [R ]t [ 1] [R ]t-1 [1] [ Q] | |
100 | |
101 % Y(t)|S(t)=s = C(s) X(t) + noise(R) | |
102 % Yt|St=1 = [1 0 -1] * [L1] + R | |
103 % [L2] | |
104 % [R ] | |
105 | |
106 % Create indices into block structure | |
107 bs = 2*ones(1, nlandmarks+1); % sizes of blocks in state space | |
108 robot_block = block(nlandmarks+1, bs); | |
109 for i=1:nlandmarks | |
110 landmark_block(:,i) = block(i, bs)'; | |
111 end | |
112 Xsz = 2*(nlandmarks+1); % 2 values for each landmark plus robot | |
113 Ysz = 2; % observe relative location | |
114 Usz = 2; % input is (dx, dy) | |
115 | |
116 | |
117 % create block-diagonal trans matrix for each switch | |
118 A = zeros(Xsz, Xsz); | |
119 for i=1:nlandmarks | |
120 bi = landmark_block(:,i); | |
121 A(bi, bi) = eye(2); | |
122 end | |
123 bi = robot_block; | |
124 A(bi, bi) = eye(2); | |
125 A = repmat(A, [1 1 nlandmarks]); % same for all switch values | |
126 | |
127 % create block-diagonal system cov | |
128 | |
129 | |
130 Qbig = zeros(Xsz, Xsz); | |
131 bi = robot_block; | |
132 Qbig(bi,bi) = Q; % only add noise to robot motion | |
133 Qbig = repmat(Qbig, [1 1 nlandmarks]); | |
134 | |
135 % create input matrix | |
136 B = zeros(Xsz, Usz); | |
137 B(robot_block,:) = eye(2); % only add input to robot position | |
138 B = repmat(B, [1 1 nlandmarks]); | |
139 | |
140 % create observation matrix for each value of the switch node | |
141 % C(:,:,i) = (0 ... I ... -I) where the I is in the i'th posn. | |
142 % This computes L(i) - R | |
143 C = zeros(Ysz, Xsz, nlandmarks); | |
144 for i=1:nlandmarks | |
145 C(:, landmark_block(:,i), i) = eye(2); | |
146 C(:, robot_block, i) = -eye(2); | |
147 end | |
148 | |
149 % create observation cov for each value of the switch node | |
150 Rbig = repmat(R, [1 1 nlandmarks]); | |
151 | |
152 % initial conditions | |
153 init_x = zeros(Xsz, 1); | |
154 init_v = zeros(Xsz, Xsz); | |
155 bi = robot_block; | |
156 init_x(bi) = init_robot_pos; | |
157 %init_V(bi, bi) = 1e-5*eye(2); % very sure of robot posn | |
158 init_V(bi, bi) = Q; % simualate uncertainty due to 1 motion step | |
159 for i=1:nlandmarks | |
160 bi = landmark_block(:,i); | |
161 init_V(bi,bi)= 1e5*eye(2); % very uncertain of landmark psosns | |
162 %init_x(bi) = true_landmark_pos(:,i); | |
163 %init_V(bi,bi)= 1e-5*eye(2); % very sure of landmark psosns | |
164 end |