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

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