-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrotationEstimator.cpp
More file actions
72 lines (56 loc) · 2.15 KB
/
rotationEstimator.cpp
File metadata and controls
72 lines (56 loc) · 2.15 KB
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
/*
* Flybrix Flight Controller -- Copyright 2018 Flying Selfie Inc. d/b/a Flybrix
*
* http://www.flybrix.com
*/
#include "rotationEstimator.h"
#include "debug.h"
#include "quickmath.h"
void RotationEstimator::updateGravity(Pose pose, const Vector3<float>& value) {
states_[(uint8_t)pose].measurement = value;
states_[(uint8_t)pose].handled = true;
}
void RotationEstimator::clear() {
for (RotationEstimator::State& state : states_) {
state.handled = false;
}
}
RotationMatrix<float> RotationEstimator::estimate() const {
RotationMatrix<float> answer;
for (const RotationEstimator::State& state : states_) {
if (!state.handled) {
DebugPrint("Calibration requires all five poses to be handled");
return answer;
}
}
Vector3<float> up = states_[(uint8_t)RotationEstimator::Pose::Flat].measurement;
quick::normalize(up);
Vector3<float> right = states_[(uint8_t)RotationEstimator::Pose::RollRight].measurement.projectOntoPlane(up);
Vector3<float> left = states_[(uint8_t)RotationEstimator::Pose::RollLeft].measurement.projectOntoPlane(up);
if (dot(right, left) > 0.0f) {
DebugPrint("Right and left roll need to be performed in opposite directions");
return answer;
}
quick::normalize(left);
Vector3<float> forward = states_[(uint8_t)RotationEstimator::Pose::PitchForward].measurement.projectOntoPlane(up).projectOntoPlane(left);
Vector3<float> back = states_[(uint8_t)RotationEstimator::Pose::PitchBack].measurement.projectOntoPlane(up).projectOntoPlane(left);
if (dot(forward, back) > 0.0f) {
DebugPrint("Forward and back pitch need to be performed in opposite directions");
return answer;
}
quick::normalize(back);
if (dot(cross(left, back), up) <= 0.0f) {
DebugPrint("Roll and pitch directions were not properly lined up");
return answer;
}
answer(0, 0) = left.x;
answer(0, 1) = left.y;
answer(0, 2) = left.z;
answer(1, 0) = back.x;
answer(1, 1) = back.y;
answer(1, 2) = back.z;
answer(2, 0) = up.x;
answer(2, 1) = up.y;
answer(2, 2) = up.z;
return answer;
}