source: ntrip/trunk/BNC/RTCM/RTCM2Decoder.cpp@ 3580

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