-
Notifications
You must be signed in to change notification settings - Fork 0
/
avionics.py
280 lines (225 loc) · 8.81 KB
/
avionics.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
273
274
275
276
277
278
279
280
import time
import board
import adafruit_bmp280
import RPi.GPIO as GPIO
import numpy as np
import serial
import datetime
from multiprocessing import Process
import sys
import matplotlib.pyplot as plt
import keyboard
import os
from pyubx2 import UBXReader
def ebimu_process(n):
############################## 파일 생성 #################################
nowTime = str(datetime.datetime.now())
fileName = nowTime[:10]+"_ebimu_"+nowTime[11:21]
try:
f = open(fileName, 'w')
log = open("Ebimu_log.txt",'w')
except:
print("Failed to open file, EBIMU")
sys.exit()
############################## 센서 연결 #################################
ser = serial.Serial('/dev/ttyUSB0',115200,timeout=0.001)
############################ 데이터 수집 #################################
buf = ""
while True:
while ser.inWaiting():
data = str(ser.read()).strip()
buf += data
if data[3] == "n":
buf = buf.replace("'","")
buf = buf.replace("b","")
try :
roll, pitch, yaw, x, y, z = map(float,buf[1:-4].split(','))
except Exception as e:
#print(buf)
print("Error from data processing : ", e)
log.write("Error from data processing : "+str(e)+"\n With '"+buf+"'\n")
buf = ""
continue
datas = [roll,pitch,yaw,x,y,z]
writeString = "*"+str(datas)[1:-1]+"\n"
f.write(writeString)
print(roll,pitch,yaw,x,y,z)
buf = ""
if __name__ == '__main__':
################################ 초기 화면 ###################################
title = "GOAT Rocket Program"
for i in range(len(title)):
os.system('clear')
print("+-----------------------+")
print('| ',end="")
print(title[:i+1], end='')
if i != len(title)-1: print('|',end='')
print((' '*(17-i))+' |')
print("+-----------------------+")
time.sleep(0.1)
############################## 부품 연결과 설정 #################################
stream = serial.Serial("/dev/ttyACM0", baudrate=19200, timeout=1)
ubr = UBXReader(stream)
gf = open("gps.txt", "a")
# 통신 모듈 연결
ser = serial.Serial(
port="/dev/ttyAMA0",
baudrate=19200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
#Bmp280 센서 연결
i2c = board.I2C()
bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c)
# 표준 대기압으로 설정
bmp280.sea_level_pressure = 1013.25
#서보 모터 설정
GPIO.setmode(GPIO.BCM)
servo_pin = 18
GPIO.setwarnings(False)
GPIO.setup(servo_pin, GPIO.OUT)
pwm = GPIO.PWM(servo_pin, 50)
pwm.start(0)
time.sleep(0.5)
print("Components Connectiont OK")
##################################
# 서보 동작 테스트 코드 추가 공간
##################################
################################# 자료 선언 ###################################
# 상태 딕셔너리
status = {
"parachute": "0", # 예시 데이터, Not yet deploy:사출 전, Deploy:사출 후, Force Deploy:강제 사출 후
"way": "DOWN", # 예시 데이터, UP:상승시, DOWN(3):하강시 괄호 안은 카운트
"ebimu": "-", # 예시 데이터, 120,512,252: x,y,z
"bmp": "0", # 예시 데이터, 50: 고도
}
WINDOW = 20
NO_DEPLOY_ALTITUDE = 1
FALLING_CONFIRMATION = 10
ESTIMATED_MAX_ALTITUDE = 400
datas = [0]
moving_averages = []
falling_count = 0
is_deployed = False
is_outlier = False
is_valid_falling = False
################################# 파일 생성 ###################################
now = str(datetime.datetime.now())
fileN = now[:10]+"_bmp_"+now[11:21]
fileS = now[:10]+"_servoLog_"+now[11:21]
f = open(fileN, 'w')
s = open(fileS, 'w')
time.sleep(0.5)
print("File creation OK")
################################ 고도 영점 연산 #################################
init_buffer = []
INIT_TIMES = 50
print("Wait Altitude Initialing...")
for i in range(INIT_TIMES):
init_buffer.append(bmp280.altitude)
if (i+1)%10 == 0:print((i+1)*2,"%",sep="")
init_altitude = sum(init_buffer)/INIT_TIMES
print("Done OK")
time.sleep(1)
############################# EBIMU 프로세스 시작 ###############################
eb_p = Process(target=ebimu_process, args=(1,))
eb_p.start()
################################## 실행 로직 ###################################
while True:
try:
(raw_data, parsed_data) = ubr.read()
print(parsed_data)
# Write parsed_data to the file
gf.write(str(parsed_data) + "\n")
except:
print("GPS Fail")
gf.write("GPS Fail \n")
################################## 고도 수집 ###################################
# 로컬 고도 계산
altitude = bmp280.altitude - init_altitude
# 이상치 판단
if -10 <= altitude <= ESTIMATED_MAX_ALTITUDE :
is_outlier = False
else:
is_outlier = True
# 데이터 리스트에 추가
if not is_outlier:
datas.append(altitude)
else:
print("Outlier data")
mean = np.mean(datas[-WINDOW:])
moving_averages.append(mean)
print("Altitude: {:.2f}".format(altitude))
f.write(f"{datetime.datetime.now()} Altitude: {altitude} m, Is_outlier: {is_outlier}\n")
################################## 방향 판단 ###################################
if moving_averages[-1] > NO_DEPLOY_ALTITUDE:
is_valid_falling = True
if len(moving_averages) > 2 and moving_averages[-2]>moving_averages[-1]:
if is_valid_falling:
falling_count += 1
status["way"] = "DOWN("+str(falling_count)+")"
else:
status["way"] = "DOWN(invalid)"
print("DOWN", falling_count)
else:
falling_count = 0
print("UP")
status["way"] = "UP("+str(is_valid_falling)+")"
############################# 강제 사출 조건 검사 #################################
if ser.in_waiting > 0:
try:
read_data = ser.readline().decode()
print("Received: '"+read_data+"'")
except:
read_data = " "
print("Received a word but Fail to decode")
time.sleep(1)
if "<E>" in read_data:
status["parachute"] = "2"
pwm.ChangeDutyCycle(9.5)
time.sleep(2)
pwm.ChangeDutyCycle(7.5)
print("Forced Deploy!")
s.write(f"{datetime.datetime.now()} Servo open: {altitude} m, Type: Force\n")
############################# 자동 사출 조건 검사 #################################
if not is_deployed and falling_count > FALLING_CONFIRMATION:
is_deployed = True
status["parachute"] = "1"
pwm.ChangeDutyCycle(9.5)
time.sleep(2)
pwm.ChangeDutyCycle(7.5)
print("Deploy!")
s.write(f"{datetime.datetime.now()} Servo open: {altitude} m, Type: Auto\n")
############################### 통신 데이터 가공 ###################################
# status["ebimu"] = ", ".join(map(str, eb_data_arr))
status["bmp"] = str(int(altitude))
# 추가해야함.
try:
status_values = list(status.values())
total_message = "/".join(map(str, status_values)) + ";"
for i in range(0, len(total_message), 55):
message = total_message[i:i + 55]
ser.write(message.encode())
print("ok")
except:
print("Transmission failure")
if keyboard.is_pressed("space"):
print("Program exited")
break
############################### 그래프 출력 ###################################
SHOW_GRAPH = False
if SHOW_GRAPH:
y = datas[WINDOW:]
x = range(WINDOW//2,len(datas)-WINDOW+WINDOW//2)
y2 = moving_averages
x2 = range(len(moving_averages))
plt.scatter(x2[-1],y2[-1],color="red", marker="*",s=500,label='Deploy!')
plt.plot(x, y, color="red")
plt.plot(x2,y2,color="blue")
plt.axhline(y=NO_DEPLOY_ALTITUDE,color="green")
plt.xlabel("Data Count")
plt.ylabel("Altitude")
plt.title("Parachute_deployment")
plt.show()