Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / flight / libraries / inc / CoordinateConversions.h
blob0ef0a3bc3155d742e2c6e94f7f3ef61a8a32e36e
1 /**
2 ******************************************************************************
4 * @file CoordinateConverions.h
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @brief Header for Coordinate conversions library in CoordinateConversions.c
7 * - all angles in deg
8 * - distances in meters
9 * - altitude above WGS-84 elipsoid
11 * @see The GNU Public License (GPL) Version 3
13 *****************************************************************************/
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 3 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 * for more details.
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #ifndef COORDINATECONVERSIONS_H_
31 #define COORDINATECONVERSIONS_H_
32 #include <math.h>
34 // ****** convert Lat,Lon,Alt to ECEF ************
35 void LLA2ECEF(const int32_t LLAi[3], float ECEF[3]);
37 // ****** convert ECEF to Lat,Lon,Alt *********
38 void ECEF2LLA(const float ECEF[3], int32_t LLA[3]);
40 void RneFromLLA(const int32_t LLAi[3], float Rne[3][3]);
42 // ****** Express LLA in a local NED Base Frame and back ********
43 void LLA2Base(const int32_t LLAi[3], const float BaseECEF[3], float Rne[3][3], float NED[3]);
44 void Base2LLA(const float NED[3], const float BaseECEF[3], float Rne[3][3], int32_t LLAi[3]);
46 // ****** Express ECEF in a local NED Base Frame and back ********
47 void ECEF2Base(const float ECEF[3], const float BaseECEF[3], float Rne[3][3], float NED[3]);
48 void Base2ECEF(const float NED[3], const float BaseECEF[3], float Rne[3][3], float ECEF[3]);
50 // ****** find rotation matrix from rotation vector
51 void Rv2Rot(float Rv[3], float R[3][3]);
53 // ****** find roll, pitch, yaw from quaternion ********
54 void Quaternion2RPY(const float q[4], float rpy[3]);
56 // ****** find quaternion from roll, pitch, yaw ********
57 void RPY2Quaternion(const float rpy[3], float q[4]);
59 // ** Find Rbe, that rotates a vector from earth fixed to body frame, from quaternion **
60 void Quaternion2R(float q[4], float Rbe[3][3]);
62 // ** Find first row of Rbe, that rotates a vector from earth fixed to body frame, from quaternion **
63 // ** This vector corresponds to the fuselage/roll vector xB **
64 void QuaternionC2xB(const float q0, const float q1, const float q2, const float q3, float x[3]);
65 void Quaternion2xB(const float q[4], float x[3]);
67 // ** Find second row of Rbe, that rotates a vector from earth fixed to body frame, from quaternion **
68 // ** This vector corresponds to the spanwise/pitch vector yB **
69 void QuaternionC2yB(const float q0, const float q1, const float q2, const float q3, float y[3]);
70 void Quaternion2yB(const float q[4], float y[3]);
72 // ** Find third row of Rbe, that rotates a vector from earth fixed to body frame, from quaternion **
73 // ** This vector corresponds to the vertical/yaw vector zB **
74 void QuaternionC2zB(const float q0, const float q1, const float q2, const float q3, float z[3]);
75 void Quaternion2zB(const float q[4], float z[3]);
77 // ****** convert Rotation Matrix to Quaternion ********
78 // ****** if R converts from e to b, q is rotation from e to b ****
79 void R2Quaternion(float R[3][3], float q[4]);
81 // ****** Rotation Matrix from Two Vector Directions ********
82 // ****** given two vector directions (v1 and v2) known in two frames (b and e) find Rbe ***
83 // ****** solution is approximate if can't be exact ***
84 uint8_t RotFrom2Vectors(const float v1b[3], const float v1e[3], const float v2b[3], const float v2e[3], float Rbe[3][3]);
86 // ****** Vector Cross Product ********
87 void CrossProduct(const float v1[3], const float v2[3], float result[3]);
89 // ****** Vector Magnitude ********
90 float VectorMagnitude(const float v[3]);
92 void quat_inverse(float q[4]);
93 void quat_copy(const float q[4], float qnew[4]);
94 void quat_mult(const float q1[4], const float q2[4], float qout[4]);
95 void rot_mult(float R[3][3], const float vec[3], float vec_out[3]);
96 /**
97 * matrix_mult_3x3f - perform a multiplication between two 3x3 float matrices
98 * result = a*b
99 * @param a
100 * @param b
101 * @param result
103 static inline void matrix_mult_3x3f(float a[3][3], float b[3][3], float result[3][3])
105 result[0][0] = a[0][0] * b[0][0] + a[1][0] * b[0][1] + a[2][0] * b[0][2];
106 result[0][1] = a[0][1] * b[0][0] + a[1][1] * b[0][1] + a[2][1] * b[0][2];
107 result[0][2] = a[0][2] * b[0][0] + a[1][2] * b[0][1] + a[2][2] * b[0][2];
109 result[1][0] = a[0][0] * b[1][0] + a[1][0] * b[1][1] + a[2][0] * b[1][2];
110 result[1][1] = a[0][1] * b[1][0] + a[1][1] * b[1][1] + a[2][1] * b[1][2];
111 result[1][2] = a[0][2] * b[1][0] + a[1][2] * b[1][1] + a[2][2] * b[1][2];
113 result[2][0] = a[0][0] * b[2][0] + a[1][0] * b[2][1] + a[2][0] * b[2][2];
114 result[2][1] = a[0][1] * b[2][0] + a[1][1] * b[2][1] + a[2][1] * b[2][2];
115 result[2][2] = a[0][2] * b[2][0] + a[1][2] * b[2][1] + a[2][2] * b[2][2];
118 static inline void matrix_inline_scale_3f(float a[3][3], float scale)
120 a[0][0] *= scale;
121 a[0][1] *= scale;
122 a[0][2] *= scale;
124 a[1][0] *= scale;
125 a[1][1] *= scale;
126 a[1][2] *= scale;
128 a[2][0] *= scale;
129 a[2][1] *= scale;
130 a[2][2] *= scale;
133 static inline void rot_about_axis_x(const float rotation, float R[3][3])
135 float s = sinf(rotation);
136 float c = cosf(rotation);
138 R[0][0] = 1;
139 R[0][1] = 0;
140 R[0][2] = 0;
142 R[1][0] = 0;
143 R[1][1] = c;
144 R[1][2] = -s;
146 R[2][0] = 0;
147 R[2][1] = s;
148 R[2][2] = c;
151 static inline void rot_about_axis_y(const float rotation, float R[3][3])
153 float s = sinf(rotation);
154 float c = cosf(rotation);
156 R[0][0] = c;
157 R[0][1] = 0;
158 R[0][2] = s;
160 R[1][0] = 0;
161 R[1][1] = 1;
162 R[1][2] = 0;
164 R[2][0] = -s;
165 R[2][1] = 0;
166 R[2][2] = c;
169 static inline void rot_about_axis_z(const float rotation, float R[3][3])
171 float s = sinf(rotation);
172 float c = cosf(rotation);
174 R[0][0] = c;
175 R[0][1] = -s;
176 R[0][2] = 0;
178 R[1][0] = s;
179 R[1][1] = c;
180 R[1][2] = 0;
182 R[2][0] = 0;
183 R[2][1] = 0;
184 R[2][2] = 1;
187 #endif // COORDINATECONVERSIONS_H_