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
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
29 ===============================================
32 #ifndef _HELPER_3DMATH_H_
33 #define _HELPER_3DMATH_H_
51 Quaternion(float nw
, float nx
, float ny
, float 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
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
);
80 float m
= getMagnitude();
87 Quaternion
getNormalized() {
88 Quaternion
r(w
, x
, y
, z
);
106 VectorInt16(int16_t nx
, int16_t ny
, int16_t nz
) {
112 float getMagnitude() {
113 return sqrt(x
*x
+ y
*y
+ z
*z
);
117 float m
= getMagnitude();
123 VectorInt16
getNormalized() {
124 VectorInt16
r(x
, y
, z
);
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']
154 VectorInt16
getRotated(Quaternion
*q
) {
155 VectorInt16
r(x
, y
, z
);
173 VectorFloat(float nx
, float ny
, float nz
) {
179 float getMagnitude() {
180 return sqrt(x
*x
+ y
*y
+ z
*z
);
184 float m
= getMagnitude();
190 VectorFloat
getNormalized() {
191 VectorFloat
r(x
, y
, z
);
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']
211 VectorFloat
getRotated(Quaternion
*q
) {
212 VectorFloat
r(x
, y
, z
);
218 #endif /* _HELPER_3DMATH_H_ */