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

Last change on this file since 1215 was 1209, checked in by weber, 18 years ago

* empty log message *

File size: 13.5 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
[1068]82 _caster.push_back(new t_bnscaster(mountpoint, outFileName, refSys));
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) {
198 *_logStream << msg << endl;
[818]199 _logStream->flush();
[812]200 }
[757]201 emit(newMessage(msg));
[756]202}
203
[760]204// Write a Program Message
205////////////////////////////////////////////////////////////////////////////
206void t_bns::slotError(const QByteArray msg) {
[816]207 if (_logStream) {
208 *_logStream << msg << endl;
[818]209 _logStream->flush();
[812]210 }
[763]211 deleteBnsEph();
[760]212 emit(error(msg));
213}
214
[769]215// New Connection
216////////////////////////////////////////////////////////////////////////////
217void t_bns::slotNewConnection() {
[1208]218//slotMessage("t_bns::slotNewConnection");
219 slotMessage("Clocks & orbits port: Waiting for client to connect"); // weber
[787]220 delete _clkSocket;
[769]221 _clkSocket = _clkServer->nextPendingConnection();
222}
223
[784]224//
225////////////////////////////////////////////////////////////////////////////
[1058]226void t_bns::slotNewEph(t_eph* ep, int nBytes) {
[784]227
228 QMutexLocker locker(&_mutex);
229
[1058]230 emit(newEphBytes(nBytes));
231
[784]232 t_ephPair* pair;
[884]233 if ( !_ephList.contains(ep->prn()) ) {
[784]234 pair = new t_ephPair();
[884]235 _ephList.insert(ep->prn(), pair);
[784]236 }
237 else {
[884]238 pair = _ephList[ep->prn()];
[784]239 }
240
241 if (pair->eph == 0) {
242 pair->eph = ep;
243 }
244 else {
[884]245 if (ep->isNewerThan(pair->eph)) {
[784]246 delete pair->oldEph;
247 pair->oldEph = pair->eph;
248 pair->eph = ep;
249 }
250 else {
251 delete ep;
252 }
253 }
254}
255
[756]256// Start
257////////////////////////////////////////////////////////////////////////////
[757]258void t_bns::run() {
[769]259
[758]260 slotMessage("============ Start BNS ============");
[770]261
[828]262 // Start Thread that retrieves broadcast Ephemeris
263 // -----------------------------------------------
[758]264 _bnseph->start();
[769]265
[770]266 // Endless loop
267 // ------------
[769]268 while (true) {
[836]269
270 if (_clkSocket && _clkSocket->thread() != currentThread()) {
271 emit(moveSocket(currentThread()));
272 }
273
[796]274 if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
275 if ( _clkSocket->canReadLine()) {
276 readEpoch();
277 }
[809]278 else {
279 _clkSocket->waitForReadyRead(10);
280 }
[769]281 }
282 else {
[794]283 msleep(10);
[769]284 }
285 }
[756]286}
287
[778]288//
289////////////////////////////////////////////////////////////////////////////
[784]290void t_bns::readEpoch() {
[778]291
[1197]292 // Read the first line (if not already read)
293 // -----------------------------------------
294 if (_clkLine.indexOf('*') == -1) {
295 _clkLine = _clkSocket->readLine();
296 if (_echoStream) {
297 *_echoStream << _clkLine;
298 _echoStream->flush();
299 }
300 emit(newClkBytes(_clkLine.length()));
[1072]301 }
302
[1197]303 if (_clkLine.indexOf('*') == -1) {
[784]304 return;
[778]305 }
306
[1197]307 QTextStream in(_clkLine);
[784]308
309 QString hlp;
[1197]310 int year, month, day, hour, min;
311 double sec;
312 in >> hlp >> year >> month >> day >> hour >> min >> sec;
313
314 int GPSweek;
[798]315 double GPSweeks;
[784]316
[1197]317 GPSweekFromYMDhms(year, month, day, hour, min, sec, GPSweek, GPSweeks);
[784]318
[1197]319 QStringList prns;
[874]320
[1197]321 // Loop over all satellites
322 // ------------------------
323 QStringList lines;
324 for (;;) {
325 if (!_clkSocket->canReadLine()) {
[1198]326 break;
[1197]327 }
328 _clkLine = _clkSocket->readLine();
329 if (_echoStream) {
330 *_echoStream << _clkLine;
331 _echoStream->flush();
332 }
333 if (_clkLine[0] == '*') {
334 return;
335 }
336 if (_clkLine[0] == 'P') {
337 _clkLine.remove(0,1);
338 lines.push_back(_clkLine);
339 }
340 }
341
342 if (lines.size() > 0) {
343
[922]344 QStringList prns;
345
[1066]346 for (int ic = 0; ic < _caster.size(); ic++) {
347 _caster.at(ic)->open();
[927]348
[1066]349 for (int oldEph = 0; oldEph <= 0; oldEph++) { // TODO: handle old ephemeris
350
351 struct ClockOrbit co;
352 memset(&co, 0, sizeof(co));
353 co.GPSEpochTime = (int)GPSweeks;
354 co.GLONASSEpochTime = (int)fmod(GPSweeks, 86400.0);
355 co.ClockDataSupplied = 1;
356 co.OrbitDataSupplied = 1;
357 co.SatRefPoint = POINT_CENTER;
358 co.SatRefDatum = DATUM_ITRF;
359
[1197]360 for (int ii = 0; ii < lines.size(); ii++) {
361
[1066]362 QString prn;
363 ColumnVector xx(5);
364 t_eph* ep = 0;
365
[1075]366 if (oldEph == 0 && ic == 0) {
[1197]367 QTextStream in(lines[ii].toAscii());
[1066]368 in >> prn;
369 prns << prn;
370 if ( _ephList.contains(prn) ) {
[1197]371 in >> xx(1) >> xx(2) >> xx(3) >> xx(4);
372 xx(1) *= 1e3;
373 xx(2) *= 1e3;
374 xx(3) *= 1e3;
375 xx(4) *= 1e-6;
[1066]376
377 t_ephPair* pair = _ephList[prn];
378 pair->xx = xx;
379 ep = pair->eph;
380 }
[872]381 }
[1066]382 else {
[1197]383 prn = prns[ii];
[1066]384 if ( _ephList.contains(prn) ) {
385 t_ephPair* pair = _ephList[prn];
386 prn = pair->eph->prn();
387 xx = pair->xx;
[1076]388 if (oldEph) {
389 ep = pair->oldEph;
390 }
391 else {
392 ep = pair->eph;
393 }
[1066]394 }
[873]395 }
[1066]396
397 if (ep != 0) {
398 struct ClockOrbit::SatData* sd = 0;
399 if (prn[0] == 'G') {
400 sd = co.Sat + co.NumberOfGPSSat;
401 ++co.NumberOfGPSSat;
402 }
403 else if (prn[0] == 'R') {
404 sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
405 ++co.NumberOfGLONASSSat;
406 }
407 if (sd) {
408 QString outLine;
[1102]409 processSatellite(oldEph, ic, _caster.at(ic)->crdTrafo(), ep,
410 GPSweek, GPSweeks, prn, xx, sd, outLine);
[1074]411 _caster.at(ic)->printAscii(outLine);
[1066]412 }
413 }
[873]414 }
[1066]415
[1123]416 if ( _caster.at(ic)->usedSocket() &&
[1066]417 (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) ) {
418 char obuffer[CLOCKORBIT_BUFFERSIZE];
419 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
420 if (len > 0) {
421 emit(newOutBytes(len));
422 _caster.at(ic)->write(obuffer, len);
[872]423 }
424 }
[863]425 }
426 }
[780]427 }
[778]428}
[784]429
430//
431////////////////////////////////////////////////////////////////////////////
[1102]432void t_bns::processSatellite(int oldEph, int iCaster, bool trafo, t_eph* ep,
433 int GPSweek, double GPSweeks, const QString& prn,
[1066]434 const ColumnVector& xx,
[1065]435 struct ClockOrbit::SatData* sd,
436 QString& outLine) {
[784]437
[799]438 ColumnVector xB(4);
[802]439 ColumnVector vv(3);
[799]440
[884]441 ep->position(GPSweek, GPSweeks, xB, vv);
[799]442
[984]443 ColumnVector xyz = xx.Rows(1,3);
[1066]444 if (trafo) {
[985]445 crdTrafo(GPSweek, xyz);
[984]446 }
[927]447
[984]448 ColumnVector dx = xyz - xB.Rows(1,3);
449
[804]450 double dClk = (xx(4) - xB(4)) * 299792458.0;
451 ColumnVector rsw(3);
[800]452
[806]453 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
[804]454
[863]455 if (sd) {
456 sd->ID = prn.mid(1).toInt();
[884]457 sd->IOD = ep->IOD();
[862]458 sd->Clock.DeltaA0 = dClk;
459 sd->Orbit.DeltaRadial = rsw(1);
460 sd->Orbit.DeltaAlongTrack = rsw(2);
461 sd->Orbit.DeltaCrossTrack = rsw(3);
[863]462 }
[862]463
[1065]464 char oldCh = (oldEph ? '!' : ' ');
465 outLine.sprintf("%c %d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
466 oldCh, GPSweek, GPSweeks, ep->prn().toAscii().data(),
467 ep->IOD(), dClk, rsw(1), rsw(2), rsw(3));
468
[1102]469 if (!oldEph && iCaster == 0) {
[923]470 if (_rnx) {
471 _rnx->write(GPSweek, GPSweeks, prn, xx);
472 }
473 if (_sp3) {
474 _sp3->write(GPSweek, GPSweeks, prn, xx);
475 }
[847]476 }
[784]477}
[836]478
479//
480////////////////////////////////////////////////////////////////////////////
481void t_bns::slotMoveSocket(QThread* tt) {
482 _clkSocket->setParent(0);
483 _clkSocket->moveToThread(tt);
[1209]484//slotMessage("bns::slotMoveSocket");
485 slotMessage("Clocks & orbits port: Socket moved to thread"); // weber
[836]486}
[984]487
488// Transform Coordinates IGS -> ETRF
489////////////////////////////////////////////////////////////////////////////
[985]490void t_bns::crdTrafo(int GPSWeek, ColumnVector& xyz) {
[984]491
[985]492 ColumnVector dx(3);
[1017]493 dx(1) = 0.054;
494 dx(2) = 0.051;
495 dx(3) = -0.048;
[1100]496 static const double arcSec = 180.0 * 3600.0 / M_PI;
497 static const double ox = 0.000081 / arcSec;
498 static const double oy = 0.000490 / arcSec;
499 static const double oz = -0.000792 / arcSec;
[984]500
[985]501 Matrix rMat(3,3); rMat = 0.0;
502 rMat(1,2) = -oz;
503 rMat(1,3) = oy;
504 rMat(2,1) = oz;
505 rMat(2,3) = -ox;
506 rMat(3,1) = -oy;
507 rMat(3,2) = ox;
508
509 // Current epoch minus 1989.0 in years
510 // ------------------------------------
511 double dt = (GPSWeek - 469.0) / 365.2422 * 7.0;
512
513 xyz += dx + dt * rMat * xyz;
[984]514}
Note: See TracBrowser for help on using the repository browser.