-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGroundProcessing.py
More file actions
248 lines (174 loc) · 5.45 KB
/
Copy pathGroundProcessing.py
File metadata and controls
248 lines (174 loc) · 5.45 KB
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
import array
import matplotlib.pyplot as plt
from decimal import *
from matplotlib.ticker import FormatStrFormatter
import struct
import Plotter
RADIOSET = None
PRINTOUT = False
#A class to represent data with a time stamp
class DATA:
def __init__(self, data, time):
self.data = data
self.time = time
#these functions are wrappers for the global variable
def initialize():
global RADIOSET
RADIOSET = initializeRadioSet()
return
def addData(data):
global RADIOSET
RADIOSET = addToRadioSet(data, RADIOSET)
return
def printToFile():
radioSetToFile(RADIOSET)
return
#the following functions are for getting data from the radio connection
#creates working radio set radioset[sensor][data]
#the first characters are ascii identifiers for radio communication
#The time list contains floats with the timestamp
#All other catagories use the ADT DATA which contain data and timestamp internal variables
#this allows data for some sensors to be sent periodically
def initializeRadioSet():
radioSet = list()
a = ['X Accelerometer - Acceleration X (g)']
b = ['Y Accelerometer - Acceleration Y (g)']
c = ['Z Accelerometer - Acceleration Z (g)']
d = ['P Barometer - Pressure (mbar)']
e = ['~ Barometer - Temperature (C)']
f = ['T Temperature Sensor - Temperature (C)']
g = ['@ IMU - Yaw']
h = ['# IMU - Roll']
i = ['$ IMU - Pitch']
j = ['L GPS - Latitude']
k = ['l GPS - Longitude']
l = ['A GPS - Altitude']
m = ['a Calculated Altitude']
n = ['s State']
o = ['b Battery Voltage']
p = ['g Ground Altitude']
t = ['t Time']
#time is always the first list for easy access
radioSet.append(t)
radioSet.append(a)
radioSet.append(b)
radioSet.append(c)
radioSet.append(d)
radioSet.append(e)
radioSet.append(f)
radioSet.append(g)
radioSet.append(h)
radioSet.append(i)
radioSet.append(j)
radioSet.append(k)
radioSet.append(l)
radioSet.append(m)
radioSet.append(n)
radioSet.append(o)
radioSet.append(p)
return radioSet
#radioData takes in a list of 5 numbers between 0 and 255 representing bytes
#The first byte is the identifier and the remainder are a float representing the data
#returns the updated radioSet set
def addToRadioSet(radioData, radioSet):
global PRINTOUT
character = chr(radioData[0])
#turns d into a float from decimal representation of 4 sepreate bytes in a list
data = radioData[1:5]
b= struct.pack('4B', *data)
#should be in little endian format from the teensy?
c=struct.unpack('<f', b)
d=c[0]
if character == 't':
radioSet[0].append(d)
if PRINTOUT:
print("t " + str(d))
else:
length = len(radioSet[0])
time = radioSet[0][length-1]
#create a data point with the the latest time
length = len(radioSet)
addData = DATA(d, time)
# Plot new Pressure data
if character == 'a':
Plotter.plot(addData.time, addData.data)
if PRINTOUT:
print(character + " " + str(addData.data))
for i in range (0, length):
s = radioSet[i][0]
if s[0] == character:
radioSet[i].append(addData)
break
return radioSet
#creates a file names "Radio_Data.csv" which contains all values in the working radio set in csv format
#currently there is no character for a sensor lacking data for a time, may need to change to a space
#Will take a longish time to execute, only bother running if you are done reading data
def radioSetToFile(radioSet):
file = open("Radio_Data.csv", "w")
sensors = len(radioSet)-1
#write the titles
string = ""
for i in range (0, sensors):
string = string + radioSet[i][0]+","
string = string + radioSet[sensors][0]+"\n"
file.write(string)
#write the data
#time is the first row
timestamps = len(radioSet[0])
#going through each time
for i in range (1, timestamps):
string = str(radioSet[0][i]) + "," #set first part to time
#going down the row of sensors
for j in range (1, sensors+1):
#now check each data point for the sensor to see if you can add data, else add just ,
datapoints = len(radioSet[j])
#there are no datapoints for the sensors
if datapoints == 1:
if j == sensors:
string = string + "\n"
else:
string = string + ","
#there are dtapoints for the sensors
else:
#go through all the datapoints and see if the time matches the time at the start of the row
for k in range (1, datapoints):
#data time is equal to current time, add it to the row and leave the for loop
if radioSet[j][k].time == radioSet[0][i]:
if j == sensors:
string = string + str(radioSet[j][k].data) + "\n"
else :
string = string + str(radioSet[j][k].data) + ","
break
#data time has past current time, add no data to the row and leave the loop
elif not isinstance(radioSet[j][k].time, str) and radioSet[j][k].time > radioSet[0][i]:
if j == sensors:
string = string + "\n"
else :
string = string + ","
break
#time has past data time so no data is added for sensor
elif k == (datapoints-1):
if j == sensors:
string = string + "\n"
else :
string = string + ","
file.write(string)
file.close()
return
# print("What file would you like to read from?")
# string = input()
# dataSet = sortDataFromSD(string)
# graphSensorVsTime(0 , dataSet)
# print("write yeet")
# string = input()
# if string == "yeet":
# print("YYYEEEEEETT")
# printSensors(dataSet)
# string = input
# print(string)
# while string != "exit":
# #printInstructions();
# string = input
# print(string)
#dataSet = sortDataFromSD("DATALOG.csv")
#graphSensorVsTime(0, dataSet)