Skip to content

Commit e5ccd3d

Browse files
Create Launch.py
Merge from flight-scripts
1 parent 4e15215 commit e5ccd3d

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed

Launch.py

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
# Imports
2+
from datetime import datetime
3+
import smbus
4+
import math
5+
import time
6+
import sys
7+
import pandas as pd
8+
import RPi.GPIO as GPIO
9+
10+
11+
# GPIO initialization
12+
GPIO.setmode(GPIO.BOARD)
13+
GPIO.setup(18, GPIO.OUT)
14+
pwm = GPIO.PWM(18, 100)
15+
pwm.start(0)
16+
17+
18+
# Register
19+
power_mgmt_1 = 0x6b
20+
power_mgmt_2 = 0x6c
21+
22+
def save_to_file():
23+
angles = (int(read_word_2c(0x3b)) / 131,
24+
int(read_word_2c(0x3d)) / 131,)
25+
26+
file = open('MPUData.csv', 'a')
27+
for i in angles:
28+
file.write(str(angles[0]) + ', ' + str(angles[1]) + '\n')
29+
30+
def read_byte(reg):
31+
return bus.read_byte_data(address, reg)
32+
33+
def read_word(reg):
34+
h = bus.read_byte_data(address, reg)
35+
l = bus.read_byte_data(address, reg+1)
36+
value = (h << 8) + l
37+
return value
38+
39+
def read_word_2c(reg):
40+
val = read_word(reg)
41+
if (val >= 0x8000):
42+
return -((65535 - val) + 1)
43+
else:
44+
return val
45+
46+
def dist(a,b):
47+
return math.sqrt((a*a)+(b*b))
48+
49+
def get_y_rotation(x,y,z):
50+
radians = math.atan2(x, dist(y,z))
51+
52+
def get_x_rotation(x,y,z):
53+
radians = math.atan2(y, dist(x,z))
54+
return math.degrees(radians)
55+
56+
bus = smbus.SMBus(1)
57+
address = 0x68
58+
59+
bus.write_byte_data(address, power_mgmt_1, 0)
60+
61+
# Misc value definitions
62+
count = 0
63+
status = True
64+
65+
66+
# Main launch loop
67+
while status:
68+
69+
deploy = False
70+
spike = False
71+
spike_t = 0
72+
dep_time = 0
73+
74+
#deployment auto-sequence
75+
if count == 168:
76+
GPIO.output(18, True)
77+
pwm.ChangeDutyCycle(5)
78+
time.sleep(1)
79+
pwm.ChangeDutyCycle(0)
80+
GPIO.output(18, False)
81+
dep_time = str(datetime.now())
82+
83+
a_x = read_word_2c(0x3b)
84+
a_y = read_word_2c(0x3d)
85+
a_z = read_word_2c(0x3f)
86+
87+
88+
# Data to screen output
89+
print ("\033c")
90+
91+
print ("Accel")
92+
print ("------")
93+
94+
print ("\n")
95+
sys.stdout.write(f"Scaled X: {a_x / 16348}")
96+
if a_x / 16348 > 1.2:
97+
spike = True
98+
spike_t = str(datetime.now())
99+
sys.stdout.flush()
100+
101+
print ("\n")
102+
sys.stdout.write(f"Scaled Y: {a_y / 16348}")
103+
if a_y / 16348 > 1.2:
104+
spike = True
105+
spike_t = str(datetime.now())
106+
sys.stdout.flush()
107+
108+
print ("\n")
109+
sys.stdout.write(f"Scaled Z: {a_z / 16348}")
110+
if a_z /16348 > 1.2:
111+
spike = True
112+
spike_t = str(datetime.now())
113+
sys.stdout.flush()
114+
115+
fail = False
116+
117+
# Data-spike output/failsafe
118+
if spike_t != dep_time:
119+
fail = True
120+
deploy = False
121+
elif spike_t == dep_time and count > 50:
122+
print ("\n")
123+
sys.stdout.write("MPU6050 Data Read: MPU HAS CONFIRMATION OF NOMINAL PARACHUTE DEPLOY")
124+
sys.stdout.flush()
125+
deploy = True
126+
127+
print ("\n")
128+
129+
print (count)
130+
131+
if not deploy and fail:
132+
sys.stdout.write("MPU6050 Data Read: CURRENT MPU DATA HAS SHOWN THAT PARAHUTE DEPLOY SEQUENCE MAY HAVE BEEN ANOMINAL!")
133+
sys.stdout.flush()
134+
135+
save_to_file()
136+
137+
time.sleep(0.1)
138+
139+
count = count+1
140+
141+
# TD confirmation
142+
if not spike and count > 168:
143+
continue
144+
elif spike and count > 168:
145+
sys.stdout.write("\n")
146+
sys.stdout.flush()
147+
sys.stdout.write("\n")
148+
sys.stdout.flush()
149+
sys.stdout.write("Tango Delta, touchdown confirmed.")
150+
sys.stdout.flush()
151+
sys.stdout.write("\n")
152+
sys.stdout.flush()
153+
sys.stdout.write("Switching to ground control systems and preparing for data review.")
154+
sys.stdout.flush()
155+
status = False
156+
elif spike and count > 300:
157+
sys.stdout.write("Tango Delta anominal, touchdown timing failure.")
158+
sys.stdout.flush()
159+
status = False
160+
else:
161+
continue
162+
163+
status = True
164+
165+
time.sleep(5)
166+
167+
# Data review and shutdown
168+
while status:
169+
print ("\n")
170+
print ("Preparing data review systems, stand by for post-flight review.")
171+
data_read = pd.read_csv('MPUData.csv', sep=', ', header=None, engine='python')
172+
time.sleep(10)
173+
for value in data_read:
174+
if abs(value) > 5:
175+
sys.stdout.write("\n")
176+
sys.stdout.flush()
177+
sys.stdiut.write("\n")
178+
sys.stdout.flush()
179+
sys.stdout.write(f"Anamoly found, G-Force value exceded nominal forces, force was equal to: {value} at point of anamoly, note that anomaly may have occured at parachute deploy but G limit still applies for deploy.")
180+
sys.stdout.flush()
181+
else:
182+
sys.stdout.write("\n")
183+
sys.stdout.flush()
184+
sys.stdout.write("No anamolies found, craft safe on the ground, proceeding to post-flight calibration.")
185+
sys.stdout.flush()
186+
GPIO.output(18, True)
187+
pwm.ChangeDutyCycle(0)
188+
sys.stdout.write("Post flight calibration done, exiting program...")
189+
sys.stdout.flush()
190+
status = False

0 commit comments

Comments
 (0)