-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontinuous_angle_tracker.py
66 lines (53 loc) · 2.11 KB
/
continuous_angle_tracker.py
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
from units import *
class ContinuousAngleTracker:
def __init__(self):
self.last_angle = Constant(0)
self.last_rate = Constant(0)
self.zero_crossing_count = 0
self.first_sample = True
def next_angle(self, newAngle):
'''
If the first received sample is negative,
ensure that the zero crossing count is
decremented.
'''
if(self.first_sample):
self.first_sample = False
if (newAngle < Constant(0.0)):
self.zero_crossing_count -= 1
'''
Calculate delta angle, adjusting appropriately
if the current sample crossed the -180/180
point.
'''
bottom_crossing = False
delta_angle = newAngle - self.last_angle
# Adjust for wraparound at -180/+180 point
if (delta_angle >= Angle(180.0, Angle.degree)):
delta_angle = Angle(360.0, Angle.degree) - delta_angle
bottom_crossing = True
elif(delta_angle <= Angle(-180.0, Angle.degree)):
delta_angle = Angle(360.0, Angle.degree) + delta_angle
bottom_crossing = True
self.last_rate = delta_angle
'''
If a zero crossing occurred, increment/decrement
the zero crossing count appropriately.
'''
if(not bottom_crossing):
if(delta_angle < Constant(0.0)):
if ((newAngle < Constant(0.0)) and (self.last_angle >= Constant(0.0))):
self.zero_crossing_count -= 1
elif(delta_angle >= Constant(0.0)):
if ((newAngle >= Constant(0.0)) and (self.last_angle < Constant(0.0))):
self.zero_crossing_count += 1
self.last_angle = newAngle
def get_angle(self):
accumulated_angle = Angle(self.zero_crossing_count * 360.0, Angle.degree)
curr_angle = self.last_angle
if (curr_angle < Constant(0.0)):
curr_angle += Angle(360.0, Angle.degree)
accumulated_angle += curr_angle
return accumulated_angle
def get_rate(self):
return self.last_rate