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

Last change on this file since 1083 was 1083, checked in by weber, 16 years ago

* empty log message *

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