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

Last change on this file since 2546 was 2493, checked in by stoecker, 15 years ago

removed double storage of RTCM3 stuff

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