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

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

* empty log message *

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