-
Notifications
You must be signed in to change notification settings - Fork 2
/
bob.py
executable file
·275 lines (229 loc) · 8.03 KB
/
bob.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
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
#!/usr/bin/env python3
#######################################################
# CSU ROCKET TEAM 2020-2021
# SRAD TELEMETRY COMPUTER
# RUNNING ON RPI-ZERO W/ CUSTOM PCB FOR ADAFRUIT SENSORS
# AND PWM AUDIO OUTPUT
#######################################################
import os
import sys
import math
import time
import board
import busio
import serial
import socket
import logging
import datetime
import subprocess
import adafruit_gps
import adafruit_fxos8700
import adafruit_mpl3115a2
from gpiozero import LED, Buzzer
#######################################################
# PREVENT FROM RUNNING MULTIPLE TIMES ON A UNIX OS
# IF PROGRAM IS RUNNING FROM BOOT, CHECK HTOP AND KILL!
#######################################################
def get_lock(process_name):
get_lock._lock_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
try:
get_lock._lock_socket.bind('\0' + process_name)
except socket.error:
print('ERROR: PROGRAM IS ALREADY RUNNING, KILL IT BEFORE TRYING TO RUN AGAIN')
sys.exit()
get_lock('aprs')
#######################################################
# LED & LOGGING SETUP
#######################################################
script_path = os.path.dirname(os.path.realpath(sys.argv[0]))
# GREEN - PROGRAM IS RUNNING
# BLUE - GPS FIX
# YELLOW - TRANSMITTING
# RED - ERROR
LED_GREEN = LED(22)
LED_RED = LED(23)
LED_BLUE = LED(5)
LED_YELLOW = LED(6)
LED_GREEN.on()
logging_format = logging.Formatter('%(asctime)s %(msecs)03dms | %(message)s',
datefmt='%Y-%m-%d %I:%M:%S%p')
root_logger = logging.getLogger()
file_handler = logging.FileHandler(script_path + '/bob.log')
file_handler.setFormatter(logging_format)
root_logger.addHandler(file_handler)
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(logging_format)
root_logger.addHandler(console_handler)
root_logger.setLevel(logging.DEBUG)
logging.info('Program Started')
######################################################
# BUZZ BUZZ IMA BEEE
######################################################
# Not used in subscale 3
#bz = Buzzer(26)
#bz.on()
#time.sleep(5)
#bz.off()
#time.sleep(1)
#for i in range(5):
# bz.on()
# time.sleep(0.2)
# bz.off()
# time.sleep(0.5)
######################################################
# CONFIG + CONSTANTS
######################################################
CALLSIGN = 'KE0SPF'
APRS_COMMENT = 'CSU ROCKET TEAM'
APRS_SYMBOL_ROCKET = 'O'
######################################################
# SENSOR SETUP
######################################################
logging.info('Setting up sensors')
uart = serial.Serial('/dev/serial0', baudrate=9600, timeout=10)
i2c = busio.I2C(board.SCL, board.SDA)
# Adafruit Ultimate GPS v3
gps_enabled = False
gps = None
try:
gps = adafruit_gps.GPS(uart, debug=False)
# Only send back GPRMC (Recommended Minimum Specific GNSS Sentence) and GPGGA (GPS Fix Data)
gps.send_command(b'PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0')
# Update once every second
gps.send_command(b'PMTK220,1000')
gps.update()
logging.info('GPS Enabled')
gps_enabled = True
except Exception as gps_exception:
logging.info('GPS Disconnected')
logging.info(str(gps_exception))
# Adafruit Barometric Altimeter MPL3511A2
altimeter_enabled = False
altimeter = None
try:
altimeter = adafruit_mpl3115a2.MPL3115A2(i2c)
altimeter.sealevel_pressure = 102520
logging.info('Altimeter Enabled, Pressure: %d', altimeter.sealevel_pressure)
altimeter_enabled = True
except Exception as altimeter_exception:
logging.info('Altimeter Disconnected')
logging.info(str(altimeter_exception))
# Adafruit 9-DOF Accelerometer
accelerometer_enabled = False
accelerometer = None
try:
accelerometer = adafruit_fxos8700.FXOS8700(i2c)
logging.info('Accelerometer Enabled')
accelerometer_enabled = True
except Exception as accelerometer_exception:
logging.info('Accelerometer Disconnected')
logging.info(str(accelerometer_exception))
# GPS buffer fills up while we take time transmitting
# Flush this buffer to get newest data available every transmit cycle
def flush_GPS_buffer(g):
f=open(script_path + "/gps.csv", "a+")
while g.update():
if g.has_fix:
f.write(str(gps.latitude))
f.write(', ')
f.write(str(gps.longitude))
if gps.altitude_m is not None:
f.write(', ')
f.write(str(gps.altitude_m))
f.write('\n')
f.close()
############################################################
# TRANSMIT LOOP
############################################################
f=open(script_path + "/gps.csv", "a+")
f.write('Program Start\n')
f.write(str(datetime.datetime.now()))
f.write('\n')
f.close()
logging.info('Starting transmissions')
while True:
# latitude in format ddmm.hhN (i.e degrees, minutes, and hundreths of a minute north)
# longitude in format ddd.mm.hhW (i.e degrees, minutes, and hundreths of a minute west)
# indeterminate position signaled by 0000.00N\00000.00W
# altitude in format aaaaaa where altitude is in feet
latitude = "0000.00N"
longitude = "00000.00W"
altitude = "000000"
course = 0
speed = 0
alt_source = "none"
# Accelerometer/Magnetometer readings
ax = ay = az = "?"
mx = my = mz = "?"
sensor_error = False
if gps_enabled:
try:
flush_GPS_buffer(gps)
if gps.has_fix:
LED_BLUE.on()
(latitude_min, latitude_deg) = math.modf(gps.latitude)
(longitude_min, longitude_deg) = math.modf(gps.longitude)
latitude_deg = '{:02d}'.format(abs(int(latitude_deg)))
longitude_deg = '{:03d}'.format(abs(int(longitude_deg)))
latitude_min = '{:05.2f}'.format(abs(round(latitude_min*60, 2)))
longitude_min = '{:05.2f}'.format(abs(round(longitude_min*60, 2)))
latitude = latitude_deg + latitude_min + 'N'
longitude = longitude_deg + longitude_min + 'W'
if gps.altitude_m is not None:
altitude = '{:06d}'.format(int(gps.altitude_m*3.28084))
alt_source = "gps"
if gps.speed_knots is not None:
speed = int(gps.speed_knots)
if gps.track_angle_deg is not None:
course = int(gps.track_angle_deg)
except Exception as gps_error:
LED_BLUE.off()
logging.info('GPS gave error')
logging.info(str(gps_error))
sensor_error = True
if altimeter_enabled:
try:
altitude = '{:06d}'.format(int(altimeter.altitude*3.28084))
alt_source = "alt"
except Exception as altimeter_error:
logging.info('Altimeter gave error')
logging.info(str(altimeter_error))
sensor_error = True
if accelerometer_enabled:
try:
ax, ay, az = accelerometer.accelerometer
except Exception as accelerometer_error:
logging.info('Accelerometer gave error')
logging.info(str(accelerometer_error))
sensor_error = True
if sensor_error:
LED_RED.on()
else:
LED_RED.off()
aprs_info = '!{}\\{}{}{:03d}/{:03d}/A={} {} a={},{},{} {}'.format(
latitude,
longitude,
APRS_SYMBOL_ROCKET,
course,
speed,
altitude,
APRS_COMMENT,
round(ax,3), round(ay,3), round(az,3),
alt_source
)
logging.info(aprs_info)
subprocess.run([script_path + '/afsk/aprs',
'-c', CALLSIGN,
'--destination', 'APCSU1',
'--o', script_path + '/packet.wav',
aprs_info])
LED_YELLOW.on()
try:
subprocess.run(['sudo',
'aplay',
'-q',
script_path + '/packet.wav'])
except Exception as aplay_error:
logging.info('aplay error')
logging.info(str(aplay_error))
LED_YELLOW.off()