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

Last change on this file since 1829 was 1829, checked in by stoecker, 15 years ago

fixed clock and orbit calls

File size: 7.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: 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
50using namespace std;
51
52// Constructor
53////////////////////////////////////////////////////////////////////////////
54RTCM3coDecoder::RTCM3coDecoder(const QString& staID) {
55
56 _staID = staID;
57
58 // File Output
59 // -----------
60 bncSettings settings;
61 QString path = settings.value("corrPath").toString();
62 if (!path.isEmpty()) {
63 expandEnvVar(path);
64 if ( path.length() > 0 && path[path.length()-1] != QDir::separator() ) {
65 path += QDir::separator();
66 }
67 _fileNameSkl = path + staID;
68 }
69 _out = 0;
70 _GPSweeks = -1.0;
71
72 connect(this, SIGNAL(newCorrLine(QString, QString, long)),
73 (bncApp*) qApp, SLOT(slotNewCorrLine(QString, QString, long)));
74
75 memset(&_co, 0, sizeof(_co));
76}
77
78// Destructor
79////////////////////////////////////////////////////////////////////////////
80RTCM3coDecoder::~RTCM3coDecoder() {
81 delete _out;
82}
83
84// Reopen Output File
85////////////////////////////////////////////////////////////////////////
86void RTCM3coDecoder::reopen() {
87
88 if (!_fileNameSkl.isEmpty()) {
89
90 bncSettings settings;
91
92 QDateTime datTim = currentDateAndTimeGPS();
93
94 QString hlpStr = bncRinex::nextEpochStr(datTim,
95 settings.value("corrIntr").toString());
96
97 QString fileName = _fileNameSkl
98 + QString("%1").arg(datTim.date().dayOfYear(), 3, 10, QChar('0'))
99 + hlpStr + datTim.toString(".yyC");
100
101 if (_fileName == fileName) {
102 return;
103 }
104 else {
105 _fileName = fileName;
106 }
107
108 delete _out;
109 if ( Qt::CheckState(settings.value("rnxAppend").toInt()) == Qt::Checked) {
110 _out = new ofstream( _fileName.toAscii().data(),
111 ios_base::out | ios_base::app );
112 }
113 else {
114 _out = new ofstream( _fileName.toAscii().data() );
115 }
116 }
117}
118
119//
120////////////////////////////////////////////////////////////////////////////
121t_irc RTCM3coDecoder::Decode(char* buffer, int bufLen, vector<string>& errmsg) {
122
123 errmsg.clear();
124
125//printf("Start with %d new bytes and %d old bytes\n", bufLen, _buffer.size());
126
127 _buffer.append(QByteArray(buffer,bufLen));
128
129 t_irc retCode = failure;
130
131 while(_buffer.size())
132 {
133 int bytesused = 0;
134 struct ClockOrbit cox;
135 memcpy(&cox, &_co, sizeof(cox)); /* save state */
136
137 GCOB_RETURN irc = GetClockOrbitBias(&_co, &_bias, _buffer.data(),
138 _buffer.size(), &bytesused);
139
140//printf("used %4d buffer %4d return %4d first byte %2x\n", bytesused, _buffer.size(), irc, (unsigned char)_buffer.data()[0]);
141 if(irc <= -30) /* not enough data found */
142 {
143 /* copy previous state back - necessary in case of MESSAGEFOLLOWS with
144 incomplete second block */
145 memcpy(&_co, &cox, sizeof(cox));
146 if (retCode != success) {
147 _GPSweeks = -1.0;
148 }
149 return retCode;
150 }
151 else if(irc >= 0)
152 {
153 _buffer = _buffer.mid(bytesused);
154
155 if(irc == GCOBR_OK) /* correctly and complete decoded */
156 {
157 reopen();
158
159//printf("TIME: gps %d glonass %d gps_tod %d\n", _co.GPSEpochTime, _co.GLONASSEpochTime, _co.GPSEpochTime%86400);
160
161 int GPSweek;
162 currentGPSWeeks(GPSweek, _GPSweeks);
163 if (_co.NumberOfGPSSat > 0) {
164 if (_GPSweeks > _co.GPSEpochTime + 86400.0) {
165 GPSweek += 1;
166 }
167 else if (_GPSweeks < _co.GPSEpochTime - 86400.0) {
168 GPSweek -= 1;
169 }
170 _GPSweeks = _co.GPSEpochTime;
171 }
172 else {
173 double GPSdaysec = fmod(_GPSweeks, 86400.0);
174 int weekDay = int((_GPSweeks - GPSdaysec) / 86400.0);
175 if (GPSdaysec > _co.GLONASSEpochTime + 3600.0) {
176 weekDay += 1;
177 if (weekDay > 6) {
178 weekDay = 0;
179 GPSweek += 1;
180 }
181 }
182 else if (GPSdaysec < _co.GLONASSEpochTime - 3600.0) {
183 weekDay -= 1;
184 if (weekDay < 0) {
185 weekDay = 6;
186 GPSweek -= 1;
187 }
188 }
189 _GPSweeks = weekDay * 86400.0 + _co.GLONASSEpochTime;
190 }
191
192 for(int ii = 0; ii < _co.NumberOfGPSSat; ++ii) {
193 QString line;
194 line.sprintf("%d %.1f G%2.2d %3d %8.3f %8.3f %8.3f %8.3f",
195 GPSweek, _GPSweeks, _co.Sat[ii].ID, _co.Sat[ii].IOD,
196 _co.Sat[ii].Clock.DeltaA0,
197 _co.Sat[ii].Orbit.DeltaRadial,
198 _co.Sat[ii].Orbit.DeltaAlongTrack,
199 _co.Sat[ii].Orbit.DeltaCrossTrack);
200 long coTime = GPSweek * 7*24*3600 + long(floor(_GPSweeks+0.5));
201 printLine(line, coTime);
202 }
203 for(int ii = CLOCKORBIT_NUMGPS;
204 ii < CLOCKORBIT_NUMGPS + _co.NumberOfGLONASSSat; ++ii) {
205 QString line;
206 line.sprintf("%d %.1f R%2.2d %3d %8.3f %8.3f %8.3f %8.3f",
207 GPSweek, _GPSweeks, _co.Sat[ii].ID, _co.Sat[ii].IOD,
208 _co.Sat[ii].Clock.DeltaA0,
209 _co.Sat[ii].Orbit.DeltaRadial,
210 _co.Sat[ii].Orbit.DeltaAlongTrack,
211 _co.Sat[ii].Orbit.DeltaCrossTrack);
212 long coTime = GPSweek * 7*24*3600 + long(floor(_GPSweeks+0.5));
213 printLine(line, coTime);
214 }
215 retCode = success;
216 memset(&_co, 0, sizeof(_co));
217 }
218 }
219 else /* error - skip 1 byte and retry */
220 {
221 memset(&_co, 0, sizeof(_co));
222 _buffer = _buffer.mid(1);
223 }
224 }
225//printf("Return with %d (success = %d) bytes %d\n", retCode, success, _buffer.size());
226 return retCode;
227}
228
229//
230////////////////////////////////////////////////////////////////////////////
231void RTCM3coDecoder::printLine(const QString& line, long coTime) {
232 if (_out) {
233 *_out << line.toAscii().data() << endl;
234 _out->flush();
235 }
236
237 emit newCorrLine(line, _staID, coTime);
238}
Note: See TracBrowser for help on using the repository browser.