-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSOCS.py
218 lines (171 loc) · 7.1 KB
/
SOCS.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
import argparse
import RPi.GPIO as GPIO
from enum import Enum
from BNOInterface import BNOInterface
import time
from PIL import Image
import executeCmds # Change for Hville
import asyncio
import os
#import mathutils
# Necessary to prevent import issues on APRSInterface
import sys
sys.path.append("./aprs_decoding/test")
# APRS Interface
if True:
from APRSInterface import APRSInterface
class PayloadSystem:
# time from launch to landing, in seconds
DESCENT_TIME = 5 * 60
# amount of values to collect for rolling launch detect average
AVERAGE_COUNT = 250
# these are the GPIO pins we are controlling the relay switch with
ANTENNA_1_PIN = 17
ANTENNA_2_PIN = 27
BACKUP_LAUNCH_TIME = 45 * 60
BACKUP_COMMAND_TIME = 7 * 60
BACKUP_COMMAND = "C3 A1 D4 C3 E5 A1 G7 C3 H8 A1 F6 C3"
# rocket state
class LaunchState(Enum):
STANDBY = 0
LAUNCH = 1
LANDING = 2
CAMERA = 3
RECOVER = 4
def __init__(self, frequency=None):
if frequency != None:
self.aprs_interface = APRSInterface(frequency=frequency)
else:
self.aprs_interface = APRSInterface()
self.sensor = BNOInterface()
self.state = self.LaunchState.STANDBY
# setup board
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
# set output pins to output
GPIO.setup(self.ANTENNA_1_PIN, GPIO.OUT)
GPIO.setup(self.ANTENNA_2_PIN, GPIO.OUT)
# launch detection
self.accelerations = [0] * self.AVERAGE_COUNT
self.idx = 0
# Variable to track whether message received
self.messageReceived = False
self.init_time = time.time()
def update(self):
currentState = self.state
if currentState is self.LaunchState.STANDBY:
# Do standby stuff
accel = self.sensor.get_linear_acceleration()
print(accel)
if accel != (None, None, None):
self.accelerations[self.idx] = abs(accel[0])
self.idx = (self.idx+1) % self.AVERAGE_COUNT
average_accel = sum(self.accelerations) / self.AVERAGE_COUNT
# print(average_accel)
if average_accel > 3:
self.delayStart = time.time()
self.state = self.LaunchState.LAUNCH
print("LIFTOFF DETECTED")
elif time.time() > self.init_time + self.BACKUP_LAUNCH_TIME:
self.delayStart = time.time()
self.state = self.LaunchState.LAUNCH
print("BACKUP TIMER ACTIVATED: LIFTOFF DETECTED")
elif currentState is self.LaunchState.LAUNCH:
# Do launch stuff
# Need to check aand make sure it's landed
if time.time() > (self.delayStart + self.DESCENT_TIME):
# This will also be done continuously in landed state so idk
cam_choice = self.choose_antenna()
self.aprs_interface.startRecv()
self.land_time = time.time()
self.state = self.LaunchState.LANDING
print("LANDED! Choosing antenna...")
elif currentState is self.LaunchState.LANDING:
# Do landing stuff
self.choose_antenna()
print("Number of messages: " + str(len(self.aprs_interface.aprsMsg)))
# Need to check that the callsign is actually from NASA; must add that here
if len(self.aprs_interface.aprsMsg) > 0:
self.aprs_interface.stop()
self.messageReceived = True
self.state = self.LaunchState.CAMERA
elif time.time() > (self.land_time + self.BACKUP_COMMAND_TIME):
self.aprs_interface.stop()
self.messageReceived = True
self.aprs_interface.aprsMsg.append(self.BACKUP_COMMAND)
self.state = self.LaunchState.CAMERA
print("BACKUP TIMER ACTIVATED: MESSAGE RECEIVED")
elif currentState is self.LaunchState.CAMERA:
self.cameraChoice = self.choose_antenna()
print(f"Full Message: {self.aprs_interface.aprsMsg[0]}")
# The index of 7 takes out the callsign
# print(f"Sliced Message: {self.aprs_interface.aprsMsg[0][7:]}")
# The index of 7 takes out the callsign
APRS_clip = self.aprs_interface.aprsMsg[0]#[7:]
count = 0
try:
while True:
os.mkdir(f"./capture{count}")
# Change to executeCmds for Hville
# Execute the commands for the camera unit
executeCmds.executeCmds(APRS_clip, self.cameraChoice, f"./capture{count}/")
time.sleep(300)
count+=1
except KeyboardInterrupt:
pass
self.state = self.LaunchState.RECOVER
def choose_antenna(self):
# setup orientation determination
gravity = self.sensor.get_gravity()
# print(f'Gravity: {gravity}')
# antenna 1 , IMU UP or IMU rotated 90 degrees CCW from up position
# (looking at the bulkhead from the aft posiiton)
# skip none type gravity
try:
gravity[2] > 0
except:
return
# choose antenna 1
if gravity[1] >= 0: # or gravity[1] < -8.5*0.707:
# set output pins to output
GPIO.output(self.ANTENNA_1_PIN, True)
GPIO.output(self.ANTENNA_2_PIN, False)
if gravity[0] > 6: #and gravity[2] < -1:
cam_choice = "ring" # Camera A
print("Ring, rotate bay")
#print(gravity)
#time.sleep(1)
else: #gravity[1] > 1 and gravity[2] < -1
cam_choice = "big" # Camera D
print("Big, rotate bay")
#print(gravity)
#time.sleep(1)
print("Chose antenna 2")
# choose antenna 2
elif gravity[1] < 0: # or gravity[1] > 8.5*0.707:
# set output pins to output
GPIO.output(self.ANTENNA_2_PIN, True)
GPIO.output(self.ANTENNA_1_PIN, False)
if gravity[0] > 6: # and gravity[2] > 1:
cam_choice = "jahn" # Camera B
print("Jahn, rotate bay")
#print(gravity)
#time.sleep(1)
else: #gravity[1] < -1 and gravity[2] > 1:cam_choice = "ring"
cam_choice = "pinky" #Camera C
print("Pinky, correct choice")
#print(gravity)
#time.sleep(1)
print("Chose antenna 1")
else:
GPIO.output(self.ANTENNA_2_PIN, True)
GPIO.output(self.ANTENNA_1_PIN, False)
cam_choice = "pinky"
#print("No camera chosen")
print(f"Error reading gravity data: {gravity}")
print(gravity)
time.sleep(1)
#print("Chose antenna 1")
#print("Pinky is the only one that should be chosen, it should be up")
cam_choice = "pinky"
return cam_choice