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

Last change on this file since 3185 was 3185, checked in by mervart, 13 years ago
File size: 14.3 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& rnxFileName,
34 const QString& sp3FileName,
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"));
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"));
246 }
247 else {
248 emit(newMessage("Broadcaster: Connection opened"));
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 QStringList& lines,
265 const QMap<QString, bncEphUser::t_ephPair*>& ephMap,
266 int year, int month, int day,
267 int GPSweek, double GPSweeks) {
268
269 this->open();
270
271 struct ClockOrbit co;
272 memset(&co, 0, sizeof(co));
273 co.GPSEpochTime = (int)GPSweeks;
274 co.GLONASSEpochTime = (int)fmod(GPSweeks, 86400.0)
275 + 3 * 3600 - gnumleap(year, month, day);
276 co.ClockDataSupplied = 1;
277 co.OrbitDataSupplied = 1;
278 co.SatRefDatum = DATUM_ITRF;
279
280 struct Bias bias;
281 memset(&bias, 0, sizeof(bias));
282 bias.GPSEpochTime = (int)GPSweeks;
283 bias.GLONASSEpochTime = (int)fmod(GPSweeks, 86400.0)
284 + 3 * 3600 - gnumleap(year, month, day);
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 in >> prn;
294 if ( ephMap.contains(prn) ) {
295 in >> xx(1) >> xx(2) >> xx(3) >> xx(4) >> xx(5)
296 >> xx(6) >> xx(7) >> xx(8) >> xx(9) >> xx(10)
297 >> xx(11) >> xx(12) >> xx(13) >> xx(14);
298 xx(1) *= 1e3; // x-crd
299 xx(2) *= 1e3; // y-crd
300 xx(3) *= 1e3; // z-crd
301 xx(4) *= 1e-6; // clk
302 xx(5) *= 1e-6; // rel. corr.
303 // xx(6), xx(7), xx(8) ... PhaseCent - CoM
304 // xx(9) ... P1-C1 DCB in meters
305 // xx(10) ... P1-P2 DCB in meters
306 // xx(11) ... dT
307 xx(12) *= 1e3; // x-crd at time + dT
308 xx(13) *= 1e3; // y-crd at time + dT
309 xx(14) *= 1e3; // z-crd at time + dT
310
311 pair = ephMap[prn];
312 }
313
314 // Use old ephemeris if the new one is too recent
315 // ----------------------------------------------
316 t_eph* ep = 0;
317 if (pair) {
318 ep = pair->last;
319 if (pair->prev && ep &&
320 ep->receptDateTime().secsTo(QDateTime::currentDateTime()) < 60) {
321 ep = pair->prev;
322 }
323 }
324
325 if (ep != 0) {
326 struct ClockOrbit::SatData* sd = 0;
327 if (prn[0] == 'G') {
328 sd = co.Sat + co.NumberOfGPSSat;
329 ++co.NumberOfGPSSat;
330 }
331 else if (prn[0] == 'R') {
332 sd = co.Sat + CLOCKORBIT_NUMGPS + co.NumberOfGLONASSSat;
333 ++co.NumberOfGLONASSSat;
334 }
335 if (sd) {
336 QString outLine;
337 processSatellite(ep, GPSweek, GPSweeks, prn, xx, sd, outLine);
338 if (_outFile) {
339 _outFile->write(GPSweek, GPSweeks, outLine);
340 }
341 }
342
343 struct Bias::BiasSat* biasSat = 0;
344 if (prn[0] == 'G') {
345 biasSat = bias.Sat + bias.NumberOfGPSSat;
346 ++bias.NumberOfGPSSat;
347 }
348 else if (prn[0] == 'R') {
349 biasSat = bias.Sat + CLOCKORBIT_NUMGPS + bias.NumberOfGLONASSSat;
350 ++bias.NumberOfGLONASSSat;
351 }
352
353 // Coefficient of Ionosphere-Free LC
354 // ---------------------------------
355 const static double a_L1_GPS = 2.54572778;
356 const static double a_L2_GPS = -1.54572778;
357 const static double a_L1_Glo = 2.53125000;
358 const static double a_L2_Glo = -1.53125000;
359
360 if (biasSat) {
361 biasSat->ID = prn.mid(1).toInt();
362 biasSat->NumberOfCodeBiases = 3;
363 if (prn[0] == 'G') {
364 biasSat->Biases[0].Type = CODETYPEGPS_L1_Z;
365 biasSat->Biases[0].Bias = - a_L2_GPS * xx(10);
366 biasSat->Biases[1].Type = CODETYPEGPS_L1_CA;
367 biasSat->Biases[1].Bias = - a_L2_GPS * xx(10) + xx(9);
368 biasSat->Biases[2].Type = CODETYPEGPS_L2_Z;
369 biasSat->Biases[2].Bias = a_L1_GPS * xx(10);
370 }
371 else if (prn[0] == 'R') {
372 biasSat->Biases[0].Type = CODETYPEGLONASS_L1_P;
373 biasSat->Biases[0].Bias = - a_L2_Glo * xx(10);
374 biasSat->Biases[1].Type = CODETYPEGLONASS_L1_CA;
375 biasSat->Biases[1].Bias = - a_L2_Glo * xx(10) + xx(9);
376 biasSat->Biases[2].Type = CODETYPEGLONASS_L2_P;
377 biasSat->Biases[2].Bias = a_L1_Glo * xx(10);
378 }
379 }
380 }
381 }
382
383 if ( this->usedSocket() &&
384 (co.NumberOfGPSSat > 0 || co.NumberOfGLONASSSat > 0) ) {
385 char obuffer[CLOCKORBIT_BUFFERSIZE];
386
387 int len = MakeClockOrbit(&co, COTYPE_AUTO, 0, obuffer, sizeof(obuffer));
388 if (len > 0) {
389 this->write(obuffer, len);
390 }
391 }
392
393 if ( this->usedSocket() &&
394 (bias.NumberOfGPSSat > 0 || bias.NumberOfGLONASSSat > 0) ) {
395 char obuffer[CLOCKORBIT_BUFFERSIZE];
396 int len = MakeBias(&bias, BTYPE_AUTO, 0, obuffer, sizeof(obuffer));
397 if (len > 0) {
398 this->write(obuffer, len);
399 }
400 }
401}
402
403//
404////////////////////////////////////////////////////////////////////////////
405void bncUploadCaster::processSatellite(t_eph* eph, int GPSweek,
406 double GPSweeks, const QString& prn,
407 const ColumnVector& xx,
408 struct ClockOrbit::SatData* sd,
409 QString& outLine) {
410
411 const double secPerWeek = 7.0 * 86400.0;
412
413 ColumnVector rsw(3);
414 ColumnVector rsw2(3);
415 double dClk;
416
417 for (int ii = 1; ii <= 2; ++ii) {
418
419 int GPSweek12 = GPSweek;
420 double GPSweeks12 = GPSweeks;
421 if (ii == 2) {
422 GPSweeks12 += xx(11);
423 if (GPSweeks12 > secPerWeek) {
424 GPSweek12 += 1;
425 GPSweeks12 -= secPerWeek;
426 }
427 }
428
429 ColumnVector xB(4);
430 ColumnVector vv(3);
431
432 eph->position(GPSweek12, GPSweeks12, xB.data(), vv.data());
433
434 ColumnVector xyz;
435 if (ii == 1) {
436 xyz = xx.Rows(1,3);
437 }
438 else {
439 xyz = xx.Rows(12,14);
440 }
441
442 // Correction Center of Mass -> Antenna Phase Center
443 // -------------------------------------------------
444 if (! _CoM) {
445 xyz(1) += xx(6);
446 xyz(2) += xx(7);
447 xyz(3) += xx(8);
448 }
449
450 if (_crdTrafo != "IGS05") {
451 crdTrafo(GPSweek12, xyz);
452 }
453
454 ColumnVector dx = xB.Rows(1,3) - xyz ;
455
456 if (ii == 1) {
457 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw);
458 dClk = (xx(4) + xx(5) - xB(4)) * 299792458.0;
459 }
460 else {
461 XYZ_to_RSW(xB.Rows(1,3), vv, dx, rsw2);
462 }
463 }
464
465 if (sd) {
466 sd->ID = prn.mid(1).toInt();
467 sd->IOD = eph->IOD();
468 sd->Clock.DeltaA0 = dClk;
469 sd->Orbit.DeltaRadial = rsw(1);
470 sd->Orbit.DeltaAlongTrack = rsw(2);
471 sd->Orbit.DeltaCrossTrack = rsw(3);
472 sd->Orbit.DotDeltaRadial = (rsw2(1) - rsw(1)) / xx(11);
473 sd->Orbit.DotDeltaAlongTrack = (rsw2(2) - rsw(2)) / xx(11);
474 sd->Orbit.DotDeltaCrossTrack = (rsw2(3) - rsw(3)) / xx(11);
475 }
476
477 outLine.sprintf("%d %.1f %s %3d %10.3f %8.3f %8.3f %8.3f\n",
478 GPSweek, GPSweeks, eph->prn().c_str(),
479 eph->IOD(), dClk, rsw(1), rsw(2), rsw(3));
480
481 if (_rnx) {
482 _rnx->write(GPSweek, GPSweeks, prn, xx);
483 }
484 if (_sp3) {
485 _sp3->write(GPSweek, GPSweeks, prn, xx);
486 }
487}
488
489// Transform Coordinates
490////////////////////////////////////////////////////////////////////////////
491void bncUploadCaster::crdTrafo(int GPSWeek, ColumnVector& xyz) {
492
493 // Current epoch minus 2000.0 in years
494 // ------------------------------------
495 double dt = (GPSWeek - (1042.0+6.0/7.0)) / 365.2422 * 7.0 + 2000.0 - _t0;
496
497 ColumnVector dx(3);
498
499 dx(1) = _dx + dt * _dxr;
500 dx(2) = _dy + dt * _dyr;
501 dx(3) = _dz + dt * _dzr;
502
503 static const double arcSec = 180.0 * 3600.0 / M_PI;
504
505 double ox = (_ox + dt * _oxr) / arcSec;
506 double oy = (_oy + dt * _oyr) / arcSec;
507 double oz = (_oz + dt * _ozr) / arcSec;
508
509 double sc = 1.0 + _sc * 1e-9 + dt * _scr * 1e-9;
510
511 Matrix rMat(3,3);
512 rMat(1,1) = 1.0;
513 rMat(1,2) = -oz;
514 rMat(1,3) = oy;
515 rMat(2,1) = oz;
516 rMat(2,2) = 1.0;
517 rMat(2,3) = -ox;
518 rMat(3,1) = -oy;
519 rMat(3,2) = ox;
520 rMat(3,3) = 1.0;
521
522 xyz = sc * rMat * xyz + dx;
523}
Note: See TracBrowser for help on using the repository browser.