-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
194 lines (158 loc) · 7.34 KB
/
common.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import unittest
import numpy as np
PI = np.pi
CPR = 8192 # odrive
MOTOR_KV = 1000 # motor spec
TORQUE_FACTOR = 8.3 # iniversal BLDC constant
TORQUE_CONSTANT = TORQUE_FACTOR / MOTOR_KV # multiply this by current to get torque
MAX_CURRENT = 10.0
STEP = 0.01 # time step, seconds
VEL_MAX_ARM = 25 # rad/s
VEL_MAX_POLE = 50 # rad/s
# safety margin
MAX_RADIANS = VEL_MAX_ARM * STEP * 0.8
# 0 is straight up
ANGLE_TERMINAL_MAX_D = 60
ANGLE_TERMINAL_MIN_D = 10
NOISE_SENSE = 0.005
NOISE_COMMAND = 0.05
TRAIN = "train"
RUN = "run"
TEST = "test"
# clamp(2, 1, -1)
def clamp(n, minn, maxn):
return max(min(maxn, n), minn)
# output: -pi <= x <= pi
def rad2Norm(x):
return (((x+PI) % (2*PI)) - PI)
# shifts range from 0 - +/-pi to 0 - 2pi
def norm2Rad(x):
return (x + 2*PI) % (2*PI)
# wrap arg around 0 - 2pi
def wrapRad(x):
return x % (2*PI)
# convert radians to degrees
def rad2Deg(x):
return (x * (360/(2*PI))) % 360
# output: -180 <= x <= 180
def deg2Norm(x):
return (((x+180) % (360)) - 180)
# convert degrees to radians
def deg2Rad(x):
return wrapRad((x/180) * PI)
# difference between two positions, accounting for wrap
# always takes the short way around
# order of arguments is order of travel
# difRads(0, PI) = PI
# difRads(PI, 0) = -PI
def difRads(a, b):
c = b-a
dir = np.sign(c)
c *= dir
if c > PI:
c = -1 * (2*PI - c)
c *= dir
return c
# convert cpr to radians
def cpr2Rad(c):
return (c / CPR) * 2 * PI
def rad2Cpr(r):
r = wrapRad(r)
return int((r / (2*PI)) * CPR)
# dither the val by a random value +/- dith
def dither(val, dith):
dith = 1 + np.random.uniform(-dith, dith)
return val * dith
def sgn():
return [-1,1][np.random.randint(2)]
class CommonTest(unittest.TestCase):
# numpy.clip() doesn't verify min<max, so neither do we...
def testClamp(self):
self.assertEqual(clamp(0, -1, 1), 0)
self.assertEqual(clamp(1, -1, 1), 1)
self.assertEqual(clamp(-1, -1, 1), -1)
self.assertEqual(clamp(2, -1, 1), 1)
self.assertEqual(clamp(-2, -1, 1), -1)
def testRad2Norm(self):
self.assertEqual(round(rad2Norm(.1), 10), round(.1, 10))
self.assertEqual(round(rad2Norm(.5*PI), 10), round(.5*PI, 10))
self.assertEqual(round(rad2Norm(PI-.1), 10), round(PI-.1, 10))
self.assertEqual(round(rad2Norm(PI+.1), 10), round(-PI+.1, 10))
self.assertEqual(round(rad2Norm(1.5*PI), 10), round(-.5*PI, 10))
self.assertEqual(round(rad2Norm(2*PI-.1), 10), round(-.1, 10))
self.assertEqual(round(rad2Norm(-.1), 10), round(-.1, 10))
self.assertEqual(round(rad2Norm(-.5*PI), 10), round(-.5*PI, 10))
self.assertEqual(round(rad2Norm(-PI+.1), 10), round(-PI+.1, 10))
self.assertEqual(round(rad2Norm(-PI-.1), 10), round(PI-.1, 10))
self.assertEqual(round(rad2Norm(-1.5*PI), 10), round(.5*PI, 10))
self.assertEqual(round(rad2Norm(-2*PI+.1), 10), round(.1, 10))
def testNorm2Rad(self):
self.assertEqual(round(norm2Rad(.1), 10), round(.1, 10))
self.assertEqual(round(norm2Rad(.5*PI), 10), round(.5*PI, 10))
self.assertEqual(round(norm2Rad(PI-.1), 10), round(PI-.1, 10))
self.assertEqual(round(norm2Rad(-PI+.1), 10), round(PI+.1, 10))
self.assertEqual(round(norm2Rad(-.5*PI), 10), round(1.5*PI, 10))
self.assertEqual(round(norm2Rad(-.1), 10), round(2*PI-.1, 10))
def testWrapRad(self):
self.assertEqual(round(wrapRad(.1), 10), round(.1, 10))
self.assertEqual(round(wrapRad(PI), 10), round(PI, 10))
self.assertEqual(round(wrapRad(2*PI-.1), 10), round(2*PI-.1, 10))
self.assertEqual(round(wrapRad(2*PI), 10), round(0, 10))
self.assertEqual(round(wrapRad(2*PI+.1), 10), round(.1, 10))
self.assertEqual(round(wrapRad(-.1), 10), round(2*PI-.1, 10))
self.assertEqual(round(wrapRad(-PI), 10), round(PI, 10))
self.assertEqual(round(wrapRad(-2*PI+.1), 10), round(.1, 10))
self.assertEqual(round(wrapRad(-2*PI), 10), round(0, 10))
self.assertEqual(round(wrapRad(-2*PI-.1), 10), round(2*PI-.1, 10))
def testRad2Deg(self):
self.assertEqual(round(rad2Deg(0), 10), round(0, 10))
self.assertEqual(round(rad2Deg(0.5*PI), 10), round(90, 10))
self.assertEqual(round(rad2Deg(PI), 10), round(180, 10))
self.assertEqual(round(rad2Deg(2*PI), 10), round(0, 10))
self.assertEqual(round(rad2Deg(2.5*PI), 10), round(90, 10))
self.assertEqual(round(rad2Deg(-.5*PI), 10), round(270, 10))
self.assertEqual(round(rad2Deg(-PI), 10), round(180, 10))
self.assertEqual(round(rad2Deg(-2*PI), 10), round(0, 10))
self.assertEqual(round(rad2Deg(-2.5*PI), 10), round(270, 10))
def testDeg2Rad(self):
self.assertEqual(round(deg2Rad(0), 10), round(0, 10))
self.assertEqual(round(deg2Rad(90), 10), round(0.5*PI, 10))
self.assertEqual(round(deg2Rad(180), 10), round(PI, 10))
self.assertEqual(round(deg2Rad(270), 10), round(1.5*PI, 10))
self.assertEqual(round(deg2Rad(360), 10), round(0, 10))
self.assertEqual(round(deg2Rad(450), 10), round(0.5*PI, 10))
self.assertEqual(round(deg2Rad(-90), 10), round(1.5*PI, 10))
self.assertEqual(round(deg2Rad(-180), 10), round(PI, 10))
self.assertEqual(round(deg2Rad(-270), 10), round(0.5*PI, 10))
self.assertEqual(round(deg2Rad(-360), 10), round(0, 10))
self.assertEqual(round(deg2Rad(-450), 10), round(1.5*PI, 10))
def testDifRads(self):
self.assertEqual(round(difRads(0, 0), 10), round(0, 10))
self.assertEqual(round(difRads(0, PI), 10), round(PI, 10))
self.assertEqual(round(difRads(PI, 0), 10), round(-PI, 10))
self.assertEqual(round(difRads(.5*PI, 1.5*PI), 10), round(PI, 10))
self.assertEqual(round(difRads(1.5*PI, .5*PI), 10), round(-PI, 10))
self.assertEqual(round(difRads(.5*PI, 1.5*PI+.1), 10), round(-PI+.1, 10))
self.assertEqual(round(difRads(1.5*PI+.1, .5*PI), 10), round(PI-.1, 10))
self.assertEqual(round(difRads(.1, 2*PI-.1), 10), round(-.2, 10))
self.assertEqual(round(difRads(2*PI-.1, .1), 10), round(.2, 10))
self.assertEqual(round(difRads(.1, -.1), 10), round(-.2, 10))
self.assertEqual(round(difRads(-.1, .1), 10), round(.2, 10))
self.assertEqual(round(difRads(-PI, PI), 10), round(0, 10))
self.assertEqual(round(difRads(-.5*PI, .5*PI), 10), round(PI, 10))
self.assertEqual(round(difRads(.5*PI, -.5*PI), 10), round(-PI, 10))
self.assertEqual(round(difRads(.5*PI, -.5*PI+.1), 10), round(-PI+.1, 10))
self.assertEqual(round(difRads(-.5*PI, .5*PI-.1), 10), round(PI-.1, 10))
def testCpr2Rad(self):
self.assertEqual(round(cpr2Rad(0), 10), round(0, 10))
self.assertEqual(round(cpr2Rad(CPR/2), 10), round(PI, 10))
self.assertEqual(round(cpr2Rad(CPR), 10), round(2*PI, 10))
def testRad2Cpr(self):
self.assertEqual(round(rad2Cpr(0), 10), round(0, 10))
self.assertEqual(round(rad2Cpr(PI), 10), round(CPR/2, 10))
self.assertEqual(round(rad2Cpr(2*PI), 10), round(0, 10))
self.assertEqual(round(rad2Cpr(-PI), 10), round(CPR/2, 10))
self.assertEqual(round(rad2Cpr(3*PI), 10), round(CPR/2, 10))
self.assertEqual(round(rad2Cpr(-3*PI), 10), round(CPR/2, 10))
if __name__ == '__main__':
unittest.main()