[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"
|
---|
[758] | 21 |
|
---|
| 22 | using namespace std;
|
---|
| 23 |
|
---|
| 24 | // Constructor
|
---|
| 25 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 26 | t_bnseph::t_bnseph(QObject* parent) : QThread(parent) {
|
---|
[839] | 27 |
|
---|
| 28 | this->setTerminationEnabled(true);
|
---|
| 29 |
|
---|
[830] | 30 | _socket = 0;
|
---|
[758] | 31 | }
|
---|
| 32 |
|
---|
| 33 | // Destructor
|
---|
| 34 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 35 | t_bnseph::~t_bnseph() {
|
---|
[759] | 36 | delete _socket;
|
---|
[758] | 37 | }
|
---|
| 38 |
|
---|
[838] | 39 | // Connect the Socket
|
---|
[758] | 40 | ////////////////////////////////////////////////////////////////////////////
|
---|
[838] | 41 | void t_bnseph::reconnect() {
|
---|
[759] | 42 |
|
---|
[838] | 43 | delete _socket;
|
---|
[759] | 44 |
|
---|
[830] | 45 | QSettings settings;
|
---|
| 46 | QString host = "localhost";
|
---|
| 47 | int port = settings.value("ephPort").toInt();
|
---|
| 48 |
|
---|
| 49 | _socket = new QTcpSocket();
|
---|
| 50 | _socket->connectToHost(host, port);
|
---|
| 51 |
|
---|
[838] | 52 | const int timeOut = 10*1000; // 10 seconds
|
---|
[830] | 53 | if (!_socket->waitForConnected(timeOut)) {
|
---|
[838] | 54 | emit(newMessage("bnseph::run Connect Timeout"));
|
---|
[830] | 55 | }
|
---|
[838] | 56 | }
|
---|
| 57 |
|
---|
| 58 | // Start
|
---|
| 59 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 60 | void t_bnseph::run() {
|
---|
| 61 |
|
---|
| 62 | emit(newMessage("bnseph::run Start"));
|
---|
| 63 |
|
---|
| 64 | while (true) {
|
---|
| 65 | if (_socket == 0 || _socket->state() != QAbstractSocket::ConnectedState) {
|
---|
| 66 | reconnect();
|
---|
| 67 | }
|
---|
| 68 | if (_socket && _socket->state() == QAbstractSocket::ConnectedState) {
|
---|
[830] | 69 | if (_socket->canReadLine()) {
|
---|
| 70 | readEph();
|
---|
| 71 | }
|
---|
| 72 | else {
|
---|
| 73 | _socket->waitForReadyRead(10);
|
---|
| 74 | }
|
---|
[759] | 75 | }
|
---|
[838] | 76 | else {
|
---|
| 77 | msleep(10);
|
---|
| 78 | }
|
---|
[759] | 79 | }
|
---|
[758] | 80 | }
|
---|
| 81 |
|
---|
[771] | 82 | // Read One Ephemeris
|
---|
| 83 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 84 | void t_bnseph::readEph() {
|
---|
| 85 |
|
---|
| 86 |
|
---|
[884] | 87 | t_eph* eph = 0;
|
---|
[782] | 88 |
|
---|
[884] | 89 | QByteArray line = _socket->readLine();
|
---|
| 90 | QTextStream in(line);
|
---|
| 91 | QString prn;
|
---|
| 92 |
|
---|
| 93 | in >> prn;
|
---|
[771] | 94 |
|
---|
[884] | 95 | int numlines = 0;
|
---|
| 96 | if (prn.indexOf('R') != -1) {
|
---|
| 97 | eph = new t_ephGlo();
|
---|
| 98 | numlines = 4;
|
---|
| 99 | }
|
---|
| 100 | else {
|
---|
| 101 | eph = new t_ephGPS();
|
---|
| 102 | numlines = 8;
|
---|
| 103 | }
|
---|
[771] | 104 |
|
---|
[884] | 105 | QStringList lines;
|
---|
| 106 | lines << line;
|
---|
| 107 |
|
---|
| 108 | for (int ii = 2; ii <= numlines; ii++) {
|
---|
[781] | 109 | QByteArray line = _socket->readLine();
|
---|
[884] | 110 | lines << line;
|
---|
| 111 | }
|
---|
[782] | 112 |
|
---|
[884] | 113 | eph->read(lines);
|
---|
[782] | 114 |
|
---|
[884] | 115 | emit(newEph(eph));
|
---|
| 116 | }
|
---|
[778] | 117 |
|
---|
[886] | 118 | // Compare Time
|
---|
| 119 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 120 | bool t_eph::isNewerThan(const t_eph* eph) const {
|
---|
| 121 | if (_GPSweek > eph->_GPSweek ||
|
---|
| 122 | (_GPSweek == eph->_GPSweek && _GPSweeks > eph->_GPSweeks)) {
|
---|
| 123 | return true;
|
---|
| 124 | }
|
---|
| 125 | else {
|
---|
| 126 | return false;
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[884] | 130 | // Read GPS Ephemeris
|
---|
| 131 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 132 | void t_ephGPS::read(const QStringList& lines) {
|
---|
[782] | 133 |
|
---|
[884] | 134 | for (int ii = 1; ii <= lines.size(); ii++) {
|
---|
| 135 | QTextStream in(lines.at(ii-1).toAscii());
|
---|
[782] | 136 |
|
---|
[884] | 137 | if (ii == 1) {
|
---|
[800] | 138 | double year, month, day, hour, minute, second;
|
---|
[884] | 139 | in >> _prn >> year >> month >> day >> hour >> minute >> second
|
---|
| 140 | >> _clock_bias >> _clock_drift >> _clock_driftrate;
|
---|
[781] | 141 |
|
---|
| 142 | if (year < 100) year += 2000;
|
---|
| 143 |
|
---|
[805] | 144 | QDateTime dateTime(QDate(int(year), int(month), int(day)),
|
---|
| 145 | QTime(int(hour), int(minute), int(second)), Qt::UTC);
|
---|
[886] | 146 |
|
---|
| 147 | GPSweekFromDateAndTime(dateTime, _GPSweek, _GPSweeks);
|
---|
| 148 | _TOC = _GPSweeks;
|
---|
[781] | 149 | }
|
---|
| 150 | else if (ii == 2) {
|
---|
[884] | 151 | in >> _IODE >> _Crs >> _Delta_n >> _M0;
|
---|
[781] | 152 | }
|
---|
| 153 | else if (ii == 3) {
|
---|
[884] | 154 | in >> _Cuc >> _e >> _Cus >> _sqrt_A;
|
---|
[781] | 155 | }
|
---|
| 156 | else if (ii == 4) {
|
---|
[884] | 157 | in >> _TOE >> _Cic >> _OMEGA0 >> _Cis;
|
---|
[781] | 158 | }
|
---|
| 159 | else if (ii == 5) {
|
---|
[884] | 160 | in >> _i0 >> _Crc >> _omega >> _OMEGADOT;
|
---|
[781] | 161 | }
|
---|
| 162 | else if (ii == 6) {
|
---|
[884] | 163 | in >> _IDOT;
|
---|
[781] | 164 | }
|
---|
| 165 | else if (ii == 7) {
|
---|
[800] | 166 | double hlp, health;
|
---|
[884] | 167 | in >> hlp >> health >> _TGD >> _IODC;
|
---|
[781] | 168 | }
|
---|
| 169 | else if (ii == 8) {
|
---|
[884] | 170 | in >> _TOW;
|
---|
[781] | 171 | }
|
---|
| 172 | }
|
---|
[884] | 173 | }
|
---|
[773] | 174 |
|
---|
[884] | 175 | // Compute GPS Satellite Position
|
---|
| 176 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 177 | void t_ephGPS::position(int GPSweek, double GPSweeks, ColumnVector& xc,
|
---|
| 178 | ColumnVector& vv) const {
|
---|
| 179 |
|
---|
| 180 | const static double secPerWeek = 7 * 86400.0;
|
---|
| 181 | const static double omegaEarth = 7292115.1467e-11;
|
---|
| 182 | const static double gmWGS = 398.6005e12;
|
---|
| 183 |
|
---|
| 184 | if (xc.Nrows() < 4) {
|
---|
| 185 | xc.ReSize(4);
|
---|
| 186 | }
|
---|
| 187 | xc = 0.0;
|
---|
| 188 |
|
---|
| 189 | if (vv.Nrows() < 3) {
|
---|
| 190 | vv.ReSize(3);
|
---|
| 191 | }
|
---|
| 192 | vv = 0.0;
|
---|
| 193 |
|
---|
| 194 | double a0 = _sqrt_A * _sqrt_A;
|
---|
| 195 | if (a0 == 0) {
|
---|
| 196 | return;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | double n0 = sqrt(gmWGS/(a0*a0*a0));
|
---|
| 200 | double tk = GPSweeks - _TOE;
|
---|
| 201 | if (GPSweek != _GPSweek) {
|
---|
| 202 | tk += (GPSweek - _GPSweek) * secPerWeek;
|
---|
| 203 | }
|
---|
| 204 | double n = n0 + _Delta_n;
|
---|
| 205 | double M = _M0 + n*tk;
|
---|
| 206 | double E = M;
|
---|
| 207 | double E_last;
|
---|
| 208 | do {
|
---|
| 209 | E_last = E;
|
---|
| 210 | E = M + _e*sin(E);
|
---|
| 211 | } while ( fabs(E-E_last)*a0 > 0.001 );
|
---|
| 212 | double v = 2.0*atan( sqrt( (1.0 + _e)/(1.0 - _e) )*tan( E/2 ) );
|
---|
| 213 | double u0 = v + _omega;
|
---|
| 214 | double sin2u0 = sin(2*u0);
|
---|
| 215 | double cos2u0 = cos(2*u0);
|
---|
| 216 | double r = a0*(1 - _e*cos(E)) + _Crc*cos2u0 + _Crs*sin2u0;
|
---|
| 217 | double i = _i0 + _IDOT*tk + _Cic*cos2u0 + _Cis*sin2u0;
|
---|
| 218 | double u = u0 + _Cuc*cos2u0 + _Cus*sin2u0;
|
---|
| 219 | double xp = r*cos(u);
|
---|
| 220 | double yp = r*sin(u);
|
---|
| 221 | double OM = _OMEGA0 + (_OMEGADOT - omegaEarth)*tk -
|
---|
| 222 | omegaEarth*_TOE;
|
---|
| 223 |
|
---|
| 224 | double sinom = sin(OM);
|
---|
| 225 | double cosom = cos(OM);
|
---|
| 226 | double sini = sin(i);
|
---|
| 227 | double cosi = cos(i);
|
---|
| 228 | xc(1) = xp*cosom - yp*cosi*sinom;
|
---|
| 229 | xc(2) = xp*sinom + yp*cosi*cosom;
|
---|
| 230 | xc(3) = yp*sini;
|
---|
| 231 |
|
---|
| 232 | double tc = GPSweeks - _TOC;
|
---|
| 233 | if (GPSweek != _GPSweek) {
|
---|
| 234 | tc += (GPSweek - _GPSweek) * secPerWeek;
|
---|
| 235 | }
|
---|
| 236 | xc(4) = _clock_bias + _clock_drift*tc + _clock_driftrate*tc*tc
|
---|
| 237 | - 4.442807633e-10 * _e * sqrt(a0) *sin(E);
|
---|
| 238 |
|
---|
| 239 | // Velocity
|
---|
| 240 | // --------
|
---|
| 241 | double tanv2 = tan(v/2);
|
---|
| 242 | double dEdM = 1 / (1 - _e*cos(E));
|
---|
| 243 | double dotv = sqrt((1.0 + _e)/(1.0 - _e)) / cos(E/2)/cos(E/2) / (1 + tanv2*tanv2)
|
---|
| 244 | * dEdM * n;
|
---|
| 245 | double dotu = dotv + (-_Cuc*sin2u0 + _Cus*cos2u0)*2*dotv;
|
---|
| 246 | double dotom = _OMEGADOT - omegaEarth;
|
---|
| 247 | double doti = _IDOT + (-_Cic*sin2u0 + _Cis*cos2u0)*2*dotv;
|
---|
| 248 | double dotr = a0 * _e*sin(E) * dEdM * n
|
---|
| 249 | + (-_Crc*sin2u0 + _Crs*cos2u0)*2*dotv;
|
---|
| 250 | double dotx = dotr*cos(u) - r*sin(u)*dotu;
|
---|
| 251 | double doty = dotr*sin(u) + r*cos(u)*dotu;
|
---|
| 252 |
|
---|
| 253 | vv(1) = cosom *dotx - cosi*sinom *doty // dX / dr
|
---|
| 254 | - xp*sinom*dotom - yp*cosi*cosom*dotom // dX / dOMEGA
|
---|
| 255 | + yp*sini*sinom*doti; // dX / di
|
---|
| 256 |
|
---|
| 257 | vv(2) = sinom *dotx + cosi*cosom *doty
|
---|
| 258 | + xp*cosom*dotom - yp*cosi*sinom*dotom
|
---|
| 259 | - yp*sini*cosom*doti;
|
---|
| 260 |
|
---|
| 261 | vv(3) = sini *doty + yp*cosi *doti;
|
---|
[771] | 262 | }
|
---|
[884] | 263 |
|
---|
[889] | 264 | // Read Glonass Ephemeris
|
---|
[884] | 265 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 266 | void t_ephGlo::read(const QStringList& lines) {
|
---|
| 267 |
|
---|
| 268 | for (int ii = 1; ii <= lines.size(); ii++) {
|
---|
| 269 | QTextStream in(lines.at(ii-1).toAscii());
|
---|
| 270 |
|
---|
| 271 | if (ii == 1) {
|
---|
| 272 | double year, month, day, hour, minute, second;
|
---|
| 273 | in >> _prn >> year >> month >> day >> hour >> minute >> second
|
---|
| 274 | >> _tau >> _gamma;
|
---|
[886] | 275 |
|
---|
| 276 | _tau = -_tau;
|
---|
[884] | 277 |
|
---|
| 278 | if (year < 100) year += 2000;
|
---|
| 279 |
|
---|
| 280 | QDateTime dateTime(QDate(int(year), int(month), int(day)),
|
---|
| 281 | QTime(int(hour), int(minute), int(second)), Qt::UTC);
|
---|
[886] | 282 |
|
---|
| 283 | GPSweekFromDateAndTime(dateTime, _GPSweek, _GPSweeks);
|
---|
[910] | 284 |
|
---|
[911] | 285 | //// beg test
|
---|
[912] | 286 | //// _gps_utc = 14.0;
|
---|
[911] | 287 | //// end test
|
---|
| 288 |
|
---|
[910] | 289 | _GPSweeks += _gps_utc;
|
---|
[884] | 290 | }
|
---|
| 291 | else if (ii == 2) {
|
---|
[886] | 292 | in >>_x_pos >> _x_velocity >> _x_acceleration >> _health;
|
---|
[884] | 293 | }
|
---|
| 294 | else if (ii == 3) {
|
---|
| 295 | in >>_y_pos >> _y_velocity >> _y_acceleration >> _frequency_number;
|
---|
| 296 | }
|
---|
| 297 | else if (ii == 4) {
|
---|
| 298 | in >>_z_pos >> _z_velocity >> _z_acceleration >> _E;
|
---|
| 299 | }
|
---|
| 300 | }
|
---|
| 301 |
|
---|
[888] | 302 | // Initialize status vector
|
---|
| 303 | // ------------------------
|
---|
[901] | 304 | _tt = _GPSweeks;
|
---|
[888] | 305 |
|
---|
[893] | 306 | _xv(1) = _x_pos * 1.e3;
|
---|
| 307 | _xv(2) = _y_pos * 1.e3;
|
---|
| 308 | _xv(3) = _z_pos * 1.e3;
|
---|
| 309 | _xv(4) = _x_velocity * 1.e3;
|
---|
| 310 | _xv(5) = _y_velocity * 1.e3;
|
---|
| 311 | _xv(6) = _z_velocity * 1.e3;
|
---|
[884] | 312 | }
|
---|
| 313 |
|
---|
| 314 | // Derivative of the state vector using a simple force model (static)
|
---|
| 315 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 316 | ColumnVector t_ephGlo::glo_deriv(double /* tt */, const ColumnVector& xv) {
|
---|
| 317 |
|
---|
| 318 | // State vector components
|
---|
| 319 | // -----------------------
|
---|
| 320 | ColumnVector rr = xv.rows(1,3);
|
---|
| 321 | ColumnVector vv = xv.rows(4,6);
|
---|
| 322 |
|
---|
| 323 | // Acceleration
|
---|
| 324 | // ------------
|
---|
| 325 | const static double GM = 398.60044e12;
|
---|
| 326 | const static double AE = 6378136.0;
|
---|
| 327 | const static double OMEGA = 7292115.e-11;
|
---|
| 328 | const static double C20 = -1082.63e-6;
|
---|
| 329 |
|
---|
| 330 | double rho = rr.norm_Frobenius();
|
---|
| 331 | double t1 = -GM/(rho*rho*rho);
|
---|
| 332 | double t2 = 3.0/2.0 * C20 * (GM*AE*AE) / (rho*rho*rho*rho*rho);
|
---|
| 333 | double t3 = OMEGA * OMEGA;
|
---|
| 334 | double t4 = 2.0 * OMEGA;
|
---|
| 335 | double z2 = rr(3) * rr(3);
|
---|
| 336 |
|
---|
| 337 | // Vector of derivatives
|
---|
| 338 | // ---------------------
|
---|
| 339 | ColumnVector va(6);
|
---|
| 340 | va(1) = vv(1);
|
---|
| 341 | va(2) = vv(2);
|
---|
| 342 | va(3) = vv(3);
|
---|
| 343 | va(4) = (t1 + t2*(1.0-5.0*z2/(rho*rho)) + t3) * rr(1) + t4*vv(2);
|
---|
| 344 | va(5) = (t1 + t2*(1.0-5.0*z2/(rho*rho)) + t3) * rr(2) - t4*vv(1);
|
---|
| 345 | va(6) = (t1 + t2*(3.0-5.0*z2/(rho*rho)) ) * rr(3);
|
---|
| 346 |
|
---|
| 347 | return va;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
[889] | 350 | // Compute Glonass Satellite Position
|
---|
[884] | 351 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 352 | void t_ephGlo::position(int GPSweek, double GPSweeks, ColumnVector& xc,
|
---|
| 353 | ColumnVector& vv) const {
|
---|
| 354 |
|
---|
[899] | 355 | const static double secPerWeek = 7 * 86400.0;
|
---|
| 356 | const static double nominalStep = 10.0;
|
---|
[891] | 357 |
|
---|
[898] | 358 | double dtPos = GPSweeks - _tt;
|
---|
[891] | 359 | if (GPSweek != _GPSweek) {
|
---|
[898] | 360 | dtPos += (GPSweek - _GPSweek) * secPerWeek;
|
---|
[891] | 361 | }
|
---|
| 362 |
|
---|
[899] | 363 | int nSteps = int(fabs(dtPos) / nominalStep) + 1;
|
---|
| 364 | double step = dtPos / nSteps;
|
---|
| 365 |
|
---|
| 366 | for (int ii = 1; ii <= nSteps; ii++) {
|
---|
[900] | 367 | _xv = rungeKutta4(_tt, _xv, step, glo_deriv);
|
---|
[899] | 368 | _tt += step;
|
---|
[891] | 369 | }
|
---|
| 370 |
|
---|
[892] | 371 | // Position and Velocity
|
---|
| 372 | // ---------------------
|
---|
[891] | 373 | xc(1) = _xv(1);
|
---|
| 374 | xc(2) = _xv(2);
|
---|
| 375 | xc(3) = _xv(3);
|
---|
| 376 |
|
---|
| 377 | vv(1) = _xv(4);
|
---|
| 378 | vv(2) = _xv(5);
|
---|
| 379 | vv(3) = _xv(6);
|
---|
[892] | 380 |
|
---|
| 381 | // Clock Correction
|
---|
| 382 | // ----------------
|
---|
| 383 | double dtClk = GPSweeks - _GPSweeks;
|
---|
| 384 | if (GPSweek != _GPSweek) {
|
---|
| 385 | dtClk += (GPSweek - _GPSweek) * secPerWeek;
|
---|
| 386 | }
|
---|
[910] | 387 | xc(4) = -_tau + _gamma * dtClk;
|
---|
[884] | 388 | }
|
---|
| 389 |
|
---|
[926] | 390 | // Glonass IOD
|
---|
| 391 | ////////////////////////////////////////////////////////////////////////////
|
---|
| 392 | int t_ephGlo::IOD() const {
|
---|
| 393 | //// return int(fmod(_GPSweeks,86400.0)) / 600;
|
---|
| 394 | unsigned int tb = int(fmod(_GPSweeks,86400.0)) * 1000; // msec of day
|
---|
| 395 |
|
---|
| 396 | // 5 LSBs of iod are equal to 5 LSBs of tb, remaining bits are zero
|
---|
| 397 | unsigned int iod = tb << 3;
|
---|
| 398 | return (iod >> 3);
|
---|
| 399 | }
|
---|