-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection.py
184 lines (155 loc) · 6.39 KB
/
collection.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
import os
import sys
import time
import serial
import signal
import logging
import threading
import numpy as np
import configparser
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from datetime import datetime
config = configparser.ConfigParser()
config.read("config.ini")
conf = config["CONNECTION"]
GYRO_PORT = conf.get("GYRO_PORT", "COM5")
GYRO_RATE = conf.get("GYRO_RATE", 57600)
MAST_PORT = conf.get("MAST_PORT", "COM4")
MAST_RATE = conf.get("MAST_RATE", 57600)
DATA_DIR = conf.get("DATA_DIR", "./data")
ECHO_THRESHOLD = 1000 # TODO Remove?
CHART_DATAPOINTS = 500
STATUS = "RUN"
CONNECTION_STATUS = {GYRO_PORT: False, MAST_PORT: False}
timestamp = datetime.now().strftime("%Y-%m-%d %H-%M-%S")
os.chdir(DATA_DIR)
gyro_file = open(timestamp + "-gyro.csv", "w+")
gyro_file.write("timestamp,elevator,load_factor,pitch_rate\n")
mast_file = open(timestamp + "-mast.csv", "w+")
mast_file.write("timestamp,alpha,beta,ias\n")
DATA = {GYRO_PORT: [], MAST_PORT: []}
FILES = {GYRO_PORT: gyro_file, MAST_PORT: mast_file}
logger = logging.getLogger()
formatter = logging.Formatter('%(asctime)s - %(levelname)s: %(message)s')
fh = logging.FileHandler(f"{timestamp}.log")
ch = logging.StreamHandler()
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
logger.setLevel(logging.INFO)
fig = plt.figure()
axes = fig.subplots(3, 2)
plt.subplots_adjust(hspace=0.5)
def animate(_):
global DATA
gyro_data = np.array(DATA[GYRO_PORT])
mast_data = np.array(DATA[MAST_PORT])
gyro_data = gyro_data[-CHART_DATAPOINTS:]
mast_data = mast_data[-CHART_DATAPOINTS:]
axes[0,0].clear()
axes[0,1].clear()
axes[1,0].clear()
axes[1,1].clear()
axes[2,0].clear()
axes[2,1].clear()
if CONNECTION_STATUS[GYRO_PORT] is False:
axes[0,0].text(0.5, 0.5,'Gyro connection failed', horizontalalignment='center', verticalalignment='center', transform = axes[0,0].transAxes)
axes[0,1].text(0.5, 0.5,'Gyro connection failed', horizontalalignment='center', verticalalignment='center', transform = axes[0,1].transAxes)
axes[1,0].text(0.5, 0.5,'Gyro connection failed', horizontalalignment='center', verticalalignment='center', transform = axes[1,0].transAxes)
elif gyro_data.size != 0:
gyro_data[:,0] = gyro_data[:,0] - time.time()
gyro_data[:,1][gyro_data[:,1]>ECHO_THRESHOLD] = ECHO_THRESHOLD
axes[0,0].plot(gyro_data[:,0], gyro_data[:,1], marker='', linestyle='solid', linewidth=1)
axes[0,1].plot(gyro_data[:,0], gyro_data[:,2], marker='', linestyle='solid', linewidth=1)
axes[1,0].plot(gyro_data[:,0], gyro_data[:,3], marker='', linestyle='solid', linewidth=1)
if CONNECTION_STATUS[MAST_PORT] is False:
axes[1,1].text(0.5, 0.5,'Mast connection failed', horizontalalignment='center', verticalalignment='center', transform = axes[1,1].transAxes)
axes[2,0].text(0.5, 0.5,'Mast connection failed', horizontalalignment='center', verticalalignment='center', transform = axes[2,0].transAxes)
axes[2,1].text(0.5, 0.5,'Mast connection failed', horizontalalignment='center', verticalalignment='center', transform = axes[2,1].transAxes)
elif mast_data.size != 0:
mast_data[:,0] = mast_data[:,0] - time.time()
mast_data[:,1] = -0.23 * mast_data[:,1] + 140.6
mast_data[:,3] = np.sqrt(2 * mast_data[:,3] / 1.225) * 3.6
axes[1,1].plot(mast_data[:,0], mast_data[:,1], marker='', linestyle='solid', linewidth=1)
axes[2,0].plot(mast_data[:,0], mast_data[:,2], marker='', linestyle='solid', linewidth=1)
axes[2,1].plot(mast_data[:,0], mast_data[:,3], marker='', linestyle='solid', linewidth=1)
axes[0,0].title.set_text("Elevator raw")
axes[0,1].title.set_text("Load factor (g)")
axes[1,0].title.set_text("Pitch rate (deg/s)")
axes[1,1].title.set_text("Alpha (deg)")
axes[2,0].title.set_text("Beta raw")
axes[2,1].title.set_text("IAS (km/h)")
axes[0,0].set_ylim(30, 80)
axes[0,1].set_ylim(-2, 1)
axes[1,0].set_ylim(-20, 20)
axes[1,1].set_ylim(-20, 20)
axes[2,0].set_ylim(0, 1024)
axes[2,1].set_ylim(0, 250)
axes[0,0].grid()
axes[0,1].grid()
axes[1,0].grid()
axes[1,1].grid()
axes[2,0].grid()
axes[2,1].grid()
ani = animation.FuncAnimation(fig, animate, interval=500)
def read_serial(port, rate, parse_func):
global STATUS, DATA
logging.info(f"Opening connection on {port} with rate {rate}")
serial_connection = None
while STATUS == "RUN":
try:
serial_connection = serial.Serial(port, rate, timeout=1)
logging.info(f"Serial connection {port} established")
while STATUS == "RUN":
line = serial_connection.readline()
line = line.strip(b"\r\n")
if line.startswith(b"I2C"):
logging.error("I2C Warning")
continue
try:
data = parse_func(line)
FILES[port].write(",".join(map(str, data)) + "\n")
DATA[port].append(data)
except (ValueError, IndexError) as e:
logging.warning(f"Invalid value recieved on {port}: {line}")
CONNECTION_STATUS[port] = True
except serial.SerialException:
CONNECTION_STATUS[port] = False
logging.error(f"Serial connection {port} failed!")
time.sleep(1)
if serial_connection:
serial_connection.close()
def parse_mast(raw):
line_split = raw.split(b",")
data = [time.time(),
float(line_split[4]),
int(line_split[3]),
float(line_split[1])]
return data
def parse_gyro(raw):
line_split = raw.split(b",")
data = [time.time(),
int(line_split[0]),
float(line_split[1]),
float(line_split[2])]
return data
def stop(*args):
global STATUS
logging.warning("User STOP Signal recieved")
STATUS = "STOP"
gyro_thread.join()
mast_thread.join()
gyro_file.close()
mast_file.close()
sys.exit(0)
logging.info("Programm started")
signal.signal(signal.SIGINT, stop)
gyro_thread = threading.Thread(target=read_serial, args=(GYRO_PORT, GYRO_RATE, parse_gyro))
mast_thread = threading.Thread(target=read_serial, args=(MAST_PORT, MAST_RATE, parse_mast))
gyro_thread.start()
mast_thread.start()
plt.show(block=False)
input()
stop()