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

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

* empty log message *

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