source: ntrip/trunk/BNS/bnseph.cpp@ 926

Last change on this file since 926 was 926, checked in by mervart, 16 years ago

* empty log message *

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