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

Last change on this file since 1812 was 1803, checked in by weber, 16 years ago

* empty log message *

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