source: ntrip/trunk/BNC/upload/bncuploadcaster.cpp@ 3208

Last change on this file since 3208 was 3208, checked in by mervart, 13 years ago
File size: 17.1 KB
Line 
1/* -------------------------------------------------------------------------
2 * BKG NTRIP Server
3 * -------------------------------------------------------------------------
4 *
5 * Class: bncUploadCaster
6 *
7 * Purpose: Connection to NTRIP Caster
8 *
9 * Author: L. Mervart
10 *
11 * Created: 29-Mar-2011
12 *
13 * Changes:
14 *
15 * -----------------------------------------------------------------------*/
16
17#include <math.h>
18#include "bncuploadcaster.h"
19#include "bncsettings.h"
20#include "bncversion.h"
21#include "bncapp.h"
22#include "bncclockrinex.h"
23#include "bncsp3.h"
24
25using namespace std;
26
27// Constructor
28////////////////////////////////////////////////////////////////////////////
29bncUploadCaster::bncUploadCaster(const QString& mountpoint,
30 const QString& outHost, int outPort,
31 const QString& password,
32 const QString& crdTrafo, bool CoM,
33 const QString& sp3FileName,
34 const QString& rnxFileName,
35 const QString& outFileName) {
36 bncSettings settings;
37
38 connect(this, SIGNAL(newMessage(QByteArray,bool)),
39 ((bncApp*)qApp), SLOT(slotMessage(const QByteArray,bool)));
40
41 _mountpoint = mountpoint;
42 _outHost = outHost;
43 _outPort = outPort;
44 _password = password;
45 _crdTrafo = crdTrafo;
46 _CoM = CoM;
47
48 _outSocket = 0;
49 _sOpenTrial = 0;
50
51 _isToBeDeleted = false;
52
53 // Raw Output
54 // ----------
55 if (!outFileName.isEmpty()) {
56 _outFile = new bncoutf(outFileName, "", 0);
57 }
58 else {
59 _outFile = 0;
60 }
61
62 // RINEX writer
63 // ------------
64 if (!rnxFileName.isEmpty()) {
65 _rnx = new bncClockRinex(rnxFileName, "", 0);
66 }
67 else {
68 _rnx = 0;
69 }
70
71 // SP3 writer
72 // ----------
73 if (!sp3FileName.isEmpty()) {
74 _sp3 = new bncSP3(sp3FileName, "", 0);
75 }
76 else {
77 _sp3 = 0;
78 }
79
80 // Set Transformation Parameters
81 // -----------------------------
82 if (_crdTrafo == "ETRF2000") {
83 _dx = 0.0541;
84 _dy = 0.0502;
85 _dz = -0.0538;
86 _dxr = -0.0002;
87 _dyr = 0.0001;
88 _dzr = -0.0018;
89 _ox = 0.000891;
90 _oy = 0.005390;
91 _oz = -0.008712;
92 _oxr = 0.000081;
93 _oyr = 0.000490;
94 _ozr = -0.000792;
95 _sc = 0.40;
96 _scr = 0.08;
97 _t0 = 2000.0;
98 }
99 else if (_crdTrafo == "NAD83") {
100 _dx = 0.9963;
101 _dy = -1.9024;
102 _dz = -0.5210;
103 _dxr = 0.0005;
104 _dyr = -0.0006;
105 _dzr = -0.0013;
106 _ox = 0.025915;
107 _oy = 0.009426;
108 _oz = 0.011599;
109 _oxr = 0.000067;
110 _oyr = -0.000757;
111 _ozr = -0.000051;
112 _sc = 0.78;
113 _scr = -0.10;
114 _t0 = 1997.0;
115 }
116 else if (_crdTrafo == "GDA94") {
117 _dx = -0.07973;
118 _dy = -0.00686;
119 _dz = 0.03803;
120 _dxr = 0.00225;
121 _dyr = -0.00062;
122 _dzr = -0.00056;
123 _ox = 0.0000351;
124 _oy = -0.0021211;
125 _oz = -0.0021411;
126 _oxr = -0.0014707;
127 _oyr = -0.0011443;
128 _ozr = -0.0011701;
129 _sc = 6.636;
130 _scr = 0.294;
131 _t0 = 1994.0;
132 }
133 else if (_crdTrafo == "SIRGAS2000") {
134 _dx = -0.0051;
135 _dy = -0.0065;
136 _dz = -0.0099;
137 _dxr = 0.0000;
138 _dyr = 0.0000;
139 _dzr = 0.0000;
140 _ox = 0.000150;
141 _oy = 0.000020;
142 _oz = 0.000021;
143 _oxr = 0.000000;
144 _oyr = 0.000000;
145 _ozr = 0.000000;
146 _sc = 0.000;
147 _scr = 0.000;
148 _t0 = 0000.0;
149 }
150 else if (_crdTrafo == "SIRGAS95") {
151 _dx = 0.0077;
152 _dy = 0.0058;
153 _dz = -0.0138;
154 _dxr = 0.0000;
155 _dyr = 0.0000;
156 _dzr = 0.0000;
157 _ox = 0.000000;
158 _oy = 0.000000;
159 _oz = -0.000030;
160 _oxr = 0.000000;
161 _oyr = 0.000000;
162 _ozr = 0.000000;
163 _sc = 1.570;
164 _scr = 0.000;
165 _t0 = 0000.0;
166 }
167 else if (_crdTrafo == "Custom") {
168 _dx = settings.value("trafo_dx").toDouble();
169 _dy = settings.value("trafo_dy").toDouble();
170 _dz = settings.value("trafo_dz").toDouble();
171 _dxr = settings.value("trafo_dxr").toDouble();
172 _dyr = settings.value("trafo_dyr").toDouble();
173 _dzr = settings.value("trafo_dzr").toDouble();
174 _ox = settings.value("trafo_ox").toDouble();
175 _oy = settings.value("trafo_oy").toDouble();
176 _oz = settings.value("trafo_oz").toDouble();
177 _oxr = settings.value("trafo_oxr").toDouble();
178 _oyr = settings.value("trafo_oyr").toDouble();
179 _ozr = settings.value("trafo_ozr").toDouble();
180 _sc = settings.value("trafo_sc").toDouble();
181 _scr = settings.value("trafo_scr").toDouble();
182 _t0 = settings.value("trafo_t0").toDouble();
183 }
184
185 // Deep copy of ephemerides
186 // ------------------------
187 _ephMap = 0;
188}
189
190// Safe Desctructor
191////////////////////////////////////////////////////////////////////////////
192void bncUploadCaster::deleteSafely() {
193 QMutexLocker locker(&_mutex);
194 _isToBeDeleted = true;
195 if (!isRunning()) {
196 delete this;
197 }
198}
199
200// Destructor
201////////////////////////////////////////////////////////////////////////////
202bncUploadCaster::~bncUploadCaster() {
203 if (isRunning()) {
204 wait();
205 }
206 if (_ephMap) {
207 QMapIterator<QString, t_eph*> it(*_ephMap);
208 while (it.hasNext()) {
209 it.next();
210 t_eph* eph = it.value();
211 delete eph;
212 }
213 delete _ephMap;
214 }
215 delete _outFile;
216 delete _rnx;
217 delete _sp3;
218}
219
220// Endless Loop
221////////////////////////////////////////////////////////////////////////////
222void bncUploadCaster::run() {
223 while (true) {
224 {
225 QMutexLocker locker(&_mutex);
226 if (_isToBeDeleted) {
227 QThread::quit();
228 deleteLater();
229 return;
230 }
231 }
232 uploadClockOrbitBias();
233 msleep(10);
234 }
235}
236
237// Start the Communication with NTRIP Caster
238////////////////////////////////////////////////////////////////////////////
239void bncUploadCaster::open() {
240
241 if (_mountpoint.isEmpty()) {
242 return;
243 }
244
245 if (_outSocket != 0 &&
246 _outSocket->state() == QAbstractSocket::ConnectedState) {
247 return;
248 }
249
250 delete _outSocket; _outSocket = 0;
251
252 double minDt = pow(2.0,_sOpenTrial);
253 if (++_sOpenTrial > 4) {
254 _sOpenTrial = 4;
255 }
256 if (_outSocketOpenTime.isValid() &&
257 _outSocketOpenTime.secsTo(QDateTime::currentDateTime()) < minDt) {
258 return;
259 }
260 else {
261 _outSocketOpenTime = QDateTime::currentDateTime();
262 }
263
264 bncSettings settings;
265 _outSocket = new QTcpSocket();
266 _outSocket->connectToHost(_outHost, _outPort);
267
268 const int timeOut = 5000; // 5 seconds
269 if (!_outSocket->waitForConnected(timeOut)) {
270 delete _outSocket;
271 _outSocket = 0;
272 emit(newMessage("Broadcaster: Connect timeout", true));
273 return;
274 }
275
276 QByteArray msg = "SOURCE " + _password.toAscii() + " /" +
277 _mountpoint.toAscii() + "\r\n" +
278 "Source-Agent: NTRIP BNC/" BNCVERSION "\r\n\r\n";
279
280 _outSocket->write(msg);
281 _outSocket->waitForBytesWritten();
282
283 _outSocket->waitForReadyRead();
284 QByteArray ans = _outSocket->readLine();
285
286 if (ans.indexOf("OK") == -1) {
287 delete _outSocket;
288 _outSocket = 0;
289 emit(newMessage("Broadcaster: Connection broken", true));
290 }
291 else {
292 emit(newMessage("Broadcaster: Connection opened", true));
293 _sOpenTrial = 0;
294 }
295}
296
297// Write buffer
298////////////////////////////////////////////////////////////////////////////
299void bncUploadCaster::write(char* buffer, unsigned len) {
300 if (_outSocket) {
301 _outSocket->write(buffer, len);
302 _outSocket->flush();
303 }
304}
305
306//
307////////////////////////////////////////////////////////////////////////////
308void bncUploadCaster::decodeRtnetStream(char* buffer, int bufLen,
309 const QMap<QString, bncEphUser::t_ephPair*>& ephPairMap) {
310
311 QMutexLocker locker(&_mutex);
312
313 // Delete old ephemeris
314 // --------------------
315 if (_ephMap) {
316 QMapIterator<QString, t_eph*> it(*_ephMap);
317 while (it.hasNext()) {
318 it.next();
319 t_eph* eph = it.value();
320 delete eph;
321 }
322 delete _ephMap;
323 }
324 _ephMap = new QMap<QString, t_eph*>;
325
326 // Make a deep copy of ephemeris
327 // -----------------------------
328 QMapIterator<QString, bncEphUser::t_ephPair*> it(ephPairMap);
329 while (it.hasNext()) {
330 it.next();
331 bncEphUser::t_ephPair* pair = it.value();
332 t_eph* ep = pair->last;
333 if (pair->prev && ep &&
334 ep->receptDateTime().secsTo(QDateTime::currentDateTime()) < 60) {
335 ep = pair->prev;
336 }
337 QString prn(ep->prn().c_str());
338 if (prn[0] == 'G') {
339 t_ephGPS* epGPS = static_cast<t_ephGPS*>(ep);
340 (*_ephMap)[prn] = new t_ephGPS(*epGPS);
341 }
342 else if (prn[0] == 'R') {
343 t_ephGlo* epGlo = static_cast<t_ephGlo*>(ep);
344 (*_ephMap)[prn] = new t_ephGlo(*epGlo);
345 }
346 else if (prn[0] == 'E') {
347 t_ephGal* epGal = static_cast<t_ephGal*>(ep);
348 (*_ephMap)[prn] = new t_ephGal(*epGal);
349 }
350 }
351
352 // Append to buffer
353 // ----------------
354 _rtnetStreamBuffer.append(QByteArray(buffer, bufLen));
355}
356
357// Function called in separate thread
358////////////////////////////////////////////////////////////////////////
359void bncUploadCaster::uploadClockOrbitBias() {
360
361 QMutexLocker locker(&_mutex);
362
363 // Prepare list of lines with satellite positions in SP3-like format
364 // -----------------------------------------------------------------
365 QStringList lines;
366 int iLast = _rtnetStreamBuffer.lastIndexOf('\n');
367 if (iLast != -1) {
368 QStringList hlpLines = _rtnetStreamBuffer.split('\n', QString::SkipEmptyParts);
369 _rtnetStreamBuffer = _rtnetStreamBuffer.mid(iLast+1);
370 for (int ii = 0; ii < hlpLines.size(); ii++) {
371 if (hlpLines[ii].indexOf('*') != -1) {
372 QTextStream in(hlpLines[ii].toAscii());
373 QString hlp;
374 int year, month, day, hour, min;
375 double sec;
376 in >> hlp >> year >> month >> day >> hour >> min >> sec;
377 _epoTime.set( year, month, day, hour, min, sec);
378 }
379 else if (_epoTime.valid()) {
380 lines << hlpLines[ii];
381 }
382 }
383 }
384
385 if (lines.size() == 0) {
386 return;
387 }
388
389 this->open();
390
391 unsigned year, month, day;
392 _epoTime.civil_date(year, month, day);
393
394 struct ClockOrbit co;
395 memset(&co, 0, sizeof(co));
396 co.GPSEpochTime = static_cast<int>(_epoTime.gpssec());
397 co.GLONASSEpochTime = static_cast<int>(fmod(_epoTime.gpssec(), 86400.0))
398 + 3 * 3600 - gnumleap(year, month, day);
399 co.ClockDataSupplied = 1;
400 co.OrbitDataSupplied = 1;
401 co.SatRefDatum = DATUM_ITRF;
402
403 struct Bias bias;
404 memset(&bias, 0, sizeof(bias));
405 bias.GPSEpochTime = co.GPSEpochTime;
406 bias.GLONASSEpochTime = co.GLONASSEpochTime;
407
408 for (int ii = 0; ii < lines.size(); ii++) {
409
410 QString prn;
411 ColumnVector xx(14); xx = 0.0;
412
413 QTextStream in(lines[ii].toAscii());
414
415 in >> prn;
416 if (prn[0] == 'P') {
417 prn.remove(0,1);
418 }
419
420 if ( _ephMap->contains(prn) ) {
421 t_eph* ep = (*_ephMap)[prn];
422
423 in >> xx(1) >> xx(2) >> xx(3) >> xx(4) >> xx(5)
424 >> xx(6) >> xx(7) >> xx(8) >> xx(9) >> xx(10)
425 >> xx(11) >> xx(12) >> xx(13) >> xx(14);
426 xx(1) *= 1e3; // x-crd
427 xx(2) *= 1e3; // y-crd
428 xx(3) *= 1e3; // z-crd
429 xx(4) *= 1e-6; // clk
430 xx(5) *= 1e-6; // rel. corr.
431 // xx(6), xx(7), xx(8) ... PhaseCent - CoM
432 // xx(9) ... P1-C1 DCB in meters
433 // xx(10) ... P1-P2 DCB in meters
434 // xx(11) ... dT
435 xx(12) *= 1e3; // x-crd at time + dT
436 xx(13) *= 1e3; // y-crd at time + dT
437 xx(14) *= 1e3; // z-crd at time + dT
438
439 struct ClockOrbit::SatData* sd = 0;
440 if (prn[0] == 'G') {
441 sd = co.Sat + co.NumberOfGPSSat;
442 ++co.NumberOfGPSSat;
443 }
444 else if (prn[0] == 'R') {
445 sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
446 ++co.NumberOfGLONASSSat;
447 }
448 if (sd) {
449 QString outLine;
450 processSatellite(ep, _epoTime.gpsw(), _epoTime.gpssec(), prn, xx, sd, outLine);
451 if (_outFile) {
452 _outFile->write(_epoTime.gpsw(), _epoTime.gpssec(), outLine);
453 }
454 }
455
456 struct Bias::BiasSat* biasSat = 0;
457 if (prn[0] == 'G') {
458 biasSat = bias.Sat + bias.NumberOfGPSSat;
459 ++bias.NumberOfGPSSat;
460 }
461 else if (prn[0] == 'R') {
462 biasSat = bias.Sat + CLOCKORBIT_NUMGPS + bias.NumberOfGLONASSSat;
463 ++bias.NumberOfGLONASSSat;
464 }
465
466 // Coefficient of Ionosphere-Free LC
467 // ---------------------------------
468 const static double a_L1_GPS = 2.54572778;
469 const static double a_L2_GPS = -1.54572778;
470 const static double a_L1_Glo = 2.53125000;
471 const static double a_L2_Glo = -1.53125000;
472
473 if (biasSat) {
474 biasSat->ID = prn.mid(1).toInt();
475 biasSat->NumberOfCodeBiases = 3;
476 if (prn[0] == 'G') {
477 biasSat->Biases[0].Type = CODETYPEGPS_L1_Z;
478 biasSat->Biases[0].Bias = - a_L2_GPS * xx(10);
479 biasSat->Biases[1].Type = CODETYPEGPS_L1_CA;
480 biasSat->Biases[1].Bias = - a_L2_GPS * xx(10) + xx(9);
481 biasSat->Biases[2].Type = CODETYPEGPS_L2_Z;
482 biasSat->Biases[2].Bias = a_L1_GPS * xx(10);
483 }
484 else if (prn[0] == 'R') {
485 biasSat->Biases[0].Type = CODETYPEGLONASS_L1_P;
486 biasSat->Biases[0].Bias = - a_L2_Glo * xx(10);
487 biasSat->Biases[1].Type = CODETYPEGLONASS_L1_CA;
488 biasSat->Biases[1].Bias = - a_L2_Glo * xx(10) + xx(9);
489 biasSat->Biases[2].Type = CODETYPEGLONASS_L2_P;
490 biasSat->Biases[2].Bias = a_L1_Glo * xx(10);
491 }
492 }
493 }
494 }
495
496 if (_outSocket && (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0)) {
497 char obuffer[CLOCKORBIT_BUFFERSIZE];
498
499 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
500 if (len > 0) {
501 this->write(obuffer, len);
502 }
503 }
504
505 if (_outSocket && (bias.NumberOfGPSSat > 0 || bias.NumberOfGLONASSSat > 0)) {
506 char obuffer[CLOCKORBIT_BUFFERSIZE];
507 int len = MakeBias(&bias, BTYPE_AUTO, 0, obuffer, sizeof(obuffer));
508 if (len > 0) {
509 this->write(obuffer, len);
510 }
511 }
512}
513
514//
515////////////////////////////////////////////////////////////////////////////
516void bncUploadCaster::processSatellite(t_eph* eph, int GPSweek,
517 double GPSweeks, const QString& prn,
518 const ColumnVector& xx,
519 struct ClockOrbit::SatData* sd,
520 QString& outLine) {
521
522 const double secPerWeek = 7.0 * 86400.0;
523
524 ColumnVector rsw(3);
525 ColumnVector rsw2(3);
526 double dClk;
527
528 for (int ii = 1; ii <= 2; ++ii) {
529
530 int GPSweek12 = GPSweek;
531 double GPSweeks12 = GPSweeks;
532 if (ii == 2) {
533 GPSweeks12 += xx(11);
534 if (GPSweeks12 > secPerWeek) {
535 GPSweek12 += 1;
536 GPSweeks12 -= secPerWeek;
537 }
538 }
539
540 ColumnVector xB(4);
541 ColumnVector vv(3);
542
543 eph->position(GPSweek12, GPSweeks12, xB.data(), vv.data());
544
545 ColumnVector xyz;
546 if (ii == 1) {
547 xyz = xx.Rows(1,3);
548 }
549 else {
550 xyz = xx.Rows(12,14);
551 }
552
553 // Correction Center of Mass -> Antenna Phase Center
554 // -------------------------------------------------
555 if (! _CoM) {
556 xyz(1) += xx(6);
557 xyz(2) += xx(7);
558 xyz(3) += xx(8);
559 }
560
561 if (_crdTrafo != "IGS05") {
562 crdTrafo(GPSweek12, xyz);
563 }
564
565 ColumnVector dx = xB.Rows(1,3) - xyz ;
566
567 if (ii == 1) {
568 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
569 dClk = (xx(4) + xx(5) - xB(4)) * 299792458.0;
570 }
571 else {
572 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw2);
573 }
574 }
575
576 if (sd) {
577 sd->ID = prn.mid(1).toInt();
578 sd->IOD = eph->IOD();
579 sd->Clock.DeltaA0 = dClk;
580 sd->Orbit.DeltaRadial = rsw(1);
581 sd->Orbit.DeltaAlongTrack = rsw(2);
582 sd->Orbit.DeltaCrossTrack = rsw(3);
583 sd->Orbit.DotDeltaRadial = (rsw2(1) - rsw(1)) / xx(11);
584 sd->Orbit.DotDeltaAlongTrack = (rsw2(2) - rsw(2)) / xx(11);
585 sd->Orbit.DotDeltaCrossTrack = (rsw2(3) - rsw(3)) / xx(11);
586 }
587
588 outLine.sprintf("%d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
589 GPSweek, GPSweeks, eph->prn().c_str(),
590 eph->IOD(), dClk, rsw(1), rsw(2), rsw(3));
591
592 if (_rnx) {
593 _rnx->write(GPSweek, GPSweeks, prn, xx);
594 }
595 if (_sp3) {
596 _sp3->write(GPSweek, GPSweeks, prn, xx);
597 }
598}
599
600// Transform Coordinates
601////////////////////////////////////////////////////////////////////////////
602void bncUploadCaster::crdTrafo(int GPSWeek, ColumnVector& xyz) {
603
604 // Current epoch minus 2000.0 in years
605 // ------------------------------------
606 double dt = (GPSWeek - (1042.0+6.0/7.0)) / 365.2422 * 7.0 + 2000.0 - _t0;
607
608 ColumnVector dx(3);
609
610 dx(1) = _dx + dt * _dxr;
611 dx(2) = _dy + dt * _dyr;
612 dx(3) = _dz + dt * _dzr;
613
614 static const double arcSec = 180.0 * 3600.0 / M_PI;
615
616 double ox = (_ox + dt * _oxr) / arcSec;
617 double oy = (_oy + dt * _oyr) / arcSec;
618 double oz = (_oz + dt * _ozr) / arcSec;
619
620 double sc = 1.0 + _sc * 1e-9 + dt * _scr * 1e-9;
621
622 Matrix rMat(3,3);
623 rMat(1,1) = 1.0;
624 rMat(1,2) = -oz;
625 rMat(1,3) = oy;
626 rMat(2,1) = oz;
627 rMat(2,2) = 1.0;
628 rMat(2,3) = -ox;
629 rMat(3,1) = -oy;
630 rMat(3,2) = ox;
631 rMat(3,3) = 1.0;
632
633 xyz = sc * rMat * xyz + dx;
634}
635
Note: See TracBrowser for help on using the repository browser.