-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
304 lines (233 loc) · 7.04 KB
/
main.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#MicroPython v1.9.2 on 2017-08-23; PYBv1.0 with STM32F405RG
import time
import machine
from machine import Pin, I2C, UART
import ssd1306
from pyb import LED
from pyb import Pin
from pyb import Timer
from pyb import ADC
import math as m
import ujson
# print ("ready for code")
# pyb.LED(1)
def collect():
"""
collect() takes a 20 sample average over 0.5 s from each ADC pin
:return: p_all: a vector of the averaged values
"""
p0 = pyb.ADC('X3')
p1 = pyb.ADC('X4')
p2 = pyb.ADC('X5')
p3 = pyb.ADC('X6')
p4 = pyb.ADC('X7')
p5 = pyb.ADC('X8')
nav = 20
npots = 6
p_all = [0] * npots
for i in range(nav): # sum loop
p_all[0] += p0.read()
p_all[1] += p1.read()
p_all[2] += p2.read()
p_all[3] += p3.read()
p_all[4] += p4.read()
p_all[5] += p5.read()
pyb.delay(25)
for i2 in range(npots): # divide loop
p_all[i2] = p_all[i2] / nav
return p_all
def calibrate():
"""
calibrate() is triggered by the USR switch. Once triggered, the user should bring each joint to its
lower bound and press the USR switch again. This saves these values in a vector called p_lo. The user
should then bring each joint to the other extreme and press the USR switch once more. This create the
p_hi vector and the code returns to normal operation
:return:
"""
f = open('bounds_file.txt', 'w')
trig = False
oled.fill(0)
oled.text('turn all joints',0,10)
oled.text('to lower bound',0,20)
oled.text('and press USR', 0, 30)
oled.show()
pyb.delay(1000)
while not trig: # waiting for USR input
if sw():
pyb.delay(100)
if sw():
trig = True
p_lo = collect()
print('lower bound collected!')
oled.text('lower bound', 10, 46)
oled.text('collected!', 10, 56)
oled.show()
trig = False
pyb.delay(500)
oled.fill(0)
oled.text('turn all joints', 0, 10)
oled.text('to upper bound',0, 20)
oled.text('and press USR', 0, 30)
oled.text('lower bound', 10, 46)
oled.text('collected!', 10, 56)
oled.show()
while not trig: # waiting for USR input
if sw():
pyb.delay(100)
if sw():
trig = True
p_hi = collect()
print('upper bound collected!')
oled.fill(0)
oled.text('upper bound', 10, 46)
oled.text('collected!', 10, 56)
oled.show()
p_list = p_lo + p_hi
a = ujson.dumps(p_list)
f.write(a)
print('wrote')
pyb.delay(1000)
oled.fill(0)
oled.text('thanks homie', 28, 50)
oled.show()
f.close()
print('closed')
pyb.delay(1000)
return p_lo, p_hi
def convert(p_val, p_list, change_bool):
"""
:param p_val: p_list; change_bool
:param change_bool:
:return:
"""
if change_bool:
f = open('bounds_file.txt', 'r')
a = f.read()
if len(a) == 0:
f.close()
f = open('bounds_file.txt','w')
substitute = [0]*6 + [4056]*6
usub = ujson.dumps(substitute)
f.write(usub)
pyb.delay(3000)
f.close()
print('bounds reset, need to recalibrate')
oled.fill(0)
oled.text('bounds have',0,0)
oled.text('been reset', 0, 10)
oled.text('to default',0,20)
oled.text('----------',0,30)
oled.show()
time.sleep(1)
oled.text('recalibrate',0,40)
oled.text('when possible', 0, 50)
oled.show()
time.sleep(2)
oled.fill(0)
oled.show()
f = open('bounds_file.txt', 'r')
a = f.read()
print(a)
p_list = ujson.loads(a)
p_lo = p_list[0:6]
p_hi = p_list[6:13]
change_bool = False
f.close()
# pyb.delay(3000)
else:
p_lo = p_list[0:6]
p_hi = p_list[6:13]
n = len(p_val)
deci = [0] * n
deg = [0] * n
rad = [0] * n
for i in range(n):
deci[i] = (p_val[i] - p_lo[i])/(p_hi[i] - p_lo[i])
rad[i] = deci[i] * m.pi - m.pi/2
deg[i] = round(deci[i] * 180 - 90)
return deg, rad, p_list, change_bool
def calculate(rad):
l = [1, 1, 1, 1, 1, 1, 1] # length of each segment
a = [0] + rad # angle vector formatted for this function
ortho = m.pi/2
r = [0, 0, ortho, ortho, ortho, 0, ortho] # rotational angle vector, r[0,1] must be 0
n = len(a)
x = 0
y = 0
z = l[0]
crt = [0, 0, 1]
# print('dist to node 0 :', l[0])
# vecmap = [[0] * 3] * n
# vecmap[0][2] = 1
for it in range(1, n):
# this is where the sph->crt transform happens for each arm wrt its base arm
# print('it:',it)
crt[0] = m.sin(a[it]) * m.cos(r[it])
crt[1] = m.sin(a[it]) * m.sin(r[it])
crt[2] = m.cos(a[it])
if it > 1:
for it2 in range(it - 1, 0, -1):
# this is where the c.sys(n)->c.sys(0) transform happens for each unit vec
# print('it2:',it2)
T = [[m.cos(r[it2]) * m.cos(a[it2]), -m.sin(r[it2]), m.cos(r[it2]) * m.sin(a[it2])], [
m.sin(r[it2]) * m.cos(a[it2]), m.cos(r[it2]), m.sin(r[it2]) * m.sin(a[it2])], [
-m.sin(a[it2]), 0, m.cos(a[it2])]]
crt[0] = crt[0] * T[0][0] + crt[1] * T[0][1] + crt[2] * T[0][2]
crt[1] = crt[0] * T[1][0] + crt[1] * T[1][1] + crt[2] * T[1][2]
crt[2] = crt[0] * T[2][0] + crt[1] * T[2][1] + crt[2] * T[2][2]
x += crt[0] * l[it]
y += crt[1] * l[it]
z += crt[2] * l[it]
# vecmap[it] = crt # logs the unit vecs for each arm wrt c.sys(0)
# node_dist = round(((x ** 2 + y ** 2 + z ** 2) ** 0.5), 4)
# print('dist to node', it + 1, ':', node_dist)
dist = (x ** 2 + y ** 2 + z ** 2) ** 0.5
pdist = round(dist, 2)
# print('\narm unit vectors: [X Y Z]\n', vecmap)
# print('\ndistance:', pdist, 'units')
return pdist
def convey(pdist):
i2c = I2C(Pin('X9'), Pin('X10'))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
oled.fill(1)
oled.show()
pyb.delay(1000)
oled.fill(0)
oled.show()
########### main code goes below ############
sw = pyb.Switch()
change = True
p_bounds = 0
i2c = I2C(scl = Pin('X9'), sda = Pin('X10'))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
oled.fill(1)
oled.show()
time.sleep(1)
oled.fill(0)
oled.show()
time.sleep(1)
oled.text('PENIS',64,50)
oled.show()
time.sleep(0.5)
oled.fill(0)
oled.show()
for k in range(40):
if sw():
pyb.delay(250)
if sw():
print('USR triggered!')
oled.fill(0)
oled.text('calibration triggered',0,0)
calibrate()
change = True
pot_vals = collect()
degrees, radians, p_bounds, change = convert(pot_vals, p_bounds, change)
distance = calculate(radians)
pdistance = str(distance) + ' cm'
oled.fill(0)
# oled.show()
oled.text(pdistance,32,32)
oled.show()
print('distance: ', distance)
oled.fill(0)
oled.show()