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

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