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

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

* empty log message *

File size: 11.8 KB
RevLine 
[758]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
[761]17#include <iostream>
18
[758]19#include "bnseph.h"
[776]20#include "bnsutils.h"
[758]21
22using namespace std;
23
24// Constructor
25////////////////////////////////////////////////////////////////////////////
26t_bnseph::t_bnseph(QObject* parent) : QThread(parent) {
[839]27
28 this->setTerminationEnabled(true);
29
[830]30 _socket = 0;
[1089]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 }
[758]58}
59
60// Destructor
61////////////////////////////////////////////////////////////////////////////
62t_bnseph::~t_bnseph() {
[759]63 delete _socket;
[1089]64 delete _echoStream;
65 delete _echoFile;
[758]66}
67
[838]68// Connect the Socket
[758]69////////////////////////////////////////////////////////////////////////////
[838]70void t_bnseph::reconnect() {
[759]71
[838]72 delete _socket;
[759]73
[830]74 QSettings settings;
[978]75 QString host = settings.value("ephHost").toString();
76 if (host.isEmpty()) {
77 host = "localhost";
78 }
[830]79 int port = settings.value("ephPort").toInt();
80
81 _socket = new QTcpSocket();
82 _socket->connectToHost(host, port);
83
[838]84 const int timeOut = 10*1000; // 10 seconds
[830]85 if (!_socket->waitForConnected(timeOut)) {
[1208]86// emit(newMessage("bnseph::run Connect Timeout"));
87 emit(newMessage("Ephemeris server: Connection timeout")); // weber
[1083]88 msleep(1000);
[830]89 }
[838]90}
91
92// Start
93////////////////////////////////////////////////////////////////////////////
94void t_bnseph::run() {
95
[1208]96//emit(newMessage("bnseph::run Start"));
97 emit(newMessage("Ephemeris server: Connection opened")); // weber
[838]98
99 while (true) {
100 if (_socket == 0 || _socket->state() != QAbstractSocket::ConnectedState) {
101 reconnect();
102 }
103 if (_socket && _socket->state() == QAbstractSocket::ConnectedState) {
[929]104 readEph();
[759]105 }
[838]106 else {
107 msleep(10);
108 }
[759]109 }
[758]110}
111
[771]112// Read One Ephemeris
113////////////////////////////////////////////////////////////////////////////
114void t_bnseph::readEph() {
115
[1058]116 int nBytes = 0;
[1125]117 t_eph* eph = 0;
[1058]118
[929]119 QByteArray line = waitForLine(_socket);
[1089]120
[1125]121 if (line.isEmpty()) {
122 return;
123 }
124
[1089]125 if (_echoStream) {
126 *_echoStream << line;
127 _echoStream->flush();
128 }
129
[1058]130 nBytes += line.length();
[782]131
[884]132 QTextStream in(line);
133 QString prn;
134
135 in >> prn;
[771]136
[884]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 }
[771]146
[884]147 QStringList lines;
148 lines << line;
149
150 for (int ii = 2; ii <= numlines; ii++) {
[929]151 QByteArray line = waitForLine(_socket);
[1125]152 if (line.isEmpty()) {
153 delete eph;
154 return;
155 }
[1089]156
157 if (_echoStream) {
158 *_echoStream << line;
159 _echoStream->flush();
160 }
161
[1058]162 nBytes += line.length();
[884]163 lines << line;
164 }
[782]165
[884]166 eph->read(lines);
[782]167
[1058]168 emit(newEph(eph, nBytes));
[884]169}
[778]170
[886]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
[884]183// Read GPS Ephemeris
184////////////////////////////////////////////////////////////////////////////
185void t_ephGPS::read(const QStringList& lines) {
[782]186
[884]187 for (int ii = 1; ii <= lines.size(); ii++) {
188 QTextStream in(lines.at(ii-1).toAscii());
[782]189
[884]190 if (ii == 1) {
[800]191 double year, month, day, hour, minute, second;
[884]192 in >> _prn >> year >> month >> day >> hour >> minute >> second
193 >> _clock_bias >> _clock_drift >> _clock_driftrate;
[781]194
195 if (year < 100) year += 2000;
196
[1101]197 QDateTime* dateTime = new QDateTime(QDate(int(year), int(month), int(day)),
198 QTime(int(hour), int(minute), int(second)), Qt::UTC);
[886]199
[1101]200 GPSweekFromDateAndTime(*dateTime, _GPSweek, _GPSweeks);
201
202 delete dateTime;
203
[886]204 _TOC = _GPSweeks;
[781]205 }
206 else if (ii == 2) {
[884]207 in >> _IODE >> _Crs >> _Delta_n >> _M0;
[781]208 }
209 else if (ii == 3) {
[884]210 in >> _Cuc >> _e >> _Cus >> _sqrt_A;
[781]211 }
212 else if (ii == 4) {
[884]213 in >> _TOE >> _Cic >> _OMEGA0 >> _Cis;
[781]214 }
215 else if (ii == 5) {
[884]216 in >> _i0 >> _Crc >> _omega >> _OMEGADOT;
[781]217 }
218 else if (ii == 6) {
[884]219 in >> _IDOT;
[781]220 }
221 else if (ii == 7) {
[800]222 double hlp, health;
[884]223 in >> hlp >> health >> _TGD >> _IODC;
[781]224 }
225 else if (ii == 8) {
[884]226 in >> _TOW;
[781]227 }
228 }
[884]229}
[773]230
[884]231// Compute GPS Satellite Position
232////////////////////////////////////////////////////////////////////////////
233void t_ephGPS::position(int GPSweek, double GPSweeks, ColumnVector& xc,
234 ColumnVector& vv) const {
235
[1100]236 static const double secPerWeek = 7 * 86400.0;
237 static const double omegaEarth = 7292115.1467e-11;
238 static const double gmWGS = 398.6005e12;
[884]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;
[771]318}
[884]319
[889]320// Read Glonass Ephemeris
[884]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;
[886]331
332 _tau = -_tau;
[884]333
334 if (year < 100) year += 2000;
335
[1101]336 QDateTime* dateTime = new QDateTime(QDate(int(year), int(month), int(day)),
337 QTime(int(hour), int(minute), int(second)), Qt::UTC);
[886]338
[1101]339 GPSweekFromDateAndTime(*dateTime, _GPSweek, _GPSweeks);
[910]340
[1101]341 delete dateTime;
342
[911]343 //// beg test
[912]344 //// _gps_utc = 14.0;
[911]345 //// end test
346
[910]347 _GPSweeks += _gps_utc;
[884]348 }
349 else if (ii == 2) {
[886]350 in >>_x_pos >> _x_velocity >> _x_acceleration >> _health;
[884]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
[888]360 // Initialize status vector
361 // ------------------------
[901]362 _tt = _GPSweeks;
[888]363
[893]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;
[884]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 // ------------
[1100]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;
[884]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
[889]408// Compute Glonass Satellite Position
[884]409////////////////////////////////////////////////////////////////////////////
410void t_ephGlo::position(int GPSweek, double GPSweeks, ColumnVector& xc,
411 ColumnVector& vv) const {
412
[1100]413 static const double secPerWeek = 7 * 86400.0;
414 static const double nominalStep = 10.0;
[891]415
[898]416 double dtPos = GPSweeks - _tt;
[891]417 if (GPSweek != _GPSweek) {
[898]418 dtPos += (GPSweek - _GPSweek) * secPerWeek;
[891]419 }
420
[899]421 int nSteps = int(fabs(dtPos) / nominalStep) + 1;
422 double step = dtPos / nSteps;
423
424 for (int ii = 1; ii <= nSteps; ii++) {
[900]425 _xv = rungeKutta4(_tt, _xv, step, glo_deriv);
[899]426 _tt += step;
[891]427 }
428
[892]429 // Position and Velocity
430 // ---------------------
[891]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);
[892]438
439 // Clock Correction
440 // ----------------
441 double dtClk = GPSweeks - _GPSweeks;
442 if (GPSweek != _GPSweek) {
443 dtClk += (GPSweek - _GPSweek) * secPerWeek;
444 }
[910]445 xc(4) = -_tau + _gamma * dtClk;
[884]446}
447
[926]448// Glonass IOD
449////////////////////////////////////////////////////////////////////////////
450int t_ephGlo::IOD() const {
451 //// return int(fmod(_GPSweeks,86400.0)) / 600;
452
[932]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
[926]456 // 5 LSBs of iod are equal to 5 LSBs of tb, remaining bits are zero
[931]457 // ----------------------------------------------------------------
458 const int shift = sizeof(tb) * 8 - 5;
459 unsigned int iod = tb << shift;
460 return (iod >> shift);
[926]461}
Note: See TracBrowser for help on using the repository browser.