-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpress_logic.py
More file actions
279 lines (255 loc) · 10.5 KB
/
Copy pathpress_logic.py
File metadata and controls
279 lines (255 loc) · 10.5 KB
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
# press_logic.py
import asyncio
import RPi.GPIO as GPIO
#import threading
from hardware import setup_gpio
import hardware
from config import PIN_SPIN_LEFT, PIN_SPIN_RIGHT, PIN_INFLATE, PIN_DEFLATE, SPIN_ROTATION, PIN_COMPRESSOR, PIN_EXTERNAL_COMPRESSOR, ONBOARD_COMPRESSOR
from utils import printBox
from controller import running_tasks
import status
import time
import status
# System flags
spinning_flag = 0
pressure_flag = 0
program_flag = 0
emerg_flag = 0
setup_gpio()
#-----SPIN CLASS-----#
class Spin:
async def left():
task = asyncio.current_task()
running_tasks.add(task)
global spinning_flag, pressure_flag, program_flag
try:
if pressure_flag == 0 and program_flag == 0:
if GPIO.input(PIN_SPIN_RIGHT):
if GPIO.input(PIN_SPIN_LEFT):
printBox("Turning Left")
status.current_action = "Turning Left"
GPIO.output(PIN_SPIN_LEFT, GPIO.LOW)
spinning_flag = 1
while not task.cancelled(): # Keep spinning until cancelled
await asyncio.sleep(0.1)
else:
printBox("Stop Turning Left")
status.current_action = ""
GPIO.output(PIN_SPIN_LEFT, GPIO.HIGH)
spinning_flag = 0
else:
printBox("ERROR: Can't spin while adjusting pressure!")
finally:
GPIO.output(PIN_SPIN_LEFT, GPIO.HIGH)
spinning_flag = 0
running_tasks.discard(task)
async def right():
task = asyncio.current_task()
running_tasks.add(task)
global spinning_flag, pressure_flag, program_flag
try:
if pressure_flag == 0 and program_flag == 0:
if GPIO.input(PIN_SPIN_LEFT):
if GPIO.input(PIN_SPIN_RIGHT):
printBox("Turning Right")
status.current_action = "Turning Right"
GPIO.output(PIN_SPIN_RIGHT, GPIO.LOW)
spinning_flag = 1
while not task.cancelled(): # Keep spinning until cancelled
await asyncio.sleep(0.1)
else:
printBox("Stop Turning Right")
status.current_action = ""
GPIO.output(PIN_SPIN_RIGHT, GPIO.HIGH)
spinning_flag = 0
else:
("ERROR: Can't spin while adjusting pressure!")
finally:
GPIO.output(PIN_SPIN_RIGHT, GPIO.HIGH)
spinning_flag = 0
running_tasks.discard(task)
#-----PRESSURE CLASS-----#
class Pressure:
async def inflate():
global spinning_flag, pressure_flag, program_flag
task = asyncio.current_task()
running_tasks.add(task)
try:
if spinning_flag == 0 and pressure_flag != 2 and program_flag == 0:
if all(GPIO.input(pin) for pin in PIN_INFLATE):
pressure_flag = 1
printBox("Pressure On")
status.current_action = "Inflating"
if ONBOARD_COMPRESSOR:
GPIO.output(PIN_COMPRESSOR, GPIO.LOW)
for pin in PIN_INFLATE:
GPIO.output(pin, GPIO.LOW)
for pin in PIN_DEFLATE:
GPIO.output(pin, GPIO.HIGH)
while not task.cancelled():
await asyncio.sleep(0.1) # Keeps task alive
else:
pressure_flag = 0
printBox("Pressure Off")
status.current_action = ""
if ONBOARD_COMPRESSOR:
GPIO.output(PIN_COMPRESSOR, GPIO.HIGH)
for pin in PIN_INFLATE:
GPIO.output(pin, GPIO.HIGH)
else:
printBox("ERROR: Can't inflate while spinning or changing pressure")
finally:
pressure_flag = 0
for pin in PIN_INFLATE:
GPIO.output(pin, GPIO.HIGH)
if ONBOARD_COMPRESSOR:
GPIO.output(PIN_COMPRESSOR, GPIO.HIGH)
running_tasks.discard(task)
#printBox("Inflate task cleaned up")
async def deflate():
global spinning_flag, pressure_flag, program_flag
task = asyncio.current_task()
running_tasks.add(task)
try:
if spinning_flag == 0 and pressure_flag != 1 and program_flag == 0:
if all(GPIO.input(pin) for pin in PIN_DEFLATE):
pressure_flag = 2
printBox("Vacuum On")
status.current_action = "Deflating"
GPIO.output(PIN_COMPRESSOR, GPIO.LOW)
for pin in PIN_INFLATE:
GPIO.output(pin, GPIO.HIGH)
for pin in PIN_DEFLATE:
GPIO.output(pin, GPIO.LOW)
while not task.cancelled():
await asyncio.sleep(0.1)
else:
pressure_flag = 0
printBox("Vacuum Off")
status.current_action = ""
GPIO.output(PIN_COMPRESSOR, GPIO.HIGH)
for pin in PIN_DEFLATE:
GPIO.output(pin, GPIO.HIGH)
else:
printBox("ERROR: Can't deflate while spinning or changing pressure")
finally:
pressure_flag = 0
for pin in PIN_DEFLATE:
GPIO.output(pin, GPIO.HIGH)
GPIO.output(PIN_COMPRESSOR, GPIO.HIGH)
running_tasks.discard(task)
#printBox("Deflate task cleaned up")
@staticmethod
async def inflateToBar(target_bar, get_current_bar):
global pressure_flag, emerg_flag
printBox(f"target = {target_bar}")
task = asyncio.current_task()
running_tasks.add(task)
pressure_flag = 1
start_time = asyncio.get_event_loop().time()
try:
while True:
current_bar = status.pressure_data
if emerg_flag == 1 or task.cancelled():
printBox("Emergency detected. Cancelling inflation.")
break
if current_bar >= target_bar:
break
for pin in PIN_INFLATE:
status.current_action = (f"Inflating to: {target_bar}")
GPIO.output(pin, GPIO.LOW)
if ONBOARD_COMPRESSOR:
GPIO.output(PIN_COMPRESSOR, GPIO.LOW)
await asyncio.sleep(1)
finally:
for pin in PIN_INFLATE:
status.current_action = ""
GPIO.output(pin, GPIO.HIGH)
if ONBOARD_COMPRESSOR:
GPIO.output(PIN_COMPRESSOR, GPIO.HIGH)
pressure_flag = 0
running_tasks.discard(task)
if emerg_flag == 1 or task.cancelled():
return None
elapsed_time = asyncio.get_event_loop().time() - start_time
return elapsed_time
@staticmethod
async def deflateToBar(target_bar, get_current_bar):
global pressure_flag, emerg_flag
printBox(f"Deflating to: {target_bar}")
task = asyncio.current_task()
running_tasks.add(task)
pressure_flag = 1
start_time = asyncio.get_event_loop().time()
try:
while True:
current_bar = status.pressure_data
if emerg_flag == 1 or task.cancelled():
printBox("Emergency detected. Cancelling inflation.")
break
if current_bar <= target_bar:
break
for pin in PIN_DEFLATE:
status.current_action = (f"Deflating to: {target_bar}")
GPIO.output(pin, GPIO.LOW)
GPIO.output(PIN_COMPRESSOR, GPIO.LOW)
await asyncio.sleep(1)
finally:
for pin in PIN_DEFLATE:
GPIO.output(pin, GPIO.HIGH)
GPIO.output(PIN_COMPRESSOR, GPIO.HIGH)
pressure_flag = 0
running_tasks.discard(task)
if emerg_flag == 1 or task.cancelled():
return None
elapsed_time = asyncio.get_event_loop().time() - start_time
return elapsed_time
async def spin_to_location(loc, label):
printBox(f"spin_to_location - {label}, time: {loc}")
status.current_action = (f"Rotating to: {label}")
global spinning_flag, pressure_flag, program_flag
spinning_flag = 1
GPIO.output(SPIN_ROTATION, GPIO.LOW)
printBox("Waiting for bump to start timing")
# Reset rotation count to 0 and wait for it to reach 1
hardware.rotationCount = 0
while hardware.rotationCount < 1:
#printBox(f"rotation count: {hardware.rotationCount}")
await asyncio.sleep(0.01)
printBox(f"Bump detected - spinning for {loc:.2f} seconds")
task = asyncio.current_task()
running_tasks.add(task)
try:
await asyncio.sleep(loc)
status.current_action = ""
except asyncio.CancelledError:
printBox(f"spin_to_location ({label}) cancelled")
finally:
GPIO.output(SPIN_ROTATION, GPIO.HIGH)
hardware.rotationCount = 0
spinning_flag = 0
running_tasks.discard(task)
printBox(f"Arrived at {label}")
async def hold_pressure(max_pressure, reset_pressure, pressure_time):
printBox(f"⏱ Holding pressure at {max_pressure:.2f} BAR for {pressure_time:.1f} sec (reset if < {reset_pressure:.2f})")
start_time = time.time()
try:
while time.time() - start_time < pressure_time:
current_bar = status.pressure_data
printBox(f"🔍 Current BAR: {current_bar:.2f}")
if current_bar < reset_pressure:
printBox(f"⚠️ Pressure dropped to {current_bar:.2f} — re-inflating to {max_pressure:.2f}")
await Pressure.inflateToBar(max_pressure, status.pressure_data)
await asyncio.sleep(0.5)
printBox("hold pressure time reached.")
except asyncio.CancelledError:
printBox("⛔ hold_pressure cancelled")
async def breakup_rotations(n):
hardware.rotationCount = 0
GPIO.output(SPIN_ROTATION, GPIO.LOW)
printBox(f"BREAKUP ROTATIONS x {n}")
while True:
if hardware.rotationCount >= n:
await asyncio.sleep(1) # brief pause
GPIO.output(SPIN_ROTATION, GPIO.HIGH)
break