-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.py
151 lines (108 loc) · 3.36 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
# This is where the magic happens!
# This file is executed on every boot (including wake-boot from deepsleep)
# Created By: Michael Pham
"""
Built for the PySquared FC Board
Version: 2.0.0
Published: Nov 19, 2024
"""
import time
import microcontroller
import lib.pysquared.nvm.register as register
import lib.pysquared.pysquared as pysquared
from lib.pysquared.config import Config
from lib.pysquared.logger import Logger
from lib.pysquared.nvm.counter import Counter
logger: Logger = Logger(
error_counter=Counter(index=register.ERRORCNT, datastore=microcontroller.nvm)
)
logger.info("Booting", software_version="2.0.0", published_date="November 19, 2024")
loiter_time: int = 5
try:
for i in range(loiter_time):
logger.info(f"Code Starting in {loiter_time-i} seconds")
time.sleep(1)
logger.debug("Initializing Config")
config: Config = Config()
c = pysquared.Satellite(config, logger)
c.watchdog_pet()
import gc # Garbage collection
import lib.pysquared.functions as functions
f = functions.functions(c, logger, config)
def initial_boot():
c.watchdog_pet()
f.beacon()
c.watchdog_pet()
f.listen()
c.watchdog_pet()
try:
c.boot_count.increment()
logger.info(
"FC Board Stats",
bytes_remaining=gc.mem_free(),
boot_number=c.boot_count.get(),
)
initial_boot()
except Exception as e:
logger.error("Error in Boot Sequence", err=e)
finally:
pass
def send_imu_data():
logger.info("Looking to get imu data...")
IMUData = []
c.watchdog_pet()
logger.info("IMU has baton")
IMUData = f.get_imu_data()
c.watchdog_pet()
f.send(IMUData)
def main():
f.beacon()
f.listen_loiter()
f.state_of_health()
f.listen_loiter()
f.all_face_data()
c.watchdog_pet()
f.send_face()
f.listen_loiter()
send_imu_data()
f.listen_loiter()
f.joke()
f.listen_loiter()
def critical_power_operations():
initial_boot()
c.watchdog_pet()
f.Long_Hybernate()
def minimum_power_operations():
initial_boot()
c.watchdog_pet()
f.Short_Hybernate()
######################### MAIN LOOP ##############################
try:
while True:
# L0 automatic tasks no matter the battery level
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)
main()
elif c.power_mode == "maximum":
c.RGB = (0, 255, 0)
main()
else:
f.listen()
except Exception as e:
logger.critical("Critical in Main Loop", err=e)
time.sleep(10)
microcontroller.on_next_reset(microcontroller.RunMode.NORMAL)
microcontroller.reset()
finally:
logger.info("Going Neutral!")
c.RGB = (0, 0, 0)
c.hardware["WDT"] = False
except Exception as e:
logger.error("An exception occured within main.py", err=e)