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

Last change on this file since 10689 was 10688, checked in by stuerze, 3 weeks ago

RTCM message 1230 (on-change) and the size of each RTCM message is added in scanRTCM

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(t_typeInfo());
122 _typeList.back().type = _PP.ID();
123 //_typeList.back().size = 0;
124
125 if (_PP.ID() == 18 || _PP.ID() == 19) {
126
127 _ObsBlock.extract(_PP);
128
129 if (_ObsBlock.valid()) {
130 decoded = true;
131
132 int epochWeek;
133 double epochSecs;
134 _ObsBlock.resolveEpoch(refWeek, refSecs, epochWeek, epochSecs);
135
136 for (int iSat = 0; iSat < _ObsBlock.nSat; iSat++) {
137 t_satObs obs;
138 char sys;
139 int num, flag;
140 if (_ObsBlock.PRN[iSat] > 100) {
141 sys = 'R';
142 num = _ObsBlock.PRN[iSat] % 100;
143 }
144 else {
145 sys = 'G';
146 num = _ObsBlock.PRN[iSat];
147 }
148 flag = t_corrSSR::getSsrNavTypeFlag(sys, num);
149 obs._prn.set(sys, num, flag);
150 obs._time.set(epochWeek, epochSecs);
151
152 t_frqObs* frqObs1C = new t_frqObs;
153 frqObs1C->_rnxType2ch = "1C";
154 frqObs1C->_codeValid = true;
155 frqObs1C->_code = _ObsBlock.rng_C1[iSat];
156 obs._obs.push_back(frqObs1C);
157
158 t_frqObs* frqObs1P = new t_frqObs;
159 frqObs1P->_rnxType2ch = (sys == 'G') ? "1W" : "1P";
160 frqObs1P->_codeValid = true;
161 frqObs1P->_code = _ObsBlock.rng_P1[iSat];
162 frqObs1P->_phaseValid = true;
163 frqObs1P->_phase = _ObsBlock.resolvedPhase_L1(iSat);
164 frqObs1P->_slipCounter = _ObsBlock.slip_L1[iSat];
165 obs._obs.push_back(frqObs1P);
166
167 t_frqObs* frqObs2P = new t_frqObs;
168 frqObs2P->_rnxType2ch = (sys == 'G') ? "2W" : "2P";
169 frqObs2P->_codeValid = true;
170 frqObs2P->_code = _ObsBlock.rng_P2[iSat];
171 frqObs2P->_phaseValid = true;
172 frqObs2P->_phase = _ObsBlock.resolvedPhase_L2(iSat);
173 frqObs2P->_slipCounter = _ObsBlock.slip_L2[iSat];
174 obs._obs.push_back(frqObs2P);
175
176 _obsList.push_back(obs);
177 }
178 _ObsBlock.clear();
179 }
180 }
181
182 else if (_PP.ID() == 20 || _PP.ID() == 21) {
183 _msg2021.extract(_PP);
184
185 if (_msg2021.valid()) {
186 decoded = true;
187 translateCorr2Obs(errmsg);
188 }
189 }
190
191 else if (_PP.ID() == 3) {
192 _msg03.extract(_PP);
193 }
194
195 else if (_PP.ID() == 22) {
196 _msg22.extract(_PP);
197 }
198
199 else if (_PP.ID() == 23) {
200 _msg23.extract(_PP);
201 }
202
203 else if (_PP.ID() == 24) {
204 _msg24.extract(_PP);
205 }
206
207 // Output for RTCM scan
208 if (_PP.ID() == 3) {
209 if (_msg03.validMsg) {
210 _antList.push_back(t_antRefPoint());
211
212 this->getStaCrd(_antList.back().xx, _antList.back().yy, _antList.back().zz);
213
214 _antList.back().type = t_antRefPoint::APC;
215 _antList.back().message = _PP.ID();
216 }
217 } else if (_PP.ID() == 23) {
218 if (_msg23.validMsg) {
219 int antlen = strlen(_msg23.antType.c_str());
220 int serlen = strlen(_msg23.antSN.c_str());
221 if ((antlen) &&
222 (_antType.empty() || strncmp(_antType.back().descriptor, _msg23.antType.c_str(), antlen) != 0)) {
223 _antType.push_back(t_antInfo());
224 memcpy(_antType.back().descriptor, _msg23.antType.c_str(), antlen);
225 _antType.back().descriptor[antlen] = 0;
226 if (serlen) {
227 memcpy(_antType.back().serialnumber, _msg23.antSN.c_str(), serlen);
228 _antType.back().serialnumber[serlen] = 0;
229 }
230 }
231 }
232 } else if (_PP.ID() == 24) {
233 if (_msg24.validMsg) {
234 _antList.push_back(t_antRefPoint());
235
236 _antList.back().xx = _msg24.x;
237 _antList.back().yy = _msg24.y;
238 _antList.back().zz = _msg24.z;
239
240 _antList.back().height_f = true;
241 _antList.back().height = _msg24.h;
242
243 _antList.back().type = t_antRefPoint::ARP;
244 _antList.back().message = _PP.ID();
245 }
246 }
247 }
248 return success;
249}
250
251void RTCM2Decoder::translateCorr2Obs(vector<string>& errmsg) {
252
253 QMutexLocker locker(&_mutex);
254
255 if (!_msg03.validMsg || !_msg2021.valid()) {
256 return;
257 }
258
259 double stax = _msg03.x + (_msg22.validMsg ? _msg22.dL1[0] : 0.0);
260 double stay = _msg03.y + (_msg22.validMsg ? _msg22.dL1[1] : 0.0);
261 double staz = _msg03.z + (_msg22.validMsg ? _msg22.dL1[2] : 0.0);
262
263 int refWeek;
264 double refSecs;
265 currentGPSWeeks(refWeek, refSecs);
266
267 // Resolve receiver time of measurement (see RTCM 2.3, page 4-42, Message 18, Note 1)
268 // ----------------------------------------------------------------------------------
269 double hoursec_est = _msg2021.hoursec(); // estimated time of measurement
270 double hoursec_rcv = rint(hoursec_est * 1e2) / 1e2; // receiver clock reading at hoursec_est
271 double rcv_clk_bias = (hoursec_est - hoursec_rcv) * c_light;
272
273 int GPSWeek;
274 double GPSWeeks;
275 resolveEpoch(hoursec_est, refWeek, refSecs, GPSWeek, GPSWeeks);
276
277 int GPSWeek_rcv;
278 double GPSWeeks_rcv;
279 resolveEpoch(hoursec_rcv, refWeek, refSecs, GPSWeek_rcv, GPSWeeks_rcv);
280
281 // Loop over all satellites
282 // ------------------------
283 for (RTCM2_2021::data_iterator icorr = _msg2021.data.begin();
284 icorr != _msg2021.data.end(); icorr++) {
285 const RTCM2_2021::HiResCorr* corr = icorr->second;
286
287 // beg test
288 if (corr->PRN >= 200) {
289 continue;
290 }
291 // end test
292
293 t_prn prn;
294 char sys;
295 int num, flag;
296 if (corr->PRN < 200) {
297 sys = 'G';
298 num = corr->PRN;
299 }
300 else {
301 sys = 'R';
302 num = corr->PRN - 200;
303 }
304 flag = t_corrSSR::getSsrNavTypeFlag(sys, num);
305 prn.set(sys, num, flag);
306
307 double L1 = 0;
308 double L2 = 0;
309 double P1 = 0;
310 double P2 = 0;
311 string obsT = "";
312
313 // new observation
314 t_satObs new_obs;
315
316 t_frqObs* frqObs1C = new t_frqObs;
317 frqObs1C->_rnxType2ch = "1C";
318 new_obs._obs.push_back(frqObs1C);
319
320 t_frqObs* frqObs1P = new t_frqObs;
321 frqObs1P->_rnxType2ch = (sys == 'G') ? "1W" : "1P";
322 new_obs._obs.push_back(frqObs1P);
323
324 t_frqObs* frqObs2P = new t_frqObs;
325 frqObs2P->_rnxType2ch = (sys == 'G') ? "2W" : "2P";
326 new_obs._obs.push_back(frqObs2P);
327
328 // missing IOD
329 vector<string> missingIOD;
330 vector<string> hasIOD;
331 for (unsigned ii = 0; ii < 4; ii++) {
332 unsigned int IODcorr = 0;
333 double corrVal = 0;
334 const t_eph* eph = 0;
335 double* obsVal = 0;
336
337 switch (ii) {
338 case 0: // --- L1 ---
339 IODcorr = corr->IODp1;
340 corrVal = corr->phase1 * LAMBDA_1;
341 obsVal = &L1;
342 obsT = "L1";
343 break;
344 case 1: // --- L2 ---
345 IODcorr = corr->IODp2;
346 corrVal = corr->phase2 * LAMBDA_2;
347 obsVal = &L2;
348 obsT = "L2";
349 break;
350 case 2: // --- P1 ---
351 IODcorr = corr->IODr1;
352 corrVal = corr->range1;
353 obsVal = &P1;
354 obsT = "P1";
355 break;
356 case 3: // --- P2 ---
357 IODcorr = corr->IODr2;
358 corrVal = corr->range2;
359 obsVal = &P2;
360 obsT = "P2";
361 break;
362 default:
363 continue;
364 }
365
366 // Select corresponding ephemerides
367 QString prnInternalStr(prn.toInternalString().c_str());
368 const t_eph* ephLast = _ephUser.ephLast(prnInternalStr);
369 const t_eph* ephPrev = _ephUser.ephPrev(prnInternalStr);
370 if (ephLast && ephLast->IOD() == IODcorr) {
371 eph = ephLast;
372 } else if (ephPrev && ephPrev->IOD() == IODcorr) {
373 eph = ephPrev;
374 }
375
376 if (eph) {
377 ostringstream msg;
378 msg << obsT << ':' << setw(3) << eph->IOD();
379 hasIOD.push_back(msg.str());
380
381 int GPSWeek_tot;
382 double GPSWeeks_tot;
383 double rho, xSat, ySat, zSat, clkSat;
384 cmpRho(eph, stax, stay, staz, GPSWeek, GPSWeeks, rho, GPSWeek_tot,
385 GPSWeeks_tot, xSat, ySat, zSat, clkSat);
386
387 *obsVal = rho - corrVal + rcv_clk_bias - clkSat;
388
389 if (*obsVal == 0)
390 *obsVal = ZEROVALUE;
391 char sys;
392 int num, flag;
393 if (corr->PRN < 200) {
394 sys = 'G';
395 num = corr->PRN;
396 }
397 else {
398 sys = 'R';
399 num = corr->PRN - 200;
400 }
401 flag = t_corrSSR::getSsrNavTypeFlag(sys, num);
402 new_obs._prn.set(sys, num, flag);
403 new_obs._time.set(GPSWeek_rcv, GPSWeeks_rcv);
404
405 // Store estimated measurements
406 // ----------------------------
407 switch (ii) {
408 case 0: // --- L1 ---
409 frqObs1P->_phaseValid = true;
410 frqObs1P->_phase = *obsVal / LAMBDA_1;
411 //frqObs1P->_slipCounter = corr->lock1;
412 frqObs1P->_slipCounter = -1; // because RTCM2 definition is vice versa to RTCM3
413 break;
414 case 1: // --- L2 ---
415 frqObs2P->_phaseValid = true;
416 frqObs2P->_phase = *obsVal / LAMBDA_2;
417 //frqObs2P->_slipCounter = corr->lock2;
418 frqObs2P->_slipCounter = -1; // because RTCM2 definition is vice versa to RTCM3
419 break;
420 case 2: // --- C1 / P1 ---
421 if (corr->Pind1) {
422 frqObs1P->_codeValid = true;
423 frqObs1P->_code = *obsVal;
424 } else {
425 frqObs1C->_codeValid = true;
426 frqObs1C->_code = *obsVal;
427 }
428 break;
429 case 3: // --- C2 / P2 ---
430 if (corr->Pind2) {
431 frqObs2P->_codeValid = true;
432 frqObs2P->_code = *obsVal;
433 }
434 break;
435 default:
436 continue;
437 }
438 } else if (IODcorr != 0) {
439 ostringstream msg;
440 msg << obsT << ':' << setw(3) << IODcorr;
441 missingIOD.push_back(msg.str());
442 }
443 } // loop over frequencies
444
445 // Error report
446 if (missingIOD.size()) {
447 ostringstream missingIODstr;
448
449 copy(missingIOD.begin(), missingIOD.end(),
450 ostream_iterator<string>(missingIODstr, " "));
451
452 errmsg.push_back(
453 "missing eph for " + prn.toString() + " , IODs "
454 + missingIODstr.str());
455 }
456
457 // Store new observation
458 if (new_obs._time.mjd() > 0) {
459 _obsList.push_back(new_obs);
460 }
461 }
462}
Note: See TracBrowser for help on using the repository browser.