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