forked from ArduPilot/ardupilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquaternion.h
84 lines (68 loc) · 2.39 KB
/
quaternion.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Copyright 2012 Andrew Tridgell, all rights reserved.
#ifndef QUATERNION_H
#define QUATERNION_H
#include <math.h>
#if MATH_CHECK_INDEXES
#include <assert.h>
#endif
class Quaternion
{
public:
float q1, q2, q3, q4;
// constructor creates a quaternion equivalent
// to roll=0, pitch=0, yaw=0
Quaternion() {
q1 = 1; q2 = q3 = q4 = 0;
}
// setting constructor
Quaternion(const float _q1, const float _q2, const float _q3, const float _q4) :
q1(_q1), q2(_q2), q3(_q3), q4(_q4) {
}
// function call operator
void operator ()(const float _q1, const float _q2, const float _q3, const float _q4)
{
q1 = _q1; q2 = _q2; q3 = _q3; q4 = _q4;
}
// check if any elements are NAN
bool is_nan(void)
{
return isnan(q1) || isnan(q2) || isnan(q3) || isnan(q4);
}
// return the rotation matrix equivalent for this quaternion
void rotation_matrix(Matrix3f &m) const;
// convert a vector from earth to body frame
void earth_to_body(Vector3f &v) const;
// create a quaternion from Euler angles
void from_euler(float roll, float pitch, float yaw);
// create eulers from a quaternion
void to_euler(float *roll, float *pitch, float *yaw) const;
// allow a quaternion to be used as an array, 0 indexed
float & operator[](uint8_t i) {
float *_v = &q1;
#if MATH_CHECK_INDEXES
assert(i >= 0 && i < 4);
#endif
return _v[i];
}
const float & operator[](uint8_t i) const {
const float *_v = &q1;
#if MATH_CHECK_INDEXES
assert(i >= 0 && i < 4);
#endif
return _v[i];
}
};
#endif // QUATERNION_H