-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathContinuousAngleTracker.cpp
78 lines (67 loc) · 1.94 KB
/
ContinuousAngleTracker.cpp
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
/*
* ContinuousAngleTracker.cpp
*
* Created on: Jul 30, 2015
* Author: Scott
*/
#include <ContinuousAngleTracker.h>
ContinuousAngleTracker::ContinuousAngleTracker() {
this->last_angle = 0.0f;
this->zero_crossing_count = 0;
this->last_rate = 0;
this->first_sample = false;
}
void ContinuousAngleTracker::NextAngle( float newAngle ) {
/* If the first received sample is negative,
* ensure that the zero crossing count is
* decremented.
*/
if ( this->first_sample ) {
this->first_sample = false;
if ( newAngle < 0.0f ) {
this->zero_crossing_count--;
}
}
/* Calculate delta angle, adjusting appropriately
* if the current sample crossed the -180/180
* point.
*/
bool bottom_crossing = false;
float delta_angle = newAngle - this->last_angle;
/* Adjust for wraparound at -180/+180 point */
if ( delta_angle >= 180.0f ){
delta_angle = 360.0f - delta_angle;
bottom_crossing = true;
} else if ( delta_angle <= -180.0f ){
delta_angle = 360.0f + delta_angle;
bottom_crossing = true;
}
this->last_rate = delta_angle;
/* If a zero crossing occurred, increment/decrement
* the zero crossing count appropriately.
*/
if ( !bottom_crossing ) {
if ( delta_angle < 0.0f ) {
if ( (newAngle < 0.0f) && (this->last_angle >= 0.0f) ) {
this->zero_crossing_count--;
}
} else if ( delta_angle >= 0.0f ) {
if ( (newAngle >= 0.0f) && (last_angle < 0.0f) ) {
this->zero_crossing_count++;
}
}
}
this->last_angle = newAngle;
}
double ContinuousAngleTracker::GetAngle() {
double accumulated_angle = (double)this->zero_crossing_count * 360.0f;
double curr_angle = (double)this->last_angle;
if ( curr_angle < 0.0f ) {
curr_angle += 360.0f;
}
accumulated_angle += curr_angle;
return accumulated_angle;
}
double ContinuousAngleTracker::GetRate() {
return this->last_rate;
}