うおーるぼっとLEDのワークショップ用

Dependencies:   mbed

Committer:
jksoft
Date:
Sat Feb 04 03:33:08 2017 +0000
Revision:
3:b707c09b4433
Parent:
0:9fcc79b3f00e
????

Who changed what in which revision?

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