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