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

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

* empty log message *

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