Skip to content

Commit c7a66f1

Browse files
Create Launch.py
This is the main file that the rocket will launch with running, currently supplies data from the MPU6050 to the main flight computer which is a Raspberry Pi.
1 parent 98ac512 commit c7a66f1

File tree

1 file changed

+178
-0
lines changed

1 file changed

+178
-0
lines changed

Launch.py

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

0 commit comments

Comments
 (0)