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

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