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

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