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

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