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

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