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

Last change on this file since 1795 was 1776, checked in by mervart, 16 years ago

* empty log message *

File size: 15.3 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 // Log File
89 // --------
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
98 QString logFileName = settings.value("logFile").toString();
99 if (logFileName.isEmpty()) {
100 _logFile = 0;
101 _logStream = 0;
102 }
103 else {
104 _logFile = new QFile(logFileName);
105 if (_logFile->open(oMode)) {
106 _logStream = new QTextStream(_logFile);
107 }
108 else {
109 _logStream = 0;
110 }
111 }
112
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
130 // RINEX writer
131 // ------------
132 if ( settings.value("rnxPath").toString().isEmpty() ) {
133 _rnx = 0;
134 }
135 else {
136 QString prep = "BNS";
137 QString ext = ".clk";
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);
142 }
143
144 // SP3 writer
145 // ----------
146 if ( settings.value("sp3Path").toString().isEmpty() ) {
147 _sp3 = 0;
148 }
149 else {
150 QString prep = "BNS";
151 QString ext = ".sp3";
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);
156 }
157}
158
159// Destructor
160////////////////////////////////////////////////////////////////////////////
161t_bns::~t_bns() {
162 deleteBnsEph();
163 delete _clkServer;
164 delete _clkSocket;
165 for (int ic = 0; ic < _caster.size(); ic++) {
166 delete _caster.at(ic);
167 }
168 delete _logStream;
169 delete _logFile;
170 delete _echoStream;
171 delete _echoFile;
172 QMapIterator<QString, t_ephPair*> it(_ephList);
173 while (it.hasNext()) {
174 it.next();
175 delete it.value();
176 }
177 delete _rnx;
178 delete _sp3;
179}
180
181// Delete bns thread
182////////////////////////////////////////////////////////////////////////////
183void t_bns::deleteBnsEph() {
184 if (_bnseph) {
185 _bnseph->terminate();
186 _bnseph->wait(100);
187 delete _bnseph;
188 _bnseph = 0;
189 }
190}
191
192// Write a Program Message
193////////////////////////////////////////////////////////////////////////////
194void t_bns::slotMessage(const QByteArray msg) {
195 if (_logStream) {
196 QString txt = QDateTime::currentDateTime().toUTC().toString("yy-MM-dd hh:mm:ss ");
197 *_logStream << txt << msg << endl;
198 _logStream->flush();
199 }
200 emit(newMessage(msg));
201}
202
203// Write a Program Message
204////////////////////////////////////////////////////////////////////////////
205void t_bns::slotError(const QByteArray msg) {
206 if (_logStream) {
207 *_logStream << msg << endl;
208 _logStream->flush();
209 }
210 deleteBnsEph();
211 emit(error(msg));
212}
213
214// New Connection
215////////////////////////////////////////////////////////////////////////////
216void t_bns::slotNewConnection() {
217//slotMessage("t_bns::slotNewConnection");
218 slotMessage("Clocks & orbits port: Waiting for client to connect"); // weber
219 delete _clkSocket;
220 _clkSocket = _clkServer->nextPendingConnection();
221}
222
223//
224////////////////////////////////////////////////////////////////////////////
225void t_bns::slotNewEph(t_eph* ep, int nBytes) {
226
227 QMutexLocker locker(&_mutex);
228
229 emit(newEphBytes(nBytes));
230
231 t_ephPair* pair;
232 if ( !_ephList.contains(ep->prn()) ) {
233 pair = new t_ephPair();
234 _ephList.insert(ep->prn(), pair);
235 }
236 else {
237 pair = _ephList[ep->prn()];
238 }
239
240 if (pair->eph == 0) {
241 pair->eph = ep;
242 }
243 else {
244 if (ep->isNewerThan(pair->eph)) {
245 delete pair->oldEph;
246 pair->oldEph = pair->eph;
247 pair->eph = ep;
248 }
249 else {
250 delete ep;
251 }
252 }
253}
254
255// Start
256////////////////////////////////////////////////////////////////////////////
257void t_bns::run() {
258
259 slotMessage("============ Start BNS ============");
260
261 // Start Thread that retrieves broadcast Ephemeris
262 // -----------------------------------------------
263 _bnseph->start();
264
265 // Endless loop
266 // ------------
267 while (true) {
268
269 if (_clkSocket && _clkSocket->thread() != currentThread()) {
270 emit(moveSocket(currentThread()));
271 }
272
273 if (_clkSocket && _clkSocket->state() == QAbstractSocket::ConnectedState) {
274 if ( _clkSocket->canReadLine()) {
275 readEpoch();
276 }
277 else {
278 _clkSocket->waitForReadyRead(10);
279 }
280 }
281 else {
282 msleep(10);
283 }
284 }
285}
286
287//
288////////////////////////////////////////////////////////////////////////////
289void t_bns::readEpoch() {
290
291 bnsSettings settings;
292
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()));
302 }
303
304 if (_clkLine.indexOf('*') == -1) {
305 return;
306 }
307
308 QTextStream in(_clkLine);
309
310 QString hlp;
311 int year, month, day, hour, min;
312 double sec;
313 in >> hlp >> year >> month >> day >> hour >> min >> sec;
314
315 int GPSweek;
316 double GPSweeks;
317
318 GPSweekFromYMDhms(year, month, day, hour, min, sec, GPSweek, GPSweeks);
319
320 QStringList prns;
321
322 // Loop over all satellites
323 // ------------------------
324 QStringList lines;
325 for (;;) {
326 if (!_clkSocket->canReadLine()) {
327 break;
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
345 QStringList prns;
346
347 for (int ic = 0; ic < _caster.size(); ic++) {
348 _caster.at(ic)->open();
349
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
361 for (int ii = 0; ii < lines.size(); ii++) {
362
363 QString prn;
364 ColumnVector xx(5); xx = 0.0;
365 t_eph* ep = 0;
366
367 if (oldEph == 0 && ic == 0) {
368 QTextStream in(lines[ii].toAscii());
369 in >> prn;
370 prns << prn;
371 if ( _ephList.contains(prn) ) {
372 in >> xx(1) >> xx(2) >> xx(3) >> xx(4) >> xx(5);
373 xx(1) *= 1e3;
374 xx(2) *= 1e3;
375 xx(3) *= 1e3;
376 xx(4) *= 1e-6;
377 xx(5) *= 1e-6;
378
379 t_ephPair* pair = _ephList[prn];
380 pair->xx = xx;
381 ep = pair->eph;
382 }
383 }
384 else {
385 prn = prns[ii];
386 if ( _ephList.contains(prn) ) {
387 t_ephPair* pair = _ephList[prn];
388 prn = pair->eph->prn();
389 xx = pair->xx;
390 if (oldEph) {
391 ep = pair->oldEph;
392 }
393 else {
394 ep = pair->eph;
395 }
396 }
397 }
398
399 if (ep != 0) {
400 struct ClockOrbit::SatData* sd = 0;
401 if (prn[0] == 'G') {
402 sd = co.Sat + co.NumberOfGPSSat;
403 ++co.NumberOfGPSSat;
404 }
405 else if (prn[0] == 'R') {
406 sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
407 ++co.NumberOfGLONASSSat;
408 }
409 if (sd) {
410 QString outLine;
411 processSatellite(oldEph, ic, _caster.at(ic)->crdTrafo(),
412 _caster.at(ic)->beClocks(), ep,
413 GPSweek, GPSweeks, prn, xx, sd, outLine);
414 _caster.at(ic)->printAscii(outLine);
415 }
416 }
417 }
418
419 if ( _caster.at(ic)->usedSocket() &&
420 (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) ) {
421 char obuffer[CLOCKORBIT_BUFFERSIZE];
422 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
423 if (len > 0) {
424 if (_caster.at(ic)->ic() == 1) { emit(newOutBytes1(len));}
425 if (_caster.at(ic)->ic() == 2) { emit(newOutBytes2(len));}
426 if (_caster.at(ic)->ic() == 3) { emit(newOutBytes3(len));}
427 _caster.at(ic)->write(obuffer, len);
428 }
429 }
430 }
431 }
432 }
433}
434
435//
436////////////////////////////////////////////////////////////////////////////
437void t_bns::processSatellite(int oldEph, int iCaster, const QString trafo,
438 bool beClocks, t_eph* ep, int GPSweek,
439 double GPSweeks, const QString& prn,
440 const ColumnVector& xx,
441 struct ClockOrbit::SatData* sd,
442 QString& outLine) {
443
444 ColumnVector xB(4);
445 ColumnVector vv(3);
446
447 ep->position(GPSweek, GPSweeks, xB, vv);
448
449 ColumnVector xyz = xx.Rows(1,3);
450 if (trafo != "IGS05") {
451 crdTrafo(GPSweek, xyz, trafo);
452 }
453
454 ColumnVector dx = xyz - xB.Rows(1,3);
455
456 ColumnVector rsw(3);
457 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
458
459 double dClk;
460 if (beClocks) {
461 dClk = (xx(4) - xB(4) - xx(5)) * 299792458.0;
462 }
463 else {
464 dClk = (xx(4) - xB(4)) * 299792458.0;
465 }
466
467 if (sd) {
468 sd->ID = prn.mid(1).toInt();
469 sd->IOD = ep->IOD();
470 sd->Clock.DeltaA0 = dClk;
471 sd->Orbit.DeltaRadial = rsw(1);
472 sd->Orbit.DeltaAlongTrack = rsw(2);
473 sd->Orbit.DeltaCrossTrack = rsw(3);
474 }
475
476 char oldCh = (oldEph ? '!' : ' ');
477 outLine.sprintf("%c %d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
478 oldCh, GPSweek, GPSweeks, ep->prn().toAscii().data(),
479 ep->IOD(), dClk, rsw(1), rsw(2), rsw(3));
480
481 if (!oldEph && iCaster == 0) {
482 if (_rnx) {
483 _rnx->write(GPSweek, GPSweeks, prn, xx);
484 }
485 if (_sp3) {
486 _sp3->write(GPSweek, GPSweeks, prn, xx);
487 }
488 }
489}
490
491//
492////////////////////////////////////////////////////////////////////////////
493void t_bns::slotMoveSocket(QThread* tt) {
494 _clkSocket->setParent(0);
495 _clkSocket->moveToThread(tt);
496//slotMessage("bns::slotMoveSocket");
497 slotMessage("Clocks & orbits port: Socket moved to thread"); // weber
498}
499
500// Transform Coordinates
501////////////////////////////////////////////////////////////////////////////
502void t_bns::crdTrafo(int GPSWeek, ColumnVector& xyz, const QString trafo) {
503
504 bnsSettings settings;
505
506 if (trafo == "ETRF2000") {
507 _dx = 0.0541;
508 _dy = 0.0502;
509 _dz = -0.0538;
510 _dxr = -0.0002;
511 _dyr = 0.0001;
512 _dzr = -0.0018;
513 _ox = 0.000891;
514 _oy = 0.005390;
515 _oz = -0.008712;
516 _oxr = 0.000081;
517 _oyr = 0.000490;
518 _ozr = -0.000792;
519 _sc = 0.40;
520 _scr = 0.08;
521 _t0 = 2000.0;
522 }
523 else if (trafo == "Custom") {
524 _dx = settings.value("trafo_dx").toDouble();
525 _dy = settings.value("trafo_dy").toDouble();
526 _dz = settings.value("trafo_dz").toDouble();
527 _dxr = settings.value("trafo_dxr").toDouble();
528 _dyr = settings.value("trafo_dyr").toDouble();
529 _dzr = settings.value("trafo_dzr").toDouble();
530 _ox = settings.value("trafo_ox").toDouble();
531 _oy = settings.value("trafo_oy").toDouble();
532 _oz = settings.value("trafo_oz").toDouble();
533 _oxr = settings.value("trafo_oxr").toDouble();
534 _oyr = settings.value("trafo_oyr").toDouble();
535 _ozr = settings.value("trafo_ozr").toDouble();
536 _sc = settings.value("trafo_sc").toDouble();
537 _scr = settings.value("trafo_scr").toDouble();
538 _t0 = settings.value("trafo_t0").toDouble();
539 }
540
541 // Current epoch minus 2000.0 in years
542 // ------------------------------------
543 double dt = (GPSWeek - (1042.0+6.0/7.0)) / 365.2422 * 7.0 + 2000.0 - _t0;
544
545 ColumnVector dx(3);
546
547 dx(1) = _dx + dt * _dxr;
548 dx(2) = _dy + dt * _dyr;
549 dx(3) = _dz + dt * _dzr;
550
551 static const double arcSec = 180.0 * 3600.0 / M_PI;
552
553 double ox = (_ox + dt * _oxr) / arcSec;
554 double oy = (_oy + dt * _oyr) / arcSec;
555 double oz = (_oz + dt * _ozr) / arcSec;
556
557 double sc = 1.0 + _sc * 1e-9 + dt * _scr * 1e-9;
558
559 Matrix rMat(3,3);
560 rMat(1,1) = 1.0;
561 rMat(1,2) = -oz;
562 rMat(1,3) = oy;
563 rMat(2,1) = oz;
564 rMat(2,2) = 1.0;
565 rMat(2,3) = -ox;
566 rMat(3,1) = -oy;
567 rMat(3,2) = ox;
568 rMat(3,3) = 1.0;
569
570 xyz = sc * rMat * xyz + dx;
571}
Note: See TracBrowser for help on using the repository browser.