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

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

* empty log message *

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