-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
pprz_geodetic_int.c
339 lines (282 loc) · 12.3 KB
/
pprz_geodetic_int.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Copyright (C) 2008-2009 Antoine Drouin <poinix@gmail.com>
* 2009-2014 Gautier Hattenberger <gautier.hattenberger@enac.fr>
* 2010-2014 Felix Ruess <felix.ruess@gmail.com>
*
* This file is part of paparazzi.
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*/
/**
* @file pprz_geodetic_int.c
* @brief Paparazzi fixed point math for geodetic calculations.
*
*
*/
#include "pprz_geodetic_int.h"
#include "pprz_algebra_int.h"
void ltp_of_ecef_rmat_from_lla_i(struct Int32RMat* ltp_of_ecef, struct LlaCoor_i* lla) {
#if USE_DOUBLE_PRECISION_TRIG
int32_t sin_lat = rint(BFP_OF_REAL(sin(RAD_OF_EM7DEG((double)lla->lat)), HIGH_RES_TRIG_FRAC));
int32_t cos_lat = rint(BFP_OF_REAL(cos(RAD_OF_EM7DEG((double)lla->lat)), HIGH_RES_TRIG_FRAC));
int32_t sin_lon = rint(BFP_OF_REAL(sin(RAD_OF_EM7DEG((double)lla->lon)), HIGH_RES_TRIG_FRAC));
int32_t cos_lon = rint(BFP_OF_REAL(cos(RAD_OF_EM7DEG((double)lla->lon)), HIGH_RES_TRIG_FRAC));
#else
int32_t sin_lat = rint(BFP_OF_REAL(sinf(RAD_OF_EM7DEG((float)lla->lat)), HIGH_RES_TRIG_FRAC));
int32_t cos_lat = rint(BFP_OF_REAL(cosf(RAD_OF_EM7DEG((float)lla->lat)), HIGH_RES_TRIG_FRAC));
int32_t sin_lon = rint(BFP_OF_REAL(sinf(RAD_OF_EM7DEG((float)lla->lon)), HIGH_RES_TRIG_FRAC));
int32_t cos_lon = rint(BFP_OF_REAL(cosf(RAD_OF_EM7DEG((float)lla->lon)), HIGH_RES_TRIG_FRAC));
#endif
ltp_of_ecef->m[0] = -sin_lon;
ltp_of_ecef->m[1] = cos_lon;
ltp_of_ecef->m[2] = 0; /* this element is always zero http://en.wikipedia.org/wiki/Geodetic_system#From_ECEF_to_ENU */
ltp_of_ecef->m[3] = (int32_t)((-(int64_t)sin_lat*(int64_t)cos_lon)>>HIGH_RES_TRIG_FRAC);
ltp_of_ecef->m[4] = (int32_t)((-(int64_t)sin_lat*(int64_t)sin_lon)>>HIGH_RES_TRIG_FRAC);
ltp_of_ecef->m[5] = cos_lat;
ltp_of_ecef->m[6] = (int32_t)(( (int64_t)cos_lat*(int64_t)cos_lon)>>HIGH_RES_TRIG_FRAC);
ltp_of_ecef->m[7] = (int32_t)(( (int64_t)cos_lat*(int64_t)sin_lon)>>HIGH_RES_TRIG_FRAC);
ltp_of_ecef->m[8] = sin_lat;
}
void ltp_def_from_ecef_i(struct LtpDef_i* def, struct EcefCoor_i* ecef) {
/* store the origin of the tangeant plane */
VECT3_COPY(def->ecef, *ecef);
/* compute the lla representation of the origin */
lla_of_ecef_i(&def->lla, &def->ecef);
/* store the rotation matrix */
ltp_of_ecef_rmat_from_lla_i(&def->ltp_of_ecef, &def->lla);
}
void ltp_def_from_lla_i(struct LtpDef_i* def, struct LlaCoor_i* lla) {
/* store the origin of the tangeant plane */
LLA_COPY(def->lla, *lla);
/* compute the ecef representation of the origin */
ecef_of_lla_i(&def->ecef, &def->lla);
/* store the rotation matrix */
ltp_of_ecef_rmat_from_lla_i(&def->ltp_of_ecef, &def->lla);
}
/** Convert a point from ECEF to local ENU.
* @param[out] enu ENU point in cm
* @param[in] def local coordinate system definition
* @param[in] ecef ECEF point in cm
*/
void enu_of_ecef_point_i(struct EnuCoor_i* enu, struct LtpDef_i* def, struct EcefCoor_i* ecef) {
struct EcefCoor_i delta;
VECT3_DIFF(delta, *ecef, def->ecef);
const int64_t tmpx = (int64_t)def->ltp_of_ecef.m[0]*delta.x +
(int64_t)def->ltp_of_ecef.m[1]*delta.y +
0; /* this element is always zero http://en.wikipedia.org/wiki/Geodetic_system#From_ECEF_to_ENU */
enu->x = (int32_t)(tmpx>>HIGH_RES_TRIG_FRAC);
const int64_t tmpy = (int64_t)def->ltp_of_ecef.m[3]*delta.x +
(int64_t)def->ltp_of_ecef.m[4]*delta.y +
(int64_t)def->ltp_of_ecef.m[5]*delta.z;
enu->y = (int32_t)(tmpy>>HIGH_RES_TRIG_FRAC);
const int64_t tmpz = (int64_t)def->ltp_of_ecef.m[6]*delta.x +
(int64_t)def->ltp_of_ecef.m[7]*delta.y +
(int64_t)def->ltp_of_ecef.m[8]*delta.z;
enu->z = (int32_t)(tmpz>>HIGH_RES_TRIG_FRAC);
}
/** Convert a point from ECEF to local NED.
* @param[out] ned NED point in cm
* @param[in] def local coordinate system definition
* @param[in] ecef ECEF point in cm
*/
void ned_of_ecef_point_i(struct NedCoor_i* ned, struct LtpDef_i* def, struct EcefCoor_i* ecef) {
struct EnuCoor_i enu;
enu_of_ecef_point_i(&enu, def, ecef);
ENU_OF_TO_NED(*ned, enu);
}
/** Convert a ECEF position to local ENU.
* @param[out] enu ENU position in meter << #INT32_POS_FRAC
* @param[in] def local coordinate system definition
* @param[in] ecef ECEF position in cm
*/
void enu_of_ecef_pos_i(struct EnuCoor_i* enu, struct LtpDef_i* def, struct EcefCoor_i* ecef) {
struct EnuCoor_i enu_cm;
enu_of_ecef_point_i(&enu_cm, def, ecef);
/* enu = (enu_cm / 100) << INT32_POS_FRAC
* to loose less range:
* enu_cm = enu << (INT32_POS_FRAC-2) / 25
* which puts max enu output Q23.8 range to 8388km / 25 = 335km
*/
INT32_VECT3_LSHIFT(*enu, enu_cm, INT32_POS_FRAC-2);
VECT3_SDIV(*enu, *enu, 25);
}
/** Convert a ECEF position to local NED.
* @param[out] ned NED position in meter << #INT32_POS_FRAC
* @param[in] def local coordinate system definition
* @param[in] ecef ECEF position in cm
*/
void ned_of_ecef_pos_i(struct NedCoor_i* ned, struct LtpDef_i* def, struct EcefCoor_i* ecef) {
struct EnuCoor_i enu;
enu_of_ecef_pos_i(&enu, def, ecef);
ENU_OF_TO_NED(*ned, enu);
}
/** Rotate a vector from ECEF to ENU.
* @param[out] enu vector in ENU coordinate system
* @param[in] def local coordinate system definition
* @param[in] ecef vector in ECEF coordinate system
*/
void enu_of_ecef_vect_i(struct EnuCoor_i* enu, struct LtpDef_i* def, struct EcefCoor_i* ecef) {
const int64_t tmpx = (int64_t)def->ltp_of_ecef.m[0]*ecef->x +
(int64_t)def->ltp_of_ecef.m[1]*ecef->y +
0; /* this element is always zero http://en.wikipedia.org/wiki/Geodetic_system#From_ECEF_to_ENU */
enu->x = (int32_t)(tmpx>>HIGH_RES_TRIG_FRAC);
const int64_t tmpy = (int64_t)def->ltp_of_ecef.m[3]*ecef->x +
(int64_t)def->ltp_of_ecef.m[4]*ecef->y +
(int64_t)def->ltp_of_ecef.m[5]*ecef->z;
enu->y = (int32_t)(tmpy>>HIGH_RES_TRIG_FRAC);
const int64_t tmpz = (int64_t)def->ltp_of_ecef.m[6]*ecef->x +
(int64_t)def->ltp_of_ecef.m[7]*ecef->y +
(int64_t)def->ltp_of_ecef.m[8]*ecef->z;
enu->z = (int32_t)(tmpz>>HIGH_RES_TRIG_FRAC);
}
/** Rotate a vector from ECEF to NED.
* @param[out] ned vector in NED coordinate system
* @param[in] def local coordinate system definition
* @param[in] ecef vector in ECEF coordinate system
*/
void ned_of_ecef_vect_i(struct NedCoor_i* ned, struct LtpDef_i* def, struct EcefCoor_i* ecef) {
struct EnuCoor_i enu;
enu_of_ecef_vect_i(&enu, def, ecef);
ENU_OF_TO_NED(*ned, enu);
}
/** Rotate a vector from ENU to ECEF.
* @param[out] ecef vector in ECEF coordinate system
* @param[in] def local coordinate system definition
* @param[in] enu vector in ENU coordinate system
*/
void ecef_of_enu_vect_i(struct EcefCoor_i* ecef, struct LtpDef_i* def, struct EnuCoor_i* enu) {
const int64_t tmpx = (int64_t)def->ltp_of_ecef.m[0] * enu->x +
(int64_t)def->ltp_of_ecef.m[3] * enu->y +
(int64_t)def->ltp_of_ecef.m[6] * enu->z;
ecef->x = (int32_t)(tmpx>>HIGH_RES_TRIG_FRAC);
const int64_t tmpy = (int64_t)def->ltp_of_ecef.m[1] * enu->x +
(int64_t)def->ltp_of_ecef.m[4] * enu->y +
(int64_t)def->ltp_of_ecef.m[7] * enu->z;
ecef->y = (int32_t)(tmpy>>HIGH_RES_TRIG_FRAC);
/* first element is always zero http://en.wikipedia.org/wiki/Geodetic_system#From_ENU_to_ECEF */
const int64_t tmpz = (int64_t)def->ltp_of_ecef.m[5] * enu->y +
(int64_t)def->ltp_of_ecef.m[8] * enu->z;
ecef->z = (int32_t)(tmpz>>HIGH_RES_TRIG_FRAC);
}
/** Rotate a vector from NED to ECEF.
* @param[out] ecef vector in ECEF coordinate system
* @param[in] def local coordinate system definition
* @param[in] ned vector in NED coordinate system
*/
void ecef_of_ned_vect_i(struct EcefCoor_i* ecef, struct LtpDef_i* def, struct NedCoor_i* ned) {
struct EnuCoor_i enu;
ENU_OF_TO_NED(enu, *ned);
ecef_of_enu_vect_i(ecef, def, &enu);
}
/** Convert a point in local ENU to ECEF.
* @param[out] ecef ECEF point in cm
* @param[in] def local coordinate system definition
* @param[in] enu ENU point in cm
*/
void ecef_of_enu_point_i(struct EcefCoor_i* ecef, struct LtpDef_i* def, struct EnuCoor_i* enu) {
ecef_of_enu_vect_i(ecef, def, enu);
INT32_VECT3_ADD(*ecef, def->ecef);
}
/** Convert a point in local NED to ECEF.
* @param[out] ecef ECEF point in cm
* @param[in] def local coordinate system definition
* @param[in] ned NED point in cm
*/
void ecef_of_ned_point_i(struct EcefCoor_i* ecef, struct LtpDef_i* def, struct NedCoor_i* ned) {
struct EnuCoor_i enu;
ENU_OF_TO_NED(enu, *ned);
ecef_of_enu_point_i(ecef, def, &enu);
}
/** Convert a local ENU position to ECEF.
* @param[out] ecef ECEF position in cm
* @param[in] def local coordinate system definition
* @param[in] enu ENU position in meter << #INT32_POS_FRAC
*/
void ecef_of_enu_pos_i(struct EcefCoor_i* ecef, struct LtpDef_i* def, struct EnuCoor_i* enu) {
/* enu_cm = (enu * 100) >> INT32_POS_FRAC
* to loose less range:
* enu_cm = (enu * 25) >> (INT32_POS_FRAC-2)
* which puts max enu input Q23.8 range to 8388km / 25 = 335km
*/
struct EnuCoor_i enu_cm;
VECT3_SMUL(enu_cm, *enu, 25);
INT32_VECT3_RSHIFT(enu_cm, enu_cm, INT32_POS_FRAC-2);
ecef_of_enu_vect_i(ecef, def, &enu_cm);
INT32_VECT3_ADD(*ecef, def->ecef);
}
/** Convert a local NED position to ECEF.
* @param[out] ecef ECEF position in cm
* @param[in] def local coordinate system definition
* @param[in] ned NED position in meter << #INT32_POS_FRAC
*/
void ecef_of_ned_pos_i(struct EcefCoor_i* ecef, struct LtpDef_i* def, struct NedCoor_i* ned) {
struct EnuCoor_i enu;
ENU_OF_TO_NED(enu, *ned);
ecef_of_enu_pos_i(ecef, def, &enu);
}
void enu_of_lla_point_i(struct EnuCoor_i* enu, struct LtpDef_i* def, struct LlaCoor_i* lla) {
struct EcefCoor_i ecef;
ecef_of_lla_i(&ecef,lla);
enu_of_ecef_point_i(enu,def,&ecef);
}
void ned_of_lla_point_i(struct NedCoor_i* ned, struct LtpDef_i* def, struct LlaCoor_i* lla) {
struct EcefCoor_i ecef;
ecef_of_lla_i(&ecef,lla);
ned_of_ecef_point_i(ned,def,&ecef);
}
void enu_of_lla_vect_i(struct EnuCoor_i* enu, struct LtpDef_i* def, struct LlaCoor_i* lla) {
struct EcefCoor_i ecef;
ecef_of_lla_i(&ecef,lla);
enu_of_ecef_vect_i(enu,def,&ecef);
}
void ned_of_lla_vect_i(struct NedCoor_i* ned, struct LtpDef_i* def, struct LlaCoor_i* lla) {
struct EcefCoor_i ecef;
ecef_of_lla_i(&ecef,lla);
ned_of_ecef_vect_i(ned,def,&ecef);
}
/*
For now we cheat and call the floating point version
Anyone up for writing it in fixed point ?
*/
#include "pprz_geodetic_float.h"
#include "pprz_geodetic_double.h"
void lla_of_ecef_i(struct LlaCoor_i* out, struct EcefCoor_i* in) {
/* convert our input to floating point */
struct EcefCoor_d in_d;
in_d.x = M_OF_CM((double)in->x);
in_d.y = M_OF_CM((double)in->y);
in_d.z = M_OF_CM((double)in->z);
/* calls the floating point transformation */
struct LlaCoor_d out_d;
lla_of_ecef_d(&out_d, &in_d);
/* convert the output to fixed point */
out->lon = (int32_t)rint(EM7DEG_OF_RAD(out_d.lon));
out->lat = (int32_t)rint(EM7DEG_OF_RAD(out_d.lat));
out->alt = (int32_t)MM_OF_M(out_d.alt);
}
void ecef_of_lla_i(struct EcefCoor_i* out, struct LlaCoor_i* in) {
/* convert our input to floating point */
struct LlaCoor_d in_d;
in_d.lon = RAD_OF_EM7DEG((double)in->lon);
in_d.lat = RAD_OF_EM7DEG((double)in->lat);
in_d.alt = M_OF_MM((double)in->alt);
/* calls the floating point transformation */
struct EcefCoor_d out_d;
ecef_of_lla_d(&out_d, &in_d);
/* convert the output to fixed point */
out->x = (int32_t)CM_OF_M(out_d.x);
out->y = (int32_t)CM_OF_M(out_d.y);
out->z = (int32_t)CM_OF_M(out_d.z);
}