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

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