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

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

* empty log message *

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