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 |
|
---|
51 | using namespace std;
|
---|
52 |
|
---|
53 | // Constructor
|
---|
54 | ////////////////////////////////////////////////////////////////////////////
|
---|
55 | RTCM3coDecoder::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 | ////////////////////////////////////////////////////////////////////////////
|
---|
81 | RTCM3coDecoder::~RTCM3coDecoder() {
|
---|
82 | delete _out;
|
---|
83 | }
|
---|
84 |
|
---|
85 | // Reopen Output File
|
---|
86 | ////////////////////////////////////////////////////////////////////////
|
---|
87 | void 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 | ////////////////////////////////////////////////////////////////////////////
|
---|
122 | t_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 | reopen();
|
---|
155 |
|
---|
156 | // Guess GPS week and sec using system time
|
---|
157 | // ----------------------------------------
|
---|
158 | int GPSweek;
|
---|
159 | double GPSweeksHlp;
|
---|
160 | currentGPSWeeks(GPSweek, GPSweeksHlp);
|
---|
161 |
|
---|
162 | // Correction Epoch from GPSEpochTime
|
---|
163 | // ----------------------------------
|
---|
164 | if (_co.NumberOfGPSSat > 0) {
|
---|
165 | if (GPSweeksHlp > _co.GPSEpochTime + 86400.0) {
|
---|
166 | GPSweek += 1;
|
---|
167 | }
|
---|
168 | else if (GPSweeksHlp < _co.GPSEpochTime - 86400.0) {
|
---|
169 | GPSweek -= 1;
|
---|
170 | }
|
---|
171 | _GPSweeks = _co.GPSEpochTime;
|
---|
172 | }
|
---|
173 |
|
---|
174 | // Correction Epoch from Glonass Epoch
|
---|
175 | // -----------------------------------
|
---|
176 | else if (_co.NumberOfGLONASSSat > 0){
|
---|
177 |
|
---|
178 | // Second of day (GPS time) from Glonass Epoch
|
---|
179 | // -------------------------------------------
|
---|
180 | QDate date = dateAndTimeFromGPSweek(GPSweek, GPSweeksHlp).date();
|
---|
181 | int leapSecond = gnumleap(date.year(), date.month(), date.day());
|
---|
182 | int GPSDaySec = _co.GLONASSEpochTime - 3 * 3600 + leapSecond;
|
---|
183 |
|
---|
184 | int weekDay = int(GPSweeksHlp/86400.0);
|
---|
185 | int GPSDaySecHlp = int(GPSweeksHlp) - weekDay * 86400;
|
---|
186 |
|
---|
187 | // Handle the difference between system clock and correction epoch
|
---|
188 | // ---------------------------------------------------------------
|
---|
189 | if (GPSDaySec < GPSDaySecHlp - 3600) {
|
---|
190 | weekDay += 1;
|
---|
191 | if (weekDay > 6) {
|
---|
192 | weekDay = 0;
|
---|
193 | GPSweek += 1;
|
---|
194 | }
|
---|
195 | }
|
---|
196 | else if (GPSDaySec > GPSDaySecHlp + 3600) {
|
---|
197 | weekDay -= 1;
|
---|
198 | if (weekDay < 0) {
|
---|
199 | weekDay = 6;
|
---|
200 | GPSweek -= 1;
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | _GPSweeks = weekDay * 86400.0 + GPSDaySec;
|
---|
205 | }
|
---|
206 |
|
---|
207 | long coTime = GPSweek * 7*24*3600 + long(floor(_GPSweeks+0.5));
|
---|
208 |
|
---|
209 | // Loop over all satellites (GPS and Glonass)
|
---|
210 | // ------------------------------------------
|
---|
211 | for (int ii = 0; ii < CLOCKORBIT_NUMGPS+_co.NumberOfGLONASSSat; ii++) {
|
---|
212 | char sysCh = ' ';
|
---|
213 | if (ii < _co.NumberOfGPSSat) {
|
---|
214 | sysCh = 'G';
|
---|
215 | }
|
---|
216 | else if (ii >= CLOCKORBIT_NUMGPS) {
|
---|
217 | sysCh = 'R';
|
---|
218 | }
|
---|
219 | if (sysCh != ' ') {
|
---|
220 |
|
---|
221 | QString linePart;
|
---|
222 | linePart.sprintf("%d %d %d %.1f %c%2.2d",
|
---|
223 | _co.messageType, _co.UpdateInterval, GPSweek, _GPSweeks,
|
---|
224 | sysCh, _co.Sat[ii].ID);
|
---|
225 |
|
---|
226 | // Combined message (orbit and clock)
|
---|
227 | // ----------------------------------
|
---|
228 | if ( _co.messageType == COTYPE_GPSCOMBINED ||
|
---|
229 | _co.messageType == COTYPE_GLONASSCOMBINED ) {
|
---|
230 | QString line;
|
---|
231 | line.sprintf(" %3d"
|
---|
232 | " %8.3f %8.3f %8.3f %8.3f"
|
---|
233 | " %10.5f %10.5f %10.5f %10.5f"
|
---|
234 | " %10.5f %10.5f %10.5f %10.5f",
|
---|
235 | _co.Sat[ii].IOD,
|
---|
236 | _co.Sat[ii].Clock.DeltaA0,
|
---|
237 | _co.Sat[ii].Orbit.DeltaRadial,
|
---|
238 | _co.Sat[ii].Orbit.DeltaAlongTrack,
|
---|
239 | _co.Sat[ii].Orbit.DeltaCrossTrack,
|
---|
240 | _co.Sat[ii].Clock.DeltaA1,
|
---|
241 | _co.Sat[ii].Orbit.DotDeltaRadial,
|
---|
242 | _co.Sat[ii].Orbit.DotDeltaAlongTrack,
|
---|
243 | _co.Sat[ii].Orbit.DotDeltaCrossTrack,
|
---|
244 | _co.Sat[ii].Clock.DeltaA2,
|
---|
245 | _co.Sat[ii].Orbit.DotDotDeltaRadial,
|
---|
246 | _co.Sat[ii].Orbit.DotDotDeltaAlongTrack,
|
---|
247 | _co.Sat[ii].Orbit.DotDotDeltaCrossTrack);
|
---|
248 | printLine(linePart+line, coTime);
|
---|
249 | }
|
---|
250 |
|
---|
251 | // Orbits only
|
---|
252 | // -----------
|
---|
253 | else if ( _co.messageType == COTYPE_GPSORBIT ||
|
---|
254 | _co.messageType == COTYPE_GLONASSORBIT ) {
|
---|
255 | QString line;
|
---|
256 | line.sprintf(" %3d"
|
---|
257 | " %8.3f %8.3f %8.3f"
|
---|
258 | " %10.5f %10.5f %10.5f"
|
---|
259 | " %10.5f %10.5f %10.5f",
|
---|
260 | _co.Sat[ii].IOD,
|
---|
261 | _co.Sat[ii].Orbit.DeltaRadial,
|
---|
262 | _co.Sat[ii].Orbit.DeltaAlongTrack,
|
---|
263 | _co.Sat[ii].Orbit.DeltaCrossTrack,
|
---|
264 | _co.Sat[ii].Orbit.DotDeltaRadial,
|
---|
265 | _co.Sat[ii].Orbit.DotDeltaAlongTrack,
|
---|
266 | _co.Sat[ii].Orbit.DotDeltaCrossTrack,
|
---|
267 | _co.Sat[ii].Orbit.DotDotDeltaRadial,
|
---|
268 | _co.Sat[ii].Orbit.DotDotDeltaAlongTrack,
|
---|
269 | _co.Sat[ii].Orbit.DotDotDeltaCrossTrack);
|
---|
270 | printLine(linePart+line, coTime);
|
---|
271 | }
|
---|
272 |
|
---|
273 | // Clocks only
|
---|
274 | // -----------
|
---|
275 | else if ( _co.messageType == COTYPE_GPSCLOCK ||
|
---|
276 | _co.messageType == COTYPE_GLONASSCLOCK ) {
|
---|
277 | QString line;
|
---|
278 | line.sprintf(" %3d %8.3f %10.5f %10.5f",
|
---|
279 | _co.Sat[ii].IOD,
|
---|
280 | _co.Sat[ii].Clock.DeltaA0,
|
---|
281 | _co.Sat[ii].Clock.DeltaA1,
|
---|
282 | _co.Sat[ii].Clock.DeltaA2);
|
---|
283 | printLine(linePart+line, coTime);
|
---|
284 | }
|
---|
285 |
|
---|
286 | // User Range Accuracy
|
---|
287 | // -------------------
|
---|
288 | else if ( _co.messageType == COTYPE_GPSURA ||
|
---|
289 | _co.messageType == COTYPE_GLONASSURA ) {
|
---|
290 | QString line;
|
---|
291 | line.sprintf(" %3d %d",
|
---|
292 | _co.Sat[ii].IOD, _co.Sat[ii].URA);
|
---|
293 | printLine(linePart+line, coTime);
|
---|
294 | }
|
---|
295 |
|
---|
296 | // High-Resolution Clocks
|
---|
297 | // ----------------------
|
---|
298 | else if ( _co.messageType == COTYPE_GPSHR ||
|
---|
299 | _co.messageType == COTYPE_GLONASSHR ) {
|
---|
300 | QString line;
|
---|
301 | line.sprintf(" %3d %8.3f",
|
---|
302 | _co.Sat[ii].IOD, _co.Sat[ii].hrclock);
|
---|
303 | printLine(linePart+line, coTime);
|
---|
304 | }
|
---|
305 | }
|
---|
306 | }
|
---|
307 |
|
---|
308 | // Loop over all satellites (GPS and Glonass)
|
---|
309 | // ------------------------------------------
|
---|
310 | for (int ii = 0; ii < CLOCKORBIT_NUMGPS + _bias.NumberOfGLONASSSat; ii++) {
|
---|
311 | char sysCh = ' ';
|
---|
312 | if (ii < _bias.NumberOfGPSSat) {
|
---|
313 | sysCh = 'G';
|
---|
314 | }
|
---|
315 | else if (ii >= CLOCKORBIT_NUMGPS) {
|
---|
316 | sysCh = 'R';
|
---|
317 | }
|
---|
318 | if (sysCh != ' ') {
|
---|
319 | QString line;
|
---|
320 | line.sprintf("%d %d %d %.1f %c%2.2d %d",
|
---|
321 | _bias.messageType, _bias.UpdateInterval, GPSweek, _GPSweeks,
|
---|
322 | sysCh, _bias.Sat[ii].ID,
|
---|
323 | _bias.Sat[ii].NumberOfCodeBiases);
|
---|
324 | for (int jj = 0; jj < _bias.Sat[ii].NumberOfCodeBiases; jj++) {
|
---|
325 | QString hlp;
|
---|
326 | hlp.sprintf(" %d %8.3f", _bias.Sat[ii].Biases[jj].Type,
|
---|
327 | _bias.Sat[ii].Biases[jj].Bias);
|
---|
328 | line += hlp;
|
---|
329 | }
|
---|
330 | printLine(line, coTime);
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | retCode = success;
|
---|
335 | memset(&_co, 0, sizeof(_co));
|
---|
336 | memset(&_bias, 0, sizeof(_bias));
|
---|
337 | }
|
---|
338 | }
|
---|
339 | }
|
---|
340 |
|
---|
341 | if (retCode != success) {
|
---|
342 | _GPSweeks = -1.0;
|
---|
343 | }
|
---|
344 | return retCode;
|
---|
345 | }
|
---|
346 |
|
---|
347 | //
|
---|
348 | ////////////////////////////////////////////////////////////////////////////
|
---|
349 | void RTCM3coDecoder::printLine(const QString& line, long coTime) {
|
---|
350 | if (_out) {
|
---|
351 | *_out << line.toAscii().data() << endl;
|
---|
352 | _out->flush();
|
---|
353 | }
|
---|
354 |
|
---|
355 | emit newCorrLine(line, _staID, coTime);
|
---|
356 | }
|
---|