adafruit_bno055 / utility / quaternion.h @ 3cae40b9
History | View | Annotate | Download (6.44 KB)
1 |
/*
|
---|---|
2 |
Inertial Measurement Unit Maths Library
|
3 |
Copyright (C) 2013-2014 Samuel Cowen
|
4 |
www.camelsoftware.com
|
5 |
|
6 |
This program is free software: you can redistribute it and/or modify
|
7 |
it under the terms of the GNU General Public License as published by
|
8 |
the Free Software Foundation, either version 3 of the License, or
|
9 |
(at your option) any later version.
|
10 |
|
11 |
This program is distributed in the hope that it will be useful,
|
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
GNU General Public License for more details.
|
15 |
|
16 |
You should have received a copy of the GNU General Public License
|
17 |
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
18 |
*/
|
19 |
|
20 |
|
21 |
#ifndef IMUMATH_QUATERNION_HPP
|
22 |
#define IMUMATH_QUATERNION_HPP
|
23 |
|
24 |
#include <stdlib.h> |
25 |
#include <string.h> |
26 |
#include <stdint.h> |
27 |
#include <math.h> |
28 |
|
29 |
#include "matrix.h" |
30 |
|
31 |
|
32 |
namespace imu |
33 |
{ |
34 |
|
35 |
class Quaternion |
36 |
{ |
37 |
public:
|
38 |
Quaternion(): _w(1.0), _x(0.0), _y(0.0), _z(0.0) {} |
39 |
|
40 |
Quaternion(double w, double x, double y, double z): |
41 |
_w(w), _x(x), _y(y), _z(z) {} |
42 |
|
43 |
Quaternion(double w, Vector<3> vec): |
44 |
_w(w), _x(vec.x()), _y(vec.y()), _z(vec.z()) {} |
45 |
|
46 |
double& w()
|
47 |
{ |
48 |
return _w;
|
49 |
} |
50 |
double& x()
|
51 |
{ |
52 |
return _x;
|
53 |
} |
54 |
double& y()
|
55 |
{ |
56 |
return _y;
|
57 |
} |
58 |
double& z()
|
59 |
{ |
60 |
return _z;
|
61 |
} |
62 |
|
63 |
double w() const |
64 |
{ |
65 |
return _w;
|
66 |
} |
67 |
double x() const |
68 |
{ |
69 |
return _x;
|
70 |
} |
71 |
double y() const |
72 |
{ |
73 |
return _y;
|
74 |
} |
75 |
double z() const |
76 |
{ |
77 |
return _z;
|
78 |
} |
79 |
|
80 |
double magnitude() const |
81 |
{ |
82 |
return sqrt(_w*_w + _x*_x + _y*_y + _z*_z);
|
83 |
} |
84 |
|
85 |
void normalize()
|
86 |
{ |
87 |
double mag = magnitude();
|
88 |
*this = this->scale(1/mag);
|
89 |
} |
90 |
|
91 |
Quaternion conjugate() const
|
92 |
{ |
93 |
return Quaternion(_w, -_x, -_y, -_z);
|
94 |
} |
95 |
|
96 |
void fromAxisAngle(const Vector<3>& axis, double theta) |
97 |
{ |
98 |
_w = cos(theta/2);
|
99 |
//only need to calculate sine of half theta once
|
100 |
double sht = sin(theta/2); |
101 |
_x = axis.x() * sht; |
102 |
_y = axis.y() * sht; |
103 |
_z = axis.z() * sht; |
104 |
} |
105 |
|
106 |
void fromMatrix(const Matrix<3>& m) |
107 |
{ |
108 |
double tr = m.trace();
|
109 |
|
110 |
double S;
|
111 |
if (tr > 0) |
112 |
{ |
113 |
S = sqrt(tr+1.0) * 2; |
114 |
_w = 0.25 * S; |
115 |
_x = (m(2, 1) - m(1, 2)) / S; |
116 |
_y = (m(0, 2) - m(2, 0)) / S; |
117 |
_z = (m(1, 0) - m(0, 1)) / S; |
118 |
} |
119 |
else if (m(0, 0) > m(1, 1) && m(0, 0) > m(2, 2)) |
120 |
{ |
121 |
S = sqrt(1.0 + m(0, 0) - m(1, 1) - m(2, 2)) * 2; |
122 |
_w = (m(2, 1) - m(1, 2)) / S; |
123 |
_x = 0.25 * S; |
124 |
_y = (m(0, 1) + m(1, 0)) / S; |
125 |
_z = (m(0, 2) + m(2, 0)) / S; |
126 |
} |
127 |
else if (m(1, 1) > m(2, 2)) |
128 |
{ |
129 |
S = sqrt(1.0 + m(1, 1) - m(0, 0) - m(2, 2)) * 2; |
130 |
_w = (m(0, 2) - m(2, 0)) / S; |
131 |
_x = (m(0, 1) + m(1, 0)) / S; |
132 |
_y = 0.25 * S; |
133 |
_z = (m(1, 2) + m(2, 1)) / S; |
134 |
} |
135 |
else
|
136 |
{ |
137 |
S = sqrt(1.0 + m(2, 2) - m(0, 0) - m(1, 1)) * 2; |
138 |
_w = (m(1, 0) - m(0, 1)) / S; |
139 |
_x = (m(0, 2) + m(2, 0)) / S; |
140 |
_y = (m(1, 2) + m(2, 1)) / S; |
141 |
_z = 0.25 * S; |
142 |
} |
143 |
} |
144 |
|
145 |
void toAxisAngle(Vector<3>& axis, double& angle) const |
146 |
{ |
147 |
double sqw = sqrt(1-_w*_w); |
148 |
if (sqw == 0) //it's a singularity and divide by zero, avoid |
149 |
return;
|
150 |
|
151 |
angle = 2 * acos(_w);
|
152 |
axis.x() = _x / sqw; |
153 |
axis.y() = _y / sqw; |
154 |
axis.z() = _z / sqw; |
155 |
} |
156 |
|
157 |
Matrix<3> toMatrix() const |
158 |
{ |
159 |
Matrix<3> ret;
|
160 |
ret.cell(0, 0) = 1 - 2*_y*_y - 2*_z*_z; |
161 |
ret.cell(0, 1) = 2*_x*_y - 2*_w*_z; |
162 |
ret.cell(0, 2) = 2*_x*_z + 2*_w*_y; |
163 |
|
164 |
ret.cell(1, 0) = 2*_x*_y + 2*_w*_z; |
165 |
ret.cell(1, 1) = 1 - 2*_x*_x - 2*_z*_z; |
166 |
ret.cell(1, 2) = 2*_y*_z - 2*_w*_x; |
167 |
|
168 |
ret.cell(2, 0) = 2*_x*_z - 2*_w*_y; |
169 |
ret.cell(2, 1) = 2*_y*_z + 2*_w*_x; |
170 |
ret.cell(2, 2) = 1 - 2*_x*_x - 2*_y*_y; |
171 |
return ret;
|
172 |
} |
173 |
|
174 |
|
175 |
// Returns euler angles that represent the quaternion. Angles are
|
176 |
// returned in rotation order and right-handed about the specified
|
177 |
// axes:
|
178 |
//
|
179 |
// v[0] is applied 1st about z (ie, roll)
|
180 |
// v[1] is applied 2nd about y (ie, pitch)
|
181 |
// v[2] is applied 3rd about x (ie, yaw)
|
182 |
//
|
183 |
// Note that this means result.x() is not a rotation about x;
|
184 |
// similarly for result.z().
|
185 |
//
|
186 |
Vector<3> toEuler() const |
187 |
{ |
188 |
Vector<3> ret;
|
189 |
double sqw = _w*_w;
|
190 |
double sqx = _x*_x;
|
191 |
double sqy = _y*_y;
|
192 |
double sqz = _z*_z;
|
193 |
|
194 |
ret.x() = atan2(2.0*(_x*_y+_z*_w),(sqx-sqy-sqz+sqw)); |
195 |
ret.y() = asin(-2.0*(_x*_z-_y*_w)/(sqx+sqy+sqz+sqw)); |
196 |
ret.z() = atan2(2.0*(_y*_z+_x*_w),(-sqx-sqy+sqz+sqw)); |
197 |
|
198 |
return ret;
|
199 |
} |
200 |
|
201 |
Vector<3> toAngularVelocity(double dt) const |
202 |
{ |
203 |
Vector<3> ret;
|
204 |
Quaternion one(1.0, 0.0, 0.0, 0.0); |
205 |
Quaternion delta = one - *this; |
206 |
Quaternion r = (delta/dt); |
207 |
r = r * 2;
|
208 |
r = r * one; |
209 |
|
210 |
ret.x() = r.x(); |
211 |
ret.y() = r.y(); |
212 |
ret.z() = r.z(); |
213 |
return ret;
|
214 |
} |
215 |
|
216 |
Vector<3> rotateVector(const Vector<2>& v) const |
217 |
{ |
218 |
return rotateVector(Vector<3>(v.x(), v.y())); |
219 |
} |
220 |
|
221 |
Vector<3> rotateVector(const Vector<3>& v) const |
222 |
{ |
223 |
Vector<3> qv(_x, _y, _z);
|
224 |
Vector<3> t = qv.cross(v) * 2.0; |
225 |
return v + t*_w + qv.cross(t);
|
226 |
} |
227 |
|
228 |
|
229 |
Quaternion operator*(const Quaternion& q) const |
230 |
{ |
231 |
return Quaternion(
|
232 |
_w*q._w - _x*q._x - _y*q._y - _z*q._z, |
233 |
_w*q._x + _x*q._w + _y*q._z - _z*q._y, |
234 |
_w*q._y - _x*q._z + _y*q._w + _z*q._x, |
235 |
_w*q._z + _x*q._y - _y*q._x + _z*q._w |
236 |
); |
237 |
} |
238 |
|
239 |
Quaternion operator+(const Quaternion& q) const |
240 |
{ |
241 |
return Quaternion(_w + q._w, _x + q._x, _y + q._y, _z + q._z);
|
242 |
} |
243 |
|
244 |
Quaternion operator-(const Quaternion& q) const |
245 |
{ |
246 |
return Quaternion(_w - q._w, _x - q._x, _y - q._y, _z - q._z);
|
247 |
} |
248 |
|
249 |
Quaternion operator/(double scalar) const |
250 |
{ |
251 |
return Quaternion(_w / scalar, _x / scalar, _y / scalar, _z / scalar);
|
252 |
} |
253 |
|
254 |
Quaternion operator*(double scalar) const |
255 |
{ |
256 |
return scale(scalar);
|
257 |
} |
258 |
|
259 |
Quaternion scale(double scalar) const |
260 |
{ |
261 |
return Quaternion(_w * scalar, _x * scalar, _y * scalar, _z * scalar);
|
262 |
} |
263 |
|
264 |
private:
|
265 |
double _w, _x, _y, _z;
|
266 |
}; |
267 |
|
268 |
} // namespace
|
269 |
|
270 |
#endif
|