|
8 | 8 | Published: Nov 19, 2024 |
9 | 9 | """ |
10 | 10 |
|
11 | | -import asyncio |
| 11 | +import gc |
12 | 12 | import time |
13 | 13 |
|
14 | 14 | import digitalio |
|
53 | 53 | config: Config = Config("config.json") |
54 | 54 |
|
55 | 55 | c = pysquared.Satellite(config, logger, __version__) |
56 | | - |
57 | | - # Start the watchdog background task |
58 | | - c.start_watchdog_background_task() |
59 | | - |
| 56 | + c.watchdog_pet() |
60 | 57 | sleep_helper = SleepHelper(c, logger) |
61 | 58 |
|
62 | 59 | radio_manager = RFM9xManager( |
|
73 | 70 |
|
74 | 71 | f = functions.functions(c, logger, config, sleep_helper, radio_manager) |
75 | 72 |
|
76 | | - async def initial_boot(): |
| 73 | + def initial_boot(): |
| 74 | + c.watchdog_pet() |
77 | 75 | f.beacon() |
| 76 | + c.watchdog_pet() |
78 | 77 | f.listen() |
| 78 | + c.watchdog_pet() |
79 | 79 |
|
80 | | - async def critical_power_operations(): |
81 | | - await initial_boot() |
82 | | - # Convert blocking operation to a non-blocking one using run_in_executor |
83 | | - # Since this is a blocking function that will likely put the device to sleep, |
84 | | - # we accept that this will block the event loop as intended |
85 | | - logger.info("Entering long hibernation (this will block the event loop)") |
86 | | - sleep_helper.long_hibernate() |
87 | | - logger.info("Woke from long hibernation") |
| 80 | + try: |
| 81 | + c.boot_count.increment() |
88 | 82 |
|
89 | | - async def minimum_power_operations(): |
90 | | - await initial_boot() |
91 | | - # Same as above - this is a blocking operation by design |
92 | | - logger.info("Entering short hibernation (this will block the event loop)") |
93 | | - sleep_helper.short_hibernate() |
94 | | - logger.info("Woke from short hibernation") |
| 83 | + logger.info( |
| 84 | + "FC Board Stats", |
| 85 | + bytes_remaining=gc.mem_free(), |
| 86 | + boot_number=c.boot_count.get(), |
| 87 | + ) |
| 88 | + |
| 89 | + initial_boot() |
| 90 | + |
| 91 | + except Exception as e: |
| 92 | + logger.error("Error in Boot Sequence", e) |
95 | 93 |
|
96 | | - async def send_imu_data(): |
| 94 | + finally: |
| 95 | + pass |
| 96 | + |
| 97 | + def send_imu_data(): |
97 | 98 | logger.info("Looking to get imu data...") |
98 | 99 | IMUData = [] |
| 100 | + c.watchdog_pet() |
| 101 | + logger.info("IMU has baton") |
99 | 102 | IMUData = f.get_imu_data() |
| 103 | + c.watchdog_pet() |
100 | 104 | f.send(IMUData) |
101 | 105 |
|
102 | | - async def main(): |
| 106 | + def main(): |
103 | 107 | f.beacon() |
| 108 | + |
104 | 109 | f.listen_loiter() |
| 110 | + |
105 | 111 | f.state_of_health() |
| 112 | + |
106 | 113 | f.listen_loiter() |
| 114 | + |
107 | 115 | f.all_face_data() |
| 116 | + c.watchdog_pet() |
108 | 117 | f.send_face() |
| 118 | + |
109 | 119 | f.listen_loiter() |
110 | | - await send_imu_data() |
| 120 | + |
| 121 | + send_imu_data() |
| 122 | + |
111 | 123 | f.listen_loiter() |
| 124 | + |
112 | 125 | f.joke() |
| 126 | + |
113 | 127 | f.listen_loiter() |
114 | 128 |
|
| 129 | + def critical_power_operations(): |
| 130 | + initial_boot() |
| 131 | + c.watchdog_pet() |
| 132 | + |
| 133 | + sleep_helper.long_hibernate() |
| 134 | + |
| 135 | + def minimum_power_operations(): |
| 136 | + initial_boot() |
| 137 | + c.watchdog_pet() |
| 138 | + |
| 139 | + sleep_helper.short_hibernate() |
| 140 | + |
115 | 141 | ######################### MAIN LOOP ############################## |
116 | | - async def main_loop(): |
117 | | - """Async main loop that runs alongside the watchdog task""" |
118 | | - try: |
119 | | - while True: |
120 | | - # L0 automatic tasks no matter the battery level |
121 | | - c.check_reboot() # Now this only checks for reboot conditions |
122 | | - |
123 | | - if c.power_mode == "critical": |
124 | | - c.rgb = (0, 0, 0) |
125 | | - await critical_power_operations() |
126 | | - |
127 | | - elif c.power_mode == "minimum": |
128 | | - c.rgb = (255, 0, 0) |
129 | | - await minimum_power_operations() |
130 | | - |
131 | | - elif c.power_mode == "normal": |
132 | | - c.rgb = (255, 255, 0) |
133 | | - await main() |
134 | | - |
135 | | - elif c.power_mode == "maximum": |
136 | | - c.rgb = (0, 255, 0) |
137 | | - await main() |
138 | | - |
139 | | - else: |
140 | | - f.listen() |
141 | | - |
142 | | - # Small yield to allow other tasks to run |
143 | | - await asyncio.sleep(0.1) |
144 | | - |
145 | | - except Exception as e: |
146 | | - logger.critical("Critical in Main Loop", e) |
147 | | - time.sleep(10) |
148 | | - microcontroller.on_next_reset(microcontroller.RunMode.NORMAL) |
149 | | - microcontroller.reset() |
150 | | - finally: |
151 | | - logger.info("Going Neutral!") |
152 | | - c.rgb = (0, 0, 0) |
153 | | - c.hardware["WDT"] = False |
154 | | - |
155 | | - # Set up the asyncio event loop |
156 | | - async def run_tasks(): |
157 | | - # The watchdog task is already started by c.start_watchdog_background_task() |
158 | | - # Just run the main loop as a task |
159 | | - main_task = asyncio.create_task(main_loop()) |
160 | | - |
161 | | - try: |
162 | | - # Wait for the main task to complete (it should run forever) |
163 | | - await main_task |
164 | | - except asyncio.CancelledError: |
165 | | - logger.info("Main task was cancelled") |
166 | | - except Exception as e: |
167 | | - logger.critical("Error in run_tasks", e) |
168 | | - raise |
169 | | - |
170 | | - # Run the asyncio event loop |
171 | 142 | try: |
172 | | - asyncio.run(run_tasks()) |
| 143 | + while True: |
| 144 | + # L0 automatic tasks no matter the battery level |
| 145 | + c.check_reboot() |
| 146 | + |
| 147 | + if c.power_mode == "critical": |
| 148 | + c.rgb = (0, 0, 0) |
| 149 | + critical_power_operations() |
| 150 | + |
| 151 | + elif c.power_mode == "minimum": |
| 152 | + c.rgb = (255, 0, 0) |
| 153 | + minimum_power_operations() |
| 154 | + |
| 155 | + elif c.power_mode == "normal": |
| 156 | + c.rgb = (255, 255, 0) |
| 157 | + main() |
| 158 | + |
| 159 | + elif c.power_mode == "maximum": |
| 160 | + c.rgb = (0, 255, 0) |
| 161 | + main() |
| 162 | + |
| 163 | + else: |
| 164 | + f.listen() |
| 165 | + |
173 | 166 | except Exception as e: |
174 | | - logger.critical("Error in asyncio.run", e) |
| 167 | + logger.critical("Critical in Main Loop", e) |
| 168 | + time.sleep(10) |
175 | 169 | microcontroller.on_next_reset(microcontroller.RunMode.NORMAL) |
176 | 170 | microcontroller.reset() |
| 171 | + finally: |
| 172 | + logger.info("Going Neutral!") |
| 173 | + |
| 174 | + c.rgb = (0, 0, 0) |
| 175 | + c.hardware["WDT"] = False |
177 | 176 |
|
178 | 177 | except Exception as e: |
179 | 178 | logger.critical("An exception occured within main.py", e) |
0 commit comments