-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathprocessing.py
More file actions
231 lines (191 loc) · 7.53 KB
/
processing.py
File metadata and controls
231 lines (191 loc) · 7.53 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
from config import *
class Message:
counter = 0
data = {}
def __init__(self):
self.reset()
def reset(self):
self.counter = 0
self.data = {}
#for key in self.data.keys():
# self.data[key] = 0
def overwrite(self, new_data):
self.data = new_data
self.counter += 1
def average(self, new_data):
if self.data == {}:
self.overwrite(new_data)
return
self.counter += 1
for key in new_data.keys(): # do averaging for every single field ...
if key == 'timestamp' or \
key == 'fieldID': # ... except for some fields
self.data[key] = new_data[key] # copy the most recent value
continue # don't do averaging for these fields
new_value = new_data[key]
avg_value = self.data[key]
avg_value = ((avg_value * (self.counter-1) + new_value) /
self.counter)
self.data[key] = avg_value
def add_other_fields(self):
self.data['fieldID'] = self.id
def magnitude(self, x, y, z):
return (x ** 2 + y ** 2 + z ** 2) ** 0.5
class ADIS(Message):
id = 'ADIS'
def add_other_fields(self):
self.data['fieldID'] = self.id
self.data['GyroscopeMagn'] = \
self.magnitude(self.data['GyroscopeX'],
self.data['GyroscopeY'],
self.data['GyroscopeZ'])
self.data['AccelerometerMagn'] = \
self.magnitude(self.data['AccelerometerX'],
self.data['AccelerometerY'],
self.data['AccelerometerZ'])
self.data['MagnetometerMagn'] = \
self.magnitude(self.data['MagnetometerX'],
self.data['MagnetometerY'],
self.data['MagnetometerZ'])
def convert(self, tokens, timestamp):
""" converts message data into a python object """
""" input: tuple containing tokenized raw message fields """
""" output: python dictionary object with fields in MKS units """
data = {
'timestamp': timestamp,
'PowerSupply': tokens[0] * self.convert.POWER_SUPPLY,
'GyroscopeX': tokens[1] * self.convert.RATE_GYRO,
'GyroscopeY': tokens[2] * self.convert.RATE_GYRO,
'GyroscopeZ': tokens[3] * self.convert.RATE_GYRO,
'AccelerometerX': tokens[4] * self.convert.ACCELEROMETER,
'AccelerometerY': tokens[5] * self.convert.ACCELEROMETER,
'AccelerometerZ': tokens[6] * self.convert.ACCELEROMETER,
'MagnetometerX': tokens[7] * self.convert.MAGNETOMETER,
'MagnetometerY': tokens[8] * self.convert.MAGNETOMETER,
'MagnetometerZ': tokens[9] * self.convert.MAGNETOMETER,
'Temperature': (tokens[10] * self.convert.TEMPERATURE +
KELVIN_MINUS_CELSIUS),
'AuxiliaryADC': tokens[11] * self.convert.AUX_ADC,
}
return data
convert.POWER_SUPPLY = 2.418 * MILLI # volts (V)
convert.RATE_GYRO = 0.05 # deg/sec
convert.ACCELEROMETER = 3.33 * MILLI * GFORCE_EQ_X_MPS2 # m/s^2
convert.MAGNETOMETER = 0.5 * MILLI * GAUSS_EQ_X_TESLA # tesla (T)
convert.TEMPERATURE = 0.14 # Celsius (C)
convert.AUX_ADC = 806.0 * MICRO # volts (V)
class GPS1(Message):
# id = 'GPS\x01'
id = 'GPS1'
def convert(self, tokens, timestamp):
data = {
'timestamp': timestamp,
'AgeOfDiff': tokens[0], # seconds (s)
'NumOfSats': tokens[1], # a number
'GPSWeek': tokens[2], # a number
'GPSTimeOfWeek': tokens[3], # seconds (s)
'Latitude': tokens[4], # degrees
'Longitude': tokens[5], # degrees
'Height': tokens[6], # meters (m)
'VNorth': tokens[7], # velocity North # meters per second (m/s)
'VEast': tokens[8], # velocity East # meters per second (m/s)
'Vup': tokens[9], # velocity up # meters per second (m/s)
'StdDevResid': tokens[10], # meters (m)
'NavMode': tokens[11], # bit flags
'ExtendedAgeOfDiff': tokens[12] # seconds (s)
}
return data
class ROLL(Message):
id = 'ROLL'
def convert(self, tokens, timestamp):
obj = {
'timestamp': timestamp,
'finPosition': tokens[0] * MICRO, # servo PWM in seconds
'rollServoDisable': float(tokens[1]), # boolean
}
return obj
class MPL3(Message):
id = 'MPL3'
def convert(self, tokens):
data = {
'field0': tokens[0],
'field1': tokens[1],
}
return data
class MPU9(Message):
id = 'MPU9'
def convert(self, tokens):
data = {
'field0': tokens[0],
'field1': tokens[1],
'field2': tokens[2],
'field3': tokens[3],
'field4': tokens[4],
'field5': tokens[5],
'field6': tokens[6],
}
return data
class ERRO(Message):
id = 'ERRO'
def convert(self, timestamp, data):
obj = {
'fieldID': 'ERRO',
'timestamp': timestamp,
'message': data,
}
return obj
class MESG(Message):
id = 'MESG'
def convert(self, timestamp, data):
obj = {
'fieldID': 'MESG',
'timestamp': timestamp,
'message': data, # string
}
return obj
class Stats():
id = 'Stats'
last_seq = sys.maxint # the sequence number of the very last packet
packets_received_total = 0
packets_lost_total = 0
packets_received_recently = 0
packets_lost_recently = 0
most_recent_timestamp = 0
time_last_packet_received = 'never'
def new_packet_received(self, seq):
self.check_for_lost_packets(seq)
self.last_seq = seq
self.packets_received_total += 1
self.packets_received_recently += 1
self.time_last_packet_received = self.get_current_time_string()
def get_current_time_string(self):
time_now = datetime.datetime.now()
return time_now.strftime("%H:%M:%S.") + time_now.strftime("%f")[0]
def check_for_lost_packets(self, seq):
difference = seq - self.last_seq
if difference <= 1:
return
packets_lost_now = seq - self.last_seq - 1
self.packets_lost_total += packets_lost_now
self.packets_lost_recently += packets_lost_now
def recent_timestamp(self, timestamp):
self.most_recent_timestamp = timestamp
def reset(self):
self.packets_received_recently = 0
self.packets_lost_recently = 0
def get(self):
obj = {
'fieldID': 'Stats',
'PacketsReceivedTotal': self.packets_received_total,
'PacketsLostTotal': self.packets_lost_total,
'PacketsReceivedRecently': self.packets_received_recently,
'PacketsLostRecently': self.packets_lost_recently,
'MostRecentTimestamp': self.most_recent_timestamp,
'TimeLastPacketReceived': self.time_last_packet_received,
}
return obj
class Messages:
adis = ADIS()
gps1 = GPS1()
roll = ROLL()
mpl3 = MPL3()