ISSDK  1.7
IoT Sensing Software Development Kit
orientation.h
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  * that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  * of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  * list of conditions and the following disclaimer in the documentation and/or
16  * other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*! \file orientation.h
36  \brief Functions to convert between various orientation representations
37 
38  Functions to convert between various orientation representations. Also
39  includes functions for manipulating quaternions.
40 */
41 
42 #ifndef ORIENTATION_H
43 #define ORIENTATION_H
44 
45 /// quaternion structure definition
46 typedef struct Quaternion
47 {
48  float q0; ///< scalar component
49  float q1; ///< x vector component
50  float q2; ///< y vector component
51  float q3; ///< z vector component
52 } Quaternion;
53 
54 // function prototypes
55 /// Aerospace NED accelerometer 3DOF tilt function, computing rotation matrix fR
56 void f3DOFTiltNED(
57  float fR[][3], ///< computed rotation matrix (output)
58  float fGc[] ///< calibrated accelerometer input vector
59 );
60 /// Android accelerometer 3DOF tilt function computing, rotation matrix fR
61 void f3DOFTiltAndroid(
62  float fR[][3], ///< computed rotation matrix (output)
63  float fGc[] ///< calibrated accelerometer input vector
64 );
65 /// Windows 8 accelerometer 3DOF tilt function computing, rotation matrix fR
66 void f3DOFTiltWin8(
67  float fR[][3], ///< computed rotation matrix (output)
68  float fGc[] ///< calibrated accelerometer input vector
69 );
70 /// Aerospace NED magnetometer 3DOF flat eCompass function, computing rotation matrix fR
72  float fR[][3], ///< computed rotation matrix (output)
73  float fBc[] ///< calibrated magnetometer reading (input)
74 );
75 /// Android magnetometer 3DOF flat eCompass function, computing rotation matrix fR
77  float fR[][3], ///< computed rotation matrix (output)
78  float fBc[] ///< calibrated magnetometer reading (input)
79 );
80 /// Windows 8 magnetometer 3DOF flat eCompass function, computing rotation matrix fR
82  float fR[][3], ///< computed rotation matrix (output)
83  float fBc[] ///< calibrated magnetometer reading (input)
84 );
85 /// NED: basic 6DOF e-Compass function, computing rotation matrix fR and magnetic inclination angle fDelta
86 void feCompassNED(
87  float fR[][3], ///< computed rotation matrix (output)
88  float *pfDelta, ///< magnetic inclination angle (output)
89  float *pfsinDelta, ///< sin of the inclination angle
90  float *pfcosDelta, ///< cos of the inclination angle
91  float fBc[], ///< calibrated magnetometer vector (input)
92  float fGc[], ///< calibrated accelerometer input vector (input)
93  float *pfmodBc, ///< modulus of the calibrated magnetic vector
94  float *pfmodGc ///< modulus of the calibrated accelerometer vector
95 );
96 /// Android: basic 6DOF e-Compass function, computing rotation matrix fR and magnetic inclination angle fDelta
97 void feCompassAndroid(
98  float fR[][3], ///< computed rotation matrix (output)
99  float *pfDelta, ///< magnetic inclination angle (output)
100  float *pfsinDelta, ///< sin of the inclination angle
101  float *pfcosDelta, ///< cos of the inclination angle
102  float fBc[], ///< calibrated magnetometer reading (input)
103  float fGc[], ///< calibrated accelerometer input vector (input)
104  float *pfmodBc, ///< modulus of the calibrated magnetic vector
105  float *pfmodGc ///< modulus of the calibrated accelerometer vector
106 );
107 /// Win8: basic 6DOF e-Compass function, computing rotation matrix fR and magnetic inclination angle fDelta
108 void feCompassWin8(
109  float fR[][3], ///< computed rotation matrix (output)
110  float *pfDelta, ///< magnetic inclination angle (output)
111  float *pfsinDelta, ///< sin of the inclination angle
112  float *pfcosDelta, ///< cos of the inclination angle
113  float fBc[], ///< calibrated magnetometer reading (input)
114  float fGc[], ///< calibrated accelerometer input vector (input)
115  float *pfmodBc, ///< modulus of the calibrated magnetic vector
116  float *pfmodGc ///< modulus of the calibrated accelerometer vector
117 );
118 /// extract the NED angles in degrees from the NED rotation matrix
120  float R[][3], ///< rotation matrix input
121  float *pfPhiDeg, ///< output: the roll angle range -180.0 <= Phi < 180.0 deg
122  float *pfTheDeg, ///< output: the pitch angle -90.0 <= Theta <= 90.0 deg
123  float *pfPsiDeg, ///< output: the yaw (compass) angle 0.0 <= Psi < 360.0 deg
124  float *pfRhoDeg, ///< output: For NED, the compass heading Rho equals the yaw angle Psi
125  float *pfChiDeg ///< output: the tilt angle from vertical Chi (0 <= Chi <= 180 deg)
126 );
127 /// extract the Android angles in degrees from the Android rotation matrix
129  float R[][3], ///< rotation matrix input
130  float *pfPhiDeg, ///< the roll angle -90.0 <= Phi <= 90.0 deg
131  float *pfTheDeg, ///< the pitch angle -180.0 <= The < 180.0 deg
132  float *pfPsiDeg, ///< yaw angle Psi with range 0.0 <= Psi < 360.0 deg
133  float *pfRhoDeg, ///< the compass heading angle Rho equals the yaw angle Psi
134  float *pfChiDeg ///< the tilt angle from vertical Chi (0 <= Chi <= 180 deg)
135 );
136 /// extract the Windows 8 angles in degrees from the Windows 8 rotation matrix
138  float R[][3], ///< rotation matrix input
139  float *pfPhiDeg, ///< the roll angle -90.0 <= Phi <= 90.0 deg
140  float *pfTheDeg, ///< pitch angle Theta in the range -180.0 <= The < 180.0 deg
141  float *pfPsiDeg, ///< yaw angle Psi in range 0.0 <= Psi < 360.0 deg
142  float *pfRhoDeg, ///< the compass angle Rho = 360 - Psi
143  float *pfChiDeg ///< tilt angle from vertical Chi (0 <= Chi <= 180 deg)
144 );
145 /// compute the orientation quaternion from a 3x3 rotation matrix
147  float R[][3], ///< Rotation matrix (input)
148  Quaternion *pq ///< Quaternion (output)
149 );
150 /// compute the rotation matrix from an orientation quaternion
152  float R[][3], ///< Rotation matrix (output)
153  const Quaternion *pq ///< Quaternion (input)
154 );
155 /// function compute the quaternion product qB * qC
156 void qAeqBxC(
157  Quaternion *pqA,
158  const Quaternion *pqB,
159  const Quaternion *pqC
160 );
161 /// function compute the quaternion product qA = qA * qB
162 void qAeqAxB(
163  Quaternion *pqA,
164  const Quaternion *pqB
165 );
166 /// function compute the quaternion product conjg(qA) * qB
168  const Quaternion *pqA,
169  const Quaternion *pqB
170 );
171 /// function normalizes a rotation quaternion and ensures q0 is non-negative
172 void fqAeqNormqA(
173  Quaternion *pqA
174 );
175 /// set a quaternion to the unit quaternion
176 void fqAeq1(
177  Quaternion *pqA
178 );
179 /// computes normalized rotation quaternion from a rotation vector (deg)
181  Quaternion *pq, ///< quaternion (output)
182  const float rvecdeg[], ///< rotation vector in degrees
183  float fscaling ///< delta Time
184 );
185 /// computes rotation vector (deg) from rotation quaternion
187  Quaternion *pq, ///< quaternion (input)
188  float rvecdeg[] ///< rotation vector in degrees (output)
189 );
190 /// function low pass filters an orientation quaternion and computes virtual gyro rotation rate
192  Quaternion *pq,
193  Quaternion *pLPq,
194  float flpf,
195  float fdeltat,
196  float fOmega[]
197 );
198 /// function computes the rotation quaternion that rotates unit vector u onto unit vector v as v=q*.u.q
199 /// using q = 1/sqrt(2) * {sqrt(1 + u.v) - u x v / sqrt(1 + u.v)}
200 void fveqconjgquq(
201  Quaternion *pfq,
202  float fu[],
203  float fv[]
204 );
205 
206 #endif // #ifndef ORIENTATION_H
void f3DOFMagnetometerMatrixAndroid(float fR[][3], float fBc[])
Android magnetometer 3DOF flat eCompass function, computing rotation matrix fR.
Definition: orientation.c:255
void fRotationVectorDegFromQuaternion(Quaternion *pq, float rvecdeg[])
computes rotation vector (deg) from rotation quaternion
Definition: orientation.c:872
void fqAeqNormqA(Quaternion *pqA)
function normalizes a rotation quaternion and ensures q0 is non-negative
Definition: orientation.c:1009
void f3DOFTiltNED(float fR[][3], float fGc[])
Aerospace NED accelerometer 3DOF tilt function, computing rotation matrix fR.
void fLPFOrientationQuaternion(Quaternion *pq, Quaternion *pLPq, float flpf, float fdeltat, float fOmega[])
function low pass filters an orientation quaternion and computes virtual gyro rotation rate ...
Definition: orientation.c:922
void feCompassNED(float fR[][3], float *pfDelta, float *pfsinDelta, float *pfcosDelta, float fBc[], float fGc[], float *pfmodBc, float *pfmodGc)
NED: basic 6DOF e-Compass function, computing rotation matrix fR and magnetic inclination angle fDelt...
Definition: orientation.c:296
float q1
x vector component
Definition: orientation.h:49
void fQuaternionFromRotationVectorDeg(Quaternion *pq, const float rvecdeg[], float fscaling)
computes normalized rotation quaternion from a rotation vector (deg)
Definition: orientation.c:723
struct Quaternion Quaternion
quaternion structure definition
Quaternion qconjgAxB(const Quaternion *pqA, const Quaternion *pqB)
function compute the quaternion product conjg(qA) * qB
Definition: orientation.c:996
#define R
Definition: status.c:54
void fqAeq1(Quaternion *pqA)
set a quaternion to the unit quaternion
Definition: orientation.c:1044
void f3DOFMagnetometerMatrixWin8(float fR[][3], float fBc[])
Windows 8 magnetometer 3DOF flat eCompass function, computing rotation matrix fR. ...
Definition: orientation.c:285
void qAeqAxB(Quaternion *pqA, const Quaternion *pqB)
function compute the quaternion product qA = qA * qB
Definition: orientation.c:979
void f3DOFTiltAndroid(float fR[][3], float fGc[])
Android accelerometer 3DOF tilt function computing, rotation matrix fR.
void fAndroidAnglesDegFromRotationMatrix(float R[][3], float *pfPhiDeg, float *pfTheDeg, float *pfPsiDeg, float *pfRhoDeg, float *pfChiDeg)
extract the Android angles in degrees from the Android rotation matrix
Definition: orientation.c:574
void fWin8AnglesDegFromRotationMatrix(float R[][3], float *pfPhiDeg, float *pfTheDeg, float *pfPsiDeg, float *pfRhoDeg, float *pfChiDeg)
extract the Windows 8 angles in degrees from the Windows 8 rotation matrix
Definition: orientation.c:631
float q2
y vector component
Definition: orientation.h:50
void feCompassWin8(float fR[][3], float *pfDelta, float *pfsinDelta, float *pfcosDelta, float fBc[], float fGc[], float *pfmodBc, float *pfmodGc)
Win8: basic 6DOF e-Compass function, computing rotation matrix fR and magnetic inclination angle fDel...
Definition: orientation.c:443
quaternion structure definition
Definition: orientation.h:46
void fNEDAnglesDegFromRotationMatrix(float R[][3], float *pfPhiDeg, float *pfTheDeg, float *pfPsiDeg, float *pfRhoDeg, float *pfChiDeg)
extract the NED angles in degrees from the NED rotation matrix
Definition: orientation.c:518
float q3
z vector component
Definition: orientation.h:51
float q0
scalar component
Definition: orientation.h:48
void f3DOFMagnetometerMatrixNED(float fR[][3], float fBc[])
Aerospace NED magnetometer 3DOF flat eCompass function, computing rotation matrix fR...
Definition: orientation.c:225
void f3DOFTiltWin8(float fR[][3], float fGc[])
Windows 8 accelerometer 3DOF tilt function computing, rotation matrix fR.
void fQuaternionFromRotationMatrix(float R[][3], Quaternion *pq)
compute the orientation quaternion from a 3x3 rotation matrix
Definition: orientation.c:791
void qAeqBxC(Quaternion *pqA, const Quaternion *pqB, const Quaternion *pqC)
function compute the quaternion product qB * qC
Definition: orientation.c:968
void fveqconjgquq(Quaternion *pfq, float fu[], float fv[])
Definition: orientation.c:1054
void feCompassAndroid(float fR[][3], float *pfDelta, float *pfsinDelta, float *pfcosDelta, float fBc[], float fGc[], float *pfmodBc, float *pfmodGc)
Android: basic 6DOF e-Compass function, computing rotation matrix fR and magnetic inclination angle f...
Definition: orientation.c:369
void fRotationMatrixFromQuaternion(float R[][3], const Quaternion *pq)
compute the rotation matrix from an orientation quaternion
Definition: orientation.c:832