source: ntrip/branches/BNC_LM/RTCM3/ephemeris.cpp@ 6398

Last change on this file since 6398 was 3571, checked in by mervart, 13 years ago
File size: 24.3 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#define PI 3.1415926535898
18// Returns nearest integer value
19////////////////////////////////////////////////////////////////////////////
20static double NearestInt(double fl, double * remain)
21{
22 bool isneg = fl < 0.0;
23 double intval;
24 if(isneg) fl *= -1.0;
25 intval = (double)((unsigned long)(fl+0.5));
26 if(isneg) {fl *= -1.0; intval *= -1.0;}
27 if(remain)
28 *remain = fl-intval;
29 return intval;
30} /* NearestInt() */
31
32// Returns CRC24
33////////////////////////////////////////////////////////////////////////////
34static unsigned long CRC24(long size, const unsigned char *buf)
35{
36 unsigned long crc = 0;
37 int i;
38
39 while(size--)
40 {
41 crc ^= (*buf++) << (16);
42 for(i = 0; i < 8; i++)
43 {
44 crc <<= 1;
45 if(crc & 0x1000000)
46 crc ^= 0x01864cfb;
47 }
48 }
49 return crc;
50} /* CRC24 */
51
[2222]52//
53////////////////////////////////////////////////////////////////////////////
[1025]54bool t_eph::isNewerThan(const t_eph* eph) const {
55 if (_GPSweek > eph->_GPSweek ||
56 (_GPSweek == eph->_GPSweek && _GPSweeks > eph->_GPSweeks)) {
57 return true;
58 }
59 else {
60 return false;
61 }
62}
63
[2222]64// Set GPS Satellite Position
65////////////////////////////////////////////////////////////////////////////
[1025]66void t_ephGPS::set(const gpsephemeris* ee) {
67
[3255]68 _prn = QString("G%1").arg(ee->satellite, 2, 10, QChar('0'));
[1025]69
70 // TODO: check if following two lines are correct
71 _GPSweek = ee->GPSweek;
72 _GPSweeks = ee->TOE;
73
74 _TOW = ee->TOW;
75 _TOC = ee->TOC;
76 _TOE = ee->TOE;
77 _IODE = ee->IODE;
78 _IODC = ee->IODC;
79
80 _clock_bias = ee->clock_bias ;
81 _clock_drift = ee->clock_drift ;
82 _clock_driftrate = ee->clock_driftrate;
83
84 _Crs = ee->Crs;
85 _Delta_n = ee->Delta_n;
86 _M0 = ee->M0;
87 _Cuc = ee->Cuc;
88 _e = ee->e;
89 _Cus = ee->Cus;
90 _sqrt_A = ee->sqrt_A;
91 _Cic = ee->Cic;
92 _OMEGA0 = ee->OMEGA0;
93 _Cis = ee->Cis;
94 _i0 = ee->i0;
95 _Crc = ee->Crc;
96 _omega = ee->omega;
97 _OMEGADOT = ee->OMEGADOT;
98 _IDOT = ee->IDOT;
99
100 _TGD = ee->TGD;
101}
102
[2222]103// Compute GPS Satellite Position (virtual)
[1025]104////////////////////////////////////////////////////////////////////////////
105void t_ephGPS::position(int GPSweek, double GPSweeks,
106 double* xc,
107 double* vv) const {
108
[1098]109 static const double secPerWeek = 7 * 86400.0;
110 static const double omegaEarth = 7292115.1467e-11;
111 static const double gmWGS = 398.6005e12;
[1025]112
113 memset(xc, 0, 4*sizeof(double));
114 memset(vv, 0, 3*sizeof(double));
115
116 double a0 = _sqrt_A * _sqrt_A;
117 if (a0 == 0) {
118 return;
119 }
120
121 double n0 = sqrt(gmWGS/(a0*a0*a0));
122 double tk = GPSweeks - _TOE;
123 if (GPSweek != _GPSweek) {
124 tk += (GPSweek - _GPSweek) * secPerWeek;
125 }
126 double n = n0 + _Delta_n;
127 double M = _M0 + n*tk;
128 double E = M;
129 double E_last;
130 do {
131 E_last = E;
132 E = M + _e*sin(E);
133 } while ( fabs(E-E_last)*a0 > 0.001 );
134 double v = 2.0*atan( sqrt( (1.0 + _e)/(1.0 - _e) )*tan( E/2 ) );
135 double u0 = v + _omega;
136 double sin2u0 = sin(2*u0);
137 double cos2u0 = cos(2*u0);
138 double r = a0*(1 - _e*cos(E)) + _Crc*cos2u0 + _Crs*sin2u0;
139 double i = _i0 + _IDOT*tk + _Cic*cos2u0 + _Cis*sin2u0;
140 double u = u0 + _Cuc*cos2u0 + _Cus*sin2u0;
141 double xp = r*cos(u);
142 double yp = r*sin(u);
143 double OM = _OMEGA0 + (_OMEGADOT - omegaEarth)*tk -
144 omegaEarth*_TOE;
145
146 double sinom = sin(OM);
147 double cosom = cos(OM);
148 double sini = sin(i);
149 double cosi = cos(i);
150 xc[0] = xp*cosom - yp*cosi*sinom;
151 xc[1] = xp*sinom + yp*cosi*cosom;
152 xc[2] = yp*sini;
153
154 double tc = GPSweeks - _TOC;
155 if (GPSweek != _GPSweek) {
156 tc += (GPSweek - _GPSweek) * secPerWeek;
157 }
[2429]158 xc[3] = _clock_bias + _clock_drift*tc + _clock_driftrate*tc*tc;
[1025]159
160 // Velocity
161 // --------
162 double tanv2 = tan(v/2);
163 double dEdM = 1 / (1 - _e*cos(E));
164 double dotv = sqrt((1.0 + _e)/(1.0 - _e)) / cos(E/2)/cos(E/2) / (1 + tanv2*tanv2)
165 * dEdM * n;
166 double dotu = dotv + (-_Cuc*sin2u0 + _Cus*cos2u0)*2*dotv;
167 double dotom = _OMEGADOT - omegaEarth;
168 double doti = _IDOT + (-_Cic*sin2u0 + _Cis*cos2u0)*2*dotv;
169 double dotr = a0 * _e*sin(E) * dEdM * n
170 + (-_Crc*sin2u0 + _Crs*cos2u0)*2*dotv;
171 double dotx = dotr*cos(u) - r*sin(u)*dotu;
172 double doty = dotr*sin(u) + r*cos(u)*dotu;
173
174 vv[0] = cosom *dotx - cosi*sinom *doty // dX / dr
175 - xp*sinom*dotom - yp*cosi*cosom*dotom // dX / dOMEGA
176 + yp*sini*sinom*doti; // dX / di
177
178 vv[1] = sinom *dotx + cosi*cosom *doty
179 + xp*cosom*dotom - yp*cosi*sinom*dotom
180 - yp*sini*cosom*doti;
181
182 vv[2] = sini *doty + yp*cosi *doti;
[2429]183
184 // Relativistic Correction
185 // -----------------------
186 // xc(4) -= 4.442807633e-10 * _e * sqrt(a0) *sin(E);
187 xc[3] -= 2.0 * (xc[0]*vv[0] + xc[1]*vv[1] + xc[2]*vv[2]) / t_CST::c / t_CST::c;
[1025]188}
189
[3255]190// build up RTCM3 for GPS
191////////////////////////////////////////////////////////////////////////////
192#define GPSTOINT(type, value) static_cast<type>(NearestInt(value,0))
[1025]193
[3255]194#define GPSADDBITS(a, b) {bitbuffer = (bitbuffer<<(a)) \
195 |(GPSTOINT(long long,b)&((1ULL<<a)-1)); \
196 numbits += (a); \
197 while(numbits >= 8) { \
198 buffer[size++] = bitbuffer>>(numbits-8);numbits -= 8;}}
199#define GPSADDBITSFLOAT(a,b,c) {long long i = GPSTOINT(long long,(b)/(c)); \
200 GPSADDBITS(a,i)};
201
202int t_ephGPS::RTCM3(unsigned char *buffer)
203{
204
205 unsigned char *startbuffer = buffer;
206 buffer= buffer+3;
207 int size = 0;
208 int numbits = 0;
209 unsigned long long bitbuffer = 0;
210 if (_ura <= 2.40){
211 _ura = 0;
212 }
213 else if (_ura <= 3.40){
214 _ura = 1;
215 }
216 else if (_ura <= 6.85){
217 _ura = 2;
218 }
219 else if (_ura <= 9.65){
220 _ura = 3;
221 }
222 else if (_ura <= 13.65){
223 _ura = 4;
224 }
225 else if (_ura <= 24.00){
226 _ura = 5;
227 }
228 else if (_ura <= 48.00){
229 _ura = 6;
230 }
231 else if (_ura <= 96.00){
232 _ura = 7;
233 }
234 else if (_ura <= 192.00){
235 _ura = 8;
236 }
237 else if (_ura <= 384.00){
238 _ura = 9;
239 }
240 else if (_ura <= 768.00){
241 _ura = 10;
242 }
243 else if (_ura <= 1536.00){
244 _ura = 11;
245 }
246 else if (_ura <= 1536.00){
247 _ura = 12;
248 }
249 else if (_ura <= 2072.00){
250 _ura = 13;
251 }
252 else if (_ura <= 6144.00){
253 _ura = 14;
254 }
255 else{
256 _ura = 15;
257 }
258
259 GPSADDBITS(12, 1019)
260 GPSADDBITS(6,_prn.right((_prn.length()-1)).toInt())
261 GPSADDBITS(10, _GPSweek)
262 GPSADDBITS(4, _ura)
263 GPSADDBITS(2,_L2Codes)
264 GPSADDBITSFLOAT(14, _IDOT, PI/static_cast<double>(1<<30)
265 /static_cast<double>(1<<13))
266 GPSADDBITS(8, _IODE)
267 GPSADDBITS(16, static_cast<int>(_TOC)>>4)
268 GPSADDBITSFLOAT(8, _clock_driftrate, 1.0/static_cast<double>(1<<30)
269 /static_cast<double>(1<<25))
270 GPSADDBITSFLOAT(16, _clock_drift, 1.0/static_cast<double>(1<<30)
271 /static_cast<double>(1<<13))
272 GPSADDBITSFLOAT(22, _clock_bias, 1.0/static_cast<double>(1<<30)
273 /static_cast<double>(1<<1))
274 GPSADDBITS(10, _IODC)
275 GPSADDBITSFLOAT(16, _Crs, 1.0/static_cast<double>(1<<5))
276 GPSADDBITSFLOAT(16, _Delta_n, PI/static_cast<double>(1<<30)
277 /static_cast<double>(1<<13))
278 GPSADDBITSFLOAT(32, _M0, PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
279 GPSADDBITSFLOAT(16, _Cuc, 1.0/static_cast<double>(1<<29))
280 GPSADDBITSFLOAT(32, _e, 1.0/static_cast<double>(1<<30)/static_cast<double>(1<<3))
281 GPSADDBITSFLOAT(16, _Cus, 1.0/static_cast<double>(1<<29))
282 GPSADDBITSFLOAT(32, _sqrt_A, 1.0/static_cast<double>(1<<19))
283 GPSADDBITS(16, static_cast<int>(_TOE)>>4)
284 GPSADDBITSFLOAT(16, _Cic, 1.0/static_cast<double>(1<<29))
285 GPSADDBITSFLOAT(32, _OMEGA0, PI/static_cast<double>(1<<30)
286 /static_cast<double>(1<<1))
287 GPSADDBITSFLOAT(16, _Cis, 1.0/static_cast<double>(1<<29))
288 GPSADDBITSFLOAT(32, _i0, PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
289 GPSADDBITSFLOAT(16, _Crc, 1.0/static_cast<double>(1<<5))
290 GPSADDBITSFLOAT(32, _omega, PI/static_cast<double>(1<<30)
291 /static_cast<double>(1<<1))
292 GPSADDBITSFLOAT(24, _OMEGADOT, PI/static_cast<double>(1<<30)
293 /static_cast<double>(1<<13))
294 GPSADDBITSFLOAT(8, _TGD, 1.0/static_cast<double>(1<<30)/static_cast<double>(1<<1))
295 GPSADDBITS(6, _health)
296 GPSADDBITS(1, _L2PFlag)
297 GPSADDBITS(1, 0) /* GPS fit interval */
298
299 startbuffer[0]=0xD3;
300 startbuffer[1]=(size >> 8);
301 startbuffer[2]=size;
302 unsigned long i = CRC24(size+3, startbuffer);
303 buffer[size++] = i >> 16;
304 buffer[size++] = i >> 8;
305 buffer[size++] = i;
306 size += 3;
307 return size;
308}
309
[2221]310// Derivative of the state vector using a simple force model (static)
311////////////////////////////////////////////////////////////////////////////
[2556]312ColumnVector t_ephGlo::glo_deriv(double /* tt */, const ColumnVector& xv,
313 double* acc) {
[2221]314
315 // State vector components
316 // -----------------------
317 ColumnVector rr = xv.rows(1,3);
318 ColumnVector vv = xv.rows(4,6);
319
320 // Acceleration
321 // ------------
322 static const double GM = 398.60044e12;
323 static const double AE = 6378136.0;
324 static const double OMEGA = 7292115.e-11;
[2561]325 static const double C20 = -1082.6257e-6;
[2221]326
327 double rho = rr.norm_Frobenius();
328 double t1 = -GM/(rho*rho*rho);
329 double t2 = 3.0/2.0 * C20 * (GM*AE*AE) / (rho*rho*rho*rho*rho);
330 double t3 = OMEGA * OMEGA;
331 double t4 = 2.0 * OMEGA;
332 double z2 = rr(3) * rr(3);
333
334 // Vector of derivatives
335 // ---------------------
336 ColumnVector va(6);
337 va(1) = vv(1);
338 va(2) = vv(2);
339 va(3) = vv(3);
[2556]340 va(4) = (t1 + t2*(1.0-5.0*z2/(rho*rho)) + t3) * rr(1) + t4*vv(2) + acc[0];
341 va(5) = (t1 + t2*(1.0-5.0*z2/(rho*rho)) + t3) * rr(2) - t4*vv(1) + acc[1];
342 va(6) = (t1 + t2*(3.0-5.0*z2/(rho*rho)) ) * rr(3) + acc[2];
[2221]343
344 return va;
345}
346
347// Compute Glonass Satellite Position (virtual)
348////////////////////////////////////////////////////////////////////////////
349void t_ephGlo::position(int GPSweek, double GPSweeks,
350 double* xc, double* vv) const {
351
352 static const double secPerWeek = 7 * 86400.0;
353 static const double nominalStep = 10.0;
354
355 memset(xc, 0, 4*sizeof(double));
356 memset(vv, 0, 3*sizeof(double));
357
358 double dtPos = GPSweeks - _tt;
359 if (GPSweek != _GPSweek) {
360 dtPos += (GPSweek - _GPSweek) * secPerWeek;
361 }
362
363 int nSteps = int(fabs(dtPos) / nominalStep) + 1;
364 double step = dtPos / nSteps;
365
[2556]366 double acc[3];
[2557]367 acc[0] = _x_acceleration * 1.e3;
[2560]368 acc[1] = _y_acceleration * 1.e3;
369 acc[2] = _z_acceleration * 1.e3;
[2221]370 for (int ii = 1; ii <= nSteps; ii++) {
[2556]371 _xv = rungeKutta4(_tt, _xv, step, acc, glo_deriv);
[2221]372 _tt += step;
373 }
374
375 // Position and Velocity
376 // ---------------------
377 xc[0] = _xv(1);
378 xc[1] = _xv(2);
379 xc[2] = _xv(3);
380
381 vv[0] = _xv(4);
382 vv[1] = _xv(5);
383 vv[2] = _xv(6);
384
385 // Clock Correction
386 // ----------------
387 double dtClk = GPSweeks - _GPSweeks;
388 if (GPSweek != _GPSweek) {
389 dtClk += (GPSweek - _GPSweek) * secPerWeek;
390 }
391 xc[3] = -_tau + _gamma * dtClk;
392}
393
394// IOD of Glonass Ephemeris (virtual)
395////////////////////////////////////////////////////////////////////////////
396int t_ephGlo::IOD() const {
[3571]397 bncTime tGPS(_GPSweek, _GPSweeks);
398 bncTime tMoscow = tGPS - _gps_utc + 3 * 3600.0;
399 return int(tMoscow.daysec() / 900);
[2221]400}
401
402// Set Glonass Ephemeris
403////////////////////////////////////////////////////////////////////////////
404void t_ephGlo::set(const glonassephemeris* ee) {
405
[3255]406 _prn = QString("R%1").arg(ee->almanac_number, 2, 10, QChar('0'));
[2223]407
[2234]408 int ww = ee->GPSWeek;
409 int tow = ee->GPSTOW;
[2257]410 updatetime(&ww, &tow, ee->tb*1000, 0); // Moscow -> GPS
[2223]411
[3264]412 // Check the day once more
413 // -----------------------
414 {
[3268]415 const double secPerDay = 24 * 3600.0;
416 const double secPerWeek = 7 * secPerDay;
[3266]417 int ww_old = ww;
418 int tow_old = tow;
[3264]419 int currentWeek;
420 double currentSec;
421 currentGPSWeeks(currentWeek, currentSec);
422 bncTime currentTime(currentWeek, currentSec);
423 bncTime hTime(ww, (double) tow);
424
425 bool changed = false;
[3268]426 if (hTime - currentTime > secPerDay/2.0) {
[3264]427 changed = true;
[3268]428 tow -= secPerDay;
429 if (tow < 0) {
430 tow += secPerWeek;
431 ww -= 1;
432 }
[3264]433 }
[3268]434 else if (hTime - currentTime < -secPerDay/2.0) {
[3264]435 changed = true;
[3268]436 tow += secPerDay;
437 if (tow > secPerWeek) {
438 tow -= secPerWeek;
439 ww += 1;
440 }
[3264]441 }
442
[3280]443 if (changed && ((bncApp*) qApp)->mode() == bncApp::batchPostProcessing) {
[3265]444 bncTime newHTime(ww, (double) tow);
[3269]445 cout << "GLONASS " << ee->almanac_number << " Time Changed at "
[3266]446 << currentTime.datestr() << " " << currentTime.timestr()
447 << endl
[3268]448 << "old: " << hTime.datestr() << " " << hTime.timestr()
[3266]449 << endl
450 << "new: " << newHTime.datestr() << " " << newHTime.timestr()
451 << endl
452 << "eph: " << ee->GPSWeek << " " << ee->GPSTOW << " " << ee->tb
453 << endl
454 << "ww, tow (old): " << ww_old << " " << tow_old
455 << endl
456 << "ww, tow (new): " << ww << " " << tow
[3267]457 << endl << endl;
[3264]458 }
459 }
460
[3263]461 bncTime hlpTime(ww, (double) tow);
[3255]462 unsigned year, month, day;
463 hlpTime.civil_date(year, month, day);
464 _gps_utc = gnumleap(year, month, day);
465
[2234]466 _GPSweek = ww;
467 _GPSweeks = tow;
[2223]468 _E = ee->E;
469 _tau = ee->tau;
470 _gamma = ee->gamma;
471 _x_pos = ee->x_pos;
472 _x_velocity = ee->x_velocity;
473 _x_acceleration = ee->x_acceleration;
474 _y_pos = ee->y_pos;
475 _y_velocity = ee->y_velocity;
476 _y_acceleration = ee->y_acceleration;
477 _z_pos = ee->z_pos;
478 _z_velocity = ee->z_velocity;
479 _z_acceleration = ee->z_acceleration;
480 _health = 0;
481 _frequency_number = ee->frequency_number;
[2257]482 _tki = ee->tk-3*60*60; if (_tki < 0) _tki += 86400;
[2223]483
484 // Initialize status vector
485 // ------------------------
486 _tt = _GPSweeks;
487
488 _xv(1) = _x_pos * 1.e3;
489 _xv(2) = _y_pos * 1.e3;
490 _xv(3) = _z_pos * 1.e3;
491 _xv(4) = _x_velocity * 1.e3;
492 _xv(5) = _y_velocity * 1.e3;
493 _xv(6) = _z_velocity * 1.e3;
[2221]494}
[2771]495
[3255]496// build up RTCM3 for GLONASS
497////////////////////////////////////////////////////////////////////////////
498#define GLONASSTOINT(type, value) static_cast<type>(NearestInt(value,0))
499
500#define GLONASSADDBITS(a, b) {bitbuffer = (bitbuffer<<(a)) \
501 |(GLONASSTOINT(long long,b)&((1ULL<<(a))-1)); \
502 numbits += (a); \
503 while(numbits >= 8) { \
504 buffer[size++] = bitbuffer>>(numbits-8);numbits -= 8;}}
505#define GLONASSADDBITSFLOATM(a,b,c) {int s; long long i; \
506 if(b < 0.0) \
507 { \
508 s = 1; \
509 i = GLONASSTOINT(long long,(-b)/(c)); \
510 if(!i) s = 0; \
511 } \
512 else \
513 { \
514 s = 0; \
515 i = GLONASSTOINT(long long,(b)/(c)); \
516 } \
517 GLONASSADDBITS(1,s) \
518 GLONASSADDBITS(a-1,i)}
519
520int t_ephGlo::RTCM3(unsigned char *buffer)
521{
522
523 int size = 0;
524 int numbits = 0;
525 long long bitbuffer = 0;
526 unsigned char *startbuffer = buffer;
527 buffer= buffer+3;
528
529 GLONASSADDBITS(12, 1020)
530 GLONASSADDBITS(6, _prn.right((_prn.length()-1)).toInt())
531 GLONASSADDBITS(5, 7+_frequency_number)
532 GLONASSADDBITS(1, 0)
533 GLONASSADDBITS(1, 0)
534 GLONASSADDBITS(2, 0)
535 _tki=_tki+3*60*60;
536 GLONASSADDBITS(5, static_cast<int>(_tki)/(60*60))
537 GLONASSADDBITS(6, (static_cast<int>(_tki)/60)%60)
538 GLONASSADDBITS(1, (static_cast<int>(_tki)/30)%30)
539 GLONASSADDBITS(1, _health)
540 GLONASSADDBITS(1, 0)
541 unsigned long long timeofday = (static_cast<int>(_tt+3*60*60-_gps_utc)%86400);
542 GLONASSADDBITS(7, timeofday/60/15)
543 GLONASSADDBITSFLOATM(24, _x_velocity*1000, 1000.0/static_cast<double>(1<<20))
544 GLONASSADDBITSFLOATM(27, _x_pos*1000, 1000.0/static_cast<double>(1<<11))
545 GLONASSADDBITSFLOATM(5, _x_acceleration*1000, 1000.0/static_cast<double>(1<<30))
546 GLONASSADDBITSFLOATM(24, _y_velocity*1000, 1000.0/static_cast<double>(1<<20))
547 GLONASSADDBITSFLOATM(27, _y_pos*1000, 1000.0/static_cast<double>(1<<11))
548 GLONASSADDBITSFLOATM(5, _y_acceleration*1000, 1000.0/static_cast<double>(1<<30))
549 GLONASSADDBITSFLOATM(24, _z_velocity*1000, 1000.0/static_cast<double>(1<<20))
550 GLONASSADDBITSFLOATM(27,_z_pos*1000, 1000.0/static_cast<double>(1<<11))
551 GLONASSADDBITSFLOATM(5, _z_acceleration*1000, 1000.0/static_cast<double>(1<<30))
552 GLONASSADDBITS(1, 0)
553 GLONASSADDBITSFLOATM(11, _gamma, 1.0/static_cast<double>(1<<30)
554 /static_cast<double>(1<<10))
555 GLONASSADDBITS(2, 0) /* GLONASS-M P */
556 GLONASSADDBITS(1, 0) /* GLONASS-M ln(3) */
557 GLONASSADDBITSFLOATM(22, _tau, 1.0/static_cast<double>(1<<30))
558 GLONASSADDBITS(5, 0) /* GLONASS-M delta tau */
559 GLONASSADDBITS(5, _E)
560 GLONASSADDBITS(1, 0) /* GLONASS-M P4 */
561 GLONASSADDBITS(4, 0) /* GLONASS-M FT */
562 GLONASSADDBITS(11, 0) /* GLONASS-M NT */
563 GLONASSADDBITS(2, 0) /* GLONASS-M active? */
564 GLONASSADDBITS(1, 0) /* GLONASS additional data */
565 GLONASSADDBITS(11, 0) /* GLONASS NA */
566 GLONASSADDBITS(32, 0) /* GLONASS tau C */
567 GLONASSADDBITS(5, 0) /* GLONASS-M N4 */
568 GLONASSADDBITS(22, 0) /* GLONASS-M tau GPS */
569 GLONASSADDBITS(1, 0) /* GLONASS-M ln(5) */
570 GLONASSADDBITS(7, 0) /* Reserved */
571
572 startbuffer[0]=0xD3;
573 startbuffer[1]=(size >> 8);
574 startbuffer[2]=size;
575 unsigned long i = CRC24(size+3, startbuffer);
576 buffer[size++] = i >> 16;
577 buffer[size++] = i >> 8;
578 buffer[size++] = i;
579 size += 3;
580 return size;
581}
582
[2771]583// Set Galileo Satellite Position
584////////////////////////////////////////////////////////////////////////////
585void t_ephGal::set(const galileoephemeris* ee) {
586
[3255]587 _prn = QString("E%1").arg(ee->satellite, 2, 10, QChar('0'));
[2771]588
589 _GPSweek = ee->Week;
590 _GPSweeks = ee->TOE;
591
592 _TOC = ee->TOC;
593 _TOE = ee->TOE;
594 _IODnav = ee->IODnav;
595
596 _clock_bias = ee->clock_bias ;
597 _clock_drift = ee->clock_drift ;
598 _clock_driftrate = ee->clock_driftrate;
599
600 _Crs = ee->Crs;
601 _Delta_n = ee->Delta_n;
602 _M0 = ee->M0;
603 _Cuc = ee->Cuc;
604 _e = ee->e;
605 _Cus = ee->Cus;
606 _sqrt_A = ee->sqrt_A;
607 _Cic = ee->Cic;
608 _OMEGA0 = ee->OMEGA0;
609 _Cis = ee->Cis;
610 _i0 = ee->i0;
611 _Crc = ee->Crc;
612 _omega = ee->omega;
613 _OMEGADOT = ee->OMEGADOT;
614 _IDOT = ee->IDOT;
615}
616
617// Compute Galileo Satellite Position (virtual)
618////////////////////////////////////////////////////////////////////////////
619void t_ephGal::position(int GPSweek, double GPSweeks,
620 double* xc,
621 double* vv) const {
622
623 static const double secPerWeek = 7 * 86400.0;
624 static const double omegaEarth = 7292115.1467e-11;
625 static const double gmWGS = 398.6005e12;
626
627 memset(xc, 0, 4*sizeof(double));
628 memset(vv, 0, 3*sizeof(double));
629
630 double a0 = _sqrt_A * _sqrt_A;
631 if (a0 == 0) {
632 return;
633 }
634
635 double n0 = sqrt(gmWGS/(a0*a0*a0));
636 double tk = GPSweeks - _TOE;
637 if (GPSweek != _GPSweek) {
638 tk += (GPSweek - _GPSweek) * secPerWeek;
639 }
640 double n = n0 + _Delta_n;
641 double M = _M0 + n*tk;
642 double E = M;
643 double E_last;
644 do {
645 E_last = E;
646 E = M + _e*sin(E);
647 } while ( fabs(E-E_last)*a0 > 0.001 );
648 double v = 2.0*atan( sqrt( (1.0 + _e)/(1.0 - _e) )*tan( E/2 ) );
649 double u0 = v + _omega;
650 double sin2u0 = sin(2*u0);
651 double cos2u0 = cos(2*u0);
652 double r = a0*(1 - _e*cos(E)) + _Crc*cos2u0 + _Crs*sin2u0;
653 double i = _i0 + _IDOT*tk + _Cic*cos2u0 + _Cis*sin2u0;
654 double u = u0 + _Cuc*cos2u0 + _Cus*sin2u0;
655 double xp = r*cos(u);
656 double yp = r*sin(u);
657 double OM = _OMEGA0 + (_OMEGADOT - omegaEarth)*tk -
658 omegaEarth*_TOE;
659
660 double sinom = sin(OM);
661 double cosom = cos(OM);
662 double sini = sin(i);
663 double cosi = cos(i);
664 xc[0] = xp*cosom - yp*cosi*sinom;
665 xc[1] = xp*sinom + yp*cosi*cosom;
666 xc[2] = yp*sini;
667
668 double tc = GPSweeks - _TOC;
669 if (GPSweek != _GPSweek) {
670 tc += (GPSweek - _GPSweek) * secPerWeek;
671 }
672 xc[3] = _clock_bias + _clock_drift*tc + _clock_driftrate*tc*tc;
673
674 // Velocity
675 // --------
676 double tanv2 = tan(v/2);
677 double dEdM = 1 / (1 - _e*cos(E));
678 double dotv = sqrt((1.0 + _e)/(1.0 - _e)) / cos(E/2)/cos(E/2) / (1 + tanv2*tanv2)
679 * dEdM * n;
680 double dotu = dotv + (-_Cuc*sin2u0 + _Cus*cos2u0)*2*dotv;
681 double dotom = _OMEGADOT - omegaEarth;
682 double doti = _IDOT + (-_Cic*sin2u0 + _Cis*cos2u0)*2*dotv;
683 double dotr = a0 * _e*sin(E) * dEdM * n
684 + (-_Crc*sin2u0 + _Crs*cos2u0)*2*dotv;
685 double dotx = dotr*cos(u) - r*sin(u)*dotu;
686 double doty = dotr*sin(u) + r*cos(u)*dotu;
687
688 vv[0] = cosom *dotx - cosi*sinom *doty // dX / dr
689 - xp*sinom*dotom - yp*cosi*cosom*dotom // dX / dOMEGA
690 + yp*sini*sinom*doti; // dX / di
691
692 vv[1] = sinom *dotx + cosi*cosom *doty
693 + xp*cosom*dotom - yp*cosi*sinom*dotom
694 - yp*sini*cosom*doti;
695
696 vv[2] = sini *doty + yp*cosi *doti;
697
698 // Relativistic Correction
699 // -----------------------
700 // xc(4) -= 4.442807633e-10 * _e * sqrt(a0) *sin(E);
701 xc[3] -= 2.0 * (xc[0]*vv[0] + xc[1]*vv[1] + xc[2]*vv[2]) / t_CST::c / t_CST::c;
702}
703
[3255]704// build up RTCM3 for Galileo
705////////////////////////////////////////////////////////////////////////////
[3279]706#define GALILEOTOINT(type, value) static_cast<type>(NearestInt(value, 0))
707
708#define GALILEOADDBITS(a, b) {bitbuffer = (bitbuffer<<(a)) \
709 |(GALILEOTOINT(long long,b)&((1LL<<a)-1)); \
710 numbits += (a); \
711 while(numbits >= 8) { \
712 buffer[size++] = bitbuffer>>(numbits-8);numbits -= 8;}}
713#define GALILEOADDBITSFLOAT(a,b,c) {long long i = GALILEOTOINT(long long,(b)/(c)); \
714 GALILEOADDBITS(a,i)};
715
[3255]716int t_ephGal::RTCM3(unsigned char *buffer) {
[3279]717 int size = 0;
718 int numbits = 0;
719 long long bitbuffer = 0;
720 unsigned char *startbuffer = buffer;
721 buffer= buffer+3;
[3255]722
[3279]723 GALILEOADDBITS(12, /*inav ? 1046 :*/ 1045)
724 GALILEOADDBITS(6, _prn.right((_prn.length()-1)).toInt())
725 GALILEOADDBITS(12, _GPSweek)
726 GALILEOADDBITS(10, _IODnav)
727 GALILEOADDBITS(8, _SISA)
728 GALILEOADDBITSFLOAT(14, _IDOT, PI/static_cast<double>(1<<30)
729 /static_cast<double>(1<<13))
730 GALILEOADDBITS(14, _TOC/60)
731 GALILEOADDBITSFLOAT(6, _clock_driftrate, 1.0/static_cast<double>(1<<30)
732 /static_cast<double>(1<<29))
733 GALILEOADDBITSFLOAT(21, _clock_drift, 1.0/static_cast<double>(1<<30)
734 /static_cast<double>(1<<16))
735 GALILEOADDBITSFLOAT(31, _clock_bias, 1.0/static_cast<double>(1<<30)
736 /static_cast<double>(1<<4))
737 GALILEOADDBITSFLOAT(16, _Crs, 1.0/static_cast<double>(1<<5))
738 GALILEOADDBITSFLOAT(16, _Delta_n, PI/static_cast<double>(1<<30)
739 /static_cast<double>(1<<13))
740 GALILEOADDBITSFLOAT(32, _M0, PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
741 GALILEOADDBITSFLOAT(16, _Cuc, 1.0/static_cast<double>(1<<29))
742 GALILEOADDBITSFLOAT(32, _e, 1.0/static_cast<double>(1<<30)/static_cast<double>(1<<3))
743 GALILEOADDBITSFLOAT(16, _Cus, 1.0/static_cast<double>(1<<29))
744 GALILEOADDBITSFLOAT(32, _sqrt_A, 1.0/static_cast<double>(1<<19))
745 GALILEOADDBITS(14, _TOE/60)
746 GALILEOADDBITSFLOAT(16, _Cic, 1.0/static_cast<double>(1<<29))
747 GALILEOADDBITSFLOAT(32, _OMEGA0, PI/static_cast<double>(1<<30)
748 /static_cast<double>(1<<1))
749 GALILEOADDBITSFLOAT(16, _Cis, 1.0/static_cast<double>(1<<29))
750 GALILEOADDBITSFLOAT(32, _i0, PI/static_cast<double>(1<<30)/static_cast<double>(1<<1))
751 GALILEOADDBITSFLOAT(16, _Crc, 1.0/static_cast<double>(1<<5))
752 GALILEOADDBITSFLOAT(32, _omega, PI/static_cast<double>(1<<30)
753 /static_cast<double>(1<<1))
754 GALILEOADDBITSFLOAT(24, _OMEGADOT, PI/static_cast<double>(1<<30)
755 /static_cast<double>(1<<13))
756 GALILEOADDBITSFLOAT(10, _BGD_1_5A, 1.0/static_cast<double>(1<<30)
757 /static_cast<double>(1<<2))
758 /*if(inav)
759 {
760 GALILEOADDBITSFLOAT(10, _BGD_1_5B, 1.0/static_cast<double>(1<<30)
761 /static_cast<double>(1<<2))
762 GALILEOADDBITS(2, _E5bHS)
763 GALILEOADDBITS(1, flags & MNFGALEPHF_E5BDINVALID)
764 }
765 else*/
766 {
767 GALILEOADDBITS(2, _E5aHS)
768 GALILEOADDBITS(1, /*flags & MNFGALEPHF_E5ADINVALID*/0)
769 }
770 _TOE = 0.9999E9;
771 GALILEOADDBITS(20, _TOE)
772
773 GALILEOADDBITS(/*inav ? 1 :*/ 3, 0) /* fill up */
774
775 startbuffer[0]=0xD3;
776 startbuffer[1]=(size >> 8);
777 startbuffer[2]=size;
778 unsigned long i = CRC24(size+3, startbuffer);
779 buffer[size++] = i >> 16;
780 buffer[size++] = i >> 8;
781 buffer[size++] = i;
782 size += 3;
783 return size;
[3255]784}
Note: See TracBrowser for help on using the repository browser.