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

Last change on this file since 1747 was 1740, checked in by weber, 16 years ago

* empty log message *

File size: 14.1 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
[1065]88 // Log File
89 // --------
[983]90 QIODevice::OpenMode oMode;
91 if (Qt::CheckState(settings.value("fileAppend").toInt()) == Qt::Checked) {
92 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered | QIODevice::Append;
93 }
94 else {
95 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered;
96 }
97
[816]98 QString logFileName = settings.value("logFile").toString();
99 if (logFileName.isEmpty()) {
[947]100 _logFile = 0;
101 _logStream = 0;
[812]102 }
[816]103 else {
104 _logFile = new QFile(logFileName);
[983]105 if (_logFile->open(oMode)) {
[816]106 _logStream = new QTextStream(_logFile);
107 }
[948]108 else {
109 _logStream = 0;
110 }
[816]111 }
[847]112
[1072]113 // Echo input from RTNet into a file
114 // ---------------------------------
115 QString echoFileName = settings.value("inpEcho").toString();
116 if (echoFileName.isEmpty()) {
117 _echoFile = 0;
118 _echoStream = 0;
119 }
120 else {
121 _echoFile = new QFile(echoFileName);
122 if (_echoFile->open(oMode)) {
123 _echoStream = new QTextStream(_echoFile);
124 }
125 else {
126 _echoStream = 0;
127 }
128 }
129
[847]130 // RINEX writer
131 // ------------
132 if ( settings.value("rnxPath").toString().isEmpty() ) {
133 _rnx = 0;
134 }
135 else {
[850]136 QString prep = "BNS";
[857]137 QString ext = ".clk";
[850]138 QString path = settings.value("rnxPath").toString();
139 QString intr = settings.value("rnxIntr").toString();
140 int sampl = settings.value("rnxSampl").toInt();
141 _rnx = new bnsRinex(prep, ext, path, intr, sampl);
[847]142 }
[848]143
144 // SP3 writer
145 // ----------
146 if ( settings.value("sp3Path").toString().isEmpty() ) {
147 _sp3 = 0;
148 }
149 else {
[850]150 QString prep = "BNS";
[857]151 QString ext = ".sp3";
[850]152 QString path = settings.value("sp3Path").toString();
153 QString intr = settings.value("sp3Intr").toString();
154 int sampl = settings.value("sp3Sampl").toInt();
155 _sp3 = new bnsSP3(prep, ext, path, intr, sampl);
[848]156 }
[756]157}
158
159// Destructor
160////////////////////////////////////////////////////////////////////////////
[757]161t_bns::~t_bns() {
[763]162 deleteBnsEph();
[769]163 delete _clkServer;
[837]164 delete _clkSocket;
[1066]165 for (int ic = 0; ic < _caster.size(); ic++) {
166 delete _caster.at(ic);
167 }
[816]168 delete _logStream;
[812]169 delete _logFile;
[1072]170 delete _echoStream;
171 delete _echoFile;
[779]172 QMapIterator<QString, t_ephPair*> it(_ephList);
173 while (it.hasNext()) {
174 it.next();
175 delete it.value();
176 }
[849]177 delete _rnx;
178 delete _sp3;
[756]179}
180
[763]181// Delete bns thread
182////////////////////////////////////////////////////////////////////////////
183void t_bns::deleteBnsEph() {
184 if (_bnseph) {
185 _bnseph->terminate();
[764]186 _bnseph->wait(100);
[763]187 delete _bnseph;
188 _bnseph = 0;
189 }
190}
191
[756]192// Write a Program Message
193////////////////////////////////////////////////////////////////////////////
[758]194void t_bns::slotMessage(const QByteArray msg) {
[816]195 if (_logStream) {
[1217]196 QString txt = QDateTime::currentDateTime().toUTC().toString("yy-MM-dd hh:mm:ss ");
197 *_logStream << txt << msg << endl;
[818]198 _logStream->flush();
[812]199 }
[757]200 emit(newMessage(msg));
[756]201}
202
[760]203// Write a Program Message
204////////////////////////////////////////////////////////////////////////////
205void t_bns::slotError(const QByteArray msg) {
[816]206 if (_logStream) {
207 *_logStream << msg << endl;
[818]208 _logStream->flush();
[812]209 }
[763]210 deleteBnsEph();
[760]211 emit(error(msg));
212}
213
[769]214// New Connection
215////////////////////////////////////////////////////////////////////////////
216void t_bns::slotNewConnection() {
[1208]217//slotMessage("t_bns::slotNewConnection");
218 slotMessage("Clocks & orbits port: Waiting for client to connect"); // weber
[787]219 delete _clkSocket;
[769]220 _clkSocket = _clkServer->nextPendingConnection();
221}
222
[784]223//
224////////////////////////////////////////////////////////////////////////////
[1058]225void t_bns::slotNewEph(t_eph* ep, int nBytes) {
[784]226
227 QMutexLocker locker(&_mutex);
228
[1058]229 emit(newEphBytes(nBytes));
230
[784]231 t_ephPair* pair;
[884]232 if ( !_ephList.contains(ep->prn()) ) {
[784]233 pair = new t_ephPair();
[884]234 _ephList.insert(ep->prn(), pair);
[784]235 }
236 else {
[884]237 pair = _ephList[ep->prn()];
[784]238 }
239
240 if (pair->eph == 0) {
241 pair->eph = ep;
242 }
243 else {
[884]244 if (ep->isNewerThan(pair->eph)) {
[784]245 delete pair->oldEph;
246 pair->oldEph = pair->eph;
247 pair->eph = ep;
248 }
249 else {
250 delete ep;
251 }
252 }
253}
254
[756]255// Start
256////////////////////////////////////////////////////////////////////////////
[757]257void t_bns::run() {
[769]258
[758]259 slotMessage("============ Start BNS ============");
[770]260
[828]261 // Start Thread that retrieves broadcast Ephemeris
262 // -----------------------------------------------
[758]263 _bnseph->start();
[769]264
[770]265 // Endless loop
266 // ------------
[769]267 while (true) {
[836]268
269 if (_clkSocket && _clkSocket->thread() != currentThread()) {
270 emit(moveSocket(currentThread()));
271 }
272
[796]273 if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
274 if ( _clkSocket->canReadLine()) {
275 readEpoch();
276 }
[809]277 else {
278 _clkSocket->waitForReadyRead(10);
279 }
[769]280 }
281 else {
[794]282 msleep(10);
[769]283 }
284 }
[756]285}
286
[778]287//
288////////////////////////////////////////////////////////////////////////////
[784]289void t_bns::readEpoch() {
[778]290
[1670]291 bnsSettings settings;
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;
[1735]364 ColumnVector xx(5); xx = 0.0;
[1066]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) ) {
[1698]372 in >> xx(1) >> xx(2) >> xx(3) >> xx(4) >> xx(5);
[1197]373 xx(1) *= 1e3;
374 xx(2) *= 1e3;
375 xx(3) *= 1e3;
376 xx(4) *= 1e-6;
[1670]377
[1066]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;
[1733]410 processSatellite(oldEph, ic, _caster.at(ic)->crdTrafo(),
411 _caster.at(ic)->beClocks(), ep,
[1102]412 GPSweek, GPSweeks, prn, xx, sd, outLine);
[1074]413 _caster.at(ic)->printAscii(outLine);
[1066]414 }
415 }
[873]416 }
[1066]417
[1123]418 if ( _caster.at(ic)->usedSocket() &&
[1066]419 (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) ) {
420 char obuffer[CLOCKORBIT_BUFFERSIZE];
421 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
422 if (len > 0) {
[1262]423 if (_caster.at(ic)->ic() == 1) { emit(newOutBytes1(len));}
424 if (_caster.at(ic)->ic() == 2) { emit(newOutBytes2(len));}
[1740]425 if (_caster.at(ic)->ic() == 3) { emit(newOutBytes3(len));}
[1066]426 _caster.at(ic)->write(obuffer, len);
[872]427 }
428 }
[863]429 }
430 }
[780]431 }
[778]432}
[784]433
434//
435////////////////////////////////////////////////////////////////////////////
[1733]436void t_bns::processSatellite(int oldEph, int iCaster, bool trafo,
437 bool beClocks, t_eph* ep, int GPSweek,
438 double GPSweeks, const QString& prn,
[1066]439 const ColumnVector& xx,
[1065]440 struct ClockOrbit::SatData* sd,
441 QString& outLine) {
[784]442
[799]443 ColumnVector xB(4);
[802]444 ColumnVector vv(3);
[799]445
[884]446 ep->position(GPSweek, GPSweeks, xB, vv);
[799]447
[984]448 ColumnVector xyz = xx.Rows(1,3);
[1066]449 if (trafo) {
[985]450 crdTrafo(GPSweek, xyz);
[984]451 }
[927]452
[1733]453 ColumnVector dx = xyz - xB.Rows(1,3);
[984]454
[804]455 ColumnVector rsw(3);
[806]456 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
[804]457
[1733]458 double dClk;
459 if (beClocks) {
460 dClk = (xx(4) - xB(4)) * 299792458.0 - xx(5);
461 }
462 else {
463 dClk = (xx(4) - xB(4)) * 299792458.0;
464 }
465
[863]466 if (sd) {
467 sd->ID = prn.mid(1).toInt();
[884]468 sd->IOD = ep->IOD();
[862]469 sd->Clock.DeltaA0 = dClk;
470 sd->Orbit.DeltaRadial = rsw(1);
471 sd->Orbit.DeltaAlongTrack = rsw(2);
472 sd->Orbit.DeltaCrossTrack = rsw(3);
[863]473 }
[862]474
[1065]475 char oldCh = (oldEph ? '!' : ' ');
476 outLine.sprintf("%c %d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
477 oldCh, GPSweek, GPSweeks, ep->prn().toAscii().data(),
478 ep->IOD(), dClk, rsw(1), rsw(2), rsw(3));
479
[1102]480 if (!oldEph && iCaster == 0) {
[923]481 if (_rnx) {
482 _rnx->write(GPSweek, GPSweeks, prn, xx);
483 }
484 if (_sp3) {
485 _sp3->write(GPSweek, GPSweeks, prn, xx);
486 }
[847]487 }
[784]488}
[836]489
490//
491////////////////////////////////////////////////////////////////////////////
492void t_bns::slotMoveSocket(QThread* tt) {
493 _clkSocket->setParent(0);
494 _clkSocket->moveToThread(tt);
[1209]495//slotMessage("bns::slotMoveSocket");
496 slotMessage("Clocks & orbits port: Socket moved to thread"); // weber
[836]497}
[984]498
499// Transform Coordinates IGS -> ETRF
500////////////////////////////////////////////////////////////////////////////
[985]501void t_bns::crdTrafo(int GPSWeek, ColumnVector& xyz) {
[984]502
[1243]503 // Current epoch minus 2000.0 in years
504 // ------------------------------------
505 double dt = (GPSWeek - (1042.0+6.0/7.0)) / 365.2422 * 7.0;
506
[985]507 ColumnVector dx(3);
[1243]508 dx(1) = 0.0541 - dt * 0.0002;
509 dx(2) = 0.0502 + dt * 0.0001;
510 dx(3) = -0.0538 - dt * 0.0018;
511
[1100]512 static const double arcSec = 180.0 * 3600.0 / M_PI;
[1245]513 double ox = ( 0.000891 + dt * 0.000081) / arcSec;
514 double oy = ( 0.005390 + dt * 0.000490) / arcSec;
515 double oz = (-0.008712 - dt * 0.000792) / arcSec;
[984]516
[1245]517 double sc = 1.0 + 0.4e-9 + dt * 0.08e-9;
[1243]518
[1245]519 Matrix rMat(3,3);
520 rMat(1,1) = 1.0;
[985]521 rMat(1,2) = -oz;
522 rMat(1,3) = oy;
523 rMat(2,1) = oz;
[1245]524 rMat(2,2) = 1.0;
[985]525 rMat(2,3) = -ox;
526 rMat(3,1) = -oy;
527 rMat(3,2) = ox;
[1245]528 rMat(3,3) = 1.0;
[985]529
[1245]530 xyz = sc * rMat * xyz + dx;
[984]531}
Note: See TracBrowser for help on using the repository browser.