forked from marynelv/IVCalibrationToolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pvUKF.m
158 lines (122 loc) · 4.27 KB
/
pvUKF.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
% Position, orientation and velocity UKF
clc
close all
%% UKF parameters
ukf_alpha = 0.1;
ukf_beta = 2;
%% x: state vector
% Composed by IMU position and velocity in the world frame
% [p_w_i; v_w_i] = x(1:6);
%% P: state covariance matrix 6-by-6
%% u: process inputs
% Composed by measured IMU acceleration and rotational velocity
% u = [accel_i_measured(1:3, i)]
%% n: process noise
%% Q: process noise covariance matrix
Qacc = eye(3) * 0.1^2;
Q = Qacc;
%% z: measurements
% See section 4.3 Measurement Model on page 11
% z is a 2n-by-1 column vector of observed pixel coordinates in the
% form [x1 y1 ... xn yn]' where n is the number of 3D feature points
%% R: measurement noise covariance matrix
% The associated block-daigonal covariance matrix of z
% R = diag(R1 ... Rn) = 0.1^2 * eye(length(z));
%% Starting index
i = 2;
j = 2;
nowTime = -0.01;
%% Initial estimate
x = [p_w(:,i); v_w(:,i-1)]; % easy as ground truth location
Ppos = diag([0.5 0.5 0.5]);
Pvel = diag([0.5 0.5 0.5]);
P = [Ppos zeros(3); zeros(3) Pvel];
%% Initialize storage matrices and figure
numCamMeasurements = size(observed_pts_c, 2);
numImuMeasurements = length(imuData);
numPoses = numImuMeasurements + numCamMeasurements;
accumPoses = zeros(3,numPoses);
distanceError = zeros(1, numPoses);
velocityError = zeros(1, numPoses);
process_params = cell(4,1);
obs_params = cell(5,1);
h = figure('Name','Position and Velocity Estimation', ...
'NumberTitle','off','Position',[10 10 1000 600]);
ukf_N = length(x);
%% Begin Kalman filter
count = 1;
while (i <= numImuMeasurements && j <= numCamMeasurements )
% Read the timestamp for the next data input
imuTime = imuData(i,3);
camTime = camData(j,3);
if (imuTime <= camTime)
%% Prediction step
pastTime = nowTime;
nowTime = imuTime;
dt = nowTime - pastTime;
process_params{1} = accel_i_measured(:,i); % usually known as u
process_params{2} = dt;
process_params{3} = q_w_i(:,i);
process_params{4} = gravity;
process_handle = @processModelPV;
[x, P] = predictUKF(x, process_handle, process_params, ...
P, Q, ukf_alpha, ukf_beta);
P
i = i + 1;
else
%% Correction Step
% Perform correction step
z = noisy_observed_pts_c(:,j);
R = 0.1^2 * eye(length(z));
q_world_IMU = q_w_i(:,j);
p_IMU_camera = repmat(p_i_c, 1, 2*ukf_N+1);
q_IMU_camera = repmat(q_i_c, 1, 2*ukf_N+1);
p_world_pts = pts_w(1:3, :);
obs_params{1} = q_world_IMU;
obs_params{2} = p_IMU_camera;
obs_params{3} = q_IMU_camera;
obs_params{4} = p_world_pts;
obs_params{5} = K;
obs_handle = @measurementModelPV;
[ x, P ] = correctUKF( x, P, R, z, obs_handle, obs_params, ukf_alpha, ukf_beta );
j = j + 1;
end
if (i < numImuMeasurements)
%% Distance error
distanceError(1,count) = norm(x(1:3) - p_w(:,i-1));
velocityError(1,count) = norm(x(4:6) - v_w(:,i-1));
%% Plot
accumPoses(:,count) = x(1:3);
count = count + 1;
if mod(count, 10) == 1
figure(h);
clf
subplot(2,2,[1, 3]);
plot3(accumPoses(1,1:count-1), accumPoses(2,1:count-1), accumPoses(3,1:count-1),'-');
hold on;
plot3(p_w(1,1:i), p_w(2,1:i), p_w(3,1:i), 'g');
% hold on;
% plot3(pts_w(1, :), pts_w(2, :), pts_w(3, :), 'r.');
axis equal
axis vis3d
xlabel('x'); ylabel('y'); zlabel('z');
grid on;
title('Motion Estimation');
subplot(2,2,2);
plot(1:count,distanceError(1:count));
maxErr = max(distanceError);
axis([0 numPoses 0 maxErr]);
% xlabel('Time');
ylabel('Squared Error');
title('Distance Error');
subplot(2,2,4);
plot(1:count,velocityError(1:count));
maxErr = max(velocityError);
axis([0 numPoses 0 maxErr]);
% xlabel('Time');
ylabel('Squared Error');
title('Velocity Error');
%pause
end
end
end