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

Last change on this file since 9448 was 3278, checked in by stoecker, 14 years ago

added type 1045

File size: 29.1 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 "bnssettings.h"
21#include "bnctime.h"
22extern "C" {
23#include "rtcm3torinex.h"
24}
25
26#define PI 3.1415926535898
27const static double c_vel = 299792458.0;
28
29using namespace std;
30using namespace BNS;
31
32// Constructor
33////////////////////////////////////////////////////////////////////////////
34t_bnseph::t_bnseph(QObject* parent) : QThread(parent) {
35
36 this->setTerminationEnabled(true);
37
38 _socket = 0;
39
40 bnsSettings settings;
41
42 QIODevice::OpenMode oMode;
43 if (Qt::CheckState(settings.value("fileAppend").toInt()) == Qt::Checked) {
44 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered | QIODevice::Append;
45 }
46 else {
47 oMode = QIODevice::WriteOnly | QIODevice::Unbuffered;
48 }
49
50 // Echo ephemeris into a file
51 // ---------------------------
52 QString echoFileName = settings.value("ephEcho").toString();
53 if (echoFileName.isEmpty()) {
54 _echoFile = 0;
55 _echoStream = 0;
56 }
57 else {
58 _echoFile = new QFile(echoFileName);
59 if (_echoFile->open(oMode)) {
60 _echoStream = new QTextStream(_echoFile);
61 }
62 else {
63 _echoStream = 0;
64 }
65 }
66}
67
68// Destructor
69////////////////////////////////////////////////////////////////////////////
70t_bnseph::~t_bnseph() {
71 delete _socket;
72 delete _echoStream;
73 delete _echoFile;
74}
75
76// Connect the Socket
77////////////////////////////////////////////////////////////////////////////
78void t_bnseph::reconnect() {
79
80 delete _socket;
81
82 bnsSettings settings;
83 QString host = settings.value("ephHost").toString();
84 if (host.isEmpty()) {
85 host = "localhost";
86 }
87 int port = settings.value("ephPort").toInt();
88
89 _socket = new QTcpSocket();
90 _socket->connectToHost(host, port);
91
92 const int timeOut = 30*1000; // 30 seconds
93 if (!_socket->waitForConnected(timeOut)) {
94// emit(newMessage("bnseph::run Connect Timeout"));
95 emit(newMessage("Ephemeris server: Connection timeout")); // weber
96 msleep(1000);
97 }
98}
99
100// Start
101////////////////////////////////////////////////////////////////////////////
102void t_bnseph::run() {
103
104//emit(newMessage("bnseph::run Start"));
105 emit(newMessage("Ephemeris server: Connection opened")); // weber
106
107 while (true) {
108 if ( _socket == 0 ||
109 (_socket->state() != QAbstractSocket::ConnectingState &&
110 _socket->state() != QAbstractSocket::ConnectedState) ) {
111 reconnect();
112 }
113 if (_socket && _socket->state() == QAbstractSocket::ConnectedState) {
114 readEph();
115 }
116 else {
117 msleep(10);
118 }
119 }
120}
121
122// Read One Ephemeris
123////////////////////////////////////////////////////////////////////////////
124void t_bnseph::readEph() {
125
126 int nBytes = 0;
127 t_eph* eph = 0;
128
129 QByteArray line = waitForLine(_socket);
130
131 if (line.isEmpty()) {
132 return;
133 }
134
135 if (_echoStream) {
136 *_echoStream << line;
137 _echoStream->flush();
138 }
139
140 nBytes += line.length();
141
142 QTextStream in(line);
143 QString prn;
144
145 in >> prn;
146
147 int numlines = 0;
148 if (prn.indexOf('R') != -1) {
149 eph = new t_ephGlo();
150 numlines = 4;
151 }
152 else if (prn.indexOf('G') != -1) {
153 eph = new t_ephGPS();
154 numlines = 8;
155 }
156 else if (prn.indexOf('E') != -1) {
157 eph = new t_ephGal();
158 numlines = 8;
159 }
160 else {
161 emit(newMessage(QString("Ephemeris server: line not recognized: %1")
162 .arg(line.data()).toAscii().data()));
163 return;
164 }
165
166 QStringList lines;
167 lines << line;
168
169 for (int ii = 2; ii <= numlines; ii++) {
170 QByteArray line = waitForLine(_socket);
171 if (line.isEmpty()) {
172 delete eph;
173 return;
174 }
175
176 if (_echoStream) {
177 *_echoStream << line;
178 _echoStream->flush();
179 }
180
181 nBytes += line.length();
182 lines << line;
183 }
184
185 if (eph->read(lines) == success) {
186 eph->setRecepDateTime(QDateTime::currentDateTime());
187 emit(newEph(eph, nBytes));
188 }
189 else {
190 emit(newMessage(QString("Broadcast message too new, excluded.\n%1")
191 .arg(lines.join("")).toAscii().data()));
192 delete eph;
193 return;
194 }
195}
196
197// Compare Time
198////////////////////////////////////////////////////////////////////////////
199bool t_eph::isNewerThan(const t_eph* eph) const {
200 if (_GPSweek > eph->_GPSweek ||
201 (_GPSweek == eph->_GPSweek && _GPSweeks > eph->_GPSweeks)) {
202 return true;
203 }
204 else {
205 return false;
206 }
207}
208
209// Read GPS Ephemeris
210////////////////////////////////////////////////////////////////////////////
211t_irc t_ephGPS::read(const QStringList& lines) {
212
213 for (int ii = 1; ii <= lines.size(); ii++) {
214 QTextStream in(lines.at(ii-1).toAscii());
215
216 if (ii == 1) {
217 double year, month, day, hour, minute, second;
218 in >> _prn >> year >> month >> day >> hour >> minute >> second
219 >> _clock_bias >> _clock_drift >> _clock_driftrate;
220
221 if (year < 100) year += 2000;
222
223 QDateTime* dateTime = new QDateTime(QDate(int(year), int(month), int(day)),
224 QTime(int(hour), int(minute), int(second)), Qt::UTC);
225
226 // do not allow too new message (actually 12h) ! /JD
227 QDateTime currTime = QDateTime::currentDateTime();
228 if( dateTime->secsTo(currTime) < -3600*12 ){
229 delete dateTime;
230 return failure;
231 }
232
233 GPSweekFromDateAndTime(*dateTime, _GPSweek, _GPSweeks);
234
235 delete dateTime;
236
237 _TOC = _GPSweeks;
238 }
239 else if (ii == 2) {
240 in >> _IODE >> _Crs >> _Delta_n >> _M0;
241 }
242 else if (ii == 3) {
243 in >> _Cuc >> _e >> _Cus >> _sqrt_A;
244 }
245 else if (ii == 4) {
246 in >> _TOE >> _Cic >> _OMEGA0 >> _Cis;
247 }
248 else if (ii == 5) {
249 in >> _i0 >> _Crc >> _omega >> _OMEGADOT;
250 }
251 else if (ii == 6) {
252 double GPSweek;
253 in >> _IDOT >> _L2Codes >> GPSweek >> _L2PFlag;
254 }
255 else if (ii == 7) {
256 in >> _ura >> _health >> _TGD >> _IODC;
257 }
258 else if (ii == 8) {
259 in >> _TOW;
260 }
261 }
262 return success;
263}
264
265// Returns nearest integer value
266////////////////////////////////////////////////////////////////////////////
267static double NearestInt(double fl, double * remain)
268{
269 bool isneg = fl < 0.0;
270 double intval;
271 if(isneg) fl *= -1.0;
272 intval = (double)((unsigned long)(fl+0.5));
273 if(isneg) {fl *= -1.0; intval *= -1.0;}
274 if(remain)
275 *remain = fl-intval;
276 return intval;
277} /* NearestInt() */
278
279// Returns CRC24
280////////////////////////////////////////////////////////////////////////////
281static unsigned long CRC24(long size, const unsigned char *buf)
282{
283 unsigned long crc = 0;
284 int i;
285
286 while(size--)
287 {
288 crc ^= (*buf++) << (16);
289 for(i = 0; i < 8; i++)
290 {
291 crc <<= 1;
292 if(crc & 0x1000000)
293 crc ^= 0x01864cfb;
294 }
295 }
296 return crc;
297} /* CRC24 */
298
299// build up RTCM3 for GPS
300////////////////////////////////////////////////////////////////////////////
301
302#define GPSTOINT(type, value) static_cast<type>(NearestInt(value,0))
303
304#define GPSADDBITS(a, b) {bitbuffer = (bitbuffer<<(a)) \
305 |(GPSTOINT(long long,b)&((1ULL<<a)-1)); \
306 numbits += (a); \
307 while(numbits >= 8) { \
308 buffer[size++] = bitbuffer>>(numbits-8);numbits -= 8;}}
309#define GPSADDBITSFLOAT(a,b,c) {long long i = GPSTOINT(long long,(b)/(c)); \
310 GPSADDBITS(a,i)};
311
312int t_ephGPS::RTCM3(unsigned char *buffer)
313{
314
315 unsigned char *startbuffer = buffer;
316 buffer= buffer+3;
317 int size = 0;
318 int numbits = 0;
319 unsigned long long bitbuffer = 0;
320 if (_ura <= 2.40){
321 _ura = 0;
322 }
323 else if (_ura <= 3.40){
324 _ura = 1;
325 }
326 else if (_ura <= 6.85){
327 _ura = 2;
328 }
329 else if (_ura <= 9.65){
330 _ura = 3;
331 }
332 else if (_ura <= 13.65){
333 _ura = 4;
334 }
335 else if (_ura <= 24.00){
336 _ura = 5;
337 }
338 else if (_ura <= 48.00){
339 _ura = 6;
340 }
341 else if (_ura <= 96.00){
342 _ura = 7;
343 }
344 else if (_ura <= 192.00){
345 _ura = 8;
346 }
347 else if (_ura <= 384.00){
348 _ura = 9;
349 }
350 else if (_ura <= 768.00){
351 _ura = 10;
352 }
353 else if (_ura <= 1536.00){
354 _ura = 11;
355 }
356 else if (_ura <= 1536.00){
357 _ura = 12;
358 }
359 else if (_ura <= 2072.00){
360 _ura = 13;
361 }
362 else if (_ura <= 6144.00){
363 _ura = 14;
364 }
365 else{
366 _ura = 15;
367 }
368
369 GPSADDBITS(12, 1019)
370 GPSADDBITS(6,_prn.right((_prn.length()-1)).toInt())
371 GPSADDBITS(10, _GPSweek)
372 GPSADDBITS(4, _ura)
373 GPSADDBITS(2,_L2Codes)
374 GPSADDBITSFLOAT(14, _IDOT, PI/static_cast<double>(1<<30)
375 /static_cast<double>(1<<13))
376 GPSADDBITS(8, _IODE)
377 GPSADDBITS(16, static_cast<int>(_TOC)>>4)
378 GPSADDBITSFLOAT(8, _clock_driftrate, 1.0/static_cast<double>(1<<30)
379 /static_cast<double>(1<<25))
380 GPSADDBITSFLOAT(16, _clock_drift, 1.0/static_cast<double>(1<<30)
381 /static_cast<double>(1<<13))
382 GPSADDBITSFLOAT(22, _clock_bias, 1.0/static_cast<double>(1<<30)
383 /static_cast<double>(1<<1))
384 GPSADDBITS(10, _IODC)
385 GPSADDBITSFLOAT(16, _Crs, 1.0/static_cast<double>(1<<5))
386 GPSADDBITSFLOAT(16, _Delta_n, PI/static_cast<double>(1<<30)
387 /static_cast<double>(1<<13))
388 GPSADDBITSFLOAT(32, _M0, PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
389 GPSADDBITSFLOAT(16, _Cuc, 1.0/static_cast<double>(1<<29))
390 GPSADDBITSFLOAT(32, _e, 1.0/static_cast<double>(1<<30)/static_cast<double>(1<<3))
391 GPSADDBITSFLOAT(16, _Cus, 1.0/static_cast<double>(1<<29))
392 GPSADDBITSFLOAT(32, _sqrt_A, 1.0/static_cast<double>(1<<19))
393 GPSADDBITS(16, static_cast<int>(_TOE)>>4)
394 GPSADDBITSFLOAT(16, _Cic, 1.0/static_cast<double>(1<<29))
395 GPSADDBITSFLOAT(32, _OMEGA0, PI/static_cast<double>(1<<30)
396 /static_cast<double>(1<<1))
397 GPSADDBITSFLOAT(16, _Cis, 1.0/static_cast<double>(1<<29))
398 GPSADDBITSFLOAT(32, _i0, PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
399 GPSADDBITSFLOAT(16, _Crc, 1.0/static_cast<double>(1<<5))
400 GPSADDBITSFLOAT(32, _omega, PI/static_cast<double>(1<<30)
401 /static_cast<double>(1<<1))
402 GPSADDBITSFLOAT(24, _OMEGADOT, PI/static_cast<double>(1<<30)
403 /static_cast<double>(1<<13))
404 GPSADDBITSFLOAT(8, _TGD, 1.0/static_cast<double>(1<<30)/static_cast<double>(1<<1))
405 GPSADDBITS(6, _health)
406 GPSADDBITS(1, _L2PFlag)
407 GPSADDBITS(1, 0) /* GPS fit interval */
408
409 startbuffer[0]=0xD3;
410 startbuffer[1]=(size >> 8);
411 startbuffer[2]=size;
412 unsigned long i = CRC24(size+3, startbuffer);
413 buffer[size++] = i >> 16;
414 buffer[size++] = i >> 8;
415 buffer[size++] = i;
416 size += 3;
417 return size;
418}
419
420// Compute GPS Satellite Position
421////////////////////////////////////////////////////////////////////////////
422void t_ephGPS::position(int GPSweek, double GPSweeks, ColumnVector& xc,
423 ColumnVector& vv) const {
424
425 static const double secPerWeek = 7 * 86400.0;
426 static const double omegaEarth = 7292115.1467e-11;
427 static const double gmWGS = 398.6005e12;
428
429 if (xc.Nrows() < 4) {
430 xc.ReSize(4);
431 }
432 xc = 0.0;
433
434 if (vv.Nrows() < 3) {
435 vv.ReSize(3);
436 }
437 vv = 0.0;
438
439 double a0 = _sqrt_A * _sqrt_A;
440 if (a0 == 0) {
441 return;
442 }
443
444 double n0 = sqrt(gmWGS/(a0*a0*a0));
445 double tk = GPSweeks - _TOE;
446 if (GPSweek != _GPSweek) {
447 tk += (GPSweek - _GPSweek) * secPerWeek;
448 }
449 double n = n0 + _Delta_n;
450 double M = _M0 + n*tk;
451 double E = M;
452 double E_last;
453 do {
454 E_last = E;
455 E = M + _e*sin(E);
456 } while ( fabs(E-E_last)*a0 > 0.001 );
457 double v = 2.0*atan( sqrt( (1.0 + _e)/(1.0 - _e) )*tan( E/2 ) );
458 double u0 = v + _omega;
459 double sin2u0 = sin(2*u0);
460 double cos2u0 = cos(2*u0);
461 double r = a0*(1 - _e*cos(E)) + _Crc*cos2u0 + _Crs*sin2u0;
462 double i = _i0 + _IDOT*tk + _Cic*cos2u0 + _Cis*sin2u0;
463 double u = u0 + _Cuc*cos2u0 + _Cus*sin2u0;
464 double xp = r*cos(u);
465 double yp = r*sin(u);
466 double OM = _OMEGA0 + (_OMEGADOT - omegaEarth)*tk -
467 omegaEarth*_TOE;
468
469 double sinom = sin(OM);
470 double cosom = cos(OM);
471 double sini = sin(i);
472 double cosi = cos(i);
473 xc(1) = xp*cosom - yp*cosi*sinom;
474 xc(2) = xp*sinom + yp*cosi*cosom;
475 xc(3) = yp*sini;
476
477 double tc = GPSweeks - _TOC;
478 if (GPSweek != _GPSweek) {
479 tc += (GPSweek - _GPSweek) * secPerWeek;
480 }
481 xc(4) = _clock_bias + _clock_drift*tc + _clock_driftrate*tc*tc;
482
483 // Velocity
484 // --------
485 double tanv2 = tan(v/2);
486 double dEdM = 1 / (1 - _e*cos(E));
487 double dotv = sqrt((1.0 + _e)/(1.0 - _e)) / cos(E/2)/cos(E/2) / (1 + tanv2*tanv2)
488 * dEdM * n;
489 double dotu = dotv + (-_Cuc*sin2u0 + _Cus*cos2u0)*2*dotv;
490 double dotom = _OMEGADOT - omegaEarth;
491 double doti = _IDOT + (-_Cic*sin2u0 + _Cis*cos2u0)*2*dotv;
492 double dotr = a0 * _e*sin(E) * dEdM * n
493 + (-_Crc*sin2u0 + _Crs*cos2u0)*2*dotv;
494 double dotx = dotr*cos(u) - r*sin(u)*dotu;
495 double doty = dotr*sin(u) + r*cos(u)*dotu;
496
497 vv(1) = cosom *dotx - cosi*sinom *doty // dX / dr
498 - xp*sinom*dotom - yp*cosi*cosom*dotom // dX / dOMEGA
499 + yp*sini*sinom*doti; // dX / di
500
501 vv(2) = sinom *dotx + cosi*cosom *doty
502 + xp*cosom*dotom - yp*cosi*sinom*dotom
503 - yp*sini*cosom*doti;
504
505 vv(3) = sini *doty + yp*cosi *doti;
506
507 // Relativistic Correction
508 // -----------------------
509 // xc(4) -= 4.442807633e-10 * _e * sqrt(a0) *sin(E);
510 xc(4) -= 2.0 * DotProduct(xc.Rows(1,3),vv) / c_vel / c_vel;
511}
512
513// Read Glonass Ephemeris
514////////////////////////////////////////////////////////////////////////////
515t_irc t_ephGlo::read(const QStringList& lines) {
516
517 static const double secPerWeek = 7 * 86400.0;
518
519 for (int ii = 1; ii <= lines.size(); ii++) {
520 QTextStream in(lines.at(ii-1).toAscii());
521
522 if (ii == 1) {
523 double year, month, day, hour, minute, second;
524 in >> _prn >> year >> month >> day >> hour >> minute >> second
525 >> _tau >> _gamma >> _tki;
526
527 _tau = -_tau;
528
529 if (year < 100) year += 2000;
530
531 // do not allow too new message (actually 24h) ! /JD
532 QDateTime mesgTime = QDateTime::fromString(QString("%1-%2-%3 %4").arg(year).arg(month).arg(day).arg(hour), "yyyy-MM-dd hh");
533 QDateTime currTime = QDateTime::currentDateTime();
534 if (mesgTime.secsTo(currTime) < -3600*24) {
535 return failure;
536 }
537
538 bncTime tHlp;
539 tHlp.set(int(year), int(month), int(day),
540 int(hour), int(minute), second);
541
542 _GPSweek = tHlp.gpsw();
543 _GPSweeks = tHlp.gpssec();
544
545 // Correct UTC -> GPS;
546 // -------------------
547 _gps_utc = gnumleap(int(year), int(month), int(day));
548 _GPSweeks += _gps_utc;
549 if (_GPSweeks >= secPerWeek) {
550 _GPSweek += 1;
551 _GPSweeks -= secPerWeek;
552 }
553 }
554 else if (ii == 2) {
555 in >>_x_pos >> _x_velocity >> _x_acceleration >> _health;
556 }
557 else if (ii == 3) {
558 in >>_y_pos >> _y_velocity >> _y_acceleration >> _frequency_number;
559 }
560 else if (ii == 4) {
561 in >>_z_pos >> _z_velocity >> _z_acceleration >> _E;
562 }
563 }
564
565 // Initialize status vector
566 // ------------------------
567 _tt = _GPSweeks;
568
569 _xv(1) = _x_pos * 1.e3;
570 _xv(2) = _y_pos * 1.e3;
571 _xv(3) = _z_pos * 1.e3;
572 _xv(4) = _x_velocity * 1.e3;
573 _xv(5) = _y_velocity * 1.e3;
574 _xv(6) = _z_velocity * 1.e3;
575
576 return success;
577}
578
579
580// build up RTCM3 for GLONASS
581////////////////////////////////////////////////////////////////////////////
582#define GLONASSTOINT(type, value) static_cast<type>(NearestInt(value,0))
583
584#define GLONASSADDBITS(a, b) {bitbuffer = (bitbuffer<<(a)) \
585 |(GLONASSTOINT(long long,b)&((1ULL<<(a))-1)); \
586 numbits += (a); \
587 while(numbits >= 8) { \
588 buffer[size++] = bitbuffer>>(numbits-8);numbits -= 8;}}
589#define GLONASSADDBITSFLOATM(a,b,c) {int s; long long i; \
590 if(b < 0.0) \
591 { \
592 s = 1; \
593 i = GLONASSTOINT(long long,(-b)/(c)); \
594 if(!i) s = 0; \
595 } \
596 else \
597 { \
598 s = 0; \
599 i = GLONASSTOINT(long long,(b)/(c)); \
600 } \
601 GLONASSADDBITS(1,s) \
602 GLONASSADDBITS(a-1,i)}
603
604int t_ephGlo::RTCM3(unsigned char *buffer)
605{
606
607 int size = 0;
608 int numbits = 0;
609 long long bitbuffer = 0;
610 unsigned char *startbuffer = buffer;
611 buffer= buffer+3;
612
613 GLONASSADDBITS(12, 1020)
614 GLONASSADDBITS(6, _prn.right((_prn.length()-1)).toInt())
615 GLONASSADDBITS(5, 7+_frequency_number)
616 GLONASSADDBITS(1, 0)
617 GLONASSADDBITS(1, 0)
618 GLONASSADDBITS(2, 0)
619 _tki=_tki+3*60*60;
620 GLONASSADDBITS(5, static_cast<int>(_tki)/(60*60))
621 GLONASSADDBITS(6, (static_cast<int>(_tki)/60)%60)
622 GLONASSADDBITS(1, (static_cast<int>(_tki)/30)%30)
623 GLONASSADDBITS(1, _health)
624 GLONASSADDBITS(1, 0)
625 unsigned long long timeofday = (static_cast<int>(_tt+3*60*60-_gps_utc)%86400);
626 GLONASSADDBITS(7, timeofday/60/15)
627 GLONASSADDBITSFLOATM(24, _x_velocity*1000, 1000.0/static_cast<double>(1<<20))
628 GLONASSADDBITSFLOATM(27, _x_pos*1000, 1000.0/static_cast<double>(1<<11))
629 GLONASSADDBITSFLOATM(5, _x_acceleration*1000, 1000.0/static_cast<double>(1<<30))
630 GLONASSADDBITSFLOATM(24, _y_velocity*1000, 1000.0/static_cast<double>(1<<20))
631 GLONASSADDBITSFLOATM(27, _y_pos*1000, 1000.0/static_cast<double>(1<<11))
632 GLONASSADDBITSFLOATM(5, _y_acceleration*1000, 1000.0/static_cast<double>(1<<30))
633 GLONASSADDBITSFLOATM(24, _z_velocity*1000, 1000.0/static_cast<double>(1<<20))
634 GLONASSADDBITSFLOATM(27,_z_pos*1000, 1000.0/static_cast<double>(1<<11))
635 GLONASSADDBITSFLOATM(5, _z_acceleration*1000, 1000.0/static_cast<double>(1<<30))
636 GLONASSADDBITS(1, 0)
637 GLONASSADDBITSFLOATM(11, _gamma, 1.0/static_cast<double>(1<<30)
638 /static_cast<double>(1<<10))
639 GLONASSADDBITS(2, 0) /* GLONASS-M P */
640 GLONASSADDBITS(1, 0) /* GLONASS-M ln(3) */
641 GLONASSADDBITSFLOATM(22, _tau, 1.0/static_cast<double>(1<<30))
642 GLONASSADDBITS(5, 0) /* GLONASS-M delta tau */
643 GLONASSADDBITS(5, _E)
644 GLONASSADDBITS(1, 0) /* GLONASS-M P4 */
645 GLONASSADDBITS(4, 0) /* GLONASS-M FT */
646 GLONASSADDBITS(11, 0) /* GLONASS-M NT */
647 GLONASSADDBITS(2, 0) /* GLONASS-M active? */
648 GLONASSADDBITS(1, 0) /* GLONASS additional data */
649 GLONASSADDBITS(11, 0) /* GLONASS NA */
650 GLONASSADDBITS(32, 0) /* GLONASS tau C */
651 GLONASSADDBITS(5, 0) /* GLONASS-M N4 */
652 GLONASSADDBITS(22, 0) /* GLONASS-M tau GPS */
653 GLONASSADDBITS(1, 0) /* GLONASS-M ln(5) */
654 GLONASSADDBITS(7, 0) /* Reserved */
655
656 startbuffer[0]=0xD3;
657 startbuffer[1]=(size >> 8);
658 startbuffer[2]=size;
659 unsigned long i = CRC24(size+3, startbuffer);
660 buffer[size++] = i >> 16;
661 buffer[size++] = i >> 8;
662 buffer[size++] = i;
663 size += 3;
664 return size;
665}
666
667// Derivative of the state vector using a simple force model (static)
668////////////////////////////////////////////////////////////////////////////
669ColumnVector t_ephGlo::glo_deriv(double /* tt */, const ColumnVector& xv,
670 double* acc) {
671
672 // State vector components
673 // -----------------------
674 ColumnVector rr = xv.rows(1,3);
675 ColumnVector vv = xv.rows(4,6);
676
677 // Acceleration
678 // ------------
679 static const double GM = 398.60044e12;
680 static const double AE = 6378136.0;
681 static const double OMEGA = 7292115.e-11;
682 static const double C20 = -1082.6257e-6;
683
684 double rho = rr.norm_Frobenius();
685 double t1 = -GM/(rho*rho*rho);
686 double t2 = 3.0/2.0 * C20 * (GM*AE*AE) / (rho*rho*rho*rho*rho);
687 double t3 = OMEGA * OMEGA;
688 double t4 = 2.0 * OMEGA;
689 double z2 = rr(3) * rr(3);
690
691 // Vector of derivatives
692 // ---------------------
693 ColumnVector va(6);
694 va(1) = vv(1);
695 va(2) = vv(2);
696 va(3) = vv(3);
697 va(4) = (t1 + t2*(1.0-5.0*z2/(rho*rho)) + t3) * rr(1) + t4*vv(2) + acc[0];
698 va(5) = (t1 + t2*(1.0-5.0*z2/(rho*rho)) + t3) * rr(2) - t4*vv(1) + acc[1];
699 va(6) = (t1 + t2*(3.0-5.0*z2/(rho*rho)) ) * rr(3) + acc[2];
700
701 return va;
702}
703
704// Compute Glonass Satellite Position
705////////////////////////////////////////////////////////////////////////////
706void t_ephGlo::position(int GPSweek, double GPSweeks, ColumnVector& xc,
707 ColumnVector& vv) const {
708
709 static const double secPerWeek = 7 * 86400.0;
710 static const double nominalStep = 10.0;
711
712 double dtPos = GPSweeks - _tt;
713 if (GPSweek != _GPSweek) {
714 dtPos += (GPSweek - _GPSweek) * secPerWeek;
715 }
716
717 int nSteps = int(fabs(dtPos) / nominalStep) + 1;
718 double step = dtPos / nSteps;
719
720 double acc[3];
721 acc[0] = _x_acceleration * 1.e3;
722 acc[1] = _y_acceleration * 1.e3;
723 acc[2] = _z_acceleration * 1.e3;
724 for (int ii = 1; ii <= nSteps; ii++) {
725 _xv = rungeKutta4(_tt, _xv, step, acc, glo_deriv);
726 _tt += step;
727 }
728
729 // Position and Velocity
730 // ---------------------
731 xc(1) = _xv(1);
732 xc(2) = _xv(2);
733 xc(3) = _xv(3);
734
735 vv(1) = _xv(4);
736 vv(2) = _xv(5);
737 vv(3) = _xv(6);
738
739 // Clock Correction
740 // ----------------
741 double dtClk = GPSweeks - _GPSweeks;
742 if (GPSweek != _GPSweek) {
743 dtClk += (GPSweek - _GPSweek) * secPerWeek;
744 }
745 xc(4) = -_tau + _gamma * dtClk;
746}
747
748// Glonass IOD
749////////////////////////////////////////////////////////////////////////////
750int t_ephGlo::IOD() const {
751
752 bool old = false;
753
754 if (old) { // 5 LSBs of iod are equal to 5 LSBs of tb
755 unsigned int tb = int(fmod(_GPSweeks,86400.0)); //sec of day
756 const int shift = sizeof(tb) * 8 - 5;
757 unsigned int iod = tb << shift;
758 return (iod >> shift);
759 }
760 else {
761 bncTime tGPS(_GPSweek, _GPSweeks);
762 int hlpWeek = _GPSweek;
763 int hlpSec = int(_GPSweeks);
764 int hlpMsec = int(_GPSweeks * 1000);
765 updatetime(&hlpWeek, &hlpSec, hlpMsec, 0);
766 bncTime tHlp(hlpWeek, hlpSec);
767 double diffSec = tGPS - tHlp;
768 bncTime tMoscow = tGPS + diffSec;
769 return int(tMoscow.daysec() / 900);
770 }
771}
772
773// Compute Galileo Satellite Position
774////////////////////////////////////////////////////////////////////////////
775void t_ephGal::position(int GPSweek, double GPSweeks, ColumnVector& xc,
776 ColumnVector& vv) const {
777
778 static const double secPerWeek = 7 * 86400.0;
779 static const double omegaEarth = 7292115.1467e-11;
780 static const double gmWGS = 398.6005e12;
781
782 xc.ReSize(4); xc = 0.0;
783 vv.ReSize(3); vv = 0.0;
784
785 double a0 = _sqrt_A * _sqrt_A;
786 if (a0 == 0) {
787 return;
788 }
789
790 double n0 = sqrt(gmWGS/(a0*a0*a0));
791 double tk = GPSweeks - _TOE;
792 if (GPSweek != _GPSweek) {
793 tk += (GPSweek - _GPSweek) * secPerWeek;
794 }
795 double n = n0 + _Delta_n;
796 double M = _M0 + n*tk;
797 double E = M;
798 double E_last;
799 do {
800 E_last = E;
801 E = M + _e*sin(E);
802 } while ( fabs(E-E_last)*a0 > 0.001 );
803 double v = 2.0*atan( sqrt( (1.0 + _e)/(1.0 - _e) )*tan( E/2 ) );
804 double u0 = v + _omega;
805 double sin2u0 = sin(2*u0);
806 double cos2u0 = cos(2*u0);
807 double r = a0*(1 - _e*cos(E)) + _Crc*cos2u0 + _Crs*sin2u0;
808 double i = _i0 + _IDOT*tk + _Cic*cos2u0 + _Cis*sin2u0;
809 double u = u0 + _Cuc*cos2u0 + _Cus*sin2u0;
810 double xp = r*cos(u);
811 double yp = r*sin(u);
812 double OM = _OMEGA0 + (_OMEGADOT - omegaEarth)*tk -
813 omegaEarth*_TOE;
814
815 double sinom = sin(OM);
816 double cosom = cos(OM);
817 double sini = sin(i);
818 double cosi = cos(i);
819 xc[0] = xp*cosom - yp*cosi*sinom;
820 xc[1] = xp*sinom + yp*cosi*cosom;
821 xc[2] = yp*sini;
822
823 double tc = GPSweeks - _TOC;
824 if (GPSweek != _GPSweek) {
825 tc += (GPSweek - _GPSweek) * secPerWeek;
826 }
827 xc[3] = _clock_bias + _clock_drift*tc + _clock_driftrate*tc*tc;
828
829 // Velocity
830 // --------
831 double tanv2 = tan(v/2);
832 double dEdM = 1 / (1 - _e*cos(E));
833 double dotv = sqrt((1.0 + _e)/(1.0 - _e)) / cos(E/2)/cos(E/2) / (1 + tanv2*tanv2)
834 * dEdM * n;
835 double dotu = dotv + (-_Cuc*sin2u0 + _Cus*cos2u0)*2*dotv;
836 double dotom = _OMEGADOT - omegaEarth;
837 double doti = _IDOT + (-_Cic*sin2u0 + _Cis*cos2u0)*2*dotv;
838 double dotr = a0 * _e*sin(E) * dEdM * n
839 + (-_Crc*sin2u0 + _Crs*cos2u0)*2*dotv;
840 double dotx = dotr*cos(u) - r*sin(u)*dotu;
841 double doty = dotr*sin(u) + r*cos(u)*dotu;
842
843 vv[0] = cosom *dotx - cosi*sinom *doty // dX / dr
844 - xp*sinom*dotom - yp*cosi*cosom*dotom // dX / dOMEGA
845 + yp*sini*sinom*doti; // dX / di
846
847 vv[1] = sinom *dotx + cosi*cosom *doty
848 + xp*cosom*dotom - yp*cosi*sinom*dotom
849 - yp*sini*cosom*doti;
850
851 vv[2] = sini *doty + yp*cosi *doti;
852
853 // Relativistic Correction
854 // -----------------------
855 // xc(4) -= 4.442807633e-10 * _e * sqrt(a0) *sin(E);
856 xc[3] -= 2.0 * (xc[0]*vv[0] + xc[1]*vv[1] + xc[2]*vv[2]) / c_vel / c_vel;
857}
858
859// Read Galileo Ephemeris
860////////////////////////////////////////////////////////////////////////////
861t_irc t_ephGal::read(const QStringList& lines) {
862
863 for (int ii = 1; ii <= lines.size(); ii++) {
864 QTextStream in(lines.at(ii-1).toAscii());
865
866 if (ii == 1) {
867 double year, month, day, hour, minute, second;
868 in >> _prn >> year >> month >> day >> hour >> minute >> second
869 >> _clock_bias >> _clock_drift >> _clock_driftrate;
870
871 if (year < 100) year += 2000;
872
873 QDateTime* dateTime = new QDateTime(QDate(int(year), int(month), int(day)),
874 QTime(int(hour), int(minute), int(second)), Qt::UTC);
875
876 // do not allow too new message (actually 12h) ! /JD
877 QDateTime currTime = QDateTime::currentDateTime();
878 if( dateTime->secsTo(currTime) < -3600*12 ){
879 delete dateTime;
880 return failure;
881 }
882
883 GPSweekFromDateAndTime(*dateTime, _GPSweek, _GPSweeks);
884
885 delete dateTime;
886
887 _TOC = _GPSweeks;
888 }
889 else if (ii == 2) {
890 in >> _IODnav >> _Crs >> _Delta_n >> _M0;
891 }
892 else if (ii == 3) {
893 in >> _Cuc >> _e >> _Cus >> _sqrt_A;
894 }
895 else if (ii == 4) {
896 in >> _TOE >> _Cic >> _OMEGA0 >> _Cis;
897 }
898 else if (ii == 5) {
899 in >> _i0 >> _Crc >> _omega >> _OMEGADOT;
900 }
901 else if (ii == 6) {
902 double GPSweek, dummy;
903 in >> _IDOT >> dummy >> GPSweek;
904 }
905 else if (ii == 7) {
906 in >> _SISA >> _E5aHS >> _BGD_1_5A >> _BGD_1_5B;
907 }
908 else if (ii == 8) {
909 double TOW;
910 in >> TOW;
911 }
912 }
913 return success;
914}
915
916#define GALILEOTOINT(type, value) static_cast<type>(NearestInt(value, 0))
917
918#define GALILEOADDBITS(a, b) {bitbuffer = (bitbuffer<<(a)) \
919 |(GALILEOTOINT(long long,b)&((1LL<<a)-1)); \
920 numbits += (a); \
921 while(numbits >= 8) { \
922 buffer[size++] = bitbuffer>>(numbits-8);numbits -= 8;}}
923#define GALILEOADDBITSFLOAT(a,b,c) {long long i = GALILEOTOINT(long long,(b)/(c)); \
924 GALILEOADDBITS(a,i)};
925// build up RTCM3 for Galileo
926////////////////////////////////////////////////////////////////////////////
927int t_ephGal::RTCM3(unsigned char *buffer) {
928 int size = 0;
929 int numbits = 0;
930 long long bitbuffer = 0;
931 unsigned char *startbuffer = buffer;
932 buffer= buffer+3;
933
934 GALILEOADDBITS(12, /*inav ? 1046 :*/ 1045)
935 GALILEOADDBITS(6, _prn.right((_prn.length()-1)).toInt())
936 GALILEOADDBITS(12, _GPSweek)
937 GALILEOADDBITS(10, _IODnav)
938 GALILEOADDBITS(8, _SISA)
939 GALILEOADDBITSFLOAT(14, _IDOT, PI/static_cast<double>(1<<30)
940 /static_cast<double>(1<<13))
941 GALILEOADDBITS(14, _TOC/60)
942 GALILEOADDBITSFLOAT(6, _clock_driftrate, 1.0/static_cast<double>(1<<30)
943 /static_cast<double>(1<<29))
944 GALILEOADDBITSFLOAT(21, _clock_drift, 1.0/static_cast<double>(1<<30)
945 /static_cast<double>(1<<16))
946 GALILEOADDBITSFLOAT(31, _clock_bias, 1.0/static_cast<double>(1<<30)
947 /static_cast<double>(1<<4))
948 GALILEOADDBITSFLOAT(16, _Crs, 1.0/static_cast<double>(1<<5))
949 GALILEOADDBITSFLOAT(16, _Delta_n, PI/static_cast<double>(1<<30)
950 /static_cast<double>(1<<13))
951 GALILEOADDBITSFLOAT(32, _M0, PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
952 GALILEOADDBITSFLOAT(16, _Cuc, 1.0/static_cast<double>(1<<29))
953 GALILEOADDBITSFLOAT(32, _e, 1.0/static_cast<double>(1<<30)/static_cast<double>(1<<3))
954 GALILEOADDBITSFLOAT(16, _Cus, 1.0/static_cast<double>(1<<29))
955 GALILEOADDBITSFLOAT(32, _sqrt_A, 1.0/static_cast<double>(1<<19))
956 GALILEOADDBITS(14, _TOE/60)
957 GALILEOADDBITSFLOAT(16, _Cic, 1.0/static_cast<double>(1<<29))
958 GALILEOADDBITSFLOAT(32, _OMEGA0, PI/static_cast<double>(1<<30)
959 /static_cast<double>(1<<1))
960 GALILEOADDBITSFLOAT(16, _Cis, 1.0/static_cast<double>(1<<29))
961 GALILEOADDBITSFLOAT(32, _i0, PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
962 GALILEOADDBITSFLOAT(16, _Crc, 1.0/static_cast<double>(1<<5))
963 GALILEOADDBITSFLOAT(32, _omega, PI/static_cast<double>(1<<30)
964 /static_cast<double>(1<<1))
965 GALILEOADDBITSFLOAT(24, _OMEGADOT, PI/static_cast<double>(1<<30)
966 /static_cast<double>(1<<13))
967 GALILEOADDBITSFLOAT(10, _BGD_1_5A, 1.0/static_cast<double>(1<<30)
968 /static_cast<double>(1<<2))
969 /*if(inav)
970 {
971 GALILEOADDBITSFLOAT(10, _BGD_1_5B, 1.0/static_cast<double>(1<<30)
972 /static_cast<double>(1<<2))
973 GALILEOADDBITS(2, _E5bHS)
974 GALILEOADDBITS(1, flags & MNFGALEPHF_E5BDINVALID)
975 }
976 else*/
977 {
978 GALILEOADDBITS(2, _E5aHS)
979 GALILEOADDBITS(1, /*flags & MNFGALEPHF_E5ADINVALID*/0)
980 }
981 _TOE = 0.9999E9;
982 GALILEOADDBITS(20, _TOE)
983
984 GALILEOADDBITS(/*inav ? 1 :*/ 3, 0) /* fill up */
985
986 startbuffer[0]=0xD3;
987 startbuffer[1]=(size >> 8);
988 startbuffer[2]=size;
989 unsigned long i = CRC24(size+3, startbuffer);
990 buffer[size++] = i >> 16;
991 buffer[size++] = i >> 8;
992 buffer[size++] = i;
993 size += 3;
994 return size;
995}
Note: See TracBrowser for help on using the repository browser.