28 #define _USE_MATH_DEFINES
35 #include <physical_constants.h>
39 const double CoordinateConversions::R_EQUATOR = 6378137.0;
40 const double CoordinateConversions::ECCENTRICITY = 8.1819190842621e-2;
55 sinLat=(float)sin(
DEG2RAD*LLA[0]);
56 sinLon=(float)sin(
DEG2RAD*LLA[1]);
57 cosLat=(float)cos(
DEG2RAD*LLA[0]);
58 cosLon=(float)cos(
DEG2RAD*LLA[1]);
71 const double a = R_EQUATOR;
72 const double e = ECCENTRICITY;
81 N = a / sqrt(1.0 - e*e*sinLat*sinLat);
83 ECEF[0] = (N+LLA[2])*cosLat*cosLon;
84 ECEF[1] = (N+LLA[2])*cosLat*sinLon;
85 ECEF[2] = ((1-e*
e)*N + LLA[2]) *
sinLat;
95 const double a = R_EQUATOR;
96 const double e = ECCENTRICITY;
97 double x=ECEF[0],
y=ECEF[1],
z=ECEF[2];
108 while (((delta > 1.0e-14)||(delta < -1.0e-14)) && (iter < 100))
110 delta = Lat - atan(z / (sqrt(x*x + y*y)*(1-(N*e*e/NplusH))));
113 N = a / sqrt(1 - esLat*esLat);
114 NplusH = sqrt(x*x + y*y)/cos(Lat);
121 if (iter==500)
return (0);
146 for(i = 0; i < 3; i++)
147 ECEF[i] = BaseECEF[i] + Rne[0][i]*NED[0] + Rne[1][i]*NED[1] + Rne[2][i]*NED[2];
167 T[0] = homeLLA[2]+6.378137E6f * M_PI / 180.0;
168 T[1] = cosf(homeLLA[0] * M_PI / 180.0)*(homeLLA[2]+6.378137E6f) * M_PI / 180.0;
171 LLA[0] = homeLLA[0] + NED[0] / T[0];
172 LLA[1] = homeLLA[1] + NED[1] / T[1];
173 LLA[2] = homeLLA[2] + NED[2] / T[2];
195 diff[0] = ECEF[0] - BaseECEF[0];
196 diff[1] = ECEF[1] - BaseECEF[1];
197 diff[2] = ECEF[2] - BaseECEF[2];
199 NED[0] = Rne[0][0] * diff[0] + Rne[0][1] * diff[1] + Rne[0][2] * diff[2];
200 NED[1] = Rne[1][0] * diff[0] + Rne[1][1] * diff[1] + Rne[1][2] * diff[2];
201 NED[2] = Rne[2][0] * diff[0] + Rne[2][1] * diff[1] + Rne[2][2] * diff[2];
215 double lat = homeLLA[0] *
DEG2RAD;
216 double alt = homeLLA[2];
219 T[0] = alt+6.378137E6;
220 T[1] = cos(lat)*(alt+6.378137E6);
224 dL[0] = (float)((LLA[0] - homeLLA[0]) *
DEG2RAD);
225 dL[1] = (float)((LLA[1] - homeLLA[1]) *
DEG2RAD);
226 dL[2] = (float)(LLA[2] - homeLLA[2]);
228 NED[0] = T[0] * dL[0];
229 NED[1] = T[1] * dL[1];
230 NED[2] = T[2] * dL[2];
236 float R13, R11, R12, R23, R33;
237 float q0s = q[0] * q[0];
238 float q1s = q[1] * q[1];
239 float q2s = q[2] * q[2];
240 float q3s = q[3] * q[3];
242 R13 = 2 * (q[1] * q[3] - q[0] * q[2]);
243 R11 = q0s + q1s - q2s - q3s;
244 R12 = 2 * (q[1] * q[2] + q[0] * q[3]);
245 R23 = 2 * (q[2] * q[3] + q[0] * q[1]);
246 R33 = q0s - q1s - q2s + q3s;
248 rpy[1] =
RAD2DEG * asinf(-R13);
249 rpy[2] =
RAD2DEG * atan2f(R12, R11);
250 rpy[0] =
RAD2DEG * atan2f(R23, R33);
258 float phi, theta, psi;
259 float cphi, sphi, ctheta, stheta, cpsi, spsi;
266 ctheta = cosf(theta);
267 stheta = sinf(theta);
271 q[0] = cphi * ctheta * cpsi + sphi * stheta * spsi;
272 q[1] = sphi * ctheta * cpsi - cphi * stheta * spsi;
273 q[2] = cphi * stheta * cpsi + sphi * ctheta * spsi;
274 q[3] = cphi * ctheta * spsi - sphi * stheta * cpsi;
288 float q0s = q[0] * q[0], q1s = q[1] * q[1], q2s = q[2] * q[2], q3s = q[3] * q[3];
290 Rbe[0][0] = q0s + q1s - q2s - q3s;
291 Rbe[0][1] = 2 * (q[1] * q[2] + q[0] * q[3]);
292 Rbe[0][2] = 2 * (q[1] * q[3] - q[0] * q[2]);
293 Rbe[1][0] = 2 * (q[1] * q[2] - q[0] * q[3]);
294 Rbe[1][1] = q0s - q1s + q2s - q3s;
295 Rbe[1][2] = 2 * (q[2] * q[3] + q[0] * q[1]);
296 Rbe[2][0] = 2 * (q[1] * q[3] + q[0] * q[2]);
297 Rbe[2][1] = 2 * (q[2] * q[3] - q[0] * q[1]);
298 Rbe[2][2] = q0s - q1s - q2s + q3s;
307 w = sqrt(std::max(0.0, 1.0 + Rbe[0][0] + Rbe[1][1] + Rbe[2][2])) / 2.0;
308 x = sqrt(std::max(0.0, 1.0 + Rbe[0][0] - Rbe[1][1] - Rbe[2][2])) / 2.0;
309 y = sqrt(std::max(0.0, 1.0 - Rbe[0][0] + Rbe[1][1] - Rbe[2][2])) / 2.0;
310 z = sqrt(std::max(0.0, 1.0 - Rbe[0][0] - Rbe[1][1] + Rbe[2][2])) / 2.0;
312 x = copysign(x, (Rbe[1][2] - Rbe[2][1]));
313 y = copysign(y, (Rbe[2][0] - Rbe[0][2]));
314 z = copysign(z, (Rbe[0][1] - Rbe[1][0]));
void LLA2ECEF(double LLA[3], double ECEF[3])
void RPY2Quaternion(const float rpy[3], float q[4])
axis equal end function NED
void LLA2NED_HomeLLA(double LLA[3], double homeLLA[3], double NED[3])
int ECEF2LLA(double ECEF[3], double LLA[3])
void LLA2NED_HomeECEF(double LLA[3], double homeECEF[3], double Rne[3][3], double NED[3])
int NED2LLA_HomeLLA(double homeLLA[3], double NED[3], double LLA[3])
void Quaternion2R(const float q[4], float Rbe[3][3])
void Quaternion2RPY(const float q[4], float rpy[3])
void LLA2Rne(double LLA[3], double Rne[3][3])
void R2Quaternion(float const Rbe[3][3], float q[4])
int NED2LLA_HomeECEF(double BaseECEF[3], double NED[3], double LLA[3])