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

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

* empty log message *

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