source: ntrip/trunk/BNC/src/RTCM/RTCM2Decoder.cpp@ 10599

Last change on this file since 10599 was 10599, checked in by stuerze, 8 days ago

ADDED: consideration of NAV type in all applications

File size: 12.7 KB
Line 
1// Part of BNC, a utility for retrieving decoding and
2// converting GNSS data streams from NTRIP broadcasters.
3//
4// Copyright (C) 2007
5// German Federal Agency for Cartography and Geodesy (BKG)
6// http://www.bkg.bund.de
7// Czech Technical University Prague, Department of Geodesy
8// http://www.fsv.cvut.cz
9//
10// Email: euref-ip@bkg.bund.de
11//
12// This program is free software; you can redistribute it and/or
13// modify it under the terms of the GNU General Public License
14// as published by the Free Software Foundation, version 2.
15//
16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with this program; if not, write to the Free Software
23// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25/* -------------------------------------------------------------------------
26 * BKG NTRIP Client
27 * -------------------------------------------------------------------------
28 *
29 * Class: RTCM2Decoder
30 *
31 * Purpose: RTCM2 Decoder
32 *
33 * Author: L. Mervart
34 *
35 * Created: 24-Aug-2006
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <math.h>
42#include <sstream>
43#include <iomanip>
44#include <set>
45
46#include "../bncutils.h"
47#include "rtcm_utils.h"
48#include "RTCM2Decoder.h"
49
50using namespace std;
51using namespace rtcm2;
52
53//
54// Constructor
55//
56
57RTCM2Decoder::RTCM2Decoder(const std::string& ID) : _ephUser(true) {
58 _ID = ID;
59}
60
61//
62// Destructor
63//
64
65RTCM2Decoder::~RTCM2Decoder() {
66}
67
68//
69t_irc RTCM2Decoder::getStaCrd(double& xx, double& yy, double& zz) {
70 if (!_msg03.validMsg) {
71 return failure;
72 }
73
74 xx = _msg03.x + (_msg22.validMsg ? _msg22.dL1[0] : 0.0);
75 yy = _msg03.y + (_msg22.validMsg ? _msg22.dL1[1] : 0.0);
76 zz = _msg03.z + (_msg22.validMsg ? _msg22.dL1[2] : 0.0);
77
78 return success;
79}
80
81//
82t_irc RTCM2Decoder::getStaCrd(double& xx, double& yy, double& zz, double& dx1,
83 double& dy1, double& dz1, double& dx2, double& dy2, double& dz2) {
84 xx = _msg03.x;
85 yy = _msg03.y;
86 zz = _msg03.z;
87
88 dx1 = (_msg22.validMsg ? _msg22.dL1[0] : 0.0);
89 dy1 = (_msg22.validMsg ? _msg22.dL1[1] : 0.0);
90 dz1 = (_msg22.validMsg ? _msg22.dL1[2] : 0.0);
91
92 dx2 = (_msg22.validMsg ? _msg22.dL2[0] : 0.0);
93 dy2 = (_msg22.validMsg ? _msg22.dL2[1] : 0.0);
94 dz2 = (_msg22.validMsg ? _msg22.dL2[2] : 0.0);
95
96 return success;
97}
98
99//
100t_irc RTCM2Decoder::Decode(char* buffer, int bufLen, vector<string>& errmsg) {
101
102 errmsg.clear();
103
104 _buffer.append(buffer, bufLen);
105 int refWeek;
106 double refSecs;
107 currentGPSWeeks(refWeek, refSecs);
108 bool decoded = false;
109
110 while (true) {
111 _PP.getPacket(_buffer);
112 if (!_PP.valid()) {
113 if (decoded) {
114 return success;
115 } else {
116 return failure;
117 }
118 }
119
120 // Store message number
121 _typeList.push_back(_PP.ID());
122
123 if (_PP.ID() == 18 || _PP.ID() == 19) {
124
125 _ObsBlock.extract(_PP);
126
127 if (_ObsBlock.valid()) {
128 decoded = true;
129
130 int epochWeek;
131 double epochSecs;
132 _ObsBlock.resolveEpoch(refWeek, refSecs, epochWeek, epochSecs);
133
134 for (int iSat = 0; iSat < _ObsBlock.nSat; iSat++) {
135 t_satObs obs;
136 char sys;
137 int num, flag;
138 if (_ObsBlock.PRN[iSat] > 100) {
139 sys = 'R';
140 num = _ObsBlock.PRN[iSat] % 100;
141 }
142 else {
143 sys = 'G';
144 num = _ObsBlock.PRN[iSat];
145 }
146 flag = t_corrSSR::getSsrNavTypeFlag(sys, num);
147 obs._prn.set(sys, num, flag);
148 obs._time.set(epochWeek, epochSecs);
149
150 t_frqObs* frqObs1C = new t_frqObs;
151 frqObs1C->_rnxType2ch = "1C";
152 frqObs1C->_codeValid = true;
153 frqObs1C->_code = _ObsBlock.rng_C1[iSat];
154 obs._obs.push_back(frqObs1C);
155
156 t_frqObs* frqObs1P = new t_frqObs;
157 frqObs1P->_rnxType2ch = (sys == 'G') ? "1W" : "1P";
158 frqObs1P->_codeValid = true;
159 frqObs1P->_code = _ObsBlock.rng_P1[iSat];
160 frqObs1P->_phaseValid = true;
161 frqObs1P->_phase = _ObsBlock.resolvedPhase_L1(iSat);
162 frqObs1P->_slipCounter = _ObsBlock.slip_L1[iSat];
163 obs._obs.push_back(frqObs1P);
164
165 t_frqObs* frqObs2P = new t_frqObs;
166 frqObs2P->_rnxType2ch = (sys == 'G') ? "2W" : "2P";
167 frqObs2P->_codeValid = true;
168 frqObs2P->_code = _ObsBlock.rng_P2[iSat];
169 frqObs2P->_phaseValid = true;
170 frqObs2P->_phase = _ObsBlock.resolvedPhase_L2(iSat);
171 frqObs2P->_slipCounter = _ObsBlock.slip_L2[iSat];
172 obs._obs.push_back(frqObs2P);
173
174 _obsList.push_back(obs);
175 }
176 _ObsBlock.clear();
177 }
178 }
179
180 else if (_PP.ID() == 20 || _PP.ID() == 21) {
181 _msg2021.extract(_PP);
182
183 if (_msg2021.valid()) {
184 decoded = true;
185 translateCorr2Obs(errmsg);
186 }
187 }
188
189 else if (_PP.ID() == 3) {
190 _msg03.extract(_PP);
191 }
192
193 else if (_PP.ID() == 22) {
194 _msg22.extract(_PP);
195 }
196
197 else if (_PP.ID() == 23) {
198 _msg23.extract(_PP);
199 }
200
201 else if (_PP.ID() == 24) {
202 _msg24.extract(_PP);
203 }
204
205 // Output for RTCM scan
206 if (_PP.ID() == 3) {
207 if (_msg03.validMsg) {
208 _antList.push_back(t_antRefPoint());
209
210 this->getStaCrd(_antList.back().xx, _antList.back().yy, _antList.back().zz);
211
212 _antList.back().type = t_antRefPoint::APC;
213 _antList.back().message = _PP.ID();
214 }
215 } else if (_PP.ID() == 23) {
216 if (_msg23.validMsg) {
217 int antlen = strlen(_msg23.antType.c_str());
218 int serlen = strlen(_msg23.antSN.c_str());
219 if ((antlen) &&
220 (_antType.empty() || strncmp(_antType.back().descriptor, _msg23.antType.c_str(), antlen) != 0)) {
221 _antType.push_back(t_antInfo());
222 memcpy(_antType.back().descriptor, _msg23.antType.c_str(), antlen);
223 _antType.back().descriptor[antlen] = 0;
224 if (serlen) {
225 memcpy(_antType.back().serialnumber, _msg23.antSN.c_str(), serlen);
226 _antType.back().serialnumber[serlen] = 0;
227 }
228 }
229 }
230 } else if (_PP.ID() == 24) {
231 if (_msg24.validMsg) {
232 _antList.push_back(t_antRefPoint());
233
234 _antList.back().xx = _msg24.x;
235 _antList.back().yy = _msg24.y;
236 _antList.back().zz = _msg24.z;
237
238 _antList.back().height_f = true;
239 _antList.back().height = _msg24.h;
240
241 _antList.back().type = t_antRefPoint::ARP;
242 _antList.back().message = _PP.ID();
243 }
244 }
245 }
246 return success;
247}
248
249void RTCM2Decoder::translateCorr2Obs(vector<string>& errmsg) {
250
251 QMutexLocker locker(&_mutex);
252
253 if (!_msg03.validMsg || !_msg2021.valid()) {
254 return;
255 }
256
257 double stax = _msg03.x + (_msg22.validMsg ? _msg22.dL1[0] : 0.0);
258 double stay = _msg03.y + (_msg22.validMsg ? _msg22.dL1[1] : 0.0);
259 double staz = _msg03.z + (_msg22.validMsg ? _msg22.dL1[2] : 0.0);
260
261 int refWeek;
262 double refSecs;
263 currentGPSWeeks(refWeek, refSecs);
264
265 // Resolve receiver time of measurement (see RTCM 2.3, page 4-42, Message 18, Note 1)
266 // ----------------------------------------------------------------------------------
267 double hoursec_est = _msg2021.hoursec(); // estimated time of measurement
268 double hoursec_rcv = rint(hoursec_est * 1e2) / 1e2; // receiver clock reading at hoursec_est
269 double rcv_clk_bias = (hoursec_est - hoursec_rcv) * c_light;
270
271 int GPSWeek;
272 double GPSWeeks;
273 resolveEpoch(hoursec_est, refWeek, refSecs, GPSWeek, GPSWeeks);
274
275 int GPSWeek_rcv;
276 double GPSWeeks_rcv;
277 resolveEpoch(hoursec_rcv, refWeek, refSecs, GPSWeek_rcv, GPSWeeks_rcv);
278
279 // Loop over all satellites
280 // ------------------------
281 for (RTCM2_2021::data_iterator icorr = _msg2021.data.begin();
282 icorr != _msg2021.data.end(); icorr++) {
283 const RTCM2_2021::HiResCorr* corr = icorr->second;
284
285 // beg test
286 if (corr->PRN >= 200) {
287 continue;
288 }
289 // end test
290
291 t_prn prn;
292 char sys;
293 int num, flag;
294 if (corr->PRN < 200) {
295 sys = 'G';
296 num = corr->PRN;
297 }
298 else {
299 sys = 'R';
300 num = corr->PRN - 200;
301 }
302 flag = t_corrSSR::getSsrNavTypeFlag(sys, num);
303 prn.set(sys, num, flag);
304
305 double L1 = 0;
306 double L2 = 0;
307 double P1 = 0;
308 double P2 = 0;
309 string obsT = "";
310
311 // new observation
312 t_satObs new_obs;
313
314 t_frqObs* frqObs1C = new t_frqObs;
315 frqObs1C->_rnxType2ch = "1C";
316 new_obs._obs.push_back(frqObs1C);
317
318 t_frqObs* frqObs1P = new t_frqObs;
319 frqObs1P->_rnxType2ch = (sys == 'G') ? "1W" : "1P";
320 new_obs._obs.push_back(frqObs1P);
321
322 t_frqObs* frqObs2P = new t_frqObs;
323 frqObs2P->_rnxType2ch = (sys == 'G') ? "2W" : "2P";
324 new_obs._obs.push_back(frqObs2P);
325
326 // missing IOD
327 vector<string> missingIOD;
328 vector<string> hasIOD;
329 for (unsigned ii = 0; ii < 4; ii++) {
330 unsigned int IODcorr = 0;
331 double corrVal = 0;
332 const t_eph* eph = 0;
333 double* obsVal = 0;
334
335 switch (ii) {
336 case 0: // --- L1 ---
337 IODcorr = corr->IODp1;
338 corrVal = corr->phase1 * LAMBDA_1;
339 obsVal = &L1;
340 obsT = "L1";
341 break;
342 case 1: // --- L2 ---
343 IODcorr = corr->IODp2;
344 corrVal = corr->phase2 * LAMBDA_2;
345 obsVal = &L2;
346 obsT = "L2";
347 break;
348 case 2: // --- P1 ---
349 IODcorr = corr->IODr1;
350 corrVal = corr->range1;
351 obsVal = &P1;
352 obsT = "P1";
353 break;
354 case 3: // --- P2 ---
355 IODcorr = corr->IODr2;
356 corrVal = corr->range2;
357 obsVal = &P2;
358 obsT = "P2";
359 break;
360 default:
361 continue;
362 }
363
364 // Select corresponding ephemerides
365 QString prnInternalStr(prn.toInternalString().c_str());
366 const t_eph* ephLast = _ephUser.ephLast(prnInternalStr);
367 const t_eph* ephPrev = _ephUser.ephPrev(prnInternalStr);
368 if (ephLast && ephLast->IOD() == IODcorr) {
369 eph = ephLast;
370 } else if (ephPrev && ephPrev->IOD() == IODcorr) {
371 eph = ephPrev;
372 }
373
374 if (eph) {
375 ostringstream msg;
376 msg << obsT << ':' << setw(3) << eph->IOD();
377 hasIOD.push_back(msg.str());
378
379 int GPSWeek_tot;
380 double GPSWeeks_tot;
381 double rho, xSat, ySat, zSat, clkSat;
382 cmpRho(eph, stax, stay, staz, GPSWeek, GPSWeeks, rho, GPSWeek_tot,
383 GPSWeeks_tot, xSat, ySat, zSat, clkSat);
384
385 *obsVal = rho - corrVal + rcv_clk_bias - clkSat;
386
387 if (*obsVal == 0)
388 *obsVal = ZEROVALUE;
389 char sys;
390 int num, flag;
391 if (corr->PRN < 200) {
392 sys = 'G';
393 num = corr->PRN;
394 }
395 else {
396 sys = 'R';
397 num = corr->PRN - 200;
398 }
399 flag = t_corrSSR::getSsrNavTypeFlag(sys, num);
400 new_obs._prn.set(sys, num, flag);
401 new_obs._time.set(GPSWeek_rcv, GPSWeeks_rcv);
402
403 // Store estimated measurements
404 // ----------------------------
405 switch (ii) {
406 case 0: // --- L1 ---
407 frqObs1P->_phaseValid = true;
408 frqObs1P->_phase = *obsVal / LAMBDA_1;
409 //frqObs1P->_slipCounter = corr->lock1;
410 frqObs1P->_slipCounter = -1; // because RTCM2 definition is vice versa to RTCM3
411 break;
412 case 1: // --- L2 ---
413 frqObs2P->_phaseValid = true;
414 frqObs2P->_phase = *obsVal / LAMBDA_2;
415 //frqObs2P->_slipCounter = corr->lock2;
416 frqObs2P->_slipCounter = -1; // because RTCM2 definition is vice versa to RTCM3
417 break;
418 case 2: // --- C1 / P1 ---
419 if (corr->Pind1) {
420 frqObs1P->_codeValid = true;
421 frqObs1P->_code = *obsVal;
422 } else {
423 frqObs1C->_codeValid = true;
424 frqObs1C->_code = *obsVal;
425 }
426 break;
427 case 3: // --- C2 / P2 ---
428 if (corr->Pind2) {
429 frqObs2P->_codeValid = true;
430 frqObs2P->_code = *obsVal;
431 }
432 break;
433 default:
434 continue;
435 }
436 } else if (IODcorr != 0) {
437 ostringstream msg;
438 msg << obsT << ':' << setw(3) << IODcorr;
439 missingIOD.push_back(msg.str());
440 }
441 } // loop over frequencies
442
443 // Error report
444 if (missingIOD.size()) {
445 ostringstream missingIODstr;
446
447 copy(missingIOD.begin(), missingIOD.end(),
448 ostream_iterator<string>(missingIODstr, " "));
449
450 errmsg.push_back(
451 "missing eph for " + prn.toString() + " , IODs "
452 + missingIODstr.str());
453 }
454
455 // Store new observation
456 if (new_obs._time.mjd() > 0) {
457 _obsList.push_back(new_obs);
458 }
459 }
460}
Note: See TracBrowser for help on using the repository browser.