-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc.py
120 lines (97 loc) · 2.59 KB
/
misc.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import wpilib
from functools import wraps
field_max_x = 16.46
def add_timing(func):
# This function shows the execution time of
# the function object passed
@wraps(func)
def wrap_func(*args, **kwargs):
from time import time
t1 = time()
result = func(*args, **kwargs)
t2 = time()
diff = t2 - t1
return result, diff
return wrap_func
# Create a python decorator that squares a return value
# (Copilot created this one based on the above comment)
def square(func):
@wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs) * abs(func(*args, **kwargs))
return wrapper
# Create python deorator that deadbands a value provided in the decorator
# (Copilot created this one based on the above comment)
def deadband(deadband: float):
def deadband_decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
value = func(*args, **kwargs)
if abs(value) < deadband:
return 0
return value
return wrapper
return deadband_decorator
# Create a python decorator that captures all exceptions and returns False
# if one occurs
def compsafe(func):
@wraps(func)
def wrapper(*args, **kwargs):
if wpilib.DriverStation.isFMSAttached():
try:
return func(*args, **kwargs)
except Exception:
return
else:
return func(*args, **kwargs)
return wrapper
def safe(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception:
return
return wrapper
def safe_bool(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception:
return False
return wrapper
def safe_float(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception:
return 0.00
return wrapper
def iterable(obj):
try:
iter(obj)
except: # noqa: E722
return False
return True
def borx(bx, flip) -> float:
if flip:
return field_max_x - bx
print("bx")
print(bx)
return bx
def bory(by, flip) -> float:
return by
def bor_rot(br, flip) -> float:
if flip:
offset = br - 90
return (90 - offset)
return br
def angle_offset(angle, target):
offset = target - angle
while offset > 180:
offset -= 360
while offset < -180:
offset += 360
return offset