forked from mfkiwl/OpenRTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSPP_5State_KF.m
177 lines (137 loc) · 5.26 KB
/
SPP_5State_KF.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
% LoadData;
idx_E = ([S.SvSystemID_t_eph] >= 17664) & ([S.SvSystemID_t_eph] <= 17664+255); %17664 is 69 ('E') << 8
SvId_E = unique(S.SvSystemID_t_eph(idx_E));
number_E = numel(unique(S.SvSystemID_t_eph(idx_E)));
ctr = 1;
idx_Galileo = cellfun(@(x)strcmp(x(1), 'E'), S.SvSystem_t_eph);
idx_GPS = cellfun(@(x)strcmp(x(1), 'G'), S.SvSystem_t_eph);
% KF Initialization
nStates = 5;
F = eye(nStates);
Q = diag([0.01 0.01 0.01 1e-3 1e-9]);
kf = Kalman.Kalman(F, Q);
kf.SetInitialStates([station_pos__m'; 0; 0]); % ECEF Position [m]
% Receiver Clock Error * c [m]
% Zenith Troposphere Delay [m]
pos = station_pos__m;
cdr = 0;
zdt = 0;
P = eye(nStates);
P_pri = eye(nStates);
P_post = eye(nStates);
X_pri = kf.X.';
X_post = kf.X.';
KfIterations = 2;
timestamps = unique(S.ObsToc);
for tt = timestamps.'
idx = S.ObsToc == tt;
kf.Predict();
X_pri(ctr,:) = kf.X.';
P_pri(:,:,ctr) = kf.P;
% filter out unhealthy satellites
idx = idx & S.Health == 0;
% Indices
code_idx = idx & (idx_Galileo | idx_GPS) & ...
(S.Code_1 > 1e3) & (S.Code_1 > 1e3) & ... % valid Pseudoranges
((S.Band == 1) | (S.Band == 5)); % E1 and E5
code_1_idx = code_idx & (S.Band == 1);
code_5_idx = code_idx & (S.Band == 5);
[ia,ib] = ismember(S.SvSystemID_t_eph(code_1_idx), S.SvSystemID_t_eph(code_5_idx));
tmp = find(code_1_idx);
code_1_idx(tmp(~ia)) = 0;
for ii = 1:KfIterations
POS = kf.X(1:3);
CDTR = kf.X(4);
ZTD = kf.X(5);
% Calc Distance
dist = Vector.EuclidianDistance_3D(S.x(code_1_idx), S.y(code_1_idx), S.z(code_1_idx), ...
POS(1), POS(2), POS(3));
% Sagnac Correction
sag = Transformation.CalcSagnac( [S.x(code_1_idx), S.y(code_1_idx), S.z(code_1_idx)], ...
[POS(1), POS(2), POS(3)]);
geo_dist = dist + sag;
% distance Vector & normalized distance Vector
d = Vector.DistanceVector_3D(S.x(code_1_idx), S.y(code_1_idx), S.z(code_1_idx), ...
POS(1), POS(2), POS(3));
e = Vector.NormalizedDistanceVector(S.x(code_1_idx), S.y(code_1_idx), S.z(code_1_idx), ...
POS(1), POS(2), POS(3));
cdt_sv = Transformation.SpeedOfLight__mDs * S.SvClockOffset(code_1_idx);
cdt_rel = Transformation.SpeedOfLight__mDs * S.RelativisticError(code_1_idx);
% elevation mask
elevation = Generic.CalcElevation(POS(1), POS(2), POS(3), d(:,1), d(:,2), d(:,3));
idx_el = elevation > (15/180*pi) & elevation < (165/180*pi);
[lat__rad, lon__rad, alt__m] = Transformation.ecef2wgs84(POS(1), POS(2), POS(3));
d_dry = 2.3 .* exp(-0.116e-3 .* alt__m);
if isinf(d_dry)
d_dry = 1e9;
end
d_wet = 0.1 + ZTD;
M_E = 1.001./sqrt(0.002001 + sin(elevation).^2);
tropo_offset = (d_dry + d_wet) .* M_E;
% Iono-Free LC
rho_iono_free = Generic.CalcIonoFreeLinearCombination(S.Code_1(code_1_idx), S.Code_5(code_5_idx), F_E1_Galileo__Hz, F_E5a_Galileo__Hz);
pseudorange_est = rho_iono_free + cdt_sv + cdt_rel - CDTR - tropo_offset;
H = [-e, ones(numel(e(:,1)),1), M_E];
dy = pseudorange_est - geo_dist;
if ii < KfIterations
kf.CorrectIterativeEKF(H, dy, eye(numel(dy)));
else
kf.CorrectEKF(H, dy, eye(numel(dy)));
end
end
pos(ctr,:) = kf.X(1:3);
cdr(ctr,:) = kf.X(4);
ztd(ctr,:) = kf.X(5);
P(ctr,:) = diag(kf.P)';
% for RTS
X_post(ctr,:) = kf.X.';
P_post(:,:,ctr) = kf.P;
ctr = ctr+1;
end
% ReadRtkPos;
% return;
%%
afigure(42);
subplot(4,2,1);
hold on; grid on;
yline(station_pos__m(1), 'Color', Color.BLACK, 'HandleVisibility','off')
plot(pos(:,1), 'DisplayName', 'EKF (5 States)')
ylim([station_pos__m(1)-50, station_pos__m(1)+50])
legend('show')
subplot(4,2,2);
hold on; grid on;
plot(station_pos__m(1) - pos(:,1), 'DisplayName', 'EKF (5 States)')
ylim([-20 20])
legend('show')
subplot(4,2,3);
hold on; grid on;
yline(station_pos__m(2), 'Color', Color.BLACK, 'HandleVisibility','off')
plot(pos(:,2), 'DisplayName', 'EKF (5 States)')
ylim([station_pos__m(2)-50, station_pos__m(2)+50])
legend('show')
subplot(4,2,4);
hold on; grid on;
plot(station_pos__m(2) - pos(:,2), 'DisplayName', 'EKF (5 States)')
ylim([-20 20])
legend('show')
subplot(4,2,5);
hold on; grid on;
yline(station_pos__m(3), 'Color', Color.BLACK, 'HandleVisibility','off')
plot(pos(:,3), 'DisplayName', 'EKF (5 States)')
ylim([station_pos__m(3)-50, station_pos__m(3)+50])
legend('show')
subplot(4,2,6);
hold on; grid on;
plot(station_pos__m(3) - pos(:,3), 'DisplayName', 'EKF (5 States)')
ylim([-20 20])
legend('show')
subplot(4,2,7);
hold on; grid on;
title('dt_r')
plot(cdr ./ Transformation.SpeedOfLight__mDs, 'DisplayName', 'EKF (5 States)')
legend('show')
subplot(4,2,8);
hold on; grid on;
title('ZTD_w')
plot(ztd, 'DisplayName', 'EKF (5 States)')
legend('show')