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