wolffd@0: function [A,B,C,Q,R,Qbig,Rbig,init_x,init_V,robot_block,landmark_block,... wolffd@0: true_landmark_pos, true_robot_pos, true_data_assoc, ... wolffd@0: obs_rel_pos, ctrl_signal] = mk_linear_slam(varargin) wolffd@0: wolffd@0: % We create data from a linear system for testing SLAM algorithms. wolffd@0: % i.e. , new robot pos = old robot pos + ctrl_signal, which is just a displacement vector. wolffd@0: % and observation = landmark_pos - robot_pos, which is just a displacement vector. wolffd@0: % wolffd@0: % The behavior is determined by the following optional arguments: wolffd@0: % wolffd@0: % 'nlandmarks' - num. landmarks wolffd@0: % 'landmarks' - 'rnd' means random locations in the unit sqyare wolffd@0: % 'square' means at [1 1], [4 1], [4 4] and [1 4] wolffd@0: % 'T' - num steps to run wolffd@0: % 'ctrl' - 'stationary' means the robot remains at [0 0], wolffd@0: % 'leftright' means the robot receives a constant contol of [1 0], wolffd@0: % 'square' means we navigate the robot around the square wolffd@0: % 'data-assoc' - 'rnd' means we observe landmarks at random wolffd@0: % 'nn' means we observe the nearest neighbor landmark wolffd@0: % 'cycle' means we observe landmarks in order 1,2,.., 1, 2, ... wolffd@0: wolffd@0: args = varargin; wolffd@0: % get mandatory params wolffd@0: for i=1:2:length(args) wolffd@0: switch args{i}, wolffd@0: case 'nlandmarks', nlandmarks = args{i+1}; wolffd@0: case 'T', T = args{i+1}; wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: % set defaults wolffd@0: true_landmark_pos = rand(2,nlandmarks); wolffd@0: true_data_assoc = []; wolffd@0: wolffd@0: % get args wolffd@0: for i=1:2:length(args) wolffd@0: switch args{i}, wolffd@0: case 'landmarks', wolffd@0: switch args{i+1}, wolffd@0: case 'rnd', true_landmark_pos = rand(2,nlandmarks); wolffd@0: case 'square', true_landmark_pos = [1 1; 4 1; 4 4; 1 4]'; wolffd@0: end wolffd@0: case 'ctrl', wolffd@0: switch args{i+1}, wolffd@0: case 'stationary', ctrl_signal = repmat([0 0]', 1, T); wolffd@0: case 'leftright', ctrl_signal = repmat([1 0]', 1, T); wolffd@0: case 'square', ctrl_signal = [repmat([1 0]', 1, T/4) repmat([0 1]', 1, T/4) ... wolffd@0: repmat([-1 0]', 1, T/4) repmat([0 -1]', 1, T/4)]; wolffd@0: end wolffd@0: case 'data-assoc', wolffd@0: switch args{i+1}, wolffd@0: case 'rnd', true_data_assoc = sample_discrete(normalise(ones(1,nlandmarks)),1,T); wolffd@0: case 'cycle', true_data_assoc = wrap(1:T, nlandmarks); wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: if isempty(true_data_assoc) wolffd@0: use_nn = 1; wolffd@0: else wolffd@0: use_nn = 0; wolffd@0: end wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: % generate data wolffd@0: wolffd@0: init_robot_pos = [0 0]'; wolffd@0: true_robot_pos = zeros(2, T); wolffd@0: true_rel_dist = zeros(2, T); wolffd@0: for t=1:T wolffd@0: if t>1 wolffd@0: true_robot_pos(:,t) = true_robot_pos(:,t-1) + ctrl_signal(:,t); wolffd@0: else wolffd@0: true_robot_pos(:,t) = init_robot_pos + ctrl_signal(:,t); wolffd@0: end wolffd@0: nn = argmin(dist2(true_robot_pos(:,t)', true_landmark_pos')); wolffd@0: if use_nn wolffd@0: true_data_assoc(t) = nn; wolffd@0: end wolffd@0: true_rel_dist(:,t) = true_landmark_pos(:, nn) - true_robot_pos(:,t); wolffd@0: end wolffd@0: wolffd@0: wolffd@0: R = 1e-3*eye(2); % noise added to observation wolffd@0: Q = 1e-3*eye(2); % noise added to robot motion wolffd@0: wolffd@0: % Create data set wolffd@0: obs_noise_seq = sample_gaussian([0 0]', R, T)'; wolffd@0: obs_rel_pos = true_rel_dist + obs_noise_seq; wolffd@0: %obs_rel_pos = true_rel_dist; wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%% wolffd@0: % Create params wolffd@0: wolffd@0: wolffd@0: % X(t) = A X(t-1) + B U(t) + noise(Q) wolffd@0: wolffd@0: % [L1] = [1 ] * [L1] + [0] * Ut + [0 ] wolffd@0: % [L2] [ 1 ] [L2] [0] [ 0 ] wolffd@0: % [R ]t [ 1] [R ]t-1 [1] [ Q] wolffd@0: wolffd@0: % Y(t)|S(t)=s = C(s) X(t) + noise(R) wolffd@0: % Yt|St=1 = [1 0 -1] * [L1] + R wolffd@0: % [L2] wolffd@0: % [R ] wolffd@0: wolffd@0: % Create indices into block structure wolffd@0: bs = 2*ones(1, nlandmarks+1); % sizes of blocks in state space wolffd@0: robot_block = block(nlandmarks+1, bs); wolffd@0: for i=1:nlandmarks wolffd@0: landmark_block(:,i) = block(i, bs)'; wolffd@0: end wolffd@0: Xsz = 2*(nlandmarks+1); % 2 values for each landmark plus robot wolffd@0: Ysz = 2; % observe relative location wolffd@0: Usz = 2; % input is (dx, dy) wolffd@0: wolffd@0: wolffd@0: % create block-diagonal trans matrix for each switch wolffd@0: A = zeros(Xsz, Xsz); wolffd@0: for i=1:nlandmarks wolffd@0: bi = landmark_block(:,i); wolffd@0: A(bi, bi) = eye(2); wolffd@0: end wolffd@0: bi = robot_block; wolffd@0: A(bi, bi) = eye(2); wolffd@0: A = repmat(A, [1 1 nlandmarks]); % same for all switch values wolffd@0: wolffd@0: % create block-diagonal system cov wolffd@0: wolffd@0: wolffd@0: Qbig = zeros(Xsz, Xsz); wolffd@0: bi = robot_block; wolffd@0: Qbig(bi,bi) = Q; % only add noise to robot motion wolffd@0: Qbig = repmat(Qbig, [1 1 nlandmarks]); wolffd@0: wolffd@0: % create input matrix wolffd@0: B = zeros(Xsz, Usz); wolffd@0: B(robot_block,:) = eye(2); % only add input to robot position wolffd@0: B = repmat(B, [1 1 nlandmarks]); wolffd@0: wolffd@0: % create observation matrix for each value of the switch node wolffd@0: % C(:,:,i) = (0 ... I ... -I) where the I is in the i'th posn. wolffd@0: % This computes L(i) - R wolffd@0: C = zeros(Ysz, Xsz, nlandmarks); wolffd@0: for i=1:nlandmarks wolffd@0: C(:, landmark_block(:,i), i) = eye(2); wolffd@0: C(:, robot_block, i) = -eye(2); wolffd@0: end wolffd@0: wolffd@0: % create observation cov for each value of the switch node wolffd@0: Rbig = repmat(R, [1 1 nlandmarks]); wolffd@0: wolffd@0: % initial conditions wolffd@0: init_x = zeros(Xsz, 1); wolffd@0: init_v = zeros(Xsz, Xsz); wolffd@0: bi = robot_block; wolffd@0: init_x(bi) = init_robot_pos; wolffd@0: %init_V(bi, bi) = 1e-5*eye(2); % very sure of robot posn wolffd@0: init_V(bi, bi) = Q; % simualate uncertainty due to 1 motion step wolffd@0: for i=1:nlandmarks wolffd@0: bi = landmark_block(:,i); wolffd@0: init_V(bi,bi)= 1e5*eye(2); % very uncertain of landmark psosns wolffd@0: %init_x(bi) = true_landmark_pos(:,i); wolffd@0: %init_V(bi,bi)= 1e-5*eye(2); % very sure of landmark psosns wolffd@0: end