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

Last change on this file since 6137 was 6137, checked in by mervart, 10 years ago
File size: 11.4 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) {
58 _ID = ID;
59}
60
61//
62// Destructor
63//
64
65RTCM2Decoder::~RTCM2Decoder() {
66}
67
68
69//
70t_irc RTCM2Decoder::getStaCrd(double& xx, double& yy, double& zz) {
71 if ( !_msg03.validMsg ) {
72 return failure;
73 }
74
75 xx = _msg03.x + (_msg22.validMsg ? _msg22.dL1[0] : 0.0);
76 yy = _msg03.y + (_msg22.validMsg ? _msg22.dL1[1] : 0.0);
77 zz = _msg03.z + (_msg22.validMsg ? _msg22.dL1[2] : 0.0);
78
79 return success;
80}
81
82//
83t_irc RTCM2Decoder::getStaCrd(double& xx, double& yy, double& zz,
84 double& dx1, double& dy1, double& dz1,
85 double& dx2, double& dy2, double& dz2) {
86 xx = _msg03.x;
87 yy = _msg03.y;
88 zz = _msg03.z;
89
90 dx1 = (_msg22.validMsg ? _msg22.dL1[0] : 0.0);
91 dy1 = (_msg22.validMsg ? _msg22.dL1[1] : 0.0);
92 dz1 = (_msg22.validMsg ? _msg22.dL1[2] : 0.0);
93
94 dx2 = (_msg22.validMsg ? _msg22.dL2[0] : 0.0);
95 dy2 = (_msg22.validMsg ? _msg22.dL2[1] : 0.0);
96 dz2 = (_msg22.validMsg ? _msg22.dL2[2] : 0.0);
97
98 return success;
99}
100
101
102//
103t_irc RTCM2Decoder::Decode(char* buffer, int bufLen, vector<string>& errmsg) {
104
105 errmsg.clear();
106
107 _buffer.append(buffer, bufLen);
108 int refWeek;
109 double refSecs;
110 currentGPSWeeks(refWeek, refSecs);
111 bool decoded = false;
112
113 while(true) {
114 _PP.getPacket(_buffer);
115 if (!_PP.valid()) {
116 if (decoded) {
117 return success;
118 } else {
119 return failure;
120 }
121 }
122
123 // Store message number
124 _typeList.push_back(_PP.ID());
125
126 if ( _PP.ID()==18 || _PP.ID()==19 ) {
127
128 _ObsBlock.extract(_PP);
129
130 if (_ObsBlock.valid()) {
131 decoded = true;
132
133 int epochWeek;
134 double epochSecs;
135 _ObsBlock.resolveEpoch(refWeek, refSecs, epochWeek, epochSecs);
136
137 for (int iSat=0; iSat < _ObsBlock.nSat; iSat++) {
138 t_satObs obs;
139 if (_ObsBlock.PRN[iSat] > 100) {
140 obs._prn.set('R', _ObsBlock.PRN[iSat] % 100);
141 }
142 else {
143 obs._prn.set('R', _ObsBlock.PRN[iSat]);
144 }
145 obs._time.set(epochWeek, epochSecs);
146 t_frqObs* frqObs1C = new t_frqObs;
147 frqObs1C->_rnxType2ch = "1C";
148 frqObs1C->_codeValid = true;
149 frqObs1C->_code = _ObsBlock.rng_C1[iSat];
150 obs._obs.push_back(frqObs1C);
151
152 t_frqObs* frqObs1P = new t_frqObs;
153 frqObs1P->_rnxType2ch = "1P";
154 frqObs1P->_codeValid = true;
155 frqObs1P->_code = _ObsBlock.rng_P1[iSat];
156 obs._obs.push_back(frqObs1P);
157 frqObs1P->_phaseValid = true;
158 frqObs1P->_phase = _ObsBlock.resolvedPhase_L1(iSat);
159 frqObs1P->_slipCounter = _ObsBlock.slip_L1[iSat];
160 obs._obs.push_back(frqObs1P);
161
162 t_frqObs* frqObs2P = new t_frqObs;
163 frqObs2P->_rnxType2ch = "2P";
164 frqObs2P->_codeValid = true;
165 frqObs2P->_code = _ObsBlock.rng_P2[iSat];
166 obs._obs.push_back(frqObs2P);
167 frqObs2P->_phaseValid = true;
168 frqObs2P->_phase = _ObsBlock.resolvedPhase_L2(iSat);
169 frqObs2P->_slipCounter = _ObsBlock.slip_L2[iSat];
170 obs._obs.push_back(frqObs2P);
171
172 _obsList.push_back(obs);
173 }
174 _ObsBlock.clear();
175 }
176 }
177
178 else if ( _PP.ID() == 20 || _PP.ID() == 21 ) {
179 _msg2021.extract(_PP);
180
181 if (_msg2021.valid()) {
182 decoded = true;
183 translateCorr2Obs(errmsg);
184 }
185 }
186
187 else if ( _PP.ID() == 3 ) {
188 _msg03.extract(_PP);
189 }
190
191 else if ( _PP.ID() == 22 ) {
192 _msg22.extract(_PP);
193 }
194
195 else if ( _PP.ID() == 23 ) {
196 _msg23.extract(_PP);
197 }
198
199 else if ( _PP.ID() == 24 ) {
200 _msg24.extract(_PP);
201 }
202
203 // Output for RTCM scan
204 if ( _PP.ID() == 3 ) {
205 if ( _msg03.validMsg ) {
206 _antList.push_back(t_antInfo());
207
208 this->getStaCrd(_antList.back().xx, _antList.back().yy, _antList.back().zz);
209
210 _antList.back().type = t_antInfo::APC;
211 _antList.back().message = _PP.ID();
212 }
213 }
214 else if ( _PP.ID() == 23 ) {
215 if ( _msg23.validMsg ) {
216 _antType.push_back(_msg23.antType.c_str());
217 }
218 }
219 else if ( _PP.ID() == 24 ) {
220 if ( _msg24.validMsg ) {
221 _antList.push_back(t_antInfo());
222
223 _antList.back().xx = _msg24.x;
224 _antList.back().yy = _msg24.y;
225 _antList.back().zz = _msg24.z;
226
227 _antList.back().height_f = true;
228 _antList.back().height = _msg24.h;
229
230 _antList.back().type = t_antInfo::ARP;
231 _antList.back().message = _PP.ID();
232 }
233 }
234 }
235 return success;
236}
237
238void RTCM2Decoder::translateCorr2Obs(vector<string>& errmsg) {
239
240 QMutexLocker locker(&_mutex);
241
242 if ( !_msg03.validMsg || !_msg2021.valid() ) {
243 return;
244 }
245
246 double stax = _msg03.x + (_msg22.validMsg ? _msg22.dL1[0] : 0.0);
247 double stay = _msg03.y + (_msg22.validMsg ? _msg22.dL1[1] : 0.0);
248 double staz = _msg03.z + (_msg22.validMsg ? _msg22.dL1[2] : 0.0);
249
250 int refWeek;
251 double refSecs;
252 currentGPSWeeks(refWeek, refSecs);
253
254 // Resolve receiver time of measurement (see RTCM 2.3, page 4-42, Message 18, Note 1)
255 // ----------------------------------------------------------------------------------
256 double hoursec_est = _msg2021.hoursec(); // estimated time of measurement
257 double hoursec_rcv = rint(hoursec_est * 1e2) / 1e2; // receiver clock reading at hoursec_est
258 double rcv_clk_bias = (hoursec_est - hoursec_rcv) * c_light;
259
260 int GPSWeek;
261 double GPSWeeks;
262 resolveEpoch(hoursec_est, refWeek, refSecs,
263 GPSWeek, GPSWeeks);
264
265 int GPSWeek_rcv;
266 double GPSWeeks_rcv;
267 resolveEpoch(hoursec_rcv, refWeek, refSecs,
268 GPSWeek_rcv, GPSWeeks_rcv);
269
270 // Loop over all satellites
271 // ------------------------
272 for (RTCM2_2021::data_iterator icorr = _msg2021.data.begin();
273 icorr != _msg2021.data.end(); icorr++) {
274 const RTCM2_2021::HiResCorr* corr = icorr->second;
275
276 // beg test
277 if ( corr->PRN >= 200 ) {
278 continue;
279 }
280 // end test
281
282 QString prn;
283 if (corr->PRN < 200) {
284 prn = 'G' + QString("%1").arg(corr->PRN, 2, 10, QChar('0'));
285 }
286 else {
287 prn = 'R' + QString("%1").arg(corr->PRN - 200, 2, 10, QChar('0'));
288 }
289
290 const t_ephPair* ePair = ephPair(prn);
291
292 double L1 = 0;
293 double L2 = 0;
294 double P1 = 0;
295 double P2 = 0;
296 string obsT = "";
297
298 // new observation
299 t_satObs* new_obs = 0;
300
301 t_frqObs* frqObs1C = new t_frqObs;
302 frqObs1C->_rnxType2ch = "1C";
303 new_obs->_obs.push_back(frqObs1C);
304
305 t_frqObs* frqObs1P = new t_frqObs;
306 frqObs1P->_rnxType2ch = "1P";
307 new_obs->_obs.push_back(frqObs1P);
308
309 t_frqObs* frqObs2P = new t_frqObs;
310 frqObs2P->_rnxType2ch = "2P";
311 new_obs->_obs.push_back(frqObs2P);
312
313 // missing IOD
314 vector<string> missingIOD;
315 vector<string> hasIOD;
316 for (unsigned ii = 0; ii < 4; ii++) {
317 int IODcorr = 0;
318 double corrVal = 0;
319 const t_eph* eph = 0;
320 double* obsVal = 0;
321
322 switch (ii) {
323 case 0: // --- L1 ---
324 IODcorr = corr->IODp1;
325 corrVal = corr->phase1 * LAMBDA_1;
326 obsVal = &L1;
327 obsT = "L1";
328 break;
329 case 1: // --- L2 ---
330 IODcorr = corr->IODp2;
331 corrVal = corr->phase2 * LAMBDA_2;
332 obsVal = &L2;
333 obsT = "L2";
334 break;
335 case 2: // --- P1 ---
336 IODcorr = corr->IODr1;
337 corrVal = corr->range1;
338 obsVal = &P1;
339 obsT = "P1";
340 break;
341 case 3: // --- P2 ---
342 IODcorr = corr->IODr2;
343 corrVal = corr->range2;
344 obsVal = &P2;
345 obsT = "P2";
346 break;
347 default:
348 continue;
349 }
350
351 // Select corresponding ephemerides
352 if (ePair) {
353 if (ePair->last && ePair->last->IOD() == IODcorr) {
354 eph = ePair->last;
355 }
356 else if (ePair->prev && ePair->prev->IOD() == IODcorr) {
357 eph = ePair->prev;
358 }
359 }
360
361 if ( eph ) {
362 ostringstream msg;
363 msg << obsT << ':' << setw(3) << eph->IOD();
364 hasIOD.push_back(msg.str());
365
366
367 int GPSWeek_tot;
368 double GPSWeeks_tot;
369 double rho, xSat, ySat, zSat, clkSat;
370 cmpRho(eph, stax, stay, staz,
371 GPSWeek, GPSWeeks,
372 rho, GPSWeek_tot, GPSWeeks_tot,
373 xSat, ySat, zSat, clkSat);
374
375 *obsVal = rho - corrVal + rcv_clk_bias - clkSat;
376
377 if ( *obsVal == 0 ) *obsVal = ZEROVALUE;
378
379 // Allocate new memory
380 // -------------------
381 if ( !new_obs ) {
382 new_obs = new t_satObs();
383 if (corr->PRN < 200) {
384 new_obs->_prn.set('G', corr->PRN);
385 }
386 else {
387 new_obs->_prn.set('R', corr->PRN-200);
388 }
389 new_obs->_time.set(GPSWeek_rcv, GPSWeeks_rcv);
390 }
391
392 // Store estimated measurements
393 // ----------------------------
394 switch (ii) {
395 case 0: // --- L1 ---
396 frqObs1P->_phaseValid = true;
397 frqObs1P->_phase = *obsVal / LAMBDA_1;
398 frqObs1P->_slipCounter = corr->lock1;
399 break;
400 case 1: // --- L2 ---
401 frqObs2P->_phaseValid = true;
402 frqObs2P->_phase = *obsVal / LAMBDA_2;
403 frqObs2P->_slipCounter = corr->lock2;
404 break;
405 case 2: // --- C1 / P1 ---
406 if ( corr->Pind1 ) {
407 frqObs1P->_codeValid = true;
408 frqObs1P->_code = *obsVal;
409 }
410 else {
411 frqObs1C->_codeValid = true;
412 frqObs1C->_code = *obsVal;
413 }
414 break;
415 case 3: // --- C2 / P2 ---
416 if ( corr->Pind2 ) {
417 frqObs2P->_codeValid = true;
418 frqObs2P->_code = *obsVal;
419 }
420 break;
421 default:
422 continue;
423 }
424 }
425 else if ( IODcorr != 0 ) {
426 ostringstream msg;
427 msg << obsT << ':' << setw(3) << IODcorr;
428 missingIOD.push_back(msg.str());
429 }
430 } // loop over frequencies
431
432 // Error report
433 if ( missingIOD.size() ) {
434 ostringstream missingIODstr;
435
436 copy(missingIOD.begin(), missingIOD.end(), ostream_iterator<string>(missingIODstr, " "));
437
438 errmsg.push_back("missing eph for " + string(prn.toAscii().data()) + " , IODs " + missingIODstr.str());
439 }
440
441 // Store new observation
442 if ( new_obs ) {
443 _obsList.push_back(*new_obs);
444 delete new_obs;
445 }
446 }
447}
Note: See TracBrowser for help on using the repository browser.