source: ntrip/trunk/BNS/bns.cpp@ 1064

Last change on this file since 1064 was 1064, checked in by mervart, 16 years ago

* empty log message *

File size: 11.8 KB
Line 
1/* -------------------------------------------------------------------------
2 * BKG NTRIP Server
3 * -------------------------------------------------------------------------
4 *
5 * Class: bns
6 *
7 * Purpose: This class implements the main application behaviour
8 *
9 * Author: L. Mervart
10 *
11 * Created: 29-Mar-2008
12 *
13 * Changes:
14 *
15 * -----------------------------------------------------------------------*/
16
17#include <math.h>
18#include <iostream>
19#include <newmatio.h>
20
21#include "bns.h"
22#include "bnsutils.h"
23#include "bnsrinex.h"
24#include "bnssp3.h"
25
26using namespace std;
27
28// Constructor
29////////////////////////////////////////////////////////////////////////////
30t_bns::t_bns(QObject* parent) : QThread(parent) {
31
32 this->setTerminationEnabled(true);
33
34 connect(this, SIGNAL(moveSocket(QThread*)),
35 this, SLOT(slotMoveSocket(QThread*)));
36
37 QSettings settings;
38
39 // Set Proxy (application-wide)
40 // ----------------------------
41 QString proxyHost = settings.value("proxyHost").toString();
42 int proxyPort = settings.value("proxyPort").toInt();
43
44 QNetworkProxy proxy;
45 if (proxyHost.isEmpty()) {
46 proxy.setType(QNetworkProxy::NoProxy);
47 }
48 else {
49 proxy.setType(QNetworkProxy::Socks5Proxy);
50 proxy.setHostName(proxyHost);
51 proxy.setPort(proxyPort);
52 }
53 QNetworkProxy::setApplicationProxy(proxy);
54
55 // Thread that handles broadcast ephemeris
56 // ---------------------------------------
57 _bnseph = new t_bnseph(parent);
58
59 connect(_bnseph, SIGNAL(newEph(t_eph*, int)),
60 this, SLOT(slotNewEph(t_eph*, int)));
61 connect(_bnseph, SIGNAL(newMessage(QByteArray)),
62 this, SLOT(slotMessage(const QByteArray)));
63 connect(_bnseph, SIGNAL(error(QByteArray)),
64 this, SLOT(slotError(const QByteArray)));
65
66 // Server listening for rtnet results
67 // ----------------------------------
68 _clkSocket = 0;
69 _clkServer = new QTcpServer;
70 _clkServer->listen(QHostAddress::Any, settings.value("clkPort").toInt());
71 connect(_clkServer, SIGNAL(newConnection()),this, SLOT(slotNewConnection()));
72
73 // Socket and file for outputting the results
74 // -------------------------------------------
75 _caster = new t_bnscaster(settings.value("mountpoint").toString());
76 connect(_caster, SIGNAL(error(const QByteArray)),
77 this, SLOT(slotError(const QByteArray)));
78 connect(_caster, SIGNAL(newMessage(const QByteArray)),
79 this, SLOT(slotMessage(const QByteArray)));
80
81 QIODevice::OpenMode oMode;
82 if (Qt::CheckState(settings.value("fileAppend").toInt()) == Qt::Checked) {
83 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered | QIODevice::Append;
84 }
85 else {
86 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered;
87 }
88
89 QString outFileName = settings.value("outFile").toString();
90 if (outFileName.isEmpty()) {
91 _outFile = 0;
92 _outStream = 0;
93 }
94 else {
95 _outFile = new QFile(outFileName);
96 if (_outFile->open(oMode)) {
97 _outStream = new QTextStream(_outFile);
98 }
99 }
100
101 // Log File
102 // --------
103 QString logFileName = settings.value("logFile").toString();
104 if (logFileName.isEmpty()) {
105 _logFile = 0;
106 _logStream = 0;
107 }
108 else {
109 _logFile = new QFile(logFileName);
110 if (_logFile->open(oMode)) {
111 _logStream = new QTextStream(_logFile);
112 }
113 else {
114 _logStream = 0;
115 }
116 }
117
118 // RINEX writer
119 // ------------
120 if ( settings.value("rnxPath").toString().isEmpty() ) {
121 _rnx = 0;
122 }
123 else {
124 QString prep = "BNS";
125 QString ext = ".clk";
126 QString path = settings.value("rnxPath").toString();
127 QString intr = settings.value("rnxIntr").toString();
128 int sampl = settings.value("rnxSampl").toInt();
129 _rnx = new bnsRinex(prep, ext, path, intr, sampl);
130 }
131
132 // SP3 writer
133 // ----------
134 if ( settings.value("sp3Path").toString().isEmpty() ) {
135 _sp3 = 0;
136 }
137 else {
138 QString prep = "BNS";
139 QString ext = ".sp3";
140 QString path = settings.value("sp3Path").toString();
141 QString intr = settings.value("sp3Intr").toString();
142 int sampl = settings.value("sp3Sampl").toInt();
143 _sp3 = new bnsSP3(prep, ext, path, intr, sampl);
144 }
145
146 // Reference frame
147 // ---------------
148 _crdTrafo = false;
149 if (settings.value("refSys").toString() == "ETRS89") {
150 _crdTrafo = true;
151 }
152}
153
154// Destructor
155////////////////////////////////////////////////////////////////////////////
156t_bns::~t_bns() {
157 deleteBnsEph();
158 delete _clkServer;
159 delete _clkSocket;
160 delete _caster;
161 delete _outStream;
162 delete _logStream;
163 delete _outFile;
164 delete _logFile;
165 QMapIterator<QString, t_ephPair*> it(_ephList);
166 while (it.hasNext()) {
167 it.next();
168 delete it.value();
169 }
170 delete _rnx;
171 delete _sp3;
172}
173
174// Delete bns thread
175////////////////////////////////////////////////////////////////////////////
176void t_bns::deleteBnsEph() {
177 if (_bnseph) {
178 _bnseph->terminate();
179 _bnseph->wait(100);
180 delete _bnseph;
181 _bnseph = 0;
182 }
183}
184
185// Write a Program Message
186////////////////////////////////////////////////////////////////////////////
187void t_bns::slotMessage(const QByteArray msg) {
188 if (_logStream) {
189 *_logStream << msg << endl;
190 _logStream->flush();
191 }
192 emit(newMessage(msg));
193}
194
195// Write a Program Message
196////////////////////////////////////////////////////////////////////////////
197void t_bns::slotError(const QByteArray msg) {
198 if (_logStream) {
199 *_logStream << msg << endl;
200 _logStream->flush();
201 }
202 deleteBnsEph();
203 emit(error(msg));
204}
205
206// New Connection
207////////////////////////////////////////////////////////////////////////////
208void t_bns::slotNewConnection() {
209 slotMessage("t_bns::slotNewConnection");
210 delete _clkSocket;
211 _clkSocket = _clkServer->nextPendingConnection();
212}
213
214//
215////////////////////////////////////////////////////////////////////////////
216void t_bns::slotNewEph(t_eph* ep, int nBytes) {
217
218 QMutexLocker locker(&_mutex);
219
220 emit(newEphBytes(nBytes));
221
222 t_ephPair* pair;
223 if ( !_ephList.contains(ep->prn()) ) {
224 pair = new t_ephPair();
225 _ephList.insert(ep->prn(), pair);
226 }
227 else {
228 pair = _ephList[ep->prn()];
229 }
230
231 if (pair->eph == 0) {
232 pair->eph = ep;
233 }
234 else {
235 if (ep->isNewerThan(pair->eph)) {
236 delete pair->oldEph;
237 pair->oldEph = pair->eph;
238 pair->eph = ep;
239 }
240 else {
241 delete ep;
242 }
243 }
244}
245
246// Start
247////////////////////////////////////////////////////////////////////////////
248void t_bns::run() {
249
250 slotMessage("============ Start BNS ============");
251
252 // Start Thread that retrieves broadcast Ephemeris
253 // -----------------------------------------------
254 _bnseph->start();
255
256 // Endless loop
257 // ------------
258 while (true) {
259
260 if (_clkSocket && _clkSocket->thread() != currentThread()) {
261 emit(moveSocket(currentThread()));
262 }
263
264 if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
265 if ( _clkSocket->canReadLine()) {
266 _caster->open();
267 readEpoch();
268 }
269 else {
270 _clkSocket->waitForReadyRead(10);
271 }
272 }
273 else {
274 msleep(10);
275 }
276 }
277}
278
279//
280////////////////////////////////////////////////////////////////////////////
281void t_bns::readEpoch() {
282
283 QByteArray line = _clkSocket->readLine();
284
285 emit(newClkBytes(line.length()));
286
287 if (line.indexOf('*') == -1) {
288 return;
289 }
290
291 QTextStream in(line);
292
293 QString hlp;
294 int GPSweek, numSat;
295 double GPSweeks;
296
297 in >> hlp >> GPSweek >> GPSweeks >> numSat;
298
299 if (numSat > 0) {
300
301 QStringList prns;
302
303 /// for (int oldEph = 0; oldEph <= 1; oldEph++) {
304 for (int oldEph = 0; oldEph <= 0; oldEph++) {
305
306 struct ClockOrbit co;
307 memset(&co, 0, sizeof(co));
308 co.GPSEpochTime = (int)GPSweeks;
309 co.GLONASSEpochTime = (int)fmod(GPSweeks, 86400.0);
310 co.ClockDataSupplied = 1;
311 co.OrbitDataSupplied = 1;
312 co.SatRefPoint = POINT_CENTER;
313 co.SatRefDatum = DATUM_ITRF;
314
315 for (int ii = 1; ii <= numSat; ii++) {
316
317 QString prn;
318 ColumnVector xx(5);
319 t_eph* ep = 0;
320
321 if (oldEph == 0) {
322 line = _clkSocket->readLine();
323 QTextStream in(line);
324 in >> prn;
325 prns << prn;
326 if ( _ephList.contains(prn) ) {
327 in >> xx(1) >> xx(2) >> xx(3) >> xx(4) >> xx(5); xx(4) *= 1e-6;
328
329 //// beg test (zero clock correction for Gerhard Wuebbena)
330 //// xx(4) -= xx(5) / 299792458.0;
331 //// end test
332
333 t_ephPair* pair = _ephList[prn];
334 pair->xx = xx;
335 ep = pair->eph;
336 }
337 }
338 else {
339 prn = prns[ii-1];
340 if ( _ephList.contains(prn) ) {
341 t_ephPair* pair = _ephList[prn];
342 prn = pair->eph->prn();
343 xx = pair->xx;
344 ep = pair->oldEph;
345 }
346 }
347
348 if (ep != 0) {
349 struct ClockOrbit::SatData* sd = 0;
350 if (prn[0] == 'G') {
351 sd = co.Sat + co.NumberOfGPSSat;
352 ++co.NumberOfGPSSat;
353 }
354 else if (prn[0] == 'R') {
355 sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
356 ++co.NumberOfGLONASSSat;
357 }
358 if (sd) {
359 processSatellite(oldEph, ep, GPSweek, GPSweeks, prn, xx, sd);
360 }
361 }
362 }
363
364 if ( (_caster->used() || _outFile) &&
365 (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) ) {
366 char obuffer[CLOCKORBIT_BUFFERSIZE];
367 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
368 if (len > 0) {
369 emit(newOutBytes(len));
370 _caster->write(obuffer, len);
371 }
372 }
373 }
374 }
375}
376
377//
378////////////////////////////////////////////////////////////////////////////
379void t_bns::processSatellite(int oldEph, t_eph* ep, int GPSweek, double GPSweeks,
380 const QString& prn, const ColumnVector& xx,
381 struct ClockOrbit::SatData* sd) {
382
383 ColumnVector xB(4);
384 ColumnVector vv(3);
385
386 ep->position(GPSweek, GPSweeks, xB, vv);
387
388 ColumnVector xyz = xx.Rows(1,3);
389 if (_crdTrafo) {
390 crdTrafo(GPSweek, xyz);
391 }
392
393 ColumnVector dx = xyz - xB.Rows(1,3);
394
395 double dClk = (xx(4) - xB(4)) * 299792458.0;
396 ColumnVector rsw(3);
397
398 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
399
400 if (sd) {
401 sd->ID = prn.mid(1).toInt();
402 sd->IOD = ep->IOD();
403 sd->Clock.DeltaA0 = dClk;
404 sd->Orbit.DeltaRadial = rsw(1);
405 sd->Orbit.DeltaAlongTrack = rsw(2);
406 sd->Orbit.DeltaCrossTrack = rsw(3);
407 }
408
409 if (_outStream) {
410 QString line;
411 char oldCh = (oldEph ? '!' : ' ');
412 line.sprintf("%c %d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
413 oldCh, GPSweek, GPSweeks, ep->prn().toAscii().data(),
414 ep->IOD(), dClk, rsw(1), rsw(2), rsw(3));
415 *_outStream << line;
416 _outStream->flush();
417 }
418 if (!oldEph) {
419 if (_rnx) {
420 _rnx->write(GPSweek, GPSweeks, prn, xx);
421 }
422 if (_sp3) {
423 _sp3->write(GPSweek, GPSweeks, prn, xx);
424 }
425 }
426}
427
428//
429////////////////////////////////////////////////////////////////////////////
430void t_bns::slotMoveSocket(QThread* tt) {
431 _clkSocket->setParent(0);
432 _clkSocket->moveToThread(tt);
433 slotMessage("bns::slotMoveSocket");
434}
435
436// Transform Coordinates IGS -> ETRF
437////////////////////////////////////////////////////////////////////////////
438void t_bns::crdTrafo(int GPSWeek, ColumnVector& xyz) {
439
440 ColumnVector dx(3);
441 dx(1) = 0.054;
442 dx(2) = 0.051;
443 dx(3) = -0.048;
444 const static double arcSec = 180.0 * 3600.0 / M_PI;
445 const static double ox = 0.000081 / arcSec;
446 const static double oy = 0.000490 / arcSec;
447 const static double oz = -0.000792 / arcSec;
448
449 Matrix rMat(3,3); rMat = 0.0;
450 rMat(1,2) = -oz;
451 rMat(1,3) = oy;
452 rMat(2,1) = oz;
453 rMat(2,3) = -ox;
454 rMat(3,1) = -oy;
455 rMat(3,2) = ox;
456
457 // Current epoch minus 1989.0 in years
458 // ------------------------------------
459 double dt = (GPSWeek - 469.0) / 365.2422 * 7.0;
460
461 xyz += dx + dt * rMat * xyz;
462}
Note: See TracBrowser for help on using the repository browser.