1 | // converting GNSS data streams from NTRIP broadcasters. |
---|
2 | // |
---|
3 | // Copyright (C) 2007 |
---|
4 | // German Federal Agency for Cartography and Geodesy (BKG) |
---|
5 | // http://www.bkg.bund.de |
---|
6 | // Czech Technical University Prague, Department of Geodesy |
---|
7 | // http://www.fsv.cvut.cz |
---|
8 | // |
---|
9 | // Email: euref-ip@bkg.bund.de |
---|
10 | // |
---|
11 | // This program is free software; you can redistribute it and/or |
---|
12 | // modify it under the terms of the GNU General Public License |
---|
13 | // as published by the Free Software Foundation, version 2. |
---|
14 | // |
---|
15 | // This program is distributed in the hope that it will be useful, |
---|
16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | // GNU General Public License for more details. |
---|
19 | // |
---|
20 | // You should have received a copy of the GNU General Public License |
---|
21 | // along with this program; if not, write to the Free Software |
---|
22 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
23 | |
---|
24 | /* ------------------------------------------------------------------------- |
---|
25 | * BKG NTRIP Client |
---|
26 | * ------------------------------------------------------------------------- |
---|
27 | * |
---|
28 | * Class: bncEphUser |
---|
29 | * |
---|
30 | * Purpose: Base for Classes that use Ephemerides |
---|
31 | * |
---|
32 | * Author: L. Mervart |
---|
33 | * |
---|
34 | * Created: 27-Jan-2011 |
---|
35 | * |
---|
36 | * Changes: |
---|
37 | * |
---|
38 | * -----------------------------------------------------------------------*/ |
---|
39 | |
---|
40 | #include <cmath> |
---|
41 | #include <iostream> |
---|
42 | |
---|
43 | #include "bncephuser.h" |
---|
44 | #include "bnccore.h" |
---|
45 | |
---|
46 | using namespace std; |
---|
47 | |
---|
48 | // Constructor |
---|
49 | //////////////////////////////////////////////////////////////////////////// |
---|
50 | bncEphUser::bncEphUser(bool connectSlots) { |
---|
51 | if (connectSlots) { |
---|
52 | connect(BNC_CORE, SIGNAL(newGPSEph(t_ephGPS)), |
---|
53 | this, SLOT(slotNewGPSEph(t_ephGPS)), Qt::DirectConnection); |
---|
54 | |
---|
55 | connect(BNC_CORE, SIGNAL(newGlonassEph(t_ephGlo)), |
---|
56 | this, SLOT(slotNewGlonassEph(t_ephGlo)), Qt::DirectConnection); |
---|
57 | |
---|
58 | connect(BNC_CORE, SIGNAL(newGalileoEph(t_ephGal)), |
---|
59 | this, SLOT(slotNewGalileoEph(t_ephGal)), Qt::DirectConnection); |
---|
60 | |
---|
61 | connect(BNC_CORE, SIGNAL(newSBASEph(t_ephSBAS)), |
---|
62 | this, SLOT(slotNewSBASEph(t_ephSBAS)), Qt::DirectConnection); |
---|
63 | |
---|
64 | connect(BNC_CORE, SIGNAL(newBDSEph(t_ephBDS)), |
---|
65 | this, SLOT(slotNewBDSEph(t_ephBDS)), Qt::DirectConnection); |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | // Destructor |
---|
70 | //////////////////////////////////////////////////////////////////////////// |
---|
71 | bncEphUser::~bncEphUser() { |
---|
72 | QMapIterator<QString, deque<t_eph*> > it(_eph); |
---|
73 | while (it.hasNext()) { |
---|
74 | it.next(); |
---|
75 | const deque<t_eph*>& qq = it.value(); |
---|
76 | for (unsigned ii = 0; ii < qq.size(); ii++) { |
---|
77 | delete qq[ii]; |
---|
78 | } |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | // New GPS Ephemeris |
---|
83 | //////////////////////////////////////////////////////////////////////////// |
---|
84 | void bncEphUser::slotNewGPSEph(t_ephGPS eph) { |
---|
85 | putNewEph(&eph, true); |
---|
86 | } |
---|
87 | |
---|
88 | // New Glonass Ephemeris |
---|
89 | //////////////////////////////////////////////////////////////////////////// |
---|
90 | void bncEphUser::slotNewGlonassEph(t_ephGlo eph) { |
---|
91 | putNewEph(&eph, true); |
---|
92 | } |
---|
93 | |
---|
94 | // New Galileo Ephemeris |
---|
95 | //////////////////////////////////////////////////////////////////////////// |
---|
96 | void bncEphUser::slotNewGalileoEph(t_ephGal eph) { |
---|
97 | putNewEph(&eph, true); |
---|
98 | } |
---|
99 | |
---|
100 | // New SBAS Ephemeris |
---|
101 | //////////////////////////////////////////////////////////////////////////// |
---|
102 | void bncEphUser::slotNewSBASEph(t_ephSBAS eph) { |
---|
103 | putNewEph(&eph, true); |
---|
104 | } |
---|
105 | |
---|
106 | // New BDS Ephemeris |
---|
107 | //////////////////////////////////////////////////////////////////////////// |
---|
108 | void bncEphUser::slotNewBDSEph(t_ephBDS eph) { |
---|
109 | putNewEph(&eph, true); |
---|
110 | } |
---|
111 | |
---|
112 | // |
---|
113 | //////////////////////////////////////////////////////////////////////////// |
---|
114 | t_irc bncEphUser::putNewEph(t_eph* eph, bool realTime) { |
---|
115 | |
---|
116 | QMutexLocker locker(&_mutex); |
---|
117 | |
---|
118 | if (eph == 0) { |
---|
119 | return failure; |
---|
120 | } |
---|
121 | |
---|
122 | const t_ephGPS* ephGPS = dynamic_cast<const t_ephGPS*>(eph); |
---|
123 | const t_ephGlo* ephGlo = dynamic_cast<const t_ephGlo*>(eph); |
---|
124 | const t_ephGal* ephGal = dynamic_cast<const t_ephGal*>(eph); |
---|
125 | const t_ephSBAS* ephSBAS = dynamic_cast<const t_ephSBAS*>(eph); |
---|
126 | const t_ephBDS* ephBDS = dynamic_cast<const t_ephBDS*>(eph); |
---|
127 | |
---|
128 | t_eph* newEph = 0; |
---|
129 | |
---|
130 | if (ephGPS) { |
---|
131 | newEph = new t_ephGPS(*ephGPS); |
---|
132 | } |
---|
133 | else if (ephGlo) { |
---|
134 | newEph = new t_ephGlo(*ephGlo); |
---|
135 | } |
---|
136 | else if (ephGal) { |
---|
137 | newEph = new t_ephGal(*ephGal); |
---|
138 | } |
---|
139 | else if (ephSBAS) { |
---|
140 | newEph = new t_ephSBAS(*ephSBAS); |
---|
141 | } |
---|
142 | else if (ephBDS) { |
---|
143 | newEph = new t_ephBDS(*ephBDS); |
---|
144 | } |
---|
145 | else { |
---|
146 | return failure; |
---|
147 | } |
---|
148 | |
---|
149 | QString prn(newEph->prn().toInternalString().c_str()); |
---|
150 | |
---|
151 | const t_eph* ephOld = ephLast(prn); |
---|
152 | |
---|
153 | if (ephOld && |
---|
154 | (ephOld->checkState() == t_eph::bad || |
---|
155 | ephOld->checkState() == t_eph::outdated || |
---|
156 | ephOld->checkState() == t_eph::unhealthy)) { |
---|
157 | ephOld = 0; |
---|
158 | } |
---|
159 | |
---|
160 | if (ephOld == 0 || newEph->isNewerThan(ephOld)) { |
---|
161 | checkEphemeris(newEph, realTime); |
---|
162 | eph->setCheckState(newEph->checkState()); |
---|
163 | } |
---|
164 | else { |
---|
165 | delete newEph; |
---|
166 | return failure; |
---|
167 | } |
---|
168 | |
---|
169 | if (newEph->checkState() != t_eph::bad && |
---|
170 | newEph->checkState() != t_eph::outdated) { |
---|
171 | deque<t_eph*>& qq = _eph[prn]; |
---|
172 | if (qq.empty() || |
---|
173 | newEph->isNewerThan(qq.back())){ |
---|
174 | qq.push_back(newEph); |
---|
175 | if (qq.size() > _maxQueueSize) { |
---|
176 | delete qq.front(); |
---|
177 | qq.pop_front(); |
---|
178 | } |
---|
179 | ephBufferChanged(); |
---|
180 | return success; |
---|
181 | } |
---|
182 | else { |
---|
183 | delete newEph; |
---|
184 | return failure; |
---|
185 | } |
---|
186 | |
---|
187 | } |
---|
188 | else { |
---|
189 | delete newEph; |
---|
190 | return failure; |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | // |
---|
195 | //////////////////////////////////////////////////////////////////////////// |
---|
196 | void bncEphUser::checkEphemeris(t_eph* eph, bool realTime) { |
---|
197 | |
---|
198 | if (!eph) { |
---|
199 | return; |
---|
200 | } |
---|
201 | |
---|
202 | // Check health status |
---|
203 | // ------------------- |
---|
204 | if (eph->isUnhealthy()) { |
---|
205 | eph->setCheckState(t_eph::unhealthy); |
---|
206 | return; |
---|
207 | } |
---|
208 | |
---|
209 | // Simple Check - check satellite radial distance |
---|
210 | // ---------------------------------------------- |
---|
211 | ColumnVector xc(6); |
---|
212 | ColumnVector vv(3); |
---|
213 | if (eph->getCrd(eph->TOC(), xc, vv, false) != success) { |
---|
214 | eph->setCheckState(t_eph::bad); |
---|
215 | return; |
---|
216 | } |
---|
217 | double rr = xc.Rows(1,3).norm_Frobenius(); |
---|
218 | |
---|
219 | const double MINDIST = 2.e7; |
---|
220 | const double MAXDIST = 6.e7; |
---|
221 | if (rr < MINDIST || rr > MAXDIST || std::isnan(rr)) { |
---|
222 | eph->setCheckState(t_eph::bad); |
---|
223 | return; |
---|
224 | } |
---|
225 | |
---|
226 | // Check whether the epoch is too far away the current time |
---|
227 | // -------------------------------------------------------- |
---|
228 | if (realTime) { |
---|
229 | bncTime toc = eph->TOC(); |
---|
230 | QDateTime now = currentDateAndTimeGPS(); |
---|
231 | bncTime currentTime(now.toString(Qt::ISODate).toStdString()); |
---|
232 | double dt = currentTime - toc; |
---|
233 | |
---|
234 | // update interval: 2h, data sets are valid for 4 hours |
---|
235 | if (eph->type() == t_eph::GPS && (dt > 4*3600.0 || dt < -2*3600.0)) { |
---|
236 | eph->setCheckState(t_eph::outdated); |
---|
237 | return; |
---|
238 | } |
---|
239 | // update interval: 3h, data sets are valid for 4 hours |
---|
240 | else if (eph->type() == t_eph::Galileo && (dt > 4*3600.0 || dt < 0.0)) { |
---|
241 | eph->setCheckState(t_eph::outdated); |
---|
242 | return; |
---|
243 | } |
---|
244 | // updated every 30 minutes |
---|
245 | else if (eph->type() == t_eph::GLONASS && (dt > 1*3600.0 || dt < -1800.0)) { |
---|
246 | eph->setCheckState(t_eph::outdated); |
---|
247 | return; |
---|
248 | } |
---|
249 | // orbit parameters are valid for 7200 seconds (minimum) |
---|
250 | else if (eph->type() == t_eph::QZSS && (dt > 2*3600.0 || dt < -1*3600.0)) { |
---|
251 | eph->setCheckState(t_eph::outdated); |
---|
252 | return; |
---|
253 | } |
---|
254 | // maximum update interval: 300 sec |
---|
255 | else if (eph->type() == t_eph::SBAS && (dt > 600 || dt < -300.0)) { |
---|
256 | eph->setCheckState(t_eph::outdated); |
---|
257 | return; |
---|
258 | } |
---|
259 | // updates 1h (GEO) up to 6 hours non-GEO |
---|
260 | else if (eph->type() == t_eph::BDS && (dt > 6*3600 || dt < 0.0)) { |
---|
261 | eph->setCheckState(t_eph::outdated); |
---|
262 | return; |
---|
263 | } |
---|
264 | // update interval: up to 24 hours |
---|
265 | else if (eph->type() == t_eph::IRNSS && fabs(dt > 24*3600.0)) { |
---|
266 | eph->setCheckState(t_eph::outdated); |
---|
267 | return; |
---|
268 | } |
---|
269 | } |
---|
270 | |
---|
271 | // Check consistency with older ephemeris |
---|
272 | // -------------------------------------- |
---|
273 | const double MAXDIFF = 10.0; |
---|
274 | const double MINDIFF = 0.005; |
---|
275 | QString prn = QString(eph->prn().toInternalString().c_str()); |
---|
276 | t_eph* ephL = ephLast(prn); |
---|
277 | |
---|
278 | if (ephL) { |
---|
279 | ColumnVector xcL(6); |
---|
280 | ColumnVector vvL(3); |
---|
281 | ephL->getCrd(eph->TOC(), xcL, vvL, false); |
---|
282 | double dt = eph->TOC() - ephL->TOC(); |
---|
283 | if (dt < 0.0) { |
---|
284 | dt += 604800.0; |
---|
285 | } |
---|
286 | double diff = (xc.Rows(1,3) - xcL.Rows(1,3)).norm_Frobenius(); |
---|
287 | double diffC = fabs(xc(4) - xcL(4)) * t_CST::c; |
---|
288 | |
---|
289 | if (diff < MAXDIFF && diffC < MAXDIFF) { |
---|
290 | if (diff < MINDIFF && diffC < MINDIFF && ephL->checkState() == t_eph::ok) { |
---|
291 | // to prevent the same data sets with different TOC values |
---|
292 | eph->setCheckState(t_eph::bad); |
---|
293 | } |
---|
294 | else { |
---|
295 | eph->setCheckState(t_eph::ok); |
---|
296 | ephL->setCheckState(t_eph::ok); |
---|
297 | } |
---|
298 | } |
---|
299 | else { |
---|
300 | if (ephL->checkState() == t_eph::ok) { |
---|
301 | eph->setCheckState(t_eph::bad); |
---|
302 | } |
---|
303 | } |
---|
304 | // some lines to allow update of ephemeris data sets after an outage |
---|
305 | // update interval: 2h, |
---|
306 | if (eph->type() == t_eph::GPS && dt > (2*3600.0 + 60.0)) { |
---|
307 | ephL->setCheckState(t_eph::outdated); |
---|
308 | return; |
---|
309 | } |
---|
310 | // update interval: 3h, |
---|
311 | else if (eph->type() == t_eph::Galileo && dt > (3*3600.0 + 60.0)) { |
---|
312 | ephL->setCheckState(t_eph::outdated); |
---|
313 | return; |
---|
314 | } |
---|
315 | // updated every 30 minutes |
---|
316 | else if (eph->type() == t_eph::GLONASS && dt > (1800.0 + 60.0)) { |
---|
317 | ephL->setCheckState(t_eph::outdated); |
---|
318 | return; |
---|
319 | } |
---|
320 | // updated every ? |
---|
321 | else if (eph->type() == t_eph::QZSS && dt > (2*3600.0 + 60.0)) { |
---|
322 | ephL->setCheckState(t_eph::outdated); |
---|
323 | return; |
---|
324 | } |
---|
325 | // maximum update interval: 300 sec |
---|
326 | else if (eph->type() == t_eph::SBAS && dt > ( 300.0)) { |
---|
327 | ephL->setCheckState(t_eph::outdated); |
---|
328 | return; |
---|
329 | } |
---|
330 | // updates 1h (GEO) up to 6 hours non-GEO |
---|
331 | else if (eph->type() == t_eph::BDS && dt > 6*3600.0) { |
---|
332 | ephL->setCheckState(t_eph::outdated); |
---|
333 | return; |
---|
334 | } |
---|
335 | // update interval: up to 24 hours |
---|
336 | else if (eph->type() == t_eph::IRNSS && dt > 24*3600.0) { |
---|
337 | ephL->setCheckState(t_eph::outdated); |
---|
338 | return; |
---|
339 | } |
---|
340 | } |
---|
341 | } |
---|