-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathkf_gins.m
273 lines (228 loc) · 8.27 KB
/
kf_gins.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
% -------------------------------------------------------------------------
% KF-GINS-Matlab: An EKF-based GNSS/INS Integrated Navigation System in Matlab
%
% Copyright (C) 2024, i2Nav Group, Wuhan University
%
% Author : Liqiang Wang
% Contact : wlq@whu.edu.cn
% Date : 2023.3.2
% -------------------------------------------------------------------------
clear;
clc;
% add function to workspace
addpath("function\");
%% define parameters and importdata process config
param = Param();
cfg = ProcessConfig1();
% cfg = ProcessConfig2();
% cfg = ProcessConfig3();
%% importdata data
% imudata
imudata = importdata(cfg.imufilepath);
imustarttime = imudata(1, 1);
imuendtime = imudata(end, 1);
% gnss data
gnssdata = importdata(cfg.gnssfilepath);
gnssdata(:, 2:3) = gnssdata(:, 2:3) * param.D2R;
if (size(gnssdata, 2) < 13)
cfg.usegnssvel = false;
end
gnssstarttime = gnssdata(1, 1);
gnssendtime = gnssdata(end, 1);
% odo data
if (cfg.useodonhc)
ododata = importdata(cfg.odofilepath);
end
%% save result
navpath = [cfg.outputfolder, '/NavResult'];
if cfg.usegnssvel
navpath = [navpath, '_GNSSVEL'];
disp("use GNSS velocity!");
end
if cfg.useodonhc
navpath = [navpath, '_ODONHC'];
disp("use ODO velocity!");
end
navpath = [navpath, '.nav'];
navfp = fopen(navpath, 'wt');
imuerrpath = [cfg.outputfolder, '/ImuError.txt'];
imuerrfp = fopen(imuerrpath, 'wt');
stdpath = [cfg.outputfolder, '/NavSTD.txt'];
stdfp = fopen(stdpath, 'wt');
%% get process time
% start time and end time
if imustarttime > gnssstarttime
starttime = imustarttime;
else
starttime = gnssstarttime;
end
if imuendtime > gnssendtime
endtime = gnssendtime;
else
endtime = imuendtime;
end
if cfg.starttime < starttime
cfg.starttime = starttime;
end
if cfg.endtime > endtime
cfg.endtime = endtime;
end
if cfg.useodonhc
% epoch to get odo vel
EPOCH_TO_GETVEL = 20;
ododatarate = 1.0 / mean(diff(ododata(:, 1)));
if cfg.odoupdaterate > ododatarate / EPOCH_TO_GETVEL
cfg.odoupdaterate = ododatarate / EPOCH_TO_GETVEL;
disp("warning: set ODO udpaterate to " + num2str(cfg.odoupdaterate) + "Hz!");
end
% odo update time
updateinterval = 1.0 / cfg.odoupdaterate;
time_to_nextupdate = updateinterval - mod(cfg.starttime, updateinterval);
odoupdatetime = cfg.starttime + time_to_nextupdate;
end
% data in process interval
imudata = imudata(imudata(:,1) >= cfg.starttime, :);
imudata = imudata(imudata(:,1) <= cfg.endtime, :);
gnssdata = gnssdata(gnssdata(:, 1) >= cfg.starttime, :);
gnssdata = gnssdata(gnssdata(:, 1) <= cfg.endtime, :);
%% for debug
disp("Start GNSS/INS Processing!");
lastprecent = 0;
%% initialization
[kf, navstate] = Initialize(cfg);
laststate = navstate;
% data index preprocess
lastimu = imudata(1, :)';
thisimu = imudata(1, :)';
imudt = thisimu(1, 1) - lastimu(1, 1);
gnssindex = 1;
while gnssdata(gnssindex, 1) < thisimu(1, 1)
gnssindex = gnssindex + 1;
end
if cfg.useodonhc
odoindex = 1;
while ododata(odoindex, 1) < thisimu(1, 1) && odoindex < size(ododata, 1)
odoindex = odoindex + 1;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% MAIN PROCEDD PROCEDURE!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for imuindex = 2:size(imudata, 1)-1
%% set value of last state
lastimu = thisimu;
laststate = navstate;
thisimu = imudata(imuindex, :)';
imudt = thisimu(1, 1) - lastimu(1, 1);
%% compensate IMU error
thisimu(2:4, 1) = (thisimu(2:4, 1) - imudt * navstate.gyrbias)./(ones(3, 1) + navstate.gyrscale);
thisimu(5:7, 1) = (thisimu(5:7, 1) - imudt * navstate.accbias)./(ones(3, 1) + navstate.accscale);
%% adjust GNSS index
while (gnssindex <= size(gnssdata, 1) && gnssdata(gnssindex, 1) < lastimu(1, 1))
gnssindex = gnssindex + 1;
end
% check whether gnss data is valid
if (gnssindex > size(gnssdata, 1))
disp('GNSS file END!');
break;
end
%% determine whether gnss update is required
if lastimu(1, 1) == gnssdata(gnssindex, 1)
% do gnss update for the current state
thisgnss = gnssdata(gnssindex, :)';
kf = GNSSUpdate(navstate, thisgnss, kf, cfg.antlever, cfg.usegnssvel, lastimu, imudt);
[kf, navstate] = ErrorFeedback(kf, navstate);
gnssindex = gnssindex + 1;
laststate = navstate;
% do propagation for current imu data
imudt = thisimu(1, 1) - lastimu(1, 1);
navstate = InsMech(laststate, lastimu, thisimu);
kf = InsPropagate(navstate, thisimu, imudt, kf, cfg.corrtime);
elseif (lastimu(1, 1) < gnssdata(gnssindex, 1) && thisimu(1, 1) > gnssdata(gnssindex, 1))
% ineterpolate imu to gnss time
[firstimu, secondimu] = interpolate(lastimu, thisimu, gnssdata(gnssindex, 1));
% do propagation for first imu
imudt = firstimu(1, 1) - lastimu(1, 1);
navstate = InsMech(laststate, lastimu, firstimu);
kf = InsPropagate(navstate, firstimu, imudt, kf, cfg.corrtime);
% do gnss update
thisgnss = gnssdata(gnssindex, :)';
kf = GNSSUpdate(navstate, thisgnss, kf, cfg.antlever, cfg.usegnssvel, firstimu, imudt);
[kf, navstate] = ErrorFeedback(kf, navstate);
gnssindex = gnssindex + 1;
laststate = navstate;
lastimu = firstimu;
% do propagation for second imu
imudt = secondimu(1, 1) - lastimu(1, 1);
navstate = InsMech(laststate, lastimu, secondimu);
kf = InsPropagate(navstate, secondimu, imudt, kf, cfg.corrtime);
else
%% only do propagation
% INS mechanization
navstate = InsMech(laststate, lastimu, thisimu);
% error propagation
kf = InsPropagate(navstate, thisimu, imudt, kf, cfg.corrtime);
end
if cfg.useodonhc
%% update odo index
while ododata(odoindex, 1) < thisimu(1, 1) && odoindex < size(ododata, 1)
odoindex = odoindex + 1;
end
%% odonhc udpate
if (thisimu(1, 1) >= odoupdatetime)
startindex = odoindex - round(EPOCH_TO_GETVEL / 2);
endindex = odoindex + round(EPOCH_TO_GETVEL / 2);
if (startindex < 1)
startindex = 1;
end
if (endindex > size(ododata, 1))
endindex = size(ododata, 1);
end
% get odovel and update
[odovel, valid] = GetOdoVel(ododata(startindex:endindex, :), thisimu(1, 1));
if valid
odonhc_vel = [odovel; 0; 0];
kf = ODONHCUpdate(navstate, odonhc_vel, kf, cfg, thisimu, imudt);
[kf, navstate] = ErrorFeedback(kf, navstate);
end
odoupdatetime = odoupdatetime + 1 / cfg.odoupdaterate;
end
end
%% save data
% write navresult to file
nav = zeros(11, 1);
nav(2, 1) = navstate.time;
nav(3:5, 1) = [navstate.pos(1) * param.R2D; navstate.pos(2) * param.R2D; navstate.pos(3)];
nav(6:8, 1) = navstate.vel;
nav(9:11, 1) = navstate.att * param.R2D;
fprintf(navfp, '%2d %12.6f %12.8f %12.8f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f \n', nav);
% write imu error, convert to common unit
imuerror = zeros(13, 1);
imuerror(1, 1) = navstate.time;
imuerror(2:4, 1) = navstate.gyrbias * param.R2D * 3600;
imuerror(5:7, 1) = navstate.accbias * 1e5;
imuerror(8:10, 1) = navstate.gyrscale * 1e6;
imuerror(11:13, 1) = navstate.accscale * 1e6;
fprintf(imuerrfp, '%12.6f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f %8.4f \n', imuerror);
% write state std, convert to common unit
std = zeros(1, 22);
std(1) = navstate.time;
for idx=1:21
std(idx + 1) = sqrt(kf.P(idx, idx));
end
std(8:10) = std(8:10) * param.R2D;
std(11:13) = std(11:13) * param.R2D *3600;
std(14:16) = std(14:16) * 1e5;
std(17:22) = std(17:22) * 1e6;
fprintf(stdfp, '%12.6f %8.6f %8.6f %8.6f %8.6f %8.6f %8.6f %8.6f %8.6f %8.6f %8.6f %8.6f %8.6f %8.6f %8.6f %8.6f %8.6f %8.6f %8.6f %8.6f %8.6f %8.6f \n', std);
%% print processing information
if (imuindex / size(imudata, 1) - lastprecent > 0.01)
disp("processing " + num2str(floor(imuindex * 100 / size(imudata, 1))) + " %!");
lastprecent = imuindex / size(imudata, 1);
end
end
% close file
fclose(imuerrfp);
fclose(navfp);
fclose(stdfp);
disp("GNSS/INS Integration Processing Finished!");