forked from camwolff02/groundstation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
343 lines (298 loc) · 11.2 KB
/
main.py
File metadata and controls
343 lines (298 loc) · 11.2 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
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
# Native python imports
from datetime import datetime
import logging
import os
from queue import Queue, Empty, Full
import threading
from typing import Dict, Optional
from dataclasses import dataclass
from threading import Event
# External library imports for lora and foxglove foxglove, logging, etc.
import cv2
import foxglove
from foxglove import Channel, Schema
from foxglove.channels import CompressedImageChannel
from foxglove.websocket import (
Capability,
WebSocketServer,
)
from foxglove.schemas import (
CompressedImage,
)
import google.protobuf.message
import board
import busio
import digitalio
from adafruit_rfm9x import RFM9x
# Local imports for custom protobuf schema and CLI
from cli import parser
from TomPacket_pb2 import TomPacket
from LocationFix_pb2 import LocationFix
from Signal_pb2 import Signal
from utils import build_file_descriptor_set, CustomListener
@dataclass
class ChannelData:
queue: Queue
stop_event: Event
thread: Optional[threading.Thread] = None
def channel_publisher(queue: Queue, channel: Channel, stop_event: Event, name: str) -> None:
while not stop_event.is_set():
try:
data = queue.get(timeout=1.0)
channel.log(data)
except Empty:
continue
def lora_reader(lora: RFM9x, rocket_channels: Dict, stop_event: Event) -> None:
while not stop_event.is_set():
packet = lora.receive(with_header=True)
if packet is not None:
print(bytes(packet))
try:
tom_packet = TomPacket()
tom_packet.ParseFromString(packet)
if tom_packet.rocket_id not in rocket_channels:
continue
if abs(tom_packet.location.altitude) > 1_000_000:
continue
# Get the channels for the specific rocket
channels = rocket_channels[tom_packet.rocket_id]
# Queue location data
if tom_packet.HasField("location"):
try:
channels["location"]["data"].queue.put(
tom_packet.location.SerializeToString(), timeout=0.1
)
except Full:
print(f"[WARNING] Location queue for rocket {tom_packet.rocket_id} is full. Dropping message.")
# Queue telemetry data
try:
channels["telemetry"]["data"].queue.put(bytes(packet), timeout=0.1)
except Full:
print(f"[WARNING] Telemetry queue for rocket {tom_packet.rocket_id} is full. Dropping message.")
# Queue signal data
signal_data = Signal(rssi=lora.last_rssi, snr=lora.last_snr)
try:
channels["signal"]["data"].queue.put(
signal_data.SerializeToString(), timeout=0.1
)
except Full:
print(f"[WARNING] Signal queue for rocket {tom_packet.rocket_id} is full. Dropping message.")
except google.protobuf.message.DecodeError:
print("[ERROR] Could not decode packet! Did flight computer shut off?")
def camera_reader(cap: cv2.VideoCapture, image_queue: Queue, stop_event: Event) -> None:
while not stop_event.is_set():
ret, frame = cap.read()
if ret:
im_packet = CompressedImage(
data=cv2.imencode(".jpeg", frame)[1].tobytes(),
format="jpeg"
)
try:
image_queue.put(im_packet, timeout=0.1)
except Full:
print("[WARNING] Image queue is full. Dropping frame.")
image_queue.pop()
image_queue.put(im_packet, timeout=0.1)
def run_telemetry_loop(
lora: RFM9x,
server: WebSocketServer,
image_channel: CompressedImageChannel | None = None,
cap: cv2.VideoCapture | None = None,
rocket_ids: list[str] = [],
) -> None:
# Create a dictionary to store channels and their queues for each rocket
rocket_channels: Dict[str, Dict[str, dict]] = {}
for rocket_id in rocket_ids:
rocket_channels[rocket_id] = {
"telemetry": {
"channel": Channel(
topic=f"/telemetry/{rocket_id}",
message_encoding="protobuf",
schema=Schema(
name=TomPacket.DESCRIPTOR.full_name,
encoding="protobuf",
data=build_file_descriptor_set(TomPacket).SerializeToString(),
),
),
"data": ChannelData(Queue(), Event())
},
"location": {
"channel": Channel(
topic=f"/location/{rocket_id}",
message_encoding="protobuf",
schema=Schema(
name=LocationFix.DESCRIPTOR.full_name,
encoding="protobuf",
data=build_file_descriptor_set(LocationFix).SerializeToString(),
),
),
"data": ChannelData(Queue(), Event())
},
"signal": {
"channel": Channel(
topic=f"/signal/{rocket_id}",
message_encoding="protobuf",
schema=Schema(
name=Signal.DESCRIPTOR.full_name,
encoding="protobuf",
data=build_file_descriptor_set(Signal).SerializeToString(),
),
),
"data": ChannelData(Queue(), Event())
},
}
# Start publisher threads for each channel
for rocket_id, channels in rocket_channels.items():
for channel_name, channel_info in channels.items():
thread = threading.Thread(
target=channel_publisher,
args=(
channel_info["data"].queue,
channel_info["channel"],
channel_info["data"].stop_event,
f"{rocket_id}-{channel_name}"
),
name=f"{rocket_id}-{channel_name}-publisher"
)
channel_info["data"].thread = thread
thread.start()
# Create and start LoRa reader thread
lora_stop_event = Event()
lora_thread = threading.Thread(
target=lora_reader,
args=(lora, rocket_channels, lora_stop_event),
name="lora-reader"
)
lora_thread.start()
# Create image publisher thread if camera is enabled
image_queue = None
image_stop_event = None
camera_stop_event = None
if cap is not None and image_channel is not None:
image_queue = Queue()
image_stop_event = Event()
camera_stop_event = Event()
# Start image publisher thread
image_thread = threading.Thread(
target=channel_publisher,
args=(image_queue, image_channel, image_stop_event, "image"),
name="image-publisher"
)
image_thread.start()
# Start camera reader thread
camera_thread = threading.Thread(
target=camera_reader,
args=(cap, image_queue, camera_stop_event),
name="camera-reader"
)
camera_thread.start()
try:
# Main thread just waits for interrupt
while True:
threading.Event().wait(1)
except KeyboardInterrupt:
print("\nShutting down threads...")
# Stop all publisher threads
for rocket_id, channels in rocket_channels.items():
for channel_info in channels.values():
channel_info["data"].stop_event.set()
if channel_info["data"].thread:
channel_info["data"].thread.join()
# Stop LoRa thread
lora_stop_event.set()
lora_thread.join()
# Stop camera and image threads if they exist
if camera_stop_event:
camera_stop_event.set()
camera_thread.join()
if image_stop_event:
image_stop_event.set()
image_thread.join()
server.stop()
def main() -> None:
args = parser.parse_args()
# INITIALIZE FOXGLOVE SERVER
foxglove.set_log_level(logging.DEBUG)
listener = CustomListener()
server = foxglove.start_server(
name=args.server_name,
host=args.address,
port=args.port,
server_listener=listener,
capabilities=[Capability.ClientPublish],
supported_encodings=["json", "protobuf"],
)
telemetry_channel = Channel(
topic="/telemetry",
message_encoding="protobuf",
schema=Schema(
name=TomPacket.DESCRIPTOR.full_name,
encoding="protobuf",
data=build_file_descriptor_set(TomPacket).SerializeToString(),
),
)
location_channel = Channel(
topic="/location",
message_encoding="protobuf",
schema=Schema(
name=LocationFix.DESCRIPTOR.full_name,
encoding="protobuf",
data=build_file_descriptor_set(LocationFix).SerializeToString(),
),
)
signal_channel = Channel(
topic="/signal",
message_encoding="protobuf",
schema=Schema(
name=Signal.DESCRIPTOR.full_name,
encoding="protobuf",
data=build_file_descriptor_set(Signal).SerializeToString(),
),
)
if args.enable_camera:
image_channel = CompressedImageChannel(topic="/camera/image_compressed")
else:
image_channel = None
# INITIALIZE IO RESOURCES
# LoRa Wiring settings
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
# Setup Chip Select and Reset pins
cs = digitalio.DigitalInOut(getattr(board, f"CE{args.spi_cs}"))
reset = digitalio.DigitalInOut(getattr(board, f"D{args.pins_reset}"))
# Initialize RFM9x
lora = RFM9x(spi, cs, reset, args.frequency / 1_000_000)
# Apply modulation settings
lora.signal_bandwidth = args.modulation_bw
lora.spreading_factor = args.modulation_sf
lora.coding_rate = args.modulation_cr
lora.preamble_length = args.preamble_len
lora.sync_word = args.sync_word
print("[INFO] LoRa initialized")
# Video capture initialization
if args.enable_camera:
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("[ERROR] Could not open camera")
cap = None
image_channel = None
else:
print("[INFO] Initialized Video Camera")
else:
cap = None
rocket_ids = args.rocket_name.split(',')
if args.enable_logging:
# Create logs directory if it doesn't exist
os.makedirs(args.log_dir, exist_ok=True)
# Create filename with current datetime
timestamp = datetime.now().strftime("%Y:%m:%d-%H:%M:%S")
path = os.path.join(args.log_dir, f"{args.rocket_name}-{timestamp}.mcap")
with foxglove.open_mcap(path):
run_telemetry_loop(
lora, server, image_channel, cap, rocket_ids
)
else:
run_telemetry_loop(
lora, server, image_channel, cap, rocket_ids
)
if __name__ == "__main__":
main()