-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
345 lines (299 loc) · 10.5 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
"""
Fall 2021, ME 106, SJSU
Spartan Racing
Automatic Wire Cutter by:
Ulises Chavarria
Josue Garcia
Francis Supnet
"""
# main.py
# Main script used throughout project
# Imports other helper scripts
from machine import Pin,PWM
import time
# project files
import encoder_v3
import cutter_v2
import tof_sensor
from pico_lcd import BL, LCD_1inch14
def change_index(list, index, positive):
# A helper function that allows the user to modify the length of wire and amount of wire. Works in tangent with modify_value()
if(stage != 1):
return index
if(positive == True):
index += 1
else:
index -= 1
if(index > len(list)-1):
index = 0
elif(index < 0):
index = len(list) - 1
return index
def stage_modify(stage, num):
# A helper function that changes stages after certain processes such as a button press
# print("Stage mod num = {}".format(num))
if(num == 0):
return stage
stage += num
if(stage < 0):
stage = 0
if(stage > MAX_STAGE):
stage = 0
print("\nStage = {}, {}".format(stage, stage_titles[stage]))
return stage
def modify_value(list, index, positive):
# A helper function that keeps integer values from 0-9
if(stage != 1):
return
if(positive == True):
list[index] += 1
else:
list[index] -= 1
if(list[index] < 0):
list[index] = 9
elif(list[index] > 9):
list[index] = 0
def list2float(list):
# Converts the first 3 elements of a list into a float such as [3,4,7] -> 34.7
number = 0
number += list[0] * 10
number += list[1]
number += list[2] / 10
return number
def display_blue_box():
# A function that displays a blue outline on the Pico LCD, part of the GUI
LCD.hline(5,5,230,LCD.blue)
LCD.hline(5,130,230,LCD.blue)
LCD.vline(5,5,125,LCD.blue)
LCD.vline(235,5,125,LCD.blue)
def button_press(length_arr, amount, list_index, stage):
# An important function that programs the buttons on the Pico LCD
if(keyA.value() == 0): # A
LCD.fill_rect(208,12,20,20,LCD.red)
stage = stage_modify(stage, 1)
else :
LCD.fill_rect(208,12,20,20,LCD.white)
LCD.rect(208,12,20,20,LCD.red)
if(keyB.value() == 0): # B
LCD.fill_rect(208,103,20,20,LCD.red)
stage = stage_modify(stage, -1)
else :
LCD.fill_rect(208,103,20,20,LCD.white)
LCD.rect(208,103,20,20,LCD.red)
if(key2.value() == 0): # UP
LCD.fill_rect(37,35,20,20,LCD.red)
modify_value(length_arr, list_index, True)
else :
LCD.fill_rect(37,35,20,20,LCD.white)
LCD.rect(37,35,20,20,LCD.red)
if(key5.value() == 0): # DOWN
LCD.fill_rect(37,85,20,20,LCD.red)
modify_value(length_arr, list_index, False)
else :
LCD.fill_rect(37,85,20,20,LCD.white)
LCD.rect(37,85,20,20,LCD.red)
if(key4.value() == 0): # LEFT
LCD.fill_rect(12,60,20,20,LCD.red)
list_index = change_index(length_arr, list_index, False)
else :
LCD.fill_rect(12,60,20,20,LCD.white)
LCD.rect(12,60,20,20,LCD.red)
if(key6.value() == 0): # RIGHT
LCD.fill_rect(62,60,20,20,LCD.red)
list_index = change_index(length_arr, list_index, True)
else :
LCD.fill_rect(62,60,20,20,LCD.white)
LCD.rect(62,60,20,20,LCD.red)
if(key3.value() == 0): # CENTER
LCD.fill_rect(37,60,20,20,LCD.red)
else :
LCD.fill_rect(37,60,20,20,LCD.white)
LCD.rect(37,60,20,20,LCD.red)
amount = length_arr[3]
return length_arr, amount, list_index, stage
def display_text(stage):
# An important function that displays the GUI on the Pico LCD
if(stage == 0):
# LCD = LCD_1inch14()
#color BRG
LCD.fill(LCD.white)
LCD.text("Raspberry Pi Pico",10,10,LCD.red)
LCD.text("PicoGo",10,30,LCD.green)
LCD.text("Pico-LCD-1.14",10,50,LCD.blue)
LCD.text("Spartan Racing - F21 ME106", 10, 70, LCD.black)
LCD.text("Ulises Chavarria", 10, 90, LCD.black)
LCD.text("Josue Garcia", 10, 100, LCD.black)
LCD.text("Francis Supnet", 10, 110, LCD.black)
LCD.fill_rect(208,12,20,20,LCD.white)
LCD.text("Next", 174, 14, LCD.blue)
LCD.rect(208,12,20,20,LCD.red)
else:
length_float = list2float(length_arr)
LCD.text("Length = {0:.1f} in".format(length_float),85,80,LCD.black)
LCD.text("Amount = {}".format(amount),85,100,LCD.black)
LCD.text("Next", 174, 14, LCD.blue)
LCD.text("Back", 174, 115, LCD.blue)
if(stage == 1):
LCD.text("Index = {}".format(list_index),85,40,LCD.black)
LCD.text("Length = {} in".format(length_arr[:3]),85,60,LCD.black)
# LCD.text("Length = {} in".format(length_str),85,80,LCD.black)
display_index()
elif(stage == 2):
LCD.text("Confirm values", 85, 40, LCD.black)
elif(stage == 3):
LCD.text("Checking ToF", 85, 40, LCD.black)
elif(stage == 4):
LCD.text("Activating motor", 85, 40, LCD.black)
elif(stage == 6):
global stage5_flash
if(stage5_flash):
LCD.fill(LCD.red)
stage5_flash = False
elif(not stage5_flash):
LCD.fill(LCD.white)
stage5_flash = True
LCD.text("CAUTION!!!", 85, 40, LCD.black)
LCD.text("Cutting wire", 85, 60, LCD.black)
LCD.text("CAUTION!!!", 85, 80, LCD.black)
elif(stage == 8):
LCD.fill(LCD.green)
LCD.text("Done", 85, 20, LCD.black)
LCD.text("Return", 160, 14, LCD.blue)
LCD.fill_rect(208,12,20,20,LCD.white)
LCD.rect(208,12,20,20,LCD.red)
def display_index():
# A helper function to create a visual indication during the data acquisition stage
x = 140
y = 55
if(stage == 1):
if(list_index == 0):
x += 20
elif(list_index == 1):
x += 42
elif(list_index == 2):
x += 64
elif(list_index == 3):
# Fill this out for 3rd index, amount
x = 150
y = 95
LCD.rect(x, y, 23, 18, LCD.blue)
def default_values():
# Used after completing a full cycle to reset all values to default
global length_arr, amount, list_index, length_float, display_wait, wire_inside, check_tof
length_arr = [0, 0, 0, 1]
amount = length_arr[3]
list_index = 0
length_float = list2float(length_arr)
display_wait = 3
wire_inside = False
check_tof = True
stage = 0
stage_titles = ["Boot up stage", "User input stage", "Data confirmation stage", "ToF check stage", "Activating motor stage (display)", "Activating motor stage", "Wire cutting stage (display)", "Wire cutting stage", "Done"]
MAX_STAGE = len(stage_titles) - 1 # 9, but index starts at 0, so modify to 8
stage5_flash = True
length_arr = [0, 0, 0, 1]
amount = length_arr[3]
list_index = 0
length_float = list2float(length_arr)
stage = stage_modify(stage, 0)
display_wait = 3
wire_inside = False
check_tof = True
if __name__=='__main__':
# Essentially the main function of the program.
# Drives the most important level of logic throughout the program and calls the helper functions
pwm = PWM(Pin(BL))
pwm.freq(1000)
pwm.duty_u16(32768) # max 65535
LCD = LCD_1inch14()
keyA = Pin(15, Pin.IN, Pin.PULL_UP) # A
keyB = Pin(17, Pin.IN, Pin.PULL_UP) # B
key2 = Pin(2, Pin.IN, Pin.PULL_UP) # UP
key3 = Pin(3, Pin.IN, Pin.PULL_UP) # CENTER
key4 = Pin(16, Pin.IN, Pin.PULL_UP) # LEFT
key5 = Pin(18, Pin.IN, Pin.PULL_UP) # DOWN
key6 = Pin(20, Pin.IN, Pin.PULL_UP) # RIGHT
default_values()
while(True):
LCD.fill(LCD.white)
# Erases portions of the LCD each cycle
LCD.rect(84, 35, 145, 60, LCD.white)
LCD.fill_rect(84, 35, 145, 60, LCD.white)
LCD.fill_rect(150, 95, 23, 18, LCD.white)
display_blue_box()
length_arr, amount, list_index, stage = button_press(length_arr, amount, list_index, stage)
length_float = list2float(length_arr)
display_text(stage)
LCD.show
if(stage == 0):
# Home screen stage
pass
elif(stage == 1):
# User input stage
pass
elif(stage == 2):
# User confirmation stage
if(not check_tof):
stage = stage_modify(stage, 0)
check_tof = True
pass
elif(stage == 3):
# Checking ToF stage
wire_inside = tof_sensor.main(check_tof)
check_tof = False
tof_sensor.stop()
if(wire_inside):
stage = stage_modify(stage, 1)
else:
LCD.fill(LCD.red)
LCD.text("No wire detected", 85, 60, LCD.black)
LCD.show()
time.sleep(0.5)
stage = stage_modify(stage, -1)
pass
elif(stage == 4):
# Activating motor stage (DISPLAY)
# print("(Main) Length to cut = {}".format(length_float))
if(display_wait > 0):
display_wait -= 1
elif(display_wait <= 0):
display_wait = 3
stage = stage_modify(stage, 1)
pass
elif(stage == 5):
# Activating motor stage
stage_setter = encoder_v3.main(length_float)
stage = stage_modify(stage, 1)
elif(stage == 6):
# Wire cutting stage (DISPLAY)
if(display_wait > 0):
display_wait -= 1
elif(display_wait <= 0):
display_wait = 3
stage = stage_modify(stage, 1)
pass
elif(stage == 7):
# Wire cutting stage
cutting_done = cutter_v2.main()
if(cutting_done):
length_arr[3] -= 1 # decreasing amount by 1
if(amount > 1 and cutting_done):
# stage = 3
check_tof = True
stage = stage_modify(stage, -4)
elif(amount == 1 and cutting_done):
# stage = 8
stage = stage_modify(stage, 1)
else:
print("Did not pass pressure check... Retrying...")
stage = stage_modify(stage, 0)
elif(stage == 8):
# Done stage. Wires have been cut. Reset values
default_values()
pass
else:
# Some sort of error happened to reach this point
print("Entered else conditional for stages.")
stage = 0
LCD.show()
time.sleep(0.1)