-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.py
177 lines (133 loc) · 4.32 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
# 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 gc
import time
import digitalio
import microcontroller
try:
from board_definitions import proveskit_rp2040_v4 as board
except ImportError:
import board
import lib.pysquared.functions as functions
import lib.pysquared.nvm.register as register
import lib.pysquared.pysquared as pysquared
from lib.pysquared.config.config import Config
from lib.pysquared.hardware.digitalio import initialize_pin
from lib.pysquared.hardware.rfm9x.factory import RFM9xFactory
from lib.pysquared.hardware.rfm9x.manager import RFM9xManager
from lib.pysquared.logger import Logger
from lib.pysquared.nvm.counter import Counter
from lib.pysquared.nvm.flag import Flag
from lib.pysquared.rtc.rtc_common import RTC
from lib.pysquared.sleep_helper import SleepHelper
from version import __version__
RTC.init()
logger: Logger = Logger(
error_counter=Counter(index=register.ERRORCNT, datastore=microcontroller.nvm),
colorized=False,
)
logger.info("Booting", software_version=__version__, 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("config.json")
c = pysquared.Satellite(config, logger, __version__)
c.watchdog_pet()
sleep_helper = SleepHelper(c, logger)
radio_manager = RFM9xManager(
logger,
Flag(index=register.FLAG, bit_index=7, datastore=microcontroller.nvm),
RFM9xFactory(
c.spi0,
initialize_pin(logger, board.SPI0_CS0, digitalio.Direction.OUTPUT, True),
initialize_pin(logger, board.RF1_RST, digitalio.Direction.OUTPUT, True),
config.radio,
),
)
f = functions.functions(c, logger, config, sleep_helper, radio_manager)
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", 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()
sleep_helper.long_hibernate()
def minimum_power_operations():
initial_boot()
c.watchdog_pet()
sleep_helper.short_hibernate()
######################### 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", 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.critical("An exception occured within main.py", e)