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