Update oXs_out_frsky.cpp
[openXsensor.git] / openXsensor / helper_3dmath.h
blob391a60d9890f4fb7381ebfc823bd2e5cb2e8c0e5
1 // I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class, 3D math helper
2 // 6/5/2012 by Jeff Rowberg <jeff@rowberg.net>
3 // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
4 //
5 // Changelog:
6 // 2012-06-05 - add 3D math helper file to DMP6 example sketch
8 /* ============================================
9 I2Cdev device library code is placed under the MIT license
10 Copyright (c) 2012 Jeff Rowberg
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
19 The above copyright notice and this permission notice shall be included in
20 all copies or substantial portions of the Software.
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 THE SOFTWARE.
29 ===============================================
32 #ifndef _HELPER_3DMATH_H_
33 #define _HELPER_3DMATH_H_
35 #include <Arduino.h>
37 class Quaternion {
38 public:
39 float w;
40 float x;
41 float y;
42 float z;
44 Quaternion() {
45 w = 1.0f;
46 x = 0.0f;
47 y = 0.0f;
48 z = 0.0f;
51 Quaternion(float nw, float nx, float ny, float nz) {
52 w = nw;
53 x = nx;
54 y = ny;
55 z = nz;
58 Quaternion getProduct(Quaternion q) {
59 // Quaternion multiplication is defined by:
60 // (Q1 * Q2).w = (w1w2 - x1x2 - y1y2 - z1z2)
61 // (Q1 * Q2).x = (w1x2 + x1w2 + y1z2 - z1y2)
62 // (Q1 * Q2).y = (w1y2 - x1z2 + y1w2 + z1x2)
63 // (Q1 * Q2).z = (w1z2 + x1y2 - y1x2 + z1w2
64 return Quaternion(
65 w*q.w - x*q.x - y*q.y - z*q.z, // new w
66 w*q.x + x*q.w + y*q.z - z*q.y, // new x
67 w*q.y - x*q.z + y*q.w + z*q.x, // new y
68 w*q.z + x*q.y - y*q.x + z*q.w); // new z
71 Quaternion getConjugate() {
72 return Quaternion(w, -x, -y, -z);
75 float getMagnitude() {
76 return sqrt(w*w + x*x + y*y + z*z);
79 void normalize() {
80 float m = getMagnitude();
81 w /= m;
82 x /= m;
83 y /= m;
84 z /= m;
87 Quaternion getNormalized() {
88 Quaternion r(w, x, y, z);
89 r.normalize();
90 return r;
94 class VectorInt16 {
95 public:
96 int16_t x;
97 int16_t y;
98 int16_t z;
100 VectorInt16() {
101 x = 0;
102 y = 0;
103 z = 0;
106 VectorInt16(int16_t nx, int16_t ny, int16_t nz) {
107 x = nx;
108 y = ny;
109 z = nz;
112 float getMagnitude() {
113 return sqrt(x*x + y*y + z*z);
116 void normalize() {
117 float m = getMagnitude();
118 x /= m;
119 y /= m;
120 z /= m;
123 VectorInt16 getNormalized() {
124 VectorInt16 r(x, y, z);
125 r.normalize();
126 return r;
129 void rotate(Quaternion *q) {
130 // http://www.cprogramming.com/tutorial/3d/quaternions.html
131 // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/transforms/index.htm
132 // http://content.gpwiki.org/index.php/OpenGL:Tutorials:Using_Quaternions_to_represent_rotation
133 // ^ or: http://webcache.googleusercontent.com/search?q=cache:xgJAp3bDNhQJ:content.gpwiki.org/index.php/OpenGL:Tutorials:Using_Quaternions_to_represent_rotation&hl=en&gl=us&strip=1
135 // P_out = q * P_in * conj(q)
136 // - P_out is the output vector
137 // - q is the orientation quaternion
138 // - P_in is the input vector (a*aReal)
139 // - conj(q) is the conjugate of the orientation quaternion (q=[w,x,y,z], q*=[w,-x,-y,-z])
140 Quaternion p(0, x, y, z);
142 // quaternion multiplication: q * p, stored back in p
143 p = q -> getProduct(p);
145 // quaternion multiplication: p * conj(q), stored back in p
146 p = p.getProduct(q -> getConjugate());
148 // p quaternion is now [0, x', y', z']
149 x = p.x;
150 y = p.y;
151 z = p.z;
154 VectorInt16 getRotated(Quaternion *q) {
155 VectorInt16 r(x, y, z);
156 r.rotate(q);
157 return r;
161 class VectorFloat {
162 public:
163 float x;
164 float y;
165 float z;
167 VectorFloat() {
168 x = 0;
169 y = 0;
170 z = 0;
173 VectorFloat(float nx, float ny, float nz) {
174 x = nx;
175 y = ny;
176 z = nz;
179 float getMagnitude() {
180 return sqrt(x*x + y*y + z*z);
183 void normalize() {
184 float m = getMagnitude();
185 x /= m;
186 y /= m;
187 z /= m;
190 VectorFloat getNormalized() {
191 VectorFloat r(x, y, z);
192 r.normalize();
193 return r;
196 void rotate(Quaternion *q) {
197 Quaternion p(0, x, y, z);
199 // quaternion multiplication: q * p, stored back in p
200 p = q -> getProduct(p);
202 // quaternion multiplication: p * conj(q), stored back in p
203 p = p.getProduct(q -> getConjugate());
205 // p quaternion is now [0, x', y', z']
206 x = p.x;
207 y = p.y;
208 z = p.z;
211 VectorFloat getRotated(Quaternion *q) {
212 VectorFloat r(x, y, z);
213 r.rotate(q);
214 return r;
218 #endif /* _HELPER_3DMATH_H_ */