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

Last change on this file since 2452 was 1299, checked in by zdenek, 16 years ago

* empty log message *

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