source: ntrip/trunk/BNC/RTCM3/ephemeris.cpp@ 4021

Last change on this file since 4021 was 4021, checked in by mervart, 12 years ago
File size: 32.0 KB
RevLine 
[1025]1#include <math.h>
2#include <sstream>
[2234]3#include <iostream>
[1025]4#include <iomanip>
[1239]5#include <cstring>
[1025]6
[2234]7#include <newmatio.h>
8
[1025]9#include "ephemeris.h"
[2221]10#include "bncutils.h"
[1025]11#include "timeutils.h"
[2285]12#include "bnctime.h"
[3280]13#include "bncapp.h"
[1025]14
15using namespace std;
16
[3255]17// Returns CRC24
18////////////////////////////////////////////////////////////////////////////
[4018]19static unsigned long CRC24(long size, const unsigned char *buf) {
[3255]20 unsigned long crc = 0;
[4018]21 int ii;
22 while (size--) {
[3255]23 crc ^= (*buf++) << (16);
[4018]24 for(ii = 0; ii < 8; ii++) {
[3255]25 crc <<= 1;
[4018]26 if (crc & 0x1000000) {
[3255]27 crc ^= 0x01864cfb;
[4018]28 }
[3255]29 }
30 }
31 return crc;
[1025]32}
33
[2222]34// Set GPS Satellite Position
35////////////////////////////////////////////////////////////////////////////
[1025]36void t_ephGPS::set(const gpsephemeris* ee) {
37
[3255]38 _prn = QString("G%1").arg(ee->satellite, 2, 10, QChar('0'));
[1025]39
[4018]40 _TOC.set(ee->GPSweek, ee->TOC);
[3734]41 _clock_bias = ee->clock_bias;
42 _clock_drift = ee->clock_drift;
[1025]43 _clock_driftrate = ee->clock_driftrate;
44
[4018]45 _IODE = ee->IODE;
[1025]46 _Crs = ee->Crs;
47 _Delta_n = ee->Delta_n;
48 _M0 = ee->M0;
[4018]49
[1025]50 _Cuc = ee->Cuc;
51 _e = ee->e;
52 _Cus = ee->Cus;
53 _sqrt_A = ee->sqrt_A;
[4018]54
55 _TOEsec = ee->TOE;
[1025]56 _Cic = ee->Cic;
57 _OMEGA0 = ee->OMEGA0;
58 _Cis = ee->Cis;
[4018]59
[1025]60 _i0 = ee->i0;
61 _Crc = ee->Crc;
62 _omega = ee->omega;
63 _OMEGADOT = ee->OMEGADOT;
[4018]64
[1025]65 _IDOT = ee->IDOT;
[4018]66 _L2Codes = 0.0;
67 _TOEweek = ee->TOW;
68 _L2PFlag = 0.0;
[1025]69
[4018]70 _ura = 0.0;
71 _health = ee->SVhealth;
[1025]72 _TGD = ee->TGD;
[4018]73 _IODC = ee->IODC;
[3659]74
[4018]75 _TOT = 0.9999e9;
76 _fitInterval = 0.0;
77
[3659]78 _ok = true;
[1025]79}
80
[2222]81// Compute GPS Satellite Position (virtual)
[1025]82////////////////////////////////////////////////////////////////////////////
83void t_ephGPS::position(int GPSweek, double GPSweeks,
[4018]84 double* xc,
85 double* vv) const {
[1025]86
[4018]87
[1098]88 static const double omegaEarth = 7292115.1467e-11;
89 static const double gmWGS = 398.6005e12;
[1025]90
91 memset(xc, 0, 4*sizeof(double));
92 memset(vv, 0, 3*sizeof(double));
93
94 double a0 = _sqrt_A * _sqrt_A;
95 if (a0 == 0) {
96 return;
97 }
98
99 double n0 = sqrt(gmWGS/(a0*a0*a0));
[4018]100
101 bncTime tt(GPSweek, GPSweeks);
102 double tk = tt - bncTime(_TOEweek, _TOEsec);
103
[1025]104 double n = n0 + _Delta_n;
105 double M = _M0 + n*tk;
106 double E = M;
107 double E_last;
108 do {
109 E_last = E;
110 E = M + _e*sin(E);
111 } while ( fabs(E-E_last)*a0 > 0.001 );
112 double v = 2.0*atan( sqrt( (1.0 + _e)/(1.0 - _e) )*tan( E/2 ) );
113 double u0 = v + _omega;
114 double sin2u0 = sin(2*u0);
115 double cos2u0 = cos(2*u0);
116 double r = a0*(1 - _e*cos(E)) + _Crc*cos2u0 + _Crs*sin2u0;
117 double i = _i0 + _IDOT*tk + _Cic*cos2u0 + _Cis*sin2u0;
118 double u = u0 + _Cuc*cos2u0 + _Cus*sin2u0;
119 double xp = r*cos(u);
120 double yp = r*sin(u);
121 double OM = _OMEGA0 + (_OMEGADOT - omegaEarth)*tk -
[4018]122 omegaEarth*_TOEsec;
[1025]123
124 double sinom = sin(OM);
125 double cosom = cos(OM);
126 double sini = sin(i);
127 double cosi = cos(i);
128 xc[0] = xp*cosom - yp*cosi*sinom;
129 xc[1] = xp*sinom + yp*cosi*cosom;
130 xc[2] = yp*sini;
131
[4018]132 double tc = tt - _TOC;
[2429]133 xc[3] = _clock_bias + _clock_drift*tc + _clock_driftrate*tc*tc;
[1025]134
135 // Velocity
136 // --------
137 double tanv2 = tan(v/2);
138 double dEdM = 1 / (1 - _e*cos(E));
139 double dotv = sqrt((1.0 + _e)/(1.0 - _e)) / cos(E/2)/cos(E/2) / (1 + tanv2*tanv2)
140 * dEdM * n;
141 double dotu = dotv + (-_Cuc*sin2u0 + _Cus*cos2u0)*2*dotv;
142 double dotom = _OMEGADOT - omegaEarth;
143 double doti = _IDOT + (-_Cic*sin2u0 + _Cis*cos2u0)*2*dotv;
144 double dotr = a0 * _e*sin(E) * dEdM * n
145 + (-_Crc*sin2u0 + _Crs*cos2u0)*2*dotv;
146 double dotx = dotr*cos(u) - r*sin(u)*dotu;
147 double doty = dotr*sin(u) + r*cos(u)*dotu;
148
149 vv[0] = cosom *dotx - cosi*sinom *doty // dX / dr
150 - xp*sinom*dotom - yp*cosi*cosom*dotom // dX / dOMEGA
151 + yp*sini*sinom*doti; // dX / di
152
153 vv[1] = sinom *dotx + cosi*cosom *doty
154 + xp*cosom*dotom - yp*cosi*sinom*dotom
155 - yp*sini*cosom*doti;
156
157 vv[2] = sini *doty + yp*cosi *doti;
[2429]158
159 // Relativistic Correction
160 // -----------------------
161 xc[3] -= 2.0 * (xc[0]*vv[0] + xc[1]*vv[1] + xc[2]*vv[2]) / t_CST::c / t_CST::c;
[1025]162}
163
[3255]164// build up RTCM3 for GPS
165////////////////////////////////////////////////////////////////////////////
[4018]166#define GPSTOINT(type, value) static_cast<type>(round(value))
[1025]167
[3255]168#define GPSADDBITS(a, b) {bitbuffer = (bitbuffer<<(a)) \
169 |(GPSTOINT(long long,b)&((1ULL<<a)-1)); \
170 numbits += (a); \
171 while(numbits >= 8) { \
172 buffer[size++] = bitbuffer>>(numbits-8);numbits -= 8;}}
[4018]173
[3255]174#define GPSADDBITSFLOAT(a,b,c) {long long i = GPSTOINT(long long,(b)/(c)); \
175 GPSADDBITS(a,i)};
176
[4018]177int t_ephGPS::RTCM3(unsigned char *buffer) {
[3255]178
179 unsigned char *startbuffer = buffer;
180 buffer= buffer+3;
181 int size = 0;
182 int numbits = 0;
183 unsigned long long bitbuffer = 0;
184 if (_ura <= 2.40){
185 _ura = 0;
186 }
187 else if (_ura <= 3.40){
188 _ura = 1;
189 }
190 else if (_ura <= 6.85){
191 _ura = 2;
192 }
193 else if (_ura <= 9.65){
194 _ura = 3;
195 }
196 else if (_ura <= 13.65){
197 _ura = 4;
198 }
199 else if (_ura <= 24.00){
200 _ura = 5;
201 }
202 else if (_ura <= 48.00){
203 _ura = 6;
204 }
205 else if (_ura <= 96.00){
206 _ura = 7;
207 }
208 else if (_ura <= 192.00){
209 _ura = 8;
210 }
211 else if (_ura <= 384.00){
212 _ura = 9;
213 }
214 else if (_ura <= 768.00){
215 _ura = 10;
216 }
217 else if (_ura <= 1536.00){
218 _ura = 11;
219 }
220 else if (_ura <= 1536.00){
221 _ura = 12;
222 }
223 else if (_ura <= 2072.00){
224 _ura = 13;
225 }
226 else if (_ura <= 6144.00){
227 _ura = 14;
228 }
229 else{
230 _ura = 15;
231 }
232
233 GPSADDBITS(12, 1019)
234 GPSADDBITS(6,_prn.right((_prn.length()-1)).toInt())
[4018]235 GPSADDBITS(10, _TOC.gpsw())
[3255]236 GPSADDBITS(4, _ura)
237 GPSADDBITS(2,_L2Codes)
[4018]238 GPSADDBITSFLOAT(14, _IDOT, M_PI/static_cast<double>(1<<30)
[3255]239 /static_cast<double>(1<<13))
240 GPSADDBITS(8, _IODE)
[4018]241 GPSADDBITS(16, static_cast<int>(_TOC.gpssec())>>4)
[3255]242 GPSADDBITSFLOAT(8, _clock_driftrate, 1.0/static_cast<double>(1<<30)
243 /static_cast<double>(1<<25))
244 GPSADDBITSFLOAT(16, _clock_drift, 1.0/static_cast<double>(1<<30)
245 /static_cast<double>(1<<13))
246 GPSADDBITSFLOAT(22, _clock_bias, 1.0/static_cast<double>(1<<30)
247 /static_cast<double>(1<<1))
248 GPSADDBITS(10, _IODC)
249 GPSADDBITSFLOAT(16, _Crs, 1.0/static_cast<double>(1<<5))
[4018]250 GPSADDBITSFLOAT(16, _Delta_n, M_PI/static_cast<double>(1<<30)
[3255]251 /static_cast<double>(1<<13))
[4018]252 GPSADDBITSFLOAT(32, _M0, M_PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
[3255]253 GPSADDBITSFLOAT(16, _Cuc, 1.0/static_cast<double>(1<<29))
254 GPSADDBITSFLOAT(32, _e, 1.0/static_cast<double>(1<<30)/static_cast<double>(1<<3))
255 GPSADDBITSFLOAT(16, _Cus, 1.0/static_cast<double>(1<<29))
256 GPSADDBITSFLOAT(32, _sqrt_A, 1.0/static_cast<double>(1<<19))
[4018]257 GPSADDBITS(16, static_cast<int>(_TOEsec)>>4)
[3255]258 GPSADDBITSFLOAT(16, _Cic, 1.0/static_cast<double>(1<<29))
[4018]259 GPSADDBITSFLOAT(32, _OMEGA0, M_PI/static_cast<double>(1<<30)
[3255]260 /static_cast<double>(1<<1))
261 GPSADDBITSFLOAT(16, _Cis, 1.0/static_cast<double>(1<<29))
[4018]262 GPSADDBITSFLOAT(32, _i0, M_PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
[3255]263 GPSADDBITSFLOAT(16, _Crc, 1.0/static_cast<double>(1<<5))
[4018]264 GPSADDBITSFLOAT(32, _omega, M_PI/static_cast<double>(1<<30)
[3255]265 /static_cast<double>(1<<1))
[4018]266 GPSADDBITSFLOAT(24, _OMEGADOT, M_PI/static_cast<double>(1<<30)
[3255]267 /static_cast<double>(1<<13))
268 GPSADDBITSFLOAT(8, _TGD, 1.0/static_cast<double>(1<<30)/static_cast<double>(1<<1))
269 GPSADDBITS(6, _health)
270 GPSADDBITS(1, _L2PFlag)
271 GPSADDBITS(1, 0) /* GPS fit interval */
272
273 startbuffer[0]=0xD3;
274 startbuffer[1]=(size >> 8);
275 startbuffer[2]=size;
276 unsigned long i = CRC24(size+3, startbuffer);
277 buffer[size++] = i >> 16;
278 buffer[size++] = i >> 8;
279 buffer[size++] = i;
280 size += 3;
281 return size;
282}
283
[2221]284// Derivative of the state vector using a simple force model (static)
285////////////////////////////////////////////////////////////////////////////
[2556]286ColumnVector t_ephGlo::glo_deriv(double /* tt */, const ColumnVector& xv,
287 double* acc) {
[2221]288
289 // State vector components
290 // -----------------------
291 ColumnVector rr = xv.rows(1,3);
292 ColumnVector vv = xv.rows(4,6);
293
294 // Acceleration
295 // ------------
296 static const double GM = 398.60044e12;
297 static const double AE = 6378136.0;
298 static const double OMEGA = 7292115.e-11;
[2561]299 static const double C20 = -1082.6257e-6;
[2221]300
301 double rho = rr.norm_Frobenius();
302 double t1 = -GM/(rho*rho*rho);
303 double t2 = 3.0/2.0 * C20 * (GM*AE*AE) / (rho*rho*rho*rho*rho);
304 double t3 = OMEGA * OMEGA;
305 double t4 = 2.0 * OMEGA;
306 double z2 = rr(3) * rr(3);
307
308 // Vector of derivatives
309 // ---------------------
310 ColumnVector va(6);
311 va(1) = vv(1);
312 va(2) = vv(2);
313 va(3) = vv(3);
[2556]314 va(4) = (t1 + t2*(1.0-5.0*z2/(rho*rho)) + t3) * rr(1) + t4*vv(2) + acc[0];
315 va(5) = (t1 + t2*(1.0-5.0*z2/(rho*rho)) + t3) * rr(2) - t4*vv(1) + acc[1];
316 va(6) = (t1 + t2*(3.0-5.0*z2/(rho*rho)) ) * rr(3) + acc[2];
[2221]317
318 return va;
319}
320
321// Compute Glonass Satellite Position (virtual)
322////////////////////////////////////////////////////////////////////////////
323void t_ephGlo::position(int GPSweek, double GPSweeks,
324 double* xc, double* vv) const {
325
326 static const double nominalStep = 10.0;
327
328 memset(xc, 0, 4*sizeof(double));
329 memset(vv, 0, 3*sizeof(double));
330
[4018]331 double dtPos = bncTime(GPSweek, GPSweeks) - _tt;
[2221]332
333 int nSteps = int(fabs(dtPos) / nominalStep) + 1;
334 double step = dtPos / nSteps;
335
[2556]336 double acc[3];
[2557]337 acc[0] = _x_acceleration * 1.e3;
[2560]338 acc[1] = _y_acceleration * 1.e3;
339 acc[2] = _z_acceleration * 1.e3;
[2221]340 for (int ii = 1; ii <= nSteps; ii++) {
[4018]341 _xv = rungeKutta4(_tt.gpssec(), _xv, step, acc, glo_deriv);
342 _tt = _tt + step;
[2221]343 }
344
345 // Position and Velocity
346 // ---------------------
347 xc[0] = _xv(1);
348 xc[1] = _xv(2);
349 xc[2] = _xv(3);
350
351 vv[0] = _xv(4);
352 vv[1] = _xv(5);
353 vv[2] = _xv(6);
354
355 // Clock Correction
356 // ----------------
[4018]357 double dtClk = bncTime(GPSweek, GPSweeks) - _TOC;
[2221]358 xc[3] = -_tau + _gamma * dtClk;
359}
360
361// IOD of Glonass Ephemeris (virtual)
362////////////////////////////////////////////////////////////////////////////
363int t_ephGlo::IOD() const {
[4018]364 bncTime tMoscow = _TOC - _gps_utc + 3 * 3600.0;
[3538]365 return int(tMoscow.daysec() / 900);
[2221]366}
367
368// Set Glonass Ephemeris
369////////////////////////////////////////////////////////////////////////////
370void t_ephGlo::set(const glonassephemeris* ee) {
371
[3255]372 _prn = QString("R%1").arg(ee->almanac_number, 2, 10, QChar('0'));
[2223]373
[2234]374 int ww = ee->GPSWeek;
375 int tow = ee->GPSTOW;
[2257]376 updatetime(&ww, &tow, ee->tb*1000, 0); // Moscow -> GPS
[2223]377
[3264]378 // Check the day once more
379 // -----------------------
380 {
[3268]381 const double secPerDay = 24 * 3600.0;
382 const double secPerWeek = 7 * secPerDay;
[3266]383 int ww_old = ww;
384 int tow_old = tow;
[3264]385 int currentWeek;
386 double currentSec;
387 currentGPSWeeks(currentWeek, currentSec);
388 bncTime currentTime(currentWeek, currentSec);
389 bncTime hTime(ww, (double) tow);
390
391 bool changed = false;
[3268]392 if (hTime - currentTime > secPerDay/2.0) {
[3264]393 changed = true;
[3268]394 tow -= secPerDay;
395 if (tow < 0) {
396 tow += secPerWeek;
397 ww -= 1;
398 }
[3264]399 }
[3268]400 else if (hTime - currentTime < -secPerDay/2.0) {
[3264]401 changed = true;
[3268]402 tow += secPerDay;
403 if (tow > secPerWeek) {
404 tow -= secPerWeek;
405 ww += 1;
406 }
[3264]407 }
408
[3280]409 if (changed && ((bncApp*) qApp)->mode() == bncApp::batchPostProcessing) {
[3265]410 bncTime newHTime(ww, (double) tow);
[3269]411 cout << "GLONASS " << ee->almanac_number << " Time Changed at "
[3266]412 << currentTime.datestr() << " " << currentTime.timestr()
413 << endl
[3268]414 << "old: " << hTime.datestr() << " " << hTime.timestr()
[3266]415 << endl
416 << "new: " << newHTime.datestr() << " " << newHTime.timestr()
417 << endl
418 << "eph: " << ee->GPSWeek << " " << ee->GPSTOW << " " << ee->tb
419 << endl
420 << "ww, tow (old): " << ww_old << " " << tow_old
421 << endl
422 << "ww, tow (new): " << ww << " " << tow
[3267]423 << endl << endl;
[3264]424 }
425 }
426
[3263]427 bncTime hlpTime(ww, (double) tow);
[3255]428 unsigned year, month, day;
429 hlpTime.civil_date(year, month, day);
430 _gps_utc = gnumleap(year, month, day);
431
[4018]432 _TOC.set(ww, tow);
[2223]433 _E = ee->E;
434 _tau = ee->tau;
435 _gamma = ee->gamma;
436 _x_pos = ee->x_pos;
437 _x_velocity = ee->x_velocity;
438 _x_acceleration = ee->x_acceleration;
439 _y_pos = ee->y_pos;
440 _y_velocity = ee->y_velocity;
441 _y_acceleration = ee->y_acceleration;
442 _z_pos = ee->z_pos;
443 _z_velocity = ee->z_velocity;
444 _z_acceleration = ee->z_acceleration;
445 _health = 0;
446 _frequency_number = ee->frequency_number;
[2257]447 _tki = ee->tk-3*60*60; if (_tki < 0) _tki += 86400;
[2223]448
449 // Initialize status vector
450 // ------------------------
[4018]451 _tt = _TOC;
[2223]452
453 _xv(1) = _x_pos * 1.e3;
454 _xv(2) = _y_pos * 1.e3;
455 _xv(3) = _z_pos * 1.e3;
456 _xv(4) = _x_velocity * 1.e3;
457 _xv(5) = _y_velocity * 1.e3;
458 _xv(6) = _z_velocity * 1.e3;
[3659]459
460 _ok = true;
[2221]461}
[2771]462
[3255]463// build up RTCM3 for GLONASS
464////////////////////////////////////////////////////////////////////////////
[4018]465#define GLONASSTOINT(type, value) static_cast<type>(round(value))
[3255]466
467#define GLONASSADDBITS(a, b) {bitbuffer = (bitbuffer<<(a)) \
468 |(GLONASSTOINT(long long,b)&((1ULL<<(a))-1)); \
469 numbits += (a); \
470 while(numbits >= 8) { \
471 buffer[size++] = bitbuffer>>(numbits-8);numbits -= 8;}}
472#define GLONASSADDBITSFLOATM(a,b,c) {int s; long long i; \
473 if(b < 0.0) \
474 { \
475 s = 1; \
476 i = GLONASSTOINT(long long,(-b)/(c)); \
477 if(!i) s = 0; \
478 } \
479 else \
480 { \
481 s = 0; \
482 i = GLONASSTOINT(long long,(b)/(c)); \
483 } \
484 GLONASSADDBITS(1,s) \
485 GLONASSADDBITS(a-1,i)}
486
487int t_ephGlo::RTCM3(unsigned char *buffer)
488{
489
490 int size = 0;
491 int numbits = 0;
492 long long bitbuffer = 0;
493 unsigned char *startbuffer = buffer;
494 buffer= buffer+3;
495
496 GLONASSADDBITS(12, 1020)
497 GLONASSADDBITS(6, _prn.right((_prn.length()-1)).toInt())
498 GLONASSADDBITS(5, 7+_frequency_number)
499 GLONASSADDBITS(1, 0)
500 GLONASSADDBITS(1, 0)
501 GLONASSADDBITS(2, 0)
502 _tki=_tki+3*60*60;
503 GLONASSADDBITS(5, static_cast<int>(_tki)/(60*60))
504 GLONASSADDBITS(6, (static_cast<int>(_tki)/60)%60)
505 GLONASSADDBITS(1, (static_cast<int>(_tki)/30)%30)
506 GLONASSADDBITS(1, _health)
507 GLONASSADDBITS(1, 0)
[4018]508 unsigned long long timeofday = (static_cast<int>(_tt.gpssec()+3*60*60-_gps_utc)%86400);
[3255]509 GLONASSADDBITS(7, timeofday/60/15)
510 GLONASSADDBITSFLOATM(24, _x_velocity*1000, 1000.0/static_cast<double>(1<<20))
511 GLONASSADDBITSFLOATM(27, _x_pos*1000, 1000.0/static_cast<double>(1<<11))
512 GLONASSADDBITSFLOATM(5, _x_acceleration*1000, 1000.0/static_cast<double>(1<<30))
513 GLONASSADDBITSFLOATM(24, _y_velocity*1000, 1000.0/static_cast<double>(1<<20))
514 GLONASSADDBITSFLOATM(27, _y_pos*1000, 1000.0/static_cast<double>(1<<11))
515 GLONASSADDBITSFLOATM(5, _y_acceleration*1000, 1000.0/static_cast<double>(1<<30))
516 GLONASSADDBITSFLOATM(24, _z_velocity*1000, 1000.0/static_cast<double>(1<<20))
517 GLONASSADDBITSFLOATM(27,_z_pos*1000, 1000.0/static_cast<double>(1<<11))
518 GLONASSADDBITSFLOATM(5, _z_acceleration*1000, 1000.0/static_cast<double>(1<<30))
519 GLONASSADDBITS(1, 0)
520 GLONASSADDBITSFLOATM(11, _gamma, 1.0/static_cast<double>(1<<30)
521 /static_cast<double>(1<<10))
522 GLONASSADDBITS(2, 0) /* GLONASS-M P */
523 GLONASSADDBITS(1, 0) /* GLONASS-M ln(3) */
524 GLONASSADDBITSFLOATM(22, _tau, 1.0/static_cast<double>(1<<30))
525 GLONASSADDBITS(5, 0) /* GLONASS-M delta tau */
526 GLONASSADDBITS(5, _E)
527 GLONASSADDBITS(1, 0) /* GLONASS-M P4 */
528 GLONASSADDBITS(4, 0) /* GLONASS-M FT */
529 GLONASSADDBITS(11, 0) /* GLONASS-M NT */
530 GLONASSADDBITS(2, 0) /* GLONASS-M active? */
531 GLONASSADDBITS(1, 0) /* GLONASS additional data */
532 GLONASSADDBITS(11, 0) /* GLONASS NA */
533 GLONASSADDBITS(32, 0) /* GLONASS tau C */
534 GLONASSADDBITS(5, 0) /* GLONASS-M N4 */
535 GLONASSADDBITS(22, 0) /* GLONASS-M tau GPS */
536 GLONASSADDBITS(1, 0) /* GLONASS-M ln(5) */
537 GLONASSADDBITS(7, 0) /* Reserved */
538
539 startbuffer[0]=0xD3;
540 startbuffer[1]=(size >> 8);
541 startbuffer[2]=size;
542 unsigned long i = CRC24(size+3, startbuffer);
543 buffer[size++] = i >> 16;
544 buffer[size++] = i >> 8;
545 buffer[size++] = i;
546 size += 3;
547 return size;
548}
549
[2771]550// Set Galileo Satellite Position
551////////////////////////////////////////////////////////////////////////////
552void t_ephGal::set(const galileoephemeris* ee) {
553
[3255]554 _prn = QString("E%1").arg(ee->satellite, 2, 10, QChar('0'));
[2771]555
[4018]556 _TOC.set(ee->Week, ee->TOC);
[3734]557 _clock_bias = ee->clock_bias;
558 _clock_drift = ee->clock_drift;
[2771]559 _clock_driftrate = ee->clock_driftrate;
560
[4018]561 _IODnav = ee->IODnav;
[2771]562 _Crs = ee->Crs;
563 _Delta_n = ee->Delta_n;
564 _M0 = ee->M0;
[4018]565
[2771]566 _Cuc = ee->Cuc;
567 _e = ee->e;
568 _Cus = ee->Cus;
569 _sqrt_A = ee->sqrt_A;
[4018]570
571 _TOEsec = ee->TOE;
[2771]572 _Cic = ee->Cic;
573 _OMEGA0 = ee->OMEGA0;
574 _Cis = ee->Cis;
[4018]575
[2771]576 _i0 = ee->i0;
577 _Crc = ee->Crc;
578 _omega = ee->omega;
579 _OMEGADOT = ee->OMEGADOT;
[4018]580
[2771]581 _IDOT = ee->IDOT;
[4018]582 _TOEweek = ee->Week;
583
[3734]584 _SISA = ee->SISA;
[4018]585 _E5aHS = ee->E5aHS;
[3734]586 _BGD_1_5A = ee->BGD_1_5A;
587 _BGD_1_5B = ee->BGD_1_5B;
[3659]588
[4018]589 _TOT = 0.9999e9;
590
[3659]591 _ok = true;
[2771]592}
593
594// Compute Galileo Satellite Position (virtual)
595////////////////////////////////////////////////////////////////////////////
596void t_ephGal::position(int GPSweek, double GPSweeks,
[4018]597 double* xc,
598 double* vv) const {
[2771]599
600 static const double omegaEarth = 7292115.1467e-11;
601 static const double gmWGS = 398.6005e12;
602
603 memset(xc, 0, 4*sizeof(double));
604 memset(vv, 0, 3*sizeof(double));
605
606 double a0 = _sqrt_A * _sqrt_A;
607 if (a0 == 0) {
608 return;
609 }
610
611 double n0 = sqrt(gmWGS/(a0*a0*a0));
[4018]612
613 bncTime tt(GPSweek, GPSweeks);
614 double tk = tt - bncTime(_TOC.gpsw(), _TOEsec);
615
[2771]616 double n = n0 + _Delta_n;
617 double M = _M0 + n*tk;
618 double E = M;
619 double E_last;
620 do {
621 E_last = E;
622 E = M + _e*sin(E);
623 } while ( fabs(E-E_last)*a0 > 0.001 );
624 double v = 2.0*atan( sqrt( (1.0 + _e)/(1.0 - _e) )*tan( E/2 ) );
625 double u0 = v + _omega;
626 double sin2u0 = sin(2*u0);
627 double cos2u0 = cos(2*u0);
628 double r = a0*(1 - _e*cos(E)) + _Crc*cos2u0 + _Crs*sin2u0;
629 double i = _i0 + _IDOT*tk + _Cic*cos2u0 + _Cis*sin2u0;
630 double u = u0 + _Cuc*cos2u0 + _Cus*sin2u0;
631 double xp = r*cos(u);
632 double yp = r*sin(u);
633 double OM = _OMEGA0 + (_OMEGADOT - omegaEarth)*tk -
[4018]634 omegaEarth*_TOEsec;
[2771]635
636 double sinom = sin(OM);
637 double cosom = cos(OM);
638 double sini = sin(i);
639 double cosi = cos(i);
640 xc[0] = xp*cosom - yp*cosi*sinom;
641 xc[1] = xp*sinom + yp*cosi*cosom;
642 xc[2] = yp*sini;
643
[4018]644 double tc = tt - _TOC;
[2771]645 xc[3] = _clock_bias + _clock_drift*tc + _clock_driftrate*tc*tc;
646
647 // Velocity
648 // --------
649 double tanv2 = tan(v/2);
650 double dEdM = 1 / (1 - _e*cos(E));
651 double dotv = sqrt((1.0 + _e)/(1.0 - _e)) / cos(E/2)/cos(E/2) / (1 + tanv2*tanv2)
652 * dEdM * n;
653 double dotu = dotv + (-_Cuc*sin2u0 + _Cus*cos2u0)*2*dotv;
654 double dotom = _OMEGADOT - omegaEarth;
655 double doti = _IDOT + (-_Cic*sin2u0 + _Cis*cos2u0)*2*dotv;
656 double dotr = a0 * _e*sin(E) * dEdM * n
657 + (-_Crc*sin2u0 + _Crs*cos2u0)*2*dotv;
658 double dotx = dotr*cos(u) - r*sin(u)*dotu;
659 double doty = dotr*sin(u) + r*cos(u)*dotu;
660
661 vv[0] = cosom *dotx - cosi*sinom *doty // dX / dr
662 - xp*sinom*dotom - yp*cosi*cosom*dotom // dX / dOMEGA
663 + yp*sini*sinom*doti; // dX / di
664
665 vv[1] = sinom *dotx + cosi*cosom *doty
666 + xp*cosom*dotom - yp*cosi*sinom*dotom
667 - yp*sini*cosom*doti;
668
669 vv[2] = sini *doty + yp*cosi *doti;
670
671 // Relativistic Correction
672 // -----------------------
673 // xc(4) -= 4.442807633e-10 * _e * sqrt(a0) *sin(E);
674 xc[3] -= 2.0 * (xc[0]*vv[0] + xc[1]*vv[1] + xc[2]*vv[2]) / t_CST::c / t_CST::c;
675}
676
[3255]677// build up RTCM3 for Galileo
678////////////////////////////////////////////////////////////////////////////
[4018]679#define GALILEOTOINT(type, value) static_cast<type>(round(value))
[3279]680
681#define GALILEOADDBITS(a, b) {bitbuffer = (bitbuffer<<(a)) \
682 |(GALILEOTOINT(long long,b)&((1LL<<a)-1)); \
683 numbits += (a); \
684 while(numbits >= 8) { \
685 buffer[size++] = bitbuffer>>(numbits-8);numbits -= 8;}}
686#define GALILEOADDBITSFLOAT(a,b,c) {long long i = GALILEOTOINT(long long,(b)/(c)); \
687 GALILEOADDBITS(a,i)};
688
[3255]689int t_ephGal::RTCM3(unsigned char *buffer) {
[3279]690 int size = 0;
691 int numbits = 0;
692 long long bitbuffer = 0;
693 unsigned char *startbuffer = buffer;
694 buffer= buffer+3;
[3255]695
[3279]696 GALILEOADDBITS(12, /*inav ? 1046 :*/ 1045)
697 GALILEOADDBITS(6, _prn.right((_prn.length()-1)).toInt())
[4018]698 GALILEOADDBITS(12, _TOC.gpsw())
[3279]699 GALILEOADDBITS(10, _IODnav)
700 GALILEOADDBITS(8, _SISA)
[4018]701 GALILEOADDBITSFLOAT(14, _IDOT, M_PI/static_cast<double>(1<<30)
[3279]702 /static_cast<double>(1<<13))
[4018]703 GALILEOADDBITS(14, _TOC.gpssec()/60)
[3279]704 GALILEOADDBITSFLOAT(6, _clock_driftrate, 1.0/static_cast<double>(1<<30)
705 /static_cast<double>(1<<29))
706 GALILEOADDBITSFLOAT(21, _clock_drift, 1.0/static_cast<double>(1<<30)
707 /static_cast<double>(1<<16))
708 GALILEOADDBITSFLOAT(31, _clock_bias, 1.0/static_cast<double>(1<<30)
709 /static_cast<double>(1<<4))
710 GALILEOADDBITSFLOAT(16, _Crs, 1.0/static_cast<double>(1<<5))
[4018]711 GALILEOADDBITSFLOAT(16, _Delta_n, M_PI/static_cast<double>(1<<30)
[3279]712 /static_cast<double>(1<<13))
[4018]713 GALILEOADDBITSFLOAT(32, _M0, M_PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
[3279]714 GALILEOADDBITSFLOAT(16, _Cuc, 1.0/static_cast<double>(1<<29))
715 GALILEOADDBITSFLOAT(32, _e, 1.0/static_cast<double>(1<<30)/static_cast<double>(1<<3))
716 GALILEOADDBITSFLOAT(16, _Cus, 1.0/static_cast<double>(1<<29))
717 GALILEOADDBITSFLOAT(32, _sqrt_A, 1.0/static_cast<double>(1<<19))
[4018]718 GALILEOADDBITS(14, _TOEsec/60)
[3279]719 GALILEOADDBITSFLOAT(16, _Cic, 1.0/static_cast<double>(1<<29))
[4018]720 GALILEOADDBITSFLOAT(32, _OMEGA0, M_PI/static_cast<double>(1<<30)
[3279]721 /static_cast<double>(1<<1))
722 GALILEOADDBITSFLOAT(16, _Cis, 1.0/static_cast<double>(1<<29))
[4018]723 GALILEOADDBITSFLOAT(32, _i0, M_PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
[3279]724 GALILEOADDBITSFLOAT(16, _Crc, 1.0/static_cast<double>(1<<5))
[4018]725 GALILEOADDBITSFLOAT(32, _omega, M_PI/static_cast<double>(1<<30)
[3279]726 /static_cast<double>(1<<1))
[4018]727 GALILEOADDBITSFLOAT(24, _OMEGADOT, M_PI/static_cast<double>(1<<30)
[3279]728 /static_cast<double>(1<<13))
729 GALILEOADDBITSFLOAT(10, _BGD_1_5A, 1.0/static_cast<double>(1<<30)
730 /static_cast<double>(1<<2))
731 /*if(inav)
732 {
733 GALILEOADDBITSFLOAT(10, _BGD_1_5B, 1.0/static_cast<double>(1<<30)
734 /static_cast<double>(1<<2))
735 GALILEOADDBITS(2, _E5bHS)
736 GALILEOADDBITS(1, flags & MNFGALEPHF_E5BDINVALID)
737 }
738 else*/
739 {
740 GALILEOADDBITS(2, _E5aHS)
741 GALILEOADDBITS(1, /*flags & MNFGALEPHF_E5ADINVALID*/0)
742 }
[4018]743 _TOEsec = 0.9999E9;
744 GALILEOADDBITS(20, _TOEsec)
[3279]745
746 GALILEOADDBITS(/*inav ? 1 :*/ 3, 0) /* fill up */
747
748 startbuffer[0]=0xD3;
749 startbuffer[1]=(size >> 8);
750 startbuffer[2]=size;
751 unsigned long i = CRC24(size+3, startbuffer);
752 buffer[size++] = i >> 16;
753 buffer[size++] = i >> 8;
754 buffer[size++] = i;
755 size += 3;
756 return size;
[3255]757}
[3659]758
759// Constructor
760//////////////////////////////////////////////////////////////////////////////
761t_ephGPS::t_ephGPS(float rnxVersion, const QStringList& lines) {
[3664]762
[3699]763 const int nLines = 8;
764
[3665]765 _ok = false;
766
[3699]767 if (lines.size() != nLines) {
[3660]768 return;
769 }
[3664]770
[3668]771 // RINEX Format
772 // ------------
773 int fieldLen = 19;
774
[3666]775 int pos[4];
776 pos[0] = (rnxVersion <= 2.12) ? 3 : 4;
[3668]777 pos[1] = pos[0] + fieldLen;
778 pos[2] = pos[1] + fieldLen;
779 pos[3] = pos[2] + fieldLen;
[3664]780
781 // Read eight lines
782 // ----------------
[3699]783 for (int iLine = 0; iLine < nLines; iLine++) {
[3664]784 QString line = lines[iLine];
785
786 if ( iLine == 0 ) {
[3666]787 QTextStream in(line.left(pos[1]).toAscii());
788
789 int year, month, day, hour, min;
790 double sec;
791
792 in >> _prn >> year >> month >> day >> hour >> min >> sec;
793
794 if (_prn.at(0) != 'G') {
[3667]795 _prn = QString("G%1").arg(_prn.toInt(), 2, 10, QLatin1Char('0'));
[3664]796 }
[3667]797
[3666]798 if (year < 80) {
799 year += 2000;
[3664]800 }
[3666]801 else if (year < 100) {
802 year += 1900;
803 }
804
[4018]805 _TOC.set(year, month, day, hour, min, sec);
[3666]806
807 if ( readDbl(line, pos[1], fieldLen, _clock_bias ) ||
808 readDbl(line, pos[2], fieldLen, _clock_drift ) ||
809 readDbl(line, pos[3], fieldLen, _clock_driftrate) ) {
810 return;
811 }
[3660]812 }
[3664]813
814 else if ( iLine == 1 ) {
[3666]815 if ( readDbl(line, pos[0], fieldLen, _IODE ) ||
816 readDbl(line, pos[1], fieldLen, _Crs ) ||
817 readDbl(line, pos[2], fieldLen, _Delta_n) ||
818 readDbl(line, pos[3], fieldLen, _M0 ) ) {
[3664]819 return;
820 }
821 }
822
823 else if ( iLine == 2 ) {
[3666]824 if ( readDbl(line, pos[0], fieldLen, _Cuc ) ||
825 readDbl(line, pos[1], fieldLen, _e ) ||
826 readDbl(line, pos[2], fieldLen, _Cus ) ||
827 readDbl(line, pos[3], fieldLen, _sqrt_A) ) {
[3664]828 return;
829 }
830 }
831
832 else if ( iLine == 3 ) {
[4018]833 if ( readDbl(line, pos[0], fieldLen, _TOEsec) ||
[3666]834 readDbl(line, pos[1], fieldLen, _Cic ) ||
835 readDbl(line, pos[2], fieldLen, _OMEGA0) ||
836 readDbl(line, pos[3], fieldLen, _Cis ) ) {
[3664]837 return;
838 }
839 }
840
841 else if ( iLine == 4 ) {
[3666]842 if ( readDbl(line, pos[0], fieldLen, _i0 ) ||
843 readDbl(line, pos[1], fieldLen, _Crc ) ||
844 readDbl(line, pos[2], fieldLen, _omega ) ||
845 readDbl(line, pos[3], fieldLen, _OMEGADOT) ) {
[3664]846 return;
847 }
848 }
849
850 else if ( iLine == 5 ) {
[4018]851 if ( readDbl(line, pos[0], fieldLen, _IDOT ) ||
852 readDbl(line, pos[1], fieldLen, _L2Codes) ||
853 readDbl(line, pos[2], fieldLen, _TOEweek ) ||
854 readDbl(line, pos[3], fieldLen, _L2PFlag) ) {
[3664]855 return;
856 }
857 }
858
859 else if ( iLine == 6 ) {
[4018]860 if ( readDbl(line, pos[0], fieldLen, _ura ) ||
[3666]861 readDbl(line, pos[1], fieldLen, _health) ||
862 readDbl(line, pos[2], fieldLen, _TGD ) ||
863 readDbl(line, pos[3], fieldLen, _IODC ) ) {
[3664]864 return;
865 }
866 }
867
868 else if ( iLine == 7 ) {
[4018]869 if ( readDbl(line, pos[0], fieldLen, _TOT) ||
870 readDbl(line, pos[1], fieldLen, _fitInterval) ) {
[3664]871 return;
872 }
873 }
[3660]874 }
[3661]875
876 _ok = true;
[3659]877}
878
879// Constructor
880//////////////////////////////////////////////////////////////////////////////
881t_ephGlo::t_ephGlo(float rnxVersion, const QStringList& lines) {
882
[3699]883 const int nLines = 4;
884
[3659]885 _ok = false;
[3699]886
887 if (lines.size() != nLines) {
888 return;
889 }
890
891 // RINEX Format
892 // ------------
893 int fieldLen = 19;
894
895 int pos[4];
896 pos[0] = (rnxVersion <= 2.12) ? 3 : 4;
897 pos[1] = pos[0] + fieldLen;
898 pos[2] = pos[1] + fieldLen;
899 pos[3] = pos[2] + fieldLen;
900
901 // Read four lines
902 // ---------------
903 for (int iLine = 0; iLine < nLines; iLine++) {
904 QString line = lines[iLine];
905
906 if ( iLine == 0 ) {
907 QTextStream in(line.left(pos[1]).toAscii());
908
909 int year, month, day, hour, min;
910 double sec;
911
912 in >> _prn >> year >> month >> day >> hour >> min >> sec;
913
914 if (_prn.at(0) != 'R') {
915 _prn = QString("R%1").arg(_prn.toInt(), 2, 10, QLatin1Char('0'));
916 }
917
918 if (year < 80) {
919 year += 2000;
920 }
921 else if (year < 100) {
922 year += 1900;
923 }
924
[3760]925 _gps_utc = gnumleap(year, month, day);
926
[4018]927 _TOC.set(year, month, day, hour, min, sec);
928 _TOC = _TOC + _gps_utc;
[3699]929
[4018]930 if ( readDbl(line, pos[1], fieldLen, _tau ) ||
931 readDbl(line, pos[2], fieldLen, _gamma) ||
932 readDbl(line, pos[3], fieldLen, _tki ) ) {
[3699]933 return;
934 }
[3765]935
936 _tau = -_tau;
[3699]937 }
938
939 else if ( iLine == 1 ) {
940 if ( readDbl(line, pos[0], fieldLen, _x_pos ) ||
941 readDbl(line, pos[1], fieldLen, _x_velocity ) ||
942 readDbl(line, pos[2], fieldLen, _x_acceleration) ||
943 readDbl(line, pos[3], fieldLen, _health ) ) {
944 return;
945 }
946 }
947
948 else if ( iLine == 2 ) {
949 if ( readDbl(line, pos[0], fieldLen, _y_pos ) ||
950 readDbl(line, pos[1], fieldLen, _y_velocity ) ||
951 readDbl(line, pos[2], fieldLen, _y_acceleration ) ||
952 readDbl(line, pos[3], fieldLen, _frequency_number) ) {
953 return;
954 }
955 }
956
957 else if ( iLine == 3 ) {
958 if ( readDbl(line, pos[0], fieldLen, _z_pos ) ||
959 readDbl(line, pos[1], fieldLen, _z_velocity ) ||
960 readDbl(line, pos[2], fieldLen, _z_acceleration) ||
961 readDbl(line, pos[3], fieldLen, _E ) ) {
962 return;
963 }
964 }
965 }
966
967 // Initialize status vector
968 // ------------------------
[4018]969 _tt = _TOC;
970 _xv.ReSize(6);
[3699]971 _xv(1) = _x_pos * 1.e3;
972 _xv(2) = _y_pos * 1.e3;
973 _xv(3) = _z_pos * 1.e3;
974 _xv(4) = _x_velocity * 1.e3;
975 _xv(5) = _y_velocity * 1.e3;
976 _xv(6) = _z_velocity * 1.e3;
977
978 _ok = true;
[3659]979}
980
981// Constructor
982//////////////////////////////////////////////////////////////////////////////
[3699]983t_ephGal::t_ephGal(float /* rnxVersion */, const QStringList& /* lines */) {
[3659]984
985 _ok = false;
986}
[4013]987
[4021]988//
[4013]989//////////////////////////////////////////////////////////////////////////////
[4021]990QString t_eph::rinexDateStr(double version) const {
[4013]991
[4021]992 QString datStr;
[4013]993
994 unsigned year, month, day, hour, min;
[4018]995 double sec;
996 _TOC.civil_date(year, month, day);
997 _TOC.civil_time(hour, min, sec);
[4013]998
[4021]999 QTextStream out(&datStr);
[4013]1000
1001 if (version < 3.0) {
[4017]1002 QString prnHlp = _prn.mid(1,2); if (prnHlp[0] == '0') prnHlp[0] = ' ';
1003 out << prnHlp << QString(" %1 %2 %3 %4 %5%6")
[4013]1004 .arg(year % 100, 2, 10, QChar('0'))
1005 .arg(month, 2)
1006 .arg(day, 2)
1007 .arg(hour, 2)
1008 .arg(min, 2)
[4016]1009 .arg(sec, 5, 'f',1);
[4013]1010 }
1011 else {
1012 out << _prn << QString(" %1 %2 %3 %4 %5 %6")
1013 .arg(year, 4)
1014 .arg(month, 2, 10, QChar('0'))
1015 .arg(day, 2, 10, QChar('0'))
1016 .arg(hour, 2, 10, QChar('0'))
1017 .arg(min, 2, 10, QChar('0'))
1018 .arg(int(sec), 2, 10, QChar('0'));
1019 }
[4021]1020
1021 return datStr;
1022}
1023
1024// RINEX Format String
1025//////////////////////////////////////////////////////////////////////////////
1026QString t_ephGlo::toString(double version) const {
1027 QString rnxStr = rinexDateStr(version);
1028 return rnxStr + "\n";
1029}
1030
1031// RINEX Format String
1032//////////////////////////////////////////////////////////////////////////////
1033QString t_ephGal::toString(double version) const {
1034 QString rnxStr = rinexDateStr(version);
1035 return rnxStr + "\n";
1036}
1037
1038// RINEX Format String
1039//////////////////////////////////////////////////////////////////////////////
1040QString t_ephGPS::toString(double version) const {
1041
1042 QString rnxStr = rinexDateStr(version);
[4013]1043
[4021]1044 QTextStream out(&rnxStr);
1045
[4013]1046 out << QString("%1%2%3\n")
1047 .arg(_clock_bias, 19, 'e', 12)
1048 .arg(_clock_drift, 19, 'e', 12)
1049 .arg(_clock_driftrate, 19, 'e', 12);
1050
[4015]1051 QString fmt = version < 3.0 ? " %1%2%3%4\n" : " %1%2%3%4\n";
1052
1053 out << QString(fmt)
[4013]1054 .arg(_IODE, 19, 'e', 12)
1055 .arg(_Crs, 19, 'e', 12)
1056 .arg(_Delta_n, 19, 'e', 12)
1057 .arg(_M0, 19, 'e', 12);
1058
[4015]1059 out << QString(fmt)
[4013]1060 .arg(_Cuc, 19, 'e', 12)
1061 .arg(_e, 19, 'e', 12)
1062 .arg(_Cus, 19, 'e', 12)
1063 .arg(_sqrt_A, 19, 'e', 12);
1064
[4015]1065 out << QString(fmt)
[4018]1066 .arg(_TOEsec, 19, 'e', 12)
[4013]1067 .arg(_Cic, 19, 'e', 12)
1068 .arg(_OMEGA0, 19, 'e', 12)
1069 .arg(_Cis, 19, 'e', 12);
1070
[4015]1071 out << QString(fmt)
[4013]1072 .arg(_i0, 19, 'e', 12)
1073 .arg(_Crc, 19, 'e', 12)
1074 .arg(_omega, 19, 'e', 12)
1075 .arg(_OMEGADOT, 19, 'e', 12);
1076
[4015]1077 out << QString(fmt)
[4018]1078 .arg(_IDOT, 19, 'e', 12)
1079 .arg(_L2Codes, 19, 'e', 12)
1080 .arg(_TOEweek, 19, 'e', 12)
1081 .arg(_L2PFlag, 19, 'e', 12);
[4013]1082
[4015]1083 out << QString(fmt)
[4018]1084 .arg(_ura, 19, 'e', 12)
[4013]1085 .arg(_health, 19, 'e', 12)
1086 .arg(_TGD, 19, 'e', 12)
1087 .arg(_IODC, 19, 'e', 12);
1088
[4015]1089 out << QString(fmt)
[4018]1090 .arg(_TOT, 19, 'e', 12)
1091 .arg(_fitInterval, 19, 'e', 12)
[4014]1092 .arg("")
1093 .arg("");
[4013]1094
1095 return rnxStr;
1096}
Note: See TracBrowser for help on using the repository browser.