1 | /* -------------------------------------------------------------------------
|
---|
2 | * BKG NTRIP Server
|
---|
3 | * -------------------------------------------------------------------------
|
---|
4 | *
|
---|
5 | * Class: bncutils
|
---|
6 | *
|
---|
7 | * Purpose: Auxiliary Functions
|
---|
8 | *
|
---|
9 | * Author: L. Mervart
|
---|
10 | *
|
---|
11 | * Created: 08-Apr-2008
|
---|
12 | *
|
---|
13 | * Changes:
|
---|
14 | *
|
---|
15 | * -----------------------------------------------------------------------*/
|
---|
16 |
|
---|
17 | #include <iostream>
|
---|
18 | #include <ctime>
|
---|
19 | #include <math.h>
|
---|
20 |
|
---|
21 | #include <QRegExp>
|
---|
22 | #include <QStringList>
|
---|
23 | #include <QDateTime>
|
---|
24 |
|
---|
25 | #include "bnsutils.h"
|
---|
26 | #include "bnseph.h"
|
---|
27 |
|
---|
28 | using namespace std;
|
---|
29 |
|
---|
30 | //
|
---|
31 | ////////////////////////////////////////////////////////////////////////////
|
---|
32 | void expandEnvVar(QString& str) {
|
---|
33 |
|
---|
34 | QRegExp rx("(\\$\\{.+\\})");
|
---|
35 |
|
---|
36 | if (rx.indexIn(str) != -1) {
|
---|
37 | QStringListIterator it(rx.capturedTexts());
|
---|
38 | if (it.hasNext()) {
|
---|
39 | QString rxStr = it.next();
|
---|
40 | QString envVar = rxStr.mid(2,rxStr.length()-3);
|
---|
41 | str.replace(rxStr, qgetenv(envVar.toAscii()));
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | }
|
---|
46 |
|
---|
47 | //
|
---|
48 | ////////////////////////////////////////////////////////////////////////////
|
---|
49 | QDateTime dateAndTimeFromGPSweek(int GPSWeek, double GPSWeeks) {
|
---|
50 |
|
---|
51 | static const QDate zeroEpoch(1980, 1, 6);
|
---|
52 |
|
---|
53 | QDate date(zeroEpoch);
|
---|
54 | QTime time(0,0,0,0);
|
---|
55 |
|
---|
56 | int weekDays = int(GPSWeeks) / 86400;
|
---|
57 | date = date.addDays( GPSWeek * 7 + weekDays );
|
---|
58 | time = time.addMSecs( int( (GPSWeeks - 86400 * weekDays) * 1e3 ) );
|
---|
59 |
|
---|
60 | return QDateTime(date,time);
|
---|
61 | }
|
---|
62 |
|
---|
63 | //
|
---|
64 | ////////////////////////////////////////////////////////////////////////////
|
---|
65 | void GPSweekFromDateAndTime(const QDateTime& dateTime,
|
---|
66 | int& GPSWeek, double& GPSWeeks) {
|
---|
67 |
|
---|
68 | static const QDateTime zeroEpoch(QDate(1980, 1, 6));
|
---|
69 |
|
---|
70 | GPSWeek = zeroEpoch.daysTo(dateTime) / 7;
|
---|
71 |
|
---|
72 | int weekDay = dateTime.date().dayOfWeek() + 1; // Qt: Monday = 1
|
---|
73 | if (weekDay > 7) weekDay = 1;
|
---|
74 |
|
---|
75 | GPSWeeks = (weekDay - 1) * 86400.0
|
---|
76 | - dateTime.time().msecsTo(QTime()) / 1e3;
|
---|
77 | }
|
---|
78 |
|
---|
79 | //
|
---|
80 | ////////////////////////////////////////////////////////////////////////////
|
---|
81 | void currentGPSWeeks(int& week, double& sec) {
|
---|
82 |
|
---|
83 | QDateTime currDateTime = QDateTime::currentDateTime().toUTC();
|
---|
84 | QDate currDate = currDateTime.date();
|
---|
85 | QTime currTime = currDateTime.time();
|
---|
86 |
|
---|
87 | week = int( (double(currDate.toJulianDay()) - 2444244.5) / 7 );
|
---|
88 |
|
---|
89 | sec = (currDate.dayOfWeek() % 7) * 24.0 * 3600.0 +
|
---|
90 | currTime.hour() * 3600.0 +
|
---|
91 | currTime.minute() * 60.0 +
|
---|
92 | currTime.second() +
|
---|
93 | currTime.msec() / 1000.0;
|
---|
94 | }
|
---|
95 |
|
---|
96 | // Satellite Position computed using broadcast ephemeris
|
---|
97 | ////////////////////////////////////////////////////////////////////////////
|
---|
98 | void satellitePosition(int GPSweek, double GPSweeks, const gpsEph* ep,
|
---|
99 | double& X, double& Y, double& Z, double& dt,
|
---|
100 | double& vX, double& vY, double& vZ) {
|
---|
101 |
|
---|
102 | const static double secPerWeek = 7 * 86400.0;
|
---|
103 | const static double omegaEarth = 7292115.1467e-11;
|
---|
104 | const static double gmWGS = 398.6005e12;
|
---|
105 |
|
---|
106 | X = Y = Z = dt = 0.0;
|
---|
107 |
|
---|
108 | double a0 = ep->sqrt_A * ep->sqrt_A;
|
---|
109 | if (a0 == 0) {
|
---|
110 | return;
|
---|
111 | }
|
---|
112 |
|
---|
113 | double n0 = sqrt(gmWGS/(a0*a0*a0));
|
---|
114 | double tk = GPSweeks - ep->TOE;
|
---|
115 | if (GPSweek != ep->GPSweek) {
|
---|
116 | tk += (GPSweek - ep->GPSweek) * secPerWeek;
|
---|
117 | }
|
---|
118 | double n = n0 + ep->Delta_n;
|
---|
119 | double M = ep->M0 + n*tk;
|
---|
120 | double E = M;
|
---|
121 | double E_last;
|
---|
122 | do {
|
---|
123 | E_last = E;
|
---|
124 | E = M + ep->e*sin(E);
|
---|
125 | } while ( fabs(E-E_last)*a0 > 0.001 );
|
---|
126 | double v = 2.0*atan( sqrt( (1.0 + ep->e)/(1.0 - ep->e) )*tan( E/2 ) );
|
---|
127 | double u0 = v + ep->omega;
|
---|
128 | double sin2u0 = sin(2*u0);
|
---|
129 | double cos2u0 = cos(2*u0);
|
---|
130 | double r = a0*(1 - ep->e*cos(E)) + ep->Crc*cos2u0 + ep->Crs*sin2u0;
|
---|
131 | double i = ep->i0 + ep->IDOT*tk + ep->Cic*cos2u0 + ep->Cis*sin2u0;
|
---|
132 | double u = u0 + ep->Cuc*cos2u0 + ep->Cus*sin2u0;
|
---|
133 | double xp = r*cos(u);
|
---|
134 | double yp = r*sin(u);
|
---|
135 | double OM = ep->OMEGA0 + (ep->OMEGADOT - omegaEarth)*tk -
|
---|
136 | omegaEarth*ep->TOE;
|
---|
137 |
|
---|
138 | double sinom = sin(OM);
|
---|
139 | double cosom = cos(OM);
|
---|
140 | double sini = sin(i);
|
---|
141 | double cosi = cos(i);
|
---|
142 | X = xp*cosom - yp*cosi*sinom;
|
---|
143 | Y = xp*sinom + yp*cosi*cosom;
|
---|
144 | Z = yp*sini;
|
---|
145 |
|
---|
146 | double tc = GPSweeks - ep->TOC;
|
---|
147 | if (GPSweek != ep->GPSweek) {
|
---|
148 | tc += (GPSweek - ep->GPSweek) * secPerWeek;
|
---|
149 | }
|
---|
150 | dt = ep->clock_bias + ep->clock_drift*tc + ep->clock_driftrate*tc*tc
|
---|
151 | - 4.442807633e-10 * ep->e * sqrt(a0) *sin(E);
|
---|
152 |
|
---|
153 | // Velocity
|
---|
154 | // --------
|
---|
155 | double tanv2 = tan(v/2);
|
---|
156 | double dEdM = 1 / (1 - ep->e*cos(E));
|
---|
157 | double dotv = sqrt((1.0 + ep->e)/(1.0 - ep->e)) / cos(E/2)/cos(E/2) / (1 + tanv2*tanv2)
|
---|
158 | * dEdM * n;
|
---|
159 | double dotu = dotv + (-ep->Cuc*sin2u0 + ep->Cus*cos2u0)*2*dotv;
|
---|
160 | double dotom = ep->OMEGADOT - omegaEarth;
|
---|
161 | double doti = ep->IDOT + (-ep->Cic*sin2u0 + ep->Cis*cos2u0)*2*dotv;
|
---|
162 | double dotr = a0 * ep->e*sin(E) * dEdM * n
|
---|
163 | + (-ep->Crc*sin2u0 + ep->Crs*cos2u0)*2*dotv;
|
---|
164 | double dotx = dotr*cos(u) - r*sin(u)*dotu;
|
---|
165 | double doty = dotr*sin(u) + r*cos(u)*dotu;
|
---|
166 |
|
---|
167 | vX = cosom *dotx - cosi*sinom *doty // dX / dr
|
---|
168 | - xp*sinom*dotom - yp*cosi*cosom*dotom // dX / dOMEGA
|
---|
169 | + yp*sini*sinom*doti; // dX / di
|
---|
170 |
|
---|
171 | vY = sinom *dotx + cosi*cosom *doty
|
---|
172 | + xp*cosom*dotom - yp*cosi*sinom*dotom
|
---|
173 | - yp*sini*cosom*doti;
|
---|
174 |
|
---|
175 | vZ = sini *doty + yp*cosi *doti;
|
---|
176 | }
|
---|
177 |
|
---|
178 | // Transformation xyz --> radial, along track, out-of-plane
|
---|
179 | ////////////////////////////////////////////////////////////////////////////
|
---|
180 | void XYZ_to_RSW(const ColumnVector& rr, const ColumnVector& vv,
|
---|
181 | const ColumnVector& xyz, ColumnVector& rsw) {
|
---|
182 |
|
---|
183 | ColumnVector cross = crossproduct(rr, vv);
|
---|
184 |
|
---|
185 | rsw.ReSize(3);
|
---|
186 | rsw(1) = DotProduct(xyz, rr) / rr.norm_Frobenius();
|
---|
187 | rsw(2) = DotProduct(xyz, vv) / vv.norm_Frobenius();
|
---|
188 | rsw(3) = DotProduct(xyz, cross) / cross.norm_Frobenius();
|
---|
189 | }
|
---|