forked from inspireFly-VT/circuitpy_flight_software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
352 lines (274 loc) · 9.98 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
'''
Created by Nicole Maggard and Michael Pham 8/19/2022
Updated for Yearling by Nicole Maggard and Rachel Sarmiento 2/4/2023
This is where the processes get scheduled, and satellite operations are handeled
'''
print("Hi inspireFly")
from lib.pysquared import cubesat as c
import asyncio
import time
import traceback
import gc #Garbage collection
import microcontroller
import functions
from debugcolor import co
from easy_comms_circuit import EasyComms
import board
from FCB_class import FCBCommunicator
from lib import rfm9xfsk
def debug_print(statement):
if c.debug:
print(co("[MAIN]" + str(statement), 'blue', 'bold'))
f=functions.functions(c)
try:
debug_print("Boot number: " + str(c.c_boot))
debug_print(str(gc.mem_free()) + " Bytes remaining")
#power cycle faces to ensure sensors are on:
c.all_faces_off()
time.sleep(1)
c.all_faces_on()
#test the battery:
c.battery_manager()
f.beacon()
f.listen()
distance1=0
distance2=0
tries=0
loiter_time = 270
dutycycle=0.15
done=True
debug_print("(iF) burn attempt status stuff")
for i in range(1):
done = c.burn('1', dutycycle, 1000, 1.2)
time.sleep(0.5)
#done = c.burn('1', dutycycle, 1000, 1)
#done=c.smart_burn('1',dutycycle)
"""
******************ALL SMART_BURN CODE (commented out for testing)******************************
debug_print("Burn attempt status: " + str(c.burnarm))
debug_print("Burn status: " + str(c.burned))
while c.burned is False and tries < 3:
debug_print("Burn attempt try: " + str(tries+1))
if tries == 0:
debug_print("Loitering for " + str(loiter_time) + " seconds")
try:
c.neopixel[0] = (0,0,0)
purple = (200, 8, 200)
led_off = (0,0,0)
for step in range(0,loiter_time):
c.neopixel[0] = purple
time.sleep(0.5)
c.neopixel[0] = led_off
time.sleep(0.5)
debug_print(f"Entering full flight software in... {loiter_time-step} seconds")
except Exception as e:
debug_print("Error in Loiter Sequence: " + ''.join(traceback.format_exception(e)))
try:
debug_print("(iF) smart burn function is executing")
dutycycle=dutycycle+0.02
done=c.smart_burn('1',dutycycle)
tries+=1
except:
debug_print("couldnt burn on try " + str(tries+1))
if done is True:
debug_print("attempt passed without error!")
if c.burned is False and tries>=2:
debug_print("Ran Out of Smart Burn Attempts. Will Attempt automated burn...")
wait=0
while(wait<5):
wait+=1
time.sleep(1)
c.burn('1',0.28,1000,2)
else:
pass
else:
debug_print("burn failed miserably!")
break
******************************************************************************************
"""
f.beacon()
f.listen()
f.state_of_health()
f.listen()
c.battery_manager()
f.battery_heater()
c.battery_manager() #Second check to make sure we have enough power to continue
f.beacon()
f.listen()
f.state_of_health()
f.listen()
except Exception as e:
debug_print("Error in Boot Sequence: " + ''.join(traceback.format_exception(e)))
finally:
debug_print("All Faces off!")
c.all_faces_off()
def critical_power_operations():
f.beacon()
f.listen()
f.state_of_health()
f.listen()
f.Long_Hybernate()
def minimum_power_operations():
f.beacon()
f.listen()
f.state_of_health()
f.listen()
f.Short_Hybernate()
def normal_power_operations():
debug_print("Entering Norm Operations")
FaceData=[]
#Defining L1 Tasks
def check_power():
gc.collect()
c.battery_manager()
f.battery_heater()
c.check_reboot()
c.battery_manager() #Second check to make sure we have enough power to continue
if c.power_mode == 'normal' or c.power_mode == 'maximum':
pwr = True
if c.power_mode == 'normal':
c.RGB=(255,255,0)
else:
c.RGB=(0,255,0)
else:
pwr = False
debug_print(c.power_mode)
gc.collect()
return pwr
async def s_lora_beacon():
while check_power():
f.beacon()
f.listen()
f.state_of_health()
f.listen()
time.sleep(1) # Guard Time
await asyncio.sleep(30)
async def g_face_data():
while check_power():
FaceData=[]
try:
debug_print("Getting face data...")
FaceData = f.all_face_data()
for _ in range(0, len(FaceData)):
debug_print("Face " + str(_) + ": " + str(FaceData[_]))
except Exception as e:
debug_print('Outta time! ' + ''.join(traceback.format_exception(e)))
gc.collect()
await asyncio.sleep(60)
async def s_face_data():
await asyncio.sleep(20)
while check_power():
try:
debug_print("Looking to send face data...")
f.send_face()
except asyncio.TimeoutError as e:
debug_print('Outta time! ' + ''.join(traceback.format_exception(e)))
gc.collect()
await asyncio.sleep(200)
async def s_imu_data():
await asyncio.sleep(45)
while check_power():
try:
debug_print("Looking to get imu data...")
IMUData=[]
debug_print("IMU has baton")
IMUData = f.get_imu_data()
f.send(IMUData)
f.face_data_baton = False
except Exception as e:
debug_print('Outta time! ' + ''.join(traceback.format_exception(e)))
gc.collect()
await asyncio.sleep(100)
async def detumble():
await asyncio.sleep(300)
while check_power():
try:
debug_print("Looking to detumble...")
f.detumble()
debug_print("Detumble complete")
except Exception as e:
debug_print(f'Outta time!' + ''.join(traceback.format_exception(e)))
gc.collect()
await asyncio.sleep(300)
async def joke():
await asyncio.sleep(500)
while check_power():
try:
debug_print("Joke send go!")
f.joke()
if f.listen_joke():
f.joke()
debug_print("done!")
except Exception as e:
debug_print(f'Outta time!' + ''.join(traceback.format_exception(e)))
gc.collect()
await asyncio.sleep(500)
async def pcb_comms():
debug_print("Yapping to the PCB now - D")
#await asyncio.sleep(600)
# Initialize communication and FCBCommunicator
com1 = EasyComms(board.TX, board.RX, baud_rate=9600)
com1.start()
fcb_comm = FCBCommunicator(com1)
# Start interaction loop
while True:
overhead_command = com1.overhead_read()
# Set the command
command = 'chunk'
time.sleep(2)
if command.lower() == 'chunk':
fcb_comm.send_command("chunk")
if fcb_comm.wait_for_acknowledgment():
jpg_bytes = fcb_comm.send_chunk_request()
if jpg_bytes is not None:
fcb_comm.save_image(jpg_bytes)
command = 'end'
if command.lower() == 'end':
fcb_comm.end_communication()
time.sleep(3)
break
else:
print("Wrong command entered, try again.")
gc.collect()
await asyncio.sleep(600)
async def main_loop():
#log_face_data_task = asyncio.create_task(l_face_data())
t1 = asyncio.create_task(s_lora_beacon())
t2 = asyncio.create_task(s_face_data())
t3 = asyncio.create_task(s_imu_data())
t4 = asyncio.create_task(g_face_data())
t5 = asyncio.create_task(detumble())
#t6 = asyncio.create_task(joke())
t7 = asyncio.create_task(pcb_comms())
await asyncio.gather(t1,t2,t3,t4,t5,t7)
asyncio.run(main_loop())
######################### MAIN LOOP ##############################
try:
c.all_faces_on()
while True:
#L0 automatic tasks no matter the battery level
c.battery_manager()
c.check_reboot()
if c.power_mode == 'critical':
c.RGB=(0,0,0)
critical_power_operations()
elif c.power_mode == 'minimum':
c.RGB=(255,0,0)
minimum_power_operations()
elif c.power_mode == 'normal':
c.RGB=(255,255,0)
normal_power_operations()
elif c.power_mode == 'maximum':
c.RGB=(0,255,0)
normal_power_operations()
else:
f.listen()
except Exception as e:
debug_print("Error in Main Loop: " + ''.join(traceback.format_exception(e)))
time.sleep(10)
microcontroller.on_next_reset(microcontroller.RunMode.NORMAL)
microcontroller.reset()
finally:
debug_print("All Faces off!")
c.all_faces_off()
c.RGB=(0,0,0)