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