-
Notifications
You must be signed in to change notification settings - Fork 2
/
Vertigo_position_pitch_roll_yaw.m
170 lines (126 loc) · 4.73 KB
/
Vertigo_position_pitch_roll_yaw.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
158
159
160
161
162
163
164
165
166
167
168
169
170
% Vertigo
% Kalman Filtering in 3 dimensions
%
% This file is part of the VertigoIMU project
%
% Jon Sowman 2017
% jon+vertigo@jonsowman.com
% jcostello@suttonmail.org
% Luke Gonsalves
% The start and end times in the data to process
window_start = 0; % Start of time window Seconds
window_end = 120;% End of time window 85 Seconds
% Extract the bit of data we want to look at
tstartidx = find(imudata(:,1) > window_start, 1);
tendidx = find(imudata(:,1) > window_end, 1);
tstartidx_gps = find(gpsdata(:,1) > window_start, 1);
tendidx_gps = find(gpsdata(:,1) > window_end, 1);
% Find the accel
data_time = imudata(tstartidx:tendidx, 1);
euldata_window = euldata (tstartidx:tendidx, :)
data_accel_down = (accel_ned(tstartidx:tendidx, 3) - 1) * 9.81;
data_accel_north = (accel_ned(tstartidx:tendidx, 1) ) * 9.81;
data_accel_east = (accel_ned(tstartidx:tendidx, 2) ) * 9.81;
% Find the gps in UTM
data_time_gps = gpsdata(tstartidx_gps:tendidx_gps, 1);
[x,y,zone] = ll2utm(gpsdata(tstartidx_gps:tendidx_gps,4),gpsdata(tstartidx_gps:tendidx_gps,3));
%[x,y,zone] = ll2utm(lat,lon); % do the job!
%gpsdata(:,1) = (gpsdata(:,1) - gpsdata(1,1)) / 1000;
North_utm_position = (x(:,1)- x(1,1));
East_utm_position = (y(:,1)- y(1,1));
%plot (North_utm_position, East_utm_position);
position_north_gps = North_utm_position
position_north_gps = position_north_gps - position_north_gps(1);
position_east_gps = East_utm_position
position_east_gps = position_east_gps - position_east_gps(1);
data_alt_gps = gpsdata(tstartidx_gps:tendidx_gps, 5);
data_alt_gps = data_alt_gps - data_alt_gps(1);
% Combined IMU and GPS
data_merged = sortrows([data_time 1*ones(length(data_time), 1) [1:length(data_time)]'; data_time_gps, 2*ones(length(data_time_gps), 1), [1:length(data_time_gps)]']);
% Integrate accel
data_vel = cumtrapz(data_time, data_accel_down);
data_pos = cumtrapz(data_time, data_vel);
data_vel_north = cumtrapz(data_time, data_accel_north); % North
data_pos_north = cumtrapz(data_time, data_vel_north);
data_vel_east = cumtrapz(data_time, data_accel_east); % East
data_pos_east = cumtrapz(data_time, data_vel_east);
% Euler: x+ = x + dx * dt
% Model: Fx + Gu (no inputs, random jerk)
% s+ = s + dt * v
% v+ = v + dt * a
Hg = [1 0 0 0]; % measure gps
Ha = [0 0 1 1]; % measure acc
% Initial state and covariance
x = [0 0 0 0]';
xn = [0 0 0 0]';
xe = [0 0 0 0]';
P = zeros(4, 4); P(3,3) = 1; P(4,4) = 1e0;
% Process noise
Q = diag([0 0 1e-4 1e-9]);
% Measurement noise
Ra = 1;
Rg = 0.01;
% Store Kalman
kal_x_stor = zeros(length(data_time), 4);
kal_xn_stor = zeros(length(data_time), 4);
kal_xe_stor = zeros(length(data_time), 4);
% Computation time
tc = data_time(1);
% Run Kalman
for i = 1:length(data_merged)
% Find dt since samples can possibly be dropped
dt = data_merged(i, 1) - tc;
tc = data_merged(i, 1);
% Find DT model
F = [1 dt 0 0; 0 1 dt 0; 0 0 1 -1; 0 0 0 1];
% Predict
xp = F * x; % no inputs
xpn = F * xn;
xpe = F * xe;
Pp = F * P * F' + Q;
% Update for accel
if data_merged(i, 2) == 1
y = data_accel_down(data_merged(i, 3)) - Ha * x;
yn = data_accel_north(data_merged(i, 3)) - Ha * xn; % North
ye = data_accel_east(data_merged(i, 3)) - Ha * xe; % East
S = Ha * P * Ha' + Ra;
K = Pp * Ha' * 1/S;
x = xp + K * y;
xn = xpn + K * yn; % North
xe = xpe + K * ye; % East
P = (eye(4) - K * Ha) * Pp;
elseif data_merged(i, 2) == 2
% Update for GPS
y = data_alt_gps(data_merged(i, 3)) - Hg * x;
yn = position_north_gps(data_merged(i, 3)) - Hg * xn; % North
ye = position_east_gps(data_merged(i, 3)) - Hg * xe; % East
S = Hg * P * Hg' + Rg;
K = Pp * Hg' * 1/S;
x = xp + K * y;
xn = xpn + K * yn; % North
xe = xpe + K * ye; % East
P = (eye(4) - K * Hg) * Pp;
end
% Store
kal_x_stor(i, :) = x;
kal_xn_stor(i, :) = xn;
kal_xe_stor(i, :) = xe;
end
%smoothing the data
smooth_east_position = smooth(kal_xe_stor(:,1));
smooth_north_position = smooth (kal_xn_stor(:,1));
figure;
plot (smooth_east_position,smooth_north_position);
legend('smooth Fusion Position');
xlabel('East Postion(m)');
ylabel('North Position(m)');
quiv_ds_rate = 20; % downsample rate
figure;
[xy,yy] = pol2cart(euldata_window(:,3)*2*pi/360,5);
quiver (decimate(smooth_east_position, quiv_ds_rate), ...
decimate(smooth_north_position, quiv_ds_rate), ...
decimate(xy, 18), ...
decimate(yy, 18));
legend('Yaw at position');
xlabel('East Position(m)');
ylabel('North Position(m)');