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

Last change on this file since 1485 was 1262, checked in by weber, 16 years ago

* empty log message *

File size: 13.9 KB
RevLine 
[756]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
[858]17#include <math.h>
[756]18#include <iostream>
[800]19#include <newmatio.h>
[756]20
21#include "bns.h"
[799]22#include "bnsutils.h"
[847]23#include "bnsrinex.h"
[848]24#include "bnssp3.h"
[756]25
26using namespace std;
27
28// Constructor
29////////////////////////////////////////////////////////////////////////////
[757]30t_bns::t_bns(QObject* parent) : QThread(parent) {
[760]31
[764]32 this->setTerminationEnabled(true);
[828]33
[836]34 connect(this, SIGNAL(moveSocket(QThread*)),
35 this, SLOT(slotMoveSocket(QThread*)));
36
[979]37 QSettings settings;
38
39 // Set Proxy (application-wide)
40 // ----------------------------
41 QString proxyHost = settings.value("proxyHost").toString();
42 int proxyPort = settings.value("proxyPort").toInt();
[980]43
44 QNetworkProxy proxy;
45 if (proxyHost.isEmpty()) {
46 proxy.setType(QNetworkProxy::NoProxy);
47 }
48 else {
[979]49 proxy.setType(QNetworkProxy::Socks5Proxy);
50 proxy.setHostName(proxyHost);
51 proxy.setPort(proxyPort);
52 }
[980]53 QNetworkProxy::setApplicationProxy(proxy);
[979]54
[828]55 // Thread that handles broadcast ephemeris
56 // ---------------------------------------
57 _bnseph = new t_bnseph(parent);
[827]58
[1059]59 connect(_bnseph, SIGNAL(newEph(t_eph*, int)),
[1058]60 this, SLOT(slotNewEph(t_eph*, int)));
[828]61 connect(_bnseph, SIGNAL(newMessage(QByteArray)),
62 this, SLOT(slotMessage(const QByteArray)));
63 connect(_bnseph, SIGNAL(error(QByteArray)),
64 this, SLOT(slotError(const QByteArray)));
[760]65
[827]66 // Server listening for rtnet results
67 // ----------------------------------
[828]68 _clkSocket = 0;
[827]69 _clkServer = new QTcpServer;
70 _clkServer->listen(QHostAddress::Any, settings.value("clkPort").toInt());
[828]71 connect(_clkServer, SIGNAL(newConnection()),this, SLOT(slotNewConnection()));
[827]72
[828]73 // Socket and file for outputting the results
74 // -------------------------------------------
[1067]75 for (int ic = 1; ic <= 2; ic++) {
[827]76
[1069]77 QString mountpoint = settings.value(QString("mountpoint_%1").arg(ic)).toString();
[1123]78 QString outFileName = settings.value(QString("outFile_%1").arg(ic)).toString();
79 if (!mountpoint.isEmpty() || !outFileName.isEmpty()) {
[1069]80 QString refSys = settings.value(QString("refSys_%1").arg(ic)).toString();
[1067]81
[1249]82 _caster.push_back(new t_bnscaster(mountpoint, outFileName, refSys, ic));
[1068]83 connect(_caster.back(), SIGNAL(error(const QByteArray)),
84 this, SLOT(slotError(const QByteArray)));
85 connect(_caster.back(), SIGNAL(newMessage(const QByteArray)),
86 this, SLOT(slotMessage(const QByteArray)));
87 }
[1067]88 }
89
[1065]90 // Log File
91 // --------
[983]92 QIODevice::OpenMode oMode;
93 if (Qt::CheckState(settings.value("fileAppend").toInt()) == Qt::Checked) {
94 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered | QIODevice::Append;
95 }
96 else {
97 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered;
98 }
99
[816]100 QString logFileName = settings.value("logFile").toString();
101 if (logFileName.isEmpty()) {
[947]102 _logFile = 0;
103 _logStream = 0;
[812]104 }
[816]105 else {
106 _logFile = new QFile(logFileName);
[983]107 if (_logFile->open(oMode)) {
[816]108 _logStream = new QTextStream(_logFile);
109 }
[948]110 else {
111 _logStream = 0;
112 }
[816]113 }
[847]114
[1072]115 // Echo input from RTNet into a file
116 // ---------------------------------
117 QString echoFileName = settings.value("inpEcho").toString();
118 if (echoFileName.isEmpty()) {
119 _echoFile = 0;
120 _echoStream = 0;
121 }
122 else {
123 _echoFile = new QFile(echoFileName);
124 if (_echoFile->open(oMode)) {
125 _echoStream = new QTextStream(_echoFile);
126 }
127 else {
128 _echoStream = 0;
129 }
130 }
131
[847]132 // RINEX writer
133 // ------------
134 if ( settings.value("rnxPath").toString().isEmpty() ) {
135 _rnx = 0;
136 }
137 else {
[850]138 QString prep = "BNS";
[857]139 QString ext = ".clk";
[850]140 QString path = settings.value("rnxPath").toString();
141 QString intr = settings.value("rnxIntr").toString();
142 int sampl = settings.value("rnxSampl").toInt();
143 _rnx = new bnsRinex(prep, ext, path, intr, sampl);
[847]144 }
[848]145
146 // SP3 writer
147 // ----------
148 if ( settings.value("sp3Path").toString().isEmpty() ) {
149 _sp3 = 0;
150 }
151 else {
[850]152 QString prep = "BNS";
[857]153 QString ext = ".sp3";
[850]154 QString path = settings.value("sp3Path").toString();
155 QString intr = settings.value("sp3Intr").toString();
156 int sampl = settings.value("sp3Sampl").toInt();
157 _sp3 = new bnsSP3(prep, ext, path, intr, sampl);
[848]158 }
[756]159}
160
161// Destructor
162////////////////////////////////////////////////////////////////////////////
[757]163t_bns::~t_bns() {
[763]164 deleteBnsEph();
[769]165 delete _clkServer;
[837]166 delete _clkSocket;
[1066]167 for (int ic = 0; ic < _caster.size(); ic++) {
168 delete _caster.at(ic);
169 }
[816]170 delete _logStream;
[812]171 delete _logFile;
[1072]172 delete _echoStream;
173 delete _echoFile;
[779]174 QMapIterator<QString, t_ephPair*> it(_ephList);
175 while (it.hasNext()) {
176 it.next();
177 delete it.value();
178 }
[849]179 delete _rnx;
180 delete _sp3;
[756]181}
182
[763]183// Delete bns thread
184////////////////////////////////////////////////////////////////////////////
185void t_bns::deleteBnsEph() {
186 if (_bnseph) {
187 _bnseph->terminate();
[764]188 _bnseph->wait(100);
[763]189 delete _bnseph;
190 _bnseph = 0;
191 }
192}
193
[756]194// Write a Program Message
195////////////////////////////////////////////////////////////////////////////
[758]196void t_bns::slotMessage(const QByteArray msg) {
[816]197 if (_logStream) {
[1217]198 QString txt = QDateTime::currentDateTime().toUTC().toString("yy-MM-dd hh:mm:ss ");
199 *_logStream << txt << msg << endl;
[818]200 _logStream->flush();
[812]201 }
[757]202 emit(newMessage(msg));
[756]203}
204
[760]205// Write a Program Message
206////////////////////////////////////////////////////////////////////////////
207void t_bns::slotError(const QByteArray msg) {
[816]208 if (_logStream) {
209 *_logStream << msg << endl;
[818]210 _logStream->flush();
[812]211 }
[763]212 deleteBnsEph();
[760]213 emit(error(msg));
214}
215
[769]216// New Connection
217////////////////////////////////////////////////////////////////////////////
218void t_bns::slotNewConnection() {
[1208]219//slotMessage("t_bns::slotNewConnection");
220 slotMessage("Clocks & orbits port: Waiting for client to connect"); // weber
[787]221 delete _clkSocket;
[769]222 _clkSocket = _clkServer->nextPendingConnection();
223}
224
[784]225//
226////////////////////////////////////////////////////////////////////////////
[1058]227void t_bns::slotNewEph(t_eph* ep, int nBytes) {
[784]228
229 QMutexLocker locker(&_mutex);
230
[1058]231 emit(newEphBytes(nBytes));
232
[784]233 t_ephPair* pair;
[884]234 if ( !_ephList.contains(ep->prn()) ) {
[784]235 pair = new t_ephPair();
[884]236 _ephList.insert(ep->prn(), pair);
[784]237 }
238 else {
[884]239 pair = _ephList[ep->prn()];
[784]240 }
241
242 if (pair->eph == 0) {
243 pair->eph = ep;
244 }
245 else {
[884]246 if (ep->isNewerThan(pair->eph)) {
[784]247 delete pair->oldEph;
248 pair->oldEph = pair->eph;
249 pair->eph = ep;
250 }
251 else {
252 delete ep;
253 }
254 }
255}
256
[756]257// Start
258////////////////////////////////////////////////////////////////////////////
[757]259void t_bns::run() {
[769]260
[758]261 slotMessage("============ Start BNS ============");
[770]262
[828]263 // Start Thread that retrieves broadcast Ephemeris
264 // -----------------------------------------------
[758]265 _bnseph->start();
[769]266
[770]267 // Endless loop
268 // ------------
[769]269 while (true) {
[836]270
271 if (_clkSocket && _clkSocket->thread() != currentThread()) {
272 emit(moveSocket(currentThread()));
273 }
274
[796]275 if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
276 if ( _clkSocket->canReadLine()) {
277 readEpoch();
278 }
[809]279 else {
280 _clkSocket->waitForReadyRead(10);
281 }
[769]282 }
283 else {
[794]284 msleep(10);
[769]285 }
286 }
[756]287}
288
[778]289//
290////////////////////////////////////////////////////////////////////////////
[784]291void t_bns::readEpoch() {
[778]292
[1197]293 // Read the first line (if not already read)
294 // -----------------------------------------
295 if (_clkLine.indexOf('*') == -1) {
296 _clkLine = _clkSocket->readLine();
297 if (_echoStream) {
298 *_echoStream << _clkLine;
299 _echoStream->flush();
300 }
301 emit(newClkBytes(_clkLine.length()));
[1072]302 }
303
[1197]304 if (_clkLine.indexOf('*') == -1) {
[784]305 return;
[778]306 }
307
[1197]308 QTextStream in(_clkLine);
[784]309
310 QString hlp;
[1197]311 int year, month, day, hour, min;
312 double sec;
313 in >> hlp >> year >> month >> day >> hour >> min >> sec;
314
315 int GPSweek;
[798]316 double GPSweeks;
[784]317
[1197]318 GPSweekFromYMDhms(year, month, day, hour, min, sec, GPSweek, GPSweeks);
[784]319
[1197]320 QStringList prns;
[874]321
[1197]322 // Loop over all satellites
323 // ------------------------
324 QStringList lines;
325 for (;;) {
326 if (!_clkSocket->canReadLine()) {
[1198]327 break;
[1197]328 }
329 _clkLine = _clkSocket->readLine();
330 if (_echoStream) {
331 *_echoStream << _clkLine;
332 _echoStream->flush();
333 }
334 if (_clkLine[0] == '*') {
335 return;
336 }
337 if (_clkLine[0] == 'P') {
338 _clkLine.remove(0,1);
339 lines.push_back(_clkLine);
340 }
341 }
342
343 if (lines.size() > 0) {
344
[922]345 QStringList prns;
346
[1066]347 for (int ic = 0; ic < _caster.size(); ic++) {
348 _caster.at(ic)->open();
[927]349
[1066]350 for (int oldEph = 0; oldEph <= 0; oldEph++) { // TODO: handle old ephemeris
351
352 struct ClockOrbit co;
353 memset(&co, 0, sizeof(co));
354 co.GPSEpochTime = (int)GPSweeks;
355 co.GLONASSEpochTime = (int)fmod(GPSweeks, 86400.0);
356 co.ClockDataSupplied = 1;
357 co.OrbitDataSupplied = 1;
358 co.SatRefPoint = POINT_CENTER;
359 co.SatRefDatum = DATUM_ITRF;
360
[1197]361 for (int ii = 0; ii < lines.size(); ii++) {
362
[1066]363 QString prn;
364 ColumnVector xx(5);
365 t_eph* ep = 0;
366
[1075]367 if (oldEph == 0 && ic == 0) {
[1197]368 QTextStream in(lines[ii].toAscii());
[1066]369 in >> prn;
370 prns << prn;
371 if ( _ephList.contains(prn) ) {
[1197]372 in >> xx(1) >> xx(2) >> xx(3) >> xx(4);
373 xx(1) *= 1e3;
374 xx(2) *= 1e3;
375 xx(3) *= 1e3;
376 xx(4) *= 1e-6;
[1066]377
378 t_ephPair* pair = _ephList[prn];
379 pair->xx = xx;
380 ep = pair->eph;
381 }
[872]382 }
[1066]383 else {
[1197]384 prn = prns[ii];
[1066]385 if ( _ephList.contains(prn) ) {
386 t_ephPair* pair = _ephList[prn];
387 prn = pair->eph->prn();
388 xx = pair->xx;
[1076]389 if (oldEph) {
390 ep = pair->oldEph;
391 }
392 else {
393 ep = pair->eph;
394 }
[1066]395 }
[873]396 }
[1066]397
398 if (ep != 0) {
399 struct ClockOrbit::SatData* sd = 0;
400 if (prn[0] == 'G') {
401 sd = co.Sat + co.NumberOfGPSSat;
402 ++co.NumberOfGPSSat;
403 }
404 else if (prn[0] == 'R') {
405 sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
406 ++co.NumberOfGLONASSSat;
407 }
408 if (sd) {
409 QString outLine;
[1102]410 processSatellite(oldEph, ic, _caster.at(ic)->crdTrafo(), ep,
411 GPSweek, GPSweeks, prn, xx, sd, outLine);
[1074]412 _caster.at(ic)->printAscii(outLine);
[1066]413 }
414 }
[873]415 }
[1066]416
[1123]417 if ( _caster.at(ic)->usedSocket() &&
[1066]418 (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) ) {
419 char obuffer[CLOCKORBIT_BUFFERSIZE];
420 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
421 if (len > 0) {
[1262]422 if (_caster.at(ic)->ic() == 1) { emit(newOutBytes1(len));}
423 if (_caster.at(ic)->ic() == 2) { emit(newOutBytes2(len));}
[1066]424 _caster.at(ic)->write(obuffer, len);
[872]425 }
426 }
[863]427 }
428 }
[780]429 }
[778]430}
[784]431
432//
433////////////////////////////////////////////////////////////////////////////
[1102]434void t_bns::processSatellite(int oldEph, int iCaster, bool trafo, t_eph* ep,
435 int GPSweek, double GPSweeks, const QString& prn,
[1066]436 const ColumnVector& xx,
[1065]437 struct ClockOrbit::SatData* sd,
438 QString& outLine) {
[784]439
[799]440 ColumnVector xB(4);
[802]441 ColumnVector vv(3);
[799]442
[884]443 ep->position(GPSweek, GPSweeks, xB, vv);
[799]444
[984]445 ColumnVector xyz = xx.Rows(1,3);
[1066]446 if (trafo) {
[985]447 crdTrafo(GPSweek, xyz);
[984]448 }
[927]449
[984]450 ColumnVector dx = xyz - xB.Rows(1,3);
451
[804]452 double dClk = (xx(4) - xB(4)) * 299792458.0;
453 ColumnVector rsw(3);
[800]454
[806]455 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
[804]456
[863]457 if (sd) {
458 sd->ID = prn.mid(1).toInt();
[884]459 sd->IOD = ep->IOD();
[862]460 sd->Clock.DeltaA0 = dClk;
461 sd->Orbit.DeltaRadial = rsw(1);
462 sd->Orbit.DeltaAlongTrack = rsw(2);
463 sd->Orbit.DeltaCrossTrack = rsw(3);
[863]464 }
[862]465
[1065]466 char oldCh = (oldEph ? '!' : ' ');
467 outLine.sprintf("%c %d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
468 oldCh, GPSweek, GPSweeks, ep->prn().toAscii().data(),
469 ep->IOD(), dClk, rsw(1), rsw(2), rsw(3));
470
[1102]471 if (!oldEph && iCaster == 0) {
[923]472 if (_rnx) {
473 _rnx->write(GPSweek, GPSweeks, prn, xx);
474 }
475 if (_sp3) {
476 _sp3->write(GPSweek, GPSweeks, prn, xx);
477 }
[847]478 }
[784]479}
[836]480
481//
482////////////////////////////////////////////////////////////////////////////
483void t_bns::slotMoveSocket(QThread* tt) {
484 _clkSocket->setParent(0);
485 _clkSocket->moveToThread(tt);
[1209]486//slotMessage("bns::slotMoveSocket");
487 slotMessage("Clocks & orbits port: Socket moved to thread"); // weber
[836]488}
[984]489
490// Transform Coordinates IGS -> ETRF
491////////////////////////////////////////////////////////////////////////////
[985]492void t_bns::crdTrafo(int GPSWeek, ColumnVector& xyz) {
[984]493
[1243]494 // Current epoch minus 2000.0 in years
495 // ------------------------------------
496 double dt = (GPSWeek - (1042.0+6.0/7.0)) / 365.2422 * 7.0;
497
[985]498 ColumnVector dx(3);
[1243]499 dx(1) = 0.0541 - dt * 0.0002;
500 dx(2) = 0.0502 + dt * 0.0001;
501 dx(3) = -0.0538 - dt * 0.0018;
502
[1100]503 static const double arcSec = 180.0 * 3600.0 / M_PI;
[1245]504 double ox = ( 0.000891 + dt * 0.000081) / arcSec;
505 double oy = ( 0.005390 + dt * 0.000490) / arcSec;
506 double oz = (-0.008712 - dt * 0.000792) / arcSec;
[984]507
[1245]508 double sc = 1.0 + 0.4e-9 + dt * 0.08e-9;
[1243]509
[1245]510 Matrix rMat(3,3);
511 rMat(1,1) = 1.0;
[985]512 rMat(1,2) = -oz;
513 rMat(1,3) = oy;
514 rMat(2,1) = oz;
[1245]515 rMat(2,2) = 1.0;
[985]516 rMat(2,3) = -ox;
517 rMat(3,1) = -oy;
518 rMat(3,2) = ox;
[1245]519 rMat(3,3) = 1.0;
[985]520
521
[1245]522 xyz = sc * rMat * xyz + dx;
[984]523}
Note: See TracBrowser for help on using the repository browser.