1 | /* -------------------------------------------------------------------------
|
---|
2 | * BKG NTRIP Server
|
---|
3 | * -------------------------------------------------------------------------
|
---|
4 | *
|
---|
5 | * Class: bnseph
|
---|
6 | *
|
---|
7 | * Purpose: Retrieve broadcast ephemeris from BNC
|
---|
8 | *
|
---|
9 | * Author: L. Mervart
|
---|
10 | *
|
---|
11 | * Created: 29-Mar-2008
|
---|
12 | *
|
---|
13 | * Changes:
|
---|
14 | *
|
---|
15 | * -----------------------------------------------------------------------*/
|
---|
16 |
|
---|
17 | #include <iostream>
|
---|
18 |
|
---|
19 | #include "bnseph.h"
|
---|
20 | #include "bnsutils.h"
|
---|
21 | #include "bnssettings.h"
|
---|
22 |
|
---|
23 | using namespace std;
|
---|
24 |
|
---|
25 | // Constructor
|
---|
26 | ////////////////////////////////////////////////////////////////////////////
|
---|
27 | t_bnseph::t_bnseph(QObject* parent) : QThread(parent) {
|
---|
28 |
|
---|
29 | this->setTerminationEnabled(true);
|
---|
30 |
|
---|
31 | _socket = 0;
|
---|
32 |
|
---|
33 | bnsSettings settings;
|
---|
34 |
|
---|
35 | QIODevice::OpenMode oMode;
|
---|
36 | if (Qt::CheckState(settings.value("fileAppend").toInt()) == Qt::Checked) {
|
---|
37 | oMode = QIODevice::WriteOnly | QIODevice::Unbuffered | QIODevice::Append;
|
---|
38 | }
|
---|
39 | else {
|
---|
40 | oMode = QIODevice::WriteOnly | QIODevice::Unbuffered;
|
---|
41 | }
|
---|
42 |
|
---|
43 | // Echo ephemeris into a file
|
---|
44 | // ---------------------------
|
---|
45 | QString echoFileName = settings.value("ephEcho").toString();
|
---|
46 | if (echoFileName.isEmpty()) {
|
---|
47 | _echoFile = 0;
|
---|
48 | _echoStream = 0;
|
---|
49 | }
|
---|
50 | else {
|
---|
51 | _echoFile = new QFile(echoFileName);
|
---|
52 | if (_echoFile->open(oMode)) {
|
---|
53 | _echoStream = new QTextStream(_echoFile);
|
---|
54 | }
|
---|
55 | else {
|
---|
56 | _echoStream = 0;
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | // Destructor
|
---|
62 | ////////////////////////////////////////////////////////////////////////////
|
---|
63 | t_bnseph::~t_bnseph() {
|
---|
64 | delete _socket;
|
---|
65 | delete _echoStream;
|
---|
66 | delete _echoFile;
|
---|
67 | }
|
---|
68 |
|
---|
69 | // Connect the Socket
|
---|
70 | ////////////////////////////////////////////////////////////////////////////
|
---|
71 | void t_bnseph::reconnect() {
|
---|
72 |
|
---|
73 | delete _socket;
|
---|
74 |
|
---|
75 | bnsSettings settings;
|
---|
76 | QString host = settings.value("ephHost").toString();
|
---|
77 | if (host.isEmpty()) {
|
---|
78 | host = "localhost";
|
---|
79 | }
|
---|
80 | int port = settings.value("ephPort").toInt();
|
---|
81 |
|
---|
82 | _socket = new QTcpSocket();
|
---|
83 | _socket->connectToHost(host, port);
|
---|
84 |
|
---|
85 | const int timeOut = 10*1000; // 10 seconds
|
---|
86 | if (!_socket->waitForConnected(timeOut)) {
|
---|
87 | // emit(newMessage("bnseph::run Connect Timeout"));
|
---|
88 | emit(newMessage("Ephemeris server: Connection timeout")); // weber
|
---|
89 | msleep(1000);
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | // Start
|
---|
94 | ////////////////////////////////////////////////////////////////////////////
|
---|
95 | void t_bnseph::run() {
|
---|
96 |
|
---|
97 | //emit(newMessage("bnseph::run Start"));
|
---|
98 | emit(newMessage("Ephemeris server: Connection opened")); // weber
|
---|
99 |
|
---|
100 | while (true) {
|
---|
101 | if (_socket == 0 || _socket->state() != QAbstractSocket::ConnectedState) {
|
---|
102 | reconnect();
|
---|
103 | }
|
---|
104 | if (_socket && _socket->state() == QAbstractSocket::ConnectedState) {
|
---|
105 | readEph();
|
---|
106 | }
|
---|
107 | else {
|
---|
108 | msleep(10);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | // Read One Ephemeris
|
---|
114 | ////////////////////////////////////////////////////////////////////////////
|
---|
115 | void t_bnseph::readEph() {
|
---|
116 |
|
---|
117 | int nBytes = 0;
|
---|
118 | t_eph* eph = 0;
|
---|
119 |
|
---|
120 | QByteArray line = waitForLine(_socket);
|
---|
121 |
|
---|
122 | if (line.isEmpty()) {
|
---|
123 | return;
|
---|
124 | }
|
---|
125 |
|
---|
126 | if (_echoStream) {
|
---|
127 | *_echoStream << line;
|
---|
128 | _echoStream->flush();
|
---|
129 | }
|
---|
130 |
|
---|
131 | nBytes += line.length();
|
---|
132 |
|
---|
133 | QTextStream in(line);
|
---|
134 | QString prn;
|
---|
135 |
|
---|
136 | in >> prn;
|
---|
137 |
|
---|
138 | int numlines = 0;
|
---|
139 | if (prn.indexOf('R') != -1) {
|
---|
140 | eph = new t_ephGlo();
|
---|
141 | numlines = 4;
|
---|
142 | }
|
---|
143 | else {
|
---|
144 | eph = new t_ephGPS();
|
---|
145 | numlines = 8;
|
---|
146 | }
|
---|
147 |
|
---|
148 | QStringList lines;
|
---|
149 | lines << line;
|
---|
150 |
|
---|
151 | for (int ii = 2; ii <= numlines; ii++) {
|
---|
152 | QByteArray line = waitForLine(_socket);
|
---|
153 | if (line.isEmpty()) {
|
---|
154 | delete eph;
|
---|
155 | return;
|
---|
156 | }
|
---|
157 |
|
---|
158 | if (_echoStream) {
|
---|
159 | *_echoStream << line;
|
---|
160 | _echoStream->flush();
|
---|
161 | }
|
---|
162 |
|
---|
163 | nBytes += line.length();
|
---|
164 | lines << line;
|
---|
165 | }
|
---|
166 |
|
---|
167 | eph->read(lines);
|
---|
168 |
|
---|
169 | emit(newEph(eph, nBytes));
|
---|
170 | }
|
---|
171 |
|
---|
172 | // Compare Time
|
---|
173 | ////////////////////////////////////////////////////////////////////////////
|
---|
174 | bool t_eph::isNewerThan(const t_eph* eph) const {
|
---|
175 | if (_GPSweek > eph->_GPSweek ||
|
---|
176 | (_GPSweek == eph->_GPSweek && _GPSweeks > eph->_GPSweeks)) {
|
---|
177 | return true;
|
---|
178 | }
|
---|
179 | else {
|
---|
180 | return false;
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | // Read GPS Ephemeris
|
---|
185 | ////////////////////////////////////////////////////////////////////////////
|
---|
186 | void t_ephGPS::read(const QStringList& lines) {
|
---|
187 |
|
---|
188 | for (int ii = 1; ii <= lines.size(); ii++) {
|
---|
189 | QTextStream in(lines.at(ii-1).toAscii());
|
---|
190 |
|
---|
191 | if (ii == 1) {
|
---|
192 | double year, month, day, hour, minute, second;
|
---|
193 | in >> _prn >> year >> month >> day >> hour >> minute >> second
|
---|
194 | >> _clock_bias >> _clock_drift >> _clock_driftrate;
|
---|
195 |
|
---|
196 | if (year < 100) year += 2000;
|
---|
197 |
|
---|
198 | QDateTime* dateTime = new QDateTime(QDate(int(year), int(month), int(day)),
|
---|
199 | QTime(int(hour), int(minute), int(second)), Qt::UTC);
|
---|
200 |
|
---|
201 | GPSweekFromDateAndTime(*dateTime, _GPSweek, _GPSweeks);
|
---|
202 |
|
---|
203 | delete dateTime;
|
---|
204 |
|
---|
205 | _TOC = _GPSweeks;
|
---|
206 | }
|
---|
207 | else if (ii == 2) {
|
---|
208 | in >> _IODE >> _Crs >> _Delta_n >> _M0;
|
---|
209 | }
|
---|
210 | else if (ii == 3) {
|
---|
211 | in >> _Cuc >> _e >> _Cus >> _sqrt_A;
|
---|
212 | }
|
---|
213 | else if (ii == 4) {
|
---|
214 | in >> _TOE >> _Cic >> _OMEGA0 >> _Cis;
|
---|
215 | }
|
---|
216 | else if (ii == 5) {
|
---|
217 | in >> _i0 >> _Crc >> _omega >> _OMEGADOT;
|
---|
218 | }
|
---|
219 | else if (ii == 6) {
|
---|
220 | in >> _IDOT;
|
---|
221 | }
|
---|
222 | else if (ii == 7) {
|
---|
223 | double hlp, health;
|
---|
224 | in >> hlp >> health >> _TGD >> _IODC;
|
---|
225 | }
|
---|
226 | else if (ii == 8) {
|
---|
227 | in >> _TOW;
|
---|
228 | }
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | // Compute GPS Satellite Position
|
---|
233 | ////////////////////////////////////////////////////////////////////////////
|
---|
234 | void t_ephGPS::position(int GPSweek, double GPSweeks, ColumnVector& xc,
|
---|
235 | ColumnVector& vv) const {
|
---|
236 |
|
---|
237 | static const double secPerWeek = 7 * 86400.0;
|
---|
238 | static const double omegaEarth = 7292115.1467e-11;
|
---|
239 | static const double gmWGS = 398.6005e12;
|
---|
240 |
|
---|
241 | if (xc.Nrows() < 4) {
|
---|
242 | xc.ReSize(4);
|
---|
243 | }
|
---|
244 | xc = 0.0;
|
---|
245 |
|
---|
246 | if (vv.Nrows() < 3) {
|
---|
247 | vv.ReSize(3);
|
---|
248 | }
|
---|
249 | vv = 0.0;
|
---|
250 |
|
---|
251 | double a0 = _sqrt_A * _sqrt_A;
|
---|
252 | if (a0 == 0) {
|
---|
253 | return;
|
---|
254 | }
|
---|
255 |
|
---|
256 | double n0 = sqrt(gmWGS/(a0*a0*a0));
|
---|
257 | double tk = GPSweeks - _TOE;
|
---|
258 | if (GPSweek != _GPSweek) {
|
---|
259 | tk += (GPSweek - _GPSweek) * secPerWeek;
|
---|
260 | }
|
---|
261 | double n = n0 + _Delta_n;
|
---|
262 | double M = _M0 + n*tk;
|
---|
263 | double E = M;
|
---|
264 | double E_last;
|
---|
265 | do {
|
---|
266 | E_last = E;
|
---|
267 | E = M + _e*sin(E);
|
---|
268 | } while ( fabs(E-E_last)*a0 > 0.001 );
|
---|
269 | double v = 2.0*atan( sqrt( (1.0 + _e)/(1.0 - _e) )*tan( E/2 ) );
|
---|
270 | double u0 = v + _omega;
|
---|
271 | double sin2u0 = sin(2*u0);
|
---|
272 | double cos2u0 = cos(2*u0);
|
---|
273 | double r = a0*(1 - _e*cos(E)) + _Crc*cos2u0 + _Crs*sin2u0;
|
---|
274 | double i = _i0 + _IDOT*tk + _Cic*cos2u0 + _Cis*sin2u0;
|
---|
275 | double u = u0 + _Cuc*cos2u0 + _Cus*sin2u0;
|
---|
276 | double xp = r*cos(u);
|
---|
277 | double yp = r*sin(u);
|
---|
278 | double OM = _OMEGA0 + (_OMEGADOT - omegaEarth)*tk -
|
---|
279 | omegaEarth*_TOE;
|
---|
280 |
|
---|
281 | double sinom = sin(OM);
|
---|
282 | double cosom = cos(OM);
|
---|
283 | double sini = sin(i);
|
---|
284 | double cosi = cos(i);
|
---|
285 | xc(1) = xp*cosom - yp*cosi*sinom;
|
---|
286 | xc(2) = xp*sinom + yp*cosi*cosom;
|
---|
287 | xc(3) = yp*sini;
|
---|
288 |
|
---|
289 | double tc = GPSweeks - _TOC;
|
---|
290 | if (GPSweek != _GPSweek) {
|
---|
291 | tc += (GPSweek - _GPSweek) * secPerWeek;
|
---|
292 | }
|
---|
293 | xc(4) = _clock_bias + _clock_drift*tc + _clock_driftrate*tc*tc
|
---|
294 | - 4.442807633e-10 * _e * sqrt(a0) *sin(E);
|
---|
295 |
|
---|
296 | // Velocity
|
---|
297 | // --------
|
---|
298 | double tanv2 = tan(v/2);
|
---|
299 | double dEdM = 1 / (1 - _e*cos(E));
|
---|
300 | double dotv = sqrt((1.0 + _e)/(1.0 - _e)) / cos(E/2)/cos(E/2) / (1 + tanv2*tanv2)
|
---|
301 | * dEdM * n;
|
---|
302 | double dotu = dotv + (-_Cuc*sin2u0 + _Cus*cos2u0)*2*dotv;
|
---|
303 | double dotom = _OMEGADOT - omegaEarth;
|
---|
304 | double doti = _IDOT + (-_Cic*sin2u0 + _Cis*cos2u0)*2*dotv;
|
---|
305 | double dotr = a0 * _e*sin(E) * dEdM * n
|
---|
306 | + (-_Crc*sin2u0 + _Crs*cos2u0)*2*dotv;
|
---|
307 | double dotx = dotr*cos(u) - r*sin(u)*dotu;
|
---|
308 | double doty = dotr*sin(u) + r*cos(u)*dotu;
|
---|
309 |
|
---|
310 | vv(1) = cosom *dotx - cosi*sinom *doty // dX / dr
|
---|
311 | - xp*sinom*dotom - yp*cosi*cosom*dotom // dX / dOMEGA
|
---|
312 | + yp*sini*sinom*doti; // dX / di
|
---|
313 |
|
---|
314 | vv(2) = sinom *dotx + cosi*cosom *doty
|
---|
315 | + xp*cosom*dotom - yp*cosi*sinom*dotom
|
---|
316 | - yp*sini*cosom*doti;
|
---|
317 |
|
---|
318 | vv(3) = sini *doty + yp*cosi *doti;
|
---|
319 | }
|
---|
320 |
|
---|
321 | // Read Glonass Ephemeris
|
---|
322 | ////////////////////////////////////////////////////////////////////////////
|
---|
323 | void t_ephGlo::read(const QStringList& lines) {
|
---|
324 |
|
---|
325 | for (int ii = 1; ii <= lines.size(); ii++) {
|
---|
326 | QTextStream in(lines.at(ii-1).toAscii());
|
---|
327 |
|
---|
328 | if (ii == 1) {
|
---|
329 | double year, month, day, hour, minute, second;
|
---|
330 | in >> _prn >> year >> month >> day >> hour >> minute >> second
|
---|
331 | >> _tau >> _gamma;
|
---|
332 |
|
---|
333 | _tau = -_tau;
|
---|
334 |
|
---|
335 | if (year < 100) year += 2000;
|
---|
336 |
|
---|
337 | QDateTime* dateTime = new QDateTime(QDate(int(year), int(month), int(day)),
|
---|
338 | QTime(int(hour), int(minute), int(second)), Qt::UTC);
|
---|
339 |
|
---|
340 | GPSweekFromDateAndTime(*dateTime, _GPSweek, _GPSweeks);
|
---|
341 |
|
---|
342 | delete dateTime;
|
---|
343 |
|
---|
344 | //// beg test
|
---|
345 | //// _gps_utc = 14.0;
|
---|
346 | //// end test
|
---|
347 |
|
---|
348 | _GPSweeks += _gps_utc;
|
---|
349 | }
|
---|
350 | else if (ii == 2) {
|
---|
351 | in >>_x_pos >> _x_velocity >> _x_acceleration >> _health;
|
---|
352 | }
|
---|
353 | else if (ii == 3) {
|
---|
354 | in >>_y_pos >> _y_velocity >> _y_acceleration >> _frequency_number;
|
---|
355 | }
|
---|
356 | else if (ii == 4) {
|
---|
357 | in >>_z_pos >> _z_velocity >> _z_acceleration >> _E;
|
---|
358 | }
|
---|
359 | }
|
---|
360 |
|
---|
361 | // Initialize status vector
|
---|
362 | // ------------------------
|
---|
363 | _tt = _GPSweeks;
|
---|
364 |
|
---|
365 | _xv(1) = _x_pos * 1.e3;
|
---|
366 | _xv(2) = _y_pos * 1.e3;
|
---|
367 | _xv(3) = _z_pos * 1.e3;
|
---|
368 | _xv(4) = _x_velocity * 1.e3;
|
---|
369 | _xv(5) = _y_velocity * 1.e3;
|
---|
370 | _xv(6) = _z_velocity * 1.e3;
|
---|
371 | }
|
---|
372 |
|
---|
373 | // Derivative of the state vector using a simple force model (static)
|
---|
374 | ////////////////////////////////////////////////////////////////////////////
|
---|
375 | ColumnVector t_ephGlo::glo_deriv(double /* tt */, const ColumnVector& xv) {
|
---|
376 |
|
---|
377 | // State vector components
|
---|
378 | // -----------------------
|
---|
379 | ColumnVector rr = xv.rows(1,3);
|
---|
380 | ColumnVector vv = xv.rows(4,6);
|
---|
381 |
|
---|
382 | // Acceleration
|
---|
383 | // ------------
|
---|
384 | static const double GM = 398.60044e12;
|
---|
385 | static const double AE = 6378136.0;
|
---|
386 | static const double OMEGA = 7292115.e-11;
|
---|
387 | static const double C20 = -1082.63e-6;
|
---|
388 |
|
---|
389 | double rho = rr.norm_Frobenius();
|
---|
390 | double t1 = -GM/(rho*rho*rho);
|
---|
391 | double t2 = 3.0/2.0 * C20 * (GM*AE*AE) / (rho*rho*rho*rho*rho);
|
---|
392 | double t3 = OMEGA * OMEGA;
|
---|
393 | double t4 = 2.0 * OMEGA;
|
---|
394 | double z2 = rr(3) * rr(3);
|
---|
395 |
|
---|
396 | // Vector of derivatives
|
---|
397 | // ---------------------
|
---|
398 | ColumnVector va(6);
|
---|
399 | va(1) = vv(1);
|
---|
400 | va(2) = vv(2);
|
---|
401 | va(3) = vv(3);
|
---|
402 | va(4) = (t1 + t2*(1.0-5.0*z2/(rho*rho)) + t3) * rr(1) + t4*vv(2);
|
---|
403 | va(5) = (t1 + t2*(1.0-5.0*z2/(rho*rho)) + t3) * rr(2) - t4*vv(1);
|
---|
404 | va(6) = (t1 + t2*(3.0-5.0*z2/(rho*rho)) ) * rr(3);
|
---|
405 |
|
---|
406 | return va;
|
---|
407 | }
|
---|
408 |
|
---|
409 | // Compute Glonass Satellite Position
|
---|
410 | ////////////////////////////////////////////////////////////////////////////
|
---|
411 | void t_ephGlo::position(int GPSweek, double GPSweeks, ColumnVector& xc,
|
---|
412 | ColumnVector& vv) const {
|
---|
413 |
|
---|
414 | static const double secPerWeek = 7 * 86400.0;
|
---|
415 | static const double nominalStep = 10.0;
|
---|
416 |
|
---|
417 | double dtPos = GPSweeks - _tt;
|
---|
418 | if (GPSweek != _GPSweek) {
|
---|
419 | dtPos += (GPSweek - _GPSweek) * secPerWeek;
|
---|
420 | }
|
---|
421 |
|
---|
422 | int nSteps = int(fabs(dtPos) / nominalStep) + 1;
|
---|
423 | double step = dtPos / nSteps;
|
---|
424 |
|
---|
425 | for (int ii = 1; ii <= nSteps; ii++) {
|
---|
426 | _xv = rungeKutta4(_tt, _xv, step, glo_deriv);
|
---|
427 | _tt += step;
|
---|
428 | }
|
---|
429 |
|
---|
430 | // Position and Velocity
|
---|
431 | // ---------------------
|
---|
432 | xc(1) = _xv(1);
|
---|
433 | xc(2) = _xv(2);
|
---|
434 | xc(3) = _xv(3);
|
---|
435 |
|
---|
436 | vv(1) = _xv(4);
|
---|
437 | vv(2) = _xv(5);
|
---|
438 | vv(3) = _xv(6);
|
---|
439 |
|
---|
440 | // Clock Correction
|
---|
441 | // ----------------
|
---|
442 | double dtClk = GPSweeks - _GPSweeks;
|
---|
443 | if (GPSweek != _GPSweek) {
|
---|
444 | dtClk += (GPSweek - _GPSweek) * secPerWeek;
|
---|
445 | }
|
---|
446 | xc(4) = -_tau + _gamma * dtClk;
|
---|
447 | }
|
---|
448 |
|
---|
449 | // Glonass IOD
|
---|
450 | ////////////////////////////////////////////////////////////////////////////
|
---|
451 | int t_ephGlo::IOD() const {
|
---|
452 | //// return int(fmod(_GPSweeks,86400.0)) / 600;
|
---|
453 |
|
---|
454 | //// unsigned int tb = int(fmod(_GPSweeks,86400.0)) * 1000; // msec of day
|
---|
455 | unsigned int tb = int(fmod(_GPSweeks,86400.0)); //sec of day
|
---|
456 |
|
---|
457 | // 5 LSBs of iod are equal to 5 LSBs of tb, remaining bits are zero
|
---|
458 | // ----------------------------------------------------------------
|
---|
459 | const int shift = sizeof(tb) * 8 - 5;
|
---|
460 | unsigned int iod = tb << shift;
|
---|
461 | return (iod >> shift);
|
---|
462 | }
|
---|