-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathflightComputer.py
More file actions
435 lines (371 loc) · 19.1 KB
/
Copy pathflightComputer.py
File metadata and controls
435 lines (371 loc) · 19.1 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2025 Sun Devil Rocketry
####################################################################################
# #
# flightComputer.py -- module with command line functions specific to the flight #
# computer #
# Author: Colton Acosta #
# Date: 2/26/2023 #
# Sun Devil Rocketry Avionics #
# #
####################################################################################
####################################################################################
# Imports #
####################################################################################
# Standard imports
import sys
import os
import time
import datetime
from matplotlib import pyplot as plt
# Project imports
from config import *
from hw_commands import byte_array_to_int
from hw_commands import byte_array_to_float
from hw_commands import get_sensor_frames
from hw_commands import sensor_extract_data_filter
import commands
import sensor_conv
####################################################################################
# Global Variables #
####################################################################################
# Serial port timeouts
if ( sdr_debug ):
default_timeout = 100 # 100 second timeout
else:
default_timeout = 1 # 1 second timeout
# Supported boards
supported_boards = [
"Flight Computer (A0002 Rev 2.0)"
]
####################################################################################
# Commands #
####################################################################################
####################################################################################
# #
# COMMAND: #
# servo #
# #
# DESCRIPTION: #
# Turn and reset servos in Flight Computer #
# #
####################################################################################
def servo( Args, serialObj ):
# Options Dictionary
servo_inputs = {
'help' : {},
'sweep' : {},
'reset': {},
}
# Maximum number of arguments
max_args = 2
# Opcode
opcode = b'\x08'
# Subcommand opcodes
sub_opcodes = {
'sweep' : b'\x00',
'reset': b'\x01'
}
# Command type -- subcommand function
command_type = 'subcommand'
################################################################################
# Basic inputs parsing #
################################################################################
parse_check = commands.parseArgs( Args ,
max_args ,
servo_inputs,
command_type )
if ( not parse_check ):
return serialObj # user inputs failed parse tests
################################################################################
# Command Specific Parsing #
################################################################################
# Check for active flight computer connection running the dual deploy firmware
if ( serialObj.controller not in supported_boards ):
print( "Error: The servo command requires an active connection to " +
"a flight computer.")
return serialObj
# Check that the flight computer is running the dual deploy firmware
if ( serialObj.firmware != "Terminal" and serialObj.firmware != "Active Roll"):
print( "Error: The servo command requires the flight computer to " +
"be running the terminal or active roll firmware. The flight computer is " +
"currently running the " + serialObj.firmware + " firmware" )
return serialObj
# Set the subcommand
subcommand = Args[0]
if (subcommand == "sweep"):
if (len(Args) != 2):
print("Make sure you specify the angle!")
return serialObj
serialObj.sendByte( opcode )
serialObj.sendByte( sub_opcodes["sweep"] )
degree = int(Args[1]).to_bytes(1,
byteorder = "big",
signed = False)
serialObj.sendByte( degree )
return serialObj
elif (subcommand == "reset"):
serialObj.sendByte( opcode )
serialObj.sendByte( sub_opcodes["reset"] )
return serialObj
####################################################################################
# #
# COMMAND: #
# dual_deploy #
# #
# DESCRIPTION: #
# Probes and tests the dual deploy firmware #
# #
####################################################################################
def dual_deploy( Args, serialObj ):
################################################################################
# Local Variables #
################################################################################
# Options Dictionary
dual_deploy_inputs = {
'help' : {},
'status' : {},
'extract': {},
'plot' : {}
}
# Maximum number of arguments
max_args = 1
# Opcode
opcode = b'\xA0'
# Subcommand opcodes
sub_opcodes = {
'status' : b'\x01',
'extract': b'\x02'
}
# Command type -- subcommand function
command_type = 'subcommand'
################################################################################
# Basic inputs parsing #
################################################################################
parse_check = commands.parseArgs( Args ,
max_args ,
dual_deploy_inputs,
command_type )
if ( not parse_check ):
return serialObj # user inputs failed parse tests
################################################################################
# Command Specific Parsing #
################################################################################
# Check for active flight computer connection running the dual deploy firmware
if ( serialObj.controller not in supported_boards ):
print( "Error: The dual-deploy command requires an active connection to " +
"a flight computer.")
return serialObj
# Check that the flight computer is running the dual deploy firmware
if ( serialObj.firmware != "Dual Deploy"):
print( "Error: The dual-deploy command requires the flight computer to " +
"be running the dual-deploy firmware. The flight computer is " +
"currently running the " + serialObj.firmware + " firmware" )
return serialObj
# Set the subcommand
subcommand = Args[0]
################################################################################
# dual-deploy help #
################################################################################
if ( subcommand == "help" ):
commands.display_help_info( "dual-deploy" )
return serialObj
################################################################################
# dual-deploy status #
################################################################################
elif ( subcommand == "status" ):
# Send the dual-deploy/status opcode
serialObj.sendByte( opcode )
serialObj.sendByte( sub_opcodes['status'] )
# Receive the recovery programmed settings
main_alt = byte_array_to_int( serialObj.readBytes( 4 ) )
drogue_delay = byte_array_to_int( serialObj.readBytes( 4 ) )
# Receive the ground pressure
ground_press = byte_array_to_float( serialObj.readBytes( 4 ) )
ground_press /= 1000
# Receive the sample rates, ms/sample
ld_sample_rate = byte_array_to_int( serialObj.readBytes( 4 ) )
ad_sample_rate = byte_array_to_int( serialObj.readBytes( 4 ) )
md_sample_rate = byte_array_to_int( serialObj.readBytes( 4 ) )
zd_sample_rate = byte_array_to_int( serialObj.readBytes( 4 ) )
# Display Results
print( "Main Deployment Altitude : " + str( main_alt ) + " ft" )
print( "Drogue Delay : " + str( drogue_delay ) + " s" )
print( "Ground Pressure : " + str( ground_press ) + " kPa" )
print( "Launch Detect Sample Rate : " + str( ld_sample_rate ) + " ms" )
print( "Apogee Detect Sample Rate : " + str( ad_sample_rate ) + " ms" )
print( "Main Altitude Detect Sample Rate: " + str( md_sample_rate ) + " ms" )
print( "Landing Detect Sample Rate : " + str( zd_sample_rate ) + " ms" )
return serialObj
################################################################################
# dual-deploy extract #
################################################################################
elif ( subcommand == "extract" ):
# Send the dual-deploy/status opcode
serialObj.sendByte( opcode )
serialObj.sendByte( sub_opcodes['extract'] )
# Get the data logger status to determine if header data is valid
status_byte = serialObj.readByte()
if ( status_byte != b'\x00' ):
print( "Error: The flash header is not valid. No flight data is " +
"available" )
# Receive the recovery programmed settings
main_alt = byte_array_to_int( serialObj.readBytes( 4 ) )
drogue_delay = byte_array_to_int( serialObj.readBytes( 4 ) )
# Receive the flight events
main_deploy_time = byte_array_to_int( serialObj.readBytes( 4 ) )
drogue_deploy_time = byte_array_to_int( serialObj.readBytes( 4 ) )
land_time = byte_array_to_int( serialObj.readBytes( 4 ) )
# Receive the ground pressure
ground_press = byte_array_to_float( serialObj.readBytes( 4 ) )
ground_press /= 1000
# Receive the flight data
rx_blocks = []
for i in range( 40960 ):
if ( i%100 == 0 ):
print( "Reading block " + str( i ) )
rx_frame_block = serialObj.readBytes( 12 )
rx_blocks.append( rx_frame_block )
# Format the flight data
sensor_frames = get_sensor_frames( "Flight Computer Lite (A0007 Rev 1.0)",
rx_blocks )
sensor_frames_filtered = sensor_extract_data_filter( sensor_frames )
# Croeate the output directory
run_date = datetime.date.today()
run_date = run_date.strftime("%m-%d-%Y")
if ( not ( os.path.exists( "output/dual-deploy" ) ) ):
os.mkdir( "output/dual-deploy" )
base_output_dir = "output/dual-deploy/" + run_date
if ( not ( os.path.exists( base_output_dir ) ) ):
os.mkdir( base_output_dir )
test_num = 0
output_dir = base_output_dir + "/data" + str( test_num )
while( os.path.exists( output_dir ) ):
test_num += 1
output_dir = base_output_dir + "/data" + str( test_num )
os.mkdir( output_dir )
# Export the header data
with open( output_dir + "/header.txt", "a") as file:
file.write( "Main Altitude : " + str( main_alt ) + " ft \n" )
file.write( "Drogue Delay : " + str( drogue_delay ) + " s \n" )
file.write( "Ground Pressure : " + str( ground_press ) + " kPa\n" )
file.write( "Main Deploy Time : " + str( main_deploy_time ) + " ms \n" )
file.write( "Drogue Deploy Time: " + str( drogue_deploy_time ) + " ms \n" )
file.write( "Landing Time : " + str( land_time ) + " ms \n" )
# Export the flight data
with open( output_dir + "/data.txt", 'w' ) as file:
for sensor_frame in sensor_frames_filtered:
for val in sensor_frame:
file.write( str( val ) )
file.write( '\t')
file.write( '\n' )
return serialObj
# dual-deploy extract #
################################################################################
# dual-deploy plot #
################################################################################
elif ( subcommand == 'plot' ):
# Find most recent date of data extraction
base_data_dirs = os.listdir( "output/dual-deploy" )
base_data_dirs_recent = []
base_data_dirs_most_recent = []
max_month = 1
for base_data_dir in base_data_dirs:
month = base_data_dir[0:2]
month = int( month )
if ( month >= max_month ):
max_month = month
for base_data_dir in base_data_dirs:
month = base_data_dir[0:2]
month = int( month )
if ( month == max_month ):
base_data_dirs_recent.append( base_data_dir )
max_day = 0
for base_data_dir in base_data_dirs_recent:
day = base_data_dir[3:5]
day = int( day )
if ( day > max_day ):
max_day = day
for base_data_dir in base_data_dirs_recent:
day = base_data_dir[3:5]
day = int( day )
if ( day == max_day ):
base_data_dirs_most_recent.append( base_data_dir )
base_data_dir = base_data_dirs_most_recent[-1]
base_data_dir = "output/dual-deploy/" + base_data_dir
# Find most recent data
data_num = 0
while ( os.path.exists( base_data_dir + "/data" + str( data_num ) ) ):
data_num += 1
data_dir = base_data_dir + "/data" + str( data_num - 1 )
header_filename = data_dir + "/header.txt"
data_filename = data_dir + "/data.txt"
# Extract the header data
with open( header_filename, "r" ) as file:
header_lines = file.readlines()
header_lines_split = []
for line in header_lines:
header_lines_split.append( line.split() )
main_deploy_alt = float( header_lines_split[0][3] )
drogue_delay = float( header_lines_split[1][3] )
ground_press = float( header_lines_split[2][3] )
main_deploy_time = float( header_lines_split[3][4] )/1000.0
drogue_deploy_time = float( header_lines_split[4][3] )/1000.0
landing_time = float( header_lines_split[5][3] )/1000.0
# Extract the flight data
sensor_time = []
sensor_pressure = []
sensor_temp = []
with open( data_filename, "r" ) as file:
data_lines = file.readlines()
for line in data_lines:
data_line_split = line.split()
sensor_time.append ( float( data_line_split[0] ) )
sensor_pressure.append( float( data_line_split[1] ) )
sensor_temp.append ( float( data_line_split[2] ) )
# Calculate Altitude
sensor_altitude = []
for press in sensor_pressure:
sensor_altitude.append( sensor_conv.pressure_to_alt( press, ground_press ) )
# Plot Pressure data
plt.figure()
plt.plot( sensor_time, sensor_pressure )
plt.title( "Pressure Data" )
plt.xlabel( "Time, s" )
plt.ylabel( "Pressure, kPa" )
plt.grid()
plt.axvline( x = main_deploy_time , color = 'b', label = "Main Deployment" )
plt.axvline( x = drogue_deploy_time, color = 'r', label = "Drogue Deployment" )
plt.axvline( x = landing_time , color = 'g', label = "Landed" )
plt.legend()
plt.show( block = False )
# Plot Temperature Data
plt.figure()
plt.plot( sensor_time, sensor_temp )
plt.title( "Temperature Data" )
plt.xlabel( "Time, s" )
plt.ylabel( "Temperature, Degrees C" )
plt.grid()
plt.axvline( x = main_deploy_time , color = 'b', label = "Main Deployment" )
plt.axvline( x = drogue_deploy_time, color = 'r', label = "Drogue Deployment" )
plt.axvline( x = landing_time , color = 'g', label = "Landed" )
plt.legend()
plt.show( block = False )
# Plot Altitude Data
plt.figure()
plt.plot( sensor_time, sensor_altitude )
plt.title( "Altitude Data" )
plt.xlabel( "Time, s" )
plt.ylabel( "Altitude, ft" )
plt.grid()
plt.axvline( x = main_deploy_time , color = 'b', label = "Main Deployment" )
plt.axvline( x = drogue_deploy_time, color = 'r', label = "Drogue Deployment" )
plt.axvline( x = landing_time , color = 'g', label = "Landed" )
plt.legend()
plt.show( block = False )
return serialObj
return serialObj
## dual_deploy ##
####################################################################################
# END OF FILE #
####################################################################################