-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
458 lines (426 loc) · 18.9 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
from pyb import UART
from pyb import Pin
from pyb import Timer
#from pyb import PWM
import micropython
import task_share
import task_share
import cotask
import Encoder
import L6206
import gc
import controller
import miscmethods
import battleship
import time
import MotorDriver
txtfile = open("labx05_ui_help_commands.txt", "r")
ui_help = txtfile.read()
# in order to keep this organized, put all methods in the 'miscmethods' folder - main.py is only for main and tasks
'''
FIRING TASK
- Manages actually shooting the gun
- Runs brushless DC motors and ball feed motor
State 0: Init
State 1: Wait to see if we are in autoplay mode
State 2: Go to Xpos and start flywheels
State 3: Load Ball
State 4: Fire
State 5: return to home(origin)
'''
def firing(vcp):
state = 0
while True:
if state == 0:
#esc.pulse_width_percent(0)
count = 0
state += 1
elif state == 1:
if(a_flag.get() == 1 and shoot_flag.get() == 1):
state = 2
else:
state = 1
elif state == 2:
set_flag.put(1)
count += 1
if(count == 4000):
count = 0
state = 3
elif state == 3:
set_flag.put(1)
bucket_des.put(50)
count += 1
if (count == 260):
count = 0
state = 4
bucket_des.put(0)
elif state == 4:
feed_des.put(4000)
set_flag.put(1)
count += 1
if(count == 500):
count = 0
state = 5
elif state == 5:
x_des.put(0)
speed_des.put(0)
set_flag.put(1)
count += 1
if(count == 3000):
count = 0
state = 1
shoot_flag.put(0)
yield(0)
'''
MOVEMENT TASK
- Manages all aiming movement of the mechanism; getting from point a to b
- Spins motors, reads encoders, runs controllers
State 0: Init
State 1: Wait/Run Controllers
State 2: Zero encoders
State 3: Set controller targets
'''
def movement(vcp):
state = 0
bcounter = 0
fcounter = 0
while True:
#update the encoders every pass through here
x_enc.update()
feed_enc.update()
#init stuff
if state == 0:
x_des.put(0)
feed_des.put(0)
x_controller.set_Target(0)
feed_controller.set_Target(0)
x_controller.ki = .0001
x_controller.kp = 1
feed_controller.ki = 0
feed_controller.kp = .05
state += 1
#maintains controllers and duties
elif state == 1:
#run pi controllers
x_mot.set_duty(x_controller.pi(-x_enc.position))
set_pt = feed_controller.pi(feed_enc.position)
feed_mot.set_duty(miscmethods.saturate(set_pt,50,-55))
#control feeding a single ball
if bucket_des.get() != 0:
bcounter += 1
bcount.put(bcounter)
if bcounter == 237:
bucket_des.put(0)
set_flag.put(1)
bcounter = 0
bcount.put(bcounter)
#control firing arm
if feed_des.get() != 0 and feed_des.get() != -4000:
fcounter +=1
if fcounter == 100:
feed_des.put(-4000)
set_flag.put(1)
fcounter = 0
if z_flag.get() == 1:
state = 2
elif set_flag.get() == 1:
state = 3
#zero all the encoders and set controller positions here
elif state == 2:
x_enc.zero()
feed_enc.zero()
x_des.put(0)
feed_des.put(0)
z_flag.put(0)
state = 3
#sets the controller targets and the duties
elif state == 3:
x_controller.set_Target(x_des.get()*67.5)
feed_controller.set_Target(feed_des.get())
esc.pulse_width_percent(float(speed_des.get())/20+5)
bucket_mot.set_duty(bucket_des.get())
state = 1
set_flag.put(0)
yield(0)
'''
UI TASK
- Takes all user input and interprets it
- Is the only task that can print to terminal
State 0: Init
State 1: Startup mode
State 2: Gameplay mode
'''
def ui(vcp,lookup,fullin):
state = 0
settings = 0 # Used to store x-pos and speed for a particular set of coord from lookup
while True:
if state == 0:
vcp.write('\033c')
vcp.write(ui_help)
state += 1
elif state == 1:
#wait for user input
if vcp.any():
input = vcp.read(1).decode()
#handling submenu stuff
#set jog value
if j_flag.get() == 1:
charin = input
#take numerical input
if charin in {'\n','\r'}:
try:
numinput = miscmethods.numerical_input(vcp,-1000,1000,fullin)
pos_new = x_des.get() + int(numinput)
x_des.put(pos_new)
set_flag.put(1)
vcp.write('\n\rX motor jogging by ' + str(numinput) + ' mm to a position of ' + str(x_des.get()) + ' mm')
state = 1
j_flag.put(0)
input = ''
fullin.clear()
except IndexError:
vcp.write('\n\rNumber not within acceptable bounds')
input = ''
fullin.clear()
except TypeError:
vcp.write('\n\rInput a numerical value')
input = ''
fullin.clear()
else:
fullin.append(charin)
vcp.write(charin)
input = ''
elif s_flag.get() == 1:
charin = input
#take numerical input
if charin in {'\n','\r'}:
try:
numinput = miscmethods.numerical_input(vcp,0,100,fullin)
speed_des.put(float(numinput))
state = 1
input = ''
fullin.clear()
s_flag.put(0)
set_flag.put(1)
vcp.write('\n\rBrushless motor speed set to ' + str(numinput) + '%')
except IndexError:
vcp.write('\n\rNumber not within acceptable bounds')
input = ''
fullin.clear()
except TypeError:
vcp.write('\n\rInput a numerical value')
input = ''
fullin.clear()
except:
vcp.write('\n\rSome other error happened')
else:
fullin.append(charin)
vcp.write(charin)
input = ''
#take setup command input
#take z input
elif (input == 'h') or (input == 'H'):
vcp.write('\033c')
vcp.write(ui_help)
elif (input == 'z') or (input == 'Z'):
z_flag.put(1)
input = ''
vcp.write('\n\rZeroing all encoders')
elif (input == 'o') or (input == 'O'):
x_des.put(0)
feed_des.put(0)
set_flag.put(1)
vcp.write('\n\rHoming the machine')
input = ''
elif (input == 'p') or (input == 'P'):
input = ''
vcp.write('\n\rX: ' + str(x_des.get()) + ' mm Firing Arm: ' + str(feed_des.get()) + ' ticks Speed: ' + str(speed_des.get()) + '%')
#take j input
elif (input == 'j') or (input == 'J'):
j_flag.put(1)
vcp.write('\n\rHow much would you like to jog the motor?\n\r')
input = ''
#take s input
elif (input == 's') or (input == 'S'):
s_flag.put(1)
vcp.write('\n\rInput desired speed for the brushless motors')
input = ''
#take l input
elif (input == 'l') or (input == "L"):
vcp.write('\n\rLoading a ball')
bucket_des.put(50)
set_flag.put(1)
input = ''
#take f input
elif (input == 'f') or (input == "F"):
vcp.write('\n\rFiring')
feed_des.put(4000)
set_flag.put(1)
input = ''
#take g input
elif (input == 'a') or (input == 'A'):
state = 2
vcp.write('\n\rWhen it is my turn, press "y", if we won press "w", if we lost press "l"')
input = ''
a_flag.put(1)
#take bad input
else:
vcp.write(input + '\n\r')
vcp.write('\n\rInvalid input. Please read the table again you illiterate moron.\n\r')
elif state == 2:
if vcp.any():
input = vcp.read(1).decode()
vcp.write(input)
#take an answer of if it's our turn/if we won
if (input == 'y') or (input == 'Y'):
coords = battleShip.calc_shot_twostate()
settings = lookup[tuple(coords)]
x_des.put(settings[0])
speed_des.put(settings[1])
vcp.write('\r\nX-Pos set to: ' + str(x_des.get()) + '\r\n')
vcp.write('Speed set to: ' + str(speed_des.get()) + '\r\n')
vcp.write('Shooting at ' + str(coords) + '\n\rDid we hit? h = hit, m = miss, n = bad shot\r\n')
shoot_flag.put(1)
input = ''
elif (input == 'w') or (input == 'W'):
vcp.write('\n\rTo the other team: get fucked losers')
input = ''
elif (input == 'l') or (input == 'L'):
vcp.write('\n\rTheir win was illegitimate')
input = ''
elif (input == 'h') or (input == 'H'):
battleShip.board[coords[0]][coords[1]] = 1
coords = battleShip.calc_shot_rand()
settings = lookup[tuple(coords)]
x_des.put(settings[0])
speed_des.put(settings[1])
vcp.write('\r\nX-Pos set to: ' + str(x_des.get()) + '\r\n')
vcp.write('Speed set to: ' + str(speed_des.get()) + '\r\n')
vcp.write('Shooting at ' + str(coords) + '\n\rDid we hit? h = hit, m = miss, n = bad shot\r\n')
shoot_flag.put(1)
input = ''
elif (input == 'm') or (input == 'M'):
battleShip.board[coords[0]][coords[1]] = -1
coords = battleShip.calc_shot_twostate()
settings = lookup[tuple(coords)]
x_des.put(settings[0])
speed_des.put(settings[1])
vcp.write('\r\nX-Pos set to: ' + str(x_des.get()) + '\r\n')
vcp.write('Speed set to: ' + str(speed_des.get()) + '\r\n')
vcp.write('Shooting at ' + str(coords) + '\n\rDid we hit? h = hit, m = miss, n = bad shot\r\n')
shoot_flag.put(1)
input = ''
elif (input == 'n') or (input == 'N'):
vcp.write('Bad Shot, re-taking shot')
x_des.put(settings[0])
speed_des.put(settings[1])
vcp.write('\r\nX-Pos set to: ' + str(x_des.get()) + '\r\n')
vcp.write('Speed set to: ' + str(speed_des.get()) + '\r\n')
vcp.write('Shooting at ' + str(coords) + '\n\rDid we hit? h = hit, m = miss, n = bad shot\r\n')
shoot_flag.put(1)
input = ''
elif (input == 'q') or (input == 'Q'):
state = 1
vcp.write('Switched to manual mode')
a_flag.put(0)
shoot_flag.put(0)
input = ''
else:
vcp.write('\n\rBad input, read the prompt you dunce')
yield(0)
if __name__ == "__main__":
#define lookup table - [coords] : [x_pos,brushless_speed]
lookup = {
tuple([0,0]): [60,8.7], #D1
tuple([0,1]): [60,9.4], #C1
tuple([0,2]): [60,10], #B1
tuple([0,3]): [60,10.35], #A1
tuple([1,0]): [340,8.7], #D2
tuple([1,1]): [340,9.4], #C2
tuple([1,2]): [340,10], #B2
tuple([1,3]): [340,10.35], #A2
tuple([2,0]): [690,8.7], #D3
tuple([2,1]): [690,9.4], #C3
tuple([2,2]): [690,10], #B3
tuple([2,3]): [690,10.35], #A3
tuple([3,0]): [820,8.7], #D4
tuple([3,1]): [820,9.4], #C4
tuple([3,2]): [820,10], #B4
tuple([3,3]): [820,10.35] #A4
}
#make shares/queues
x_des = task_share.Share ('h', thread_protect = False, name = "x_des") # desired x pos [mm]
feed_des = task_share.Share ('h', thread_protect = False, name = "feed_des") # desired feed angle [rad]
bucket_des = task_share.Share ('h', thread_protect = False, name = "bucket_des") # desired bucket motor angle [rad]
shot = task_share.Share ('h', thread_protect = False, name = "shot") # contains motor positions/speeds for the shot
jog = task_share.Share ('h', thread_protect = False, name = "jog") # contains how much to jog the selected motor
bl_speed = task_share.Share ('h', thread_protect = False, name = "bl_speed") # contains brushless motor speed
speed_des = task_share.Share ('f', thread_protect = False, name = "speed_des") # desired brushless speed
#flags
z_flag = task_share.Share ('h', thread_protect = False, name = "z_flag") # flag that says to zero everything
m_flag = task_share.Share ('h', thread_protect = False, name = "m_flag") # flag that says to switch to motor selection menu
j_flag = task_share.Share ('h', thread_protect = False, name = "j_flag") # flag that says to switch to jog menu
a_flag = task_share.Share ('h', thread_protect = False, name = "j_flag") # flag that says whether autoplay is on/off
s_flag = task_share.Share ('h', thread_protect = False, name = "s_flag") # flag that says to switch to brushless speed menu
shoot_flag = task_share.Share ('h', thread_protect = False, name = "shooting_flag") # flag that says the shot is being taken
set_flag = task_share.Share ('h', thread_protect = False, name = "set_flag") # flag that says to set controller targets
bcount = task_share.Share ('h', thread_protect = False, name = "bcount_flag")
vcp = pyb.USB_VCP()
fullin = list()
# Create the tasks. If trace is enabled for any task, memory will be
# allocated for state transition tracing, and the application will run out
# of memory after a while and quit. Therefore, use tracing only for
# debugging and set trace to False when it's not needed
task_ui = cotask.Task(ui(vcp,lookup,fullin), name = 'Task_Ui', priority = 1, period = 5, profile = True, trace = False)
task_movement = cotask.Task(movement(vcp), name = 'Task_Movement', priority = 1, period = 5, profile = True, trace = False)
task_firing = cotask.Task(firing(vcp), name = 'Task_Firing', priority = 1, period = 5, profile = True, trace = False)
cotask.task_list.append(task_ui)
cotask.task_list.append(task_movement)
cotask.task_list.append(task_firing)
#making objects:
#battleship
battleShip = battleship.battleship()
#encoders
print('\033c')
print('\nEncoders Enabled')
tim_enc1 = Timer(4,period = 65535, prescaler = 0)
x_enc = Encoder.Encoder(tim_enc1,Pin.cpu.B7,Pin.cpu.B6)
tim_enc2 = Timer(8,period = 65535, prescaler = 0)
feed_enc = Encoder.Encoder(tim_enc2,Pin.cpu.C6,Pin.cpu.C7)
#tim_enc3 = Timer(6,period = 65535, prescaler = 0)
#feed_enc = Encoder.Encoder(tim_enc3,Pin.cpu.C7,Pin.cpu.C6) #need to set cpu pins for this one
#tim_enc4 = Timer(6,period = 65535, prescaler = 0) #need a different timer
#brushed motors
print('Brushed Motors Enabled')
tim_A = Timer(3, freq = 20000)
tim_B = Timer(2, freq = 20000)
tim_C = Timer(1, freq = 20000)
x_mot = L6206.L6206(tim_A, Pin.cpu.B4, Pin.cpu.B5, Pin.cpu.A10)
feed_mot = L6206.L6206(tim_B, Pin.cpu.A0, Pin.cpu.A1, Pin.cpu.C1)
bucket_mot = MotorDriver.MotorDriver(tim_C, Pin.cpu.A8, Pin.cpu.A9)
x_mot.enable()
feed_mot.enable() #enables both bucket and feed motor cause same pin
#brushless motors (80 mhz, 80 prescaler, each tick is 1 us)
print('Brushless Motors Enabled')
pin_for_ESC = Pin(Pin.cpu.A6, mode=Pin.OUT_PP)
#pin_for_leftESC = Pin(Pin.cpu.A9, mode=Pin.OUT_PP)
timer_pwm = Timer(16, freq=50)
# direction based from looking towards motors looking towards direction that ball shoots
#left_esc = timer_pwm.channel(2, mode=Timer.PWM, pin=pin_for_leftESC, pulse_width_percent=10)
esc = timer_pwm.channel(1, mode=Timer.PWM, pin=pin_for_ESC, pulse_width_percent=10)
#controllers
x_controller = controller.controller()
feed_controller = controller.controller()
#misc
input("Press enter once you have turned the power supply on")
esc.pulse_width_percent(5)
# Run the memory garbage collector to ensure memory is as defragmented as
# possible before the real-time scheduler is started
gc.collect()
try:
while(True):
cotask.task_list.pri_sched()
except KeyboardInterrupt:
print('\n\r\r> Program Terminated')
esc.pulse_width_percent(0)
x_mot.set_duty(0)