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

Last change on this file since 1796 was 1796, checked in by mervart, 15 years ago

* empty log message *

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