source: ntrip/trunk/BNC/RTCM3/RTCM3coDecoder.cpp@ 3024

Last change on this file since 3024 was 3024, checked in by mervart, 13 years ago
File size: 12.0 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: RTCM3coDecoder
30 *
31 * Purpose: RTCM3 Clock Orbit Decoder
32 *
33 * Author: L. Mervart
34 *
35 * Created: 05-May-2008
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <stdio.h>
42#include <math.h>
43
44#include "RTCM3coDecoder.h"
45#include "bncutils.h"
46#include "bncrinex.h"
47#include "bncapp.h"
48#include "bncsettings.h"
49#include "rtcm3torinex.h"
50
51using namespace std;
52
53// Constructor
54////////////////////////////////////////////////////////////////////////////
55RTCM3coDecoder::RTCM3coDecoder(const QString& staID) {
56
57 _staID = staID;
58
59 // File Output
60 // -----------
61 bncSettings settings;
62 QString path = settings.value("corrPath").toString();
63 if (!path.isEmpty()) {
64 expandEnvVar(path);
65 if ( path.length() > 0 && path[path.length()-1] != QDir::separator() ) {
66 path += QDir::separator();
67 }
68 _fileNameSkl = path + staID;
69 }
70 _out = 0;
71 _GPSweeks = -1.0;
72
73 connect(this, SIGNAL(newCorrLine(QString, QString, long)),
74 (bncApp*) qApp, SLOT(slotNewCorrLine(QString, QString, long)));
75
76 memset(&_co, 0, sizeof(_co));
77}
78
79// Destructor
80////////////////////////////////////////////////////////////////////////////
81RTCM3coDecoder::~RTCM3coDecoder() {
82 delete _out;
83}
84
85// Reopen Output File
86////////////////////////////////////////////////////////////////////////
87void RTCM3coDecoder::reopen() {
88
89 if (!_fileNameSkl.isEmpty()) {
90
91 bncSettings settings;
92
93 QDateTime datTim = currentDateAndTimeGPS();
94
95 QString hlpStr = bncRinex::nextEpochStr(datTim,
96 settings.value("corrIntr").toString());
97
98 QString fileName = _fileNameSkl
99 + QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0'))
100 + hlpStr + datTim.toString(".yyC");
101
102 if (_fileName == fileName) {
103 return;
104 }
105 else {
106 _fileName = fileName;
107 }
108
109 delete _out;
110 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
111 _out = new ofstream( _fileName.toAscii().data(),
112 ios_base::out | ios_base::app );
113 }
114 else {
115 _out = new ofstream( _fileName.toAscii().data() );
116 }
117 }
118}
119
120//
121////////////////////////////////////////////////////////////////////////////
122t_irc RTCM3coDecoder::Decode(char* buffer, int bufLen, vector<string>& errmsg) {
123
124 errmsg.clear();
125
126 _buffer.append(QByteArray(buffer,bufLen));
127
128 t_irc retCode = failure;
129
130 while(_buffer.size()) {
131
132 int bytesused = 0;
133 struct ClockOrbit co_sav;
134 memcpy(&co_sav, &_co, sizeof(co_sav)); // save state
135
136 GCOB_RETURN irc = GetClockOrbitBias(&_co, &_bias, _buffer.data(),
137 _buffer.size(), &bytesused);
138
139 if (irc <= -30) { // not enough data - restore state and exit loop
140 memcpy(&_co, &co_sav, sizeof(co_sav));
141 break;
142 }
143
144 else if (irc < 0) { // error - skip 1 byte and retry
145 memset(&_co, 0, sizeof(_co));
146 memset(&_bias, 0, sizeof(_bias));
147 _buffer = _buffer.mid(bytesused ? bytesused : 1);
148 }
149
150 else { // OK or MESSAGEFOLLOWS
151 _buffer = _buffer.mid(bytesused);
152
153 if ( irc == GCOBR_OK &&
154 (_co.NumberOfGPSSat > 0 || _co.NumberOfGLONASSSat > 0) ) {
155
156 reopen();
157
158 // Guess GPS week and sec using system time
159 // ----------------------------------------
160 int GPSweek;
161 double GPSweeksHlp;
162 currentGPSWeeks(GPSweek, GPSweeksHlp);
163
164 // Correction Epoch from GPSEpochTime
165 // ----------------------------------
166 if (_co.NumberOfGPSSat > 0) {
167 if (GPSweeksHlp > _co.GPSEpochTime + 86400.0) {
168 GPSweek += 1;
169 }
170 else if (GPSweeksHlp < _co.GPSEpochTime - 86400.0) {
171 GPSweek -= 1;
172 }
173 _GPSweeks = _co.GPSEpochTime;
174 }
175
176 // Correction Epoch from Glonass Epoch
177 // -----------------------------------
178 else if (_co.NumberOfGLONASSSat > 0){
179
180 // Second of day (GPS time) from Glonass Epoch
181 // -------------------------------------------
182 QDate date = dateAndTimeFromGPSweek(GPSweek, GPSweeksHlp).date();
183 int leapSecond = gnumleap(date.year(), date.month(), date.day());
184 int GPSDaySec = _co.GLONASSEpochTime - 3 * 3600 + leapSecond;
185
186 int weekDay = int(GPSweeksHlp/86400.0);
187 int GPSDaySecHlp = int(GPSweeksHlp) - weekDay * 86400;
188
189 // Handle the difference between system clock and correction epoch
190 // ---------------------------------------------------------------
191 if (GPSDaySec < GPSDaySecHlp - 3600) {
192 weekDay += 1;
193 if (weekDay > 6) {
194 weekDay = 0;
195 GPSweek += 1;
196 }
197 }
198 else if (GPSDaySec > GPSDaySecHlp + 3600) {
199 weekDay -= 1;
200 if (weekDay < 0) {
201 weekDay = 6;
202 GPSweek -= 1;
203 }
204 }
205
206 _GPSweeks = weekDay * 86400.0 + GPSDaySec;
207 }
208
209 QStringList asciiLines = corrsToASCIIlines(GPSweek, _GPSweeks,
210 _co, &_bias);
211
212 long coTime = GPSweek * 7*24*3600 + long(floor(_GPSweeks+0.5));
213
214 QStringListIterator it(asciiLines);
215 while (it.hasNext()) {
216 QString line = it.next();
217 printLine(line, coTime);
218 }
219
220 retCode = success;
221 memset(&_co, 0, sizeof(_co));
222 memset(&_bias, 0, sizeof(_bias));
223 }
224 }
225 }
226
227 if (retCode != success) {
228 _GPSweeks = -1.0;
229 }
230 return retCode;
231}
232
233//
234////////////////////////////////////////////////////////////////////////////
235void RTCM3coDecoder::printLine(const QString& line, long coTime) {
236 if (_out) {
237 *_out << line.toAscii().data() << endl;
238 _out->flush();
239 }
240
241 emit newCorrLine(line, _staID, coTime);
242}
243
244//
245////////////////////////////////////////////////////////////////////////////
246QStringList RTCM3coDecoder::corrsToASCIIlines(int GPSweek, double GPSweeks,
247 const ClockOrbit& co,
248 const Bias* bias) {
249
250 QStringList retLines;
251
252 // Loop over all satellites (GPS and Glonass)
253 // ------------------------------------------
254 if (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) {
255 QString line1;
256 line1.sprintf("! Orbits/Clocks: %d GPS %d Glonass",
257 co.NumberOfGPSSat, co.NumberOfGLONASSSat);
258 retLines << line1;
259 }
260 for (int ii = 0; ii < CLOCKORBIT_NUMGPS+co.NumberOfGLONASSSat; ii++) {
261 char sysCh = ' ';
262 if (ii < co.NumberOfGPSSat) {
263 sysCh = 'G';
264 }
265 else if (ii >= CLOCKORBIT_NUMGPS) {
266 sysCh = 'R';
267 }
268
269 if (sysCh != ' ') {
270
271 QString linePart;
272 linePart.sprintf("%d %d %d %.1f %c%2.2d",
273 co.messageType, co.UpdateInterval, GPSweek, GPSweeks,
274 sysCh, co.Sat[ii].ID);
275
276 // Combined message (orbit and clock)
277 // ----------------------------------
278 if ( co.messageType == COTYPE_GPSCOMBINED ||
279 co.messageType == COTYPE_GLONASSCOMBINED ) {
280 QString line;
281 line.sprintf(" %3d"
282 " %8.3f %8.3f %8.3f %8.3f"
283 " %10.5f %10.5f %10.5f %10.5f"
284 " %10.5f",
285 co.Sat[ii].IOD,
286 co.Sat[ii].Clock.DeltaA0,
287 co.Sat[ii].Orbit.DeltaRadial,
288 co.Sat[ii].Orbit.DeltaAlongTrack,
289 co.Sat[ii].Orbit.DeltaCrossTrack,
290 co.Sat[ii].Clock.DeltaA1,
291 co.Sat[ii].Orbit.DotDeltaRadial,
292 co.Sat[ii].Orbit.DotDeltaAlongTrack,
293 co.Sat[ii].Orbit.DotDeltaCrossTrack,
294 co.Sat[ii].Clock.DeltaA2);
295 retLines << linePart+line;
296 }
297
298 // Orbits only
299 // -----------
300 else if ( co.messageType == COTYPE_GPSORBIT ||
301 co.messageType == COTYPE_GLONASSORBIT ) {
302 QString line;
303 line.sprintf(" %3d"
304 " %8.3f %8.3f %8.3f"
305 " %10.5f %10.5f %10.5f",
306 co.Sat[ii].IOD,
307 co.Sat[ii].Orbit.DeltaRadial,
308 co.Sat[ii].Orbit.DeltaAlongTrack,
309 co.Sat[ii].Orbit.DeltaCrossTrack,
310 co.Sat[ii].Orbit.DotDeltaRadial,
311 co.Sat[ii].Orbit.DotDeltaAlongTrack,
312 co.Sat[ii].Orbit.DotDeltaCrossTrack);
313 retLines << linePart+line;
314 }
315
316 // Clocks only
317 // -----------
318 else if ( co.messageType == COTYPE_GPSCLOCK ||
319 co.messageType == COTYPE_GLONASSCLOCK ) {
320 QString line;
321 line.sprintf(" %3d %8.3f %10.5f %10.5f",
322 co.Sat[ii].IOD,
323 co.Sat[ii].Clock.DeltaA0,
324 co.Sat[ii].Clock.DeltaA1,
325 co.Sat[ii].Clock.DeltaA2);
326 retLines << linePart+line;
327 }
328
329 // User Range Accuracy
330 // -------------------
331 else if ( co.messageType == COTYPE_GPSURA ||
332 co.messageType == COTYPE_GLONASSURA ) {
333 QString line;
334 line.sprintf(" %3d %f",
335 co.Sat[ii].IOD, co.Sat[ii].UserRangeAccuracy);
336 retLines << linePart+line;
337 }
338
339 // High-Resolution Clocks
340 // ----------------------
341 else if ( co.messageType == COTYPE_GPSHR ||
342 co.messageType == COTYPE_GLONASSHR ) {
343 QString line;
344 line.sprintf(" %3d %8.3f",
345 co.Sat[ii].IOD, co.Sat[ii].hrclock);
346 retLines << linePart+line;
347 }
348 }
349 }
350
351 // Loop over all satellites (GPS and Glonass)
352 // ------------------------------------------
353 if (bias) {
354 if (bias->NumberOfGPSSat > 0 || bias->NumberOfGLONASSSat > 0) {
355 QString line1;
356 line1.sprintf("! Biases: %d GPS %d Glonass",
357 bias->NumberOfGPSSat, bias->NumberOfGLONASSSat);
358 retLines << line1;
359 }
360 for (int ii = 0; ii < CLOCKORBIT_NUMGPS + bias->NumberOfGLONASSSat; ii++) {
361 char sysCh = ' ';
362 int messageType;
363 if (ii < bias->NumberOfGPSSat) {
364 sysCh = 'G';
365 messageType = BTYPE_GPS;
366 }
367 else if (ii >= CLOCKORBIT_NUMGPS) {
368 sysCh = 'R';
369 messageType = BTYPE_GLONASS;
370 }
371 if (sysCh != ' ') {
372 QString line;
373 line.sprintf("%d %d %d %.1f %c%2.2d %d",
374 messageType, bias->UpdateInterval, GPSweek, GPSweeks,
375 sysCh, bias->Sat[ii].ID,
376 bias->Sat[ii].NumberOfCodeBiases);
377 for (int jj = 0; jj < bias->Sat[ii].NumberOfCodeBiases; jj++) {
378 QString hlp;
379 hlp.sprintf(" %d %8.3f", bias->Sat[ii].Biases[jj].Type,
380 bias->Sat[ii].Biases[jj].Bias);
381 line += hlp;
382 }
383 retLines << line;
384 }
385 }
386 }
387
388 return retLines;
389}
Note: See TracBrowser for help on using the repository browser.