-
Notifications
You must be signed in to change notification settings - Fork 38
/
plot.py
272 lines (226 loc) · 6.64 KB
/
plot.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
# Plots sensor data logged using the `log-sensors` example
#
# Usage:
#
# $ pipenv run plot.py /path/to/data.txt $WHAT
#
# where `$WHAT` can be one of "accel", "gyro", "gyro-calibrated", "mag" or
# "mag-calibrated"
import cobs.cobs
import itertools
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
import struct
import sys
# apply plot style
sns.set()
# Constants
N = 18 # frame size
K_AR = 8.75e-3 # gyroscope sensitivity
K_G = 2 / (1 << 15) # accelerometer sensitivity
DT = 1 / 220 # sampling period
# Parse input file
with open(sys.argv[1], 'rb') as f:
data = f.read()
mx, my, mz = [], [], []
arx, ary, arz = [], [], []
gx, gy, gz = [], [], []
for (is_separator, frame) in itertools.groupby(data, lambda x: x == 0):
if is_separator:
continue
try:
frame = cobs.cobs.decode(bytes(frame))
except cobs.cobs.DecodeError:
sys.stderr.write('X')
sys.stderr.flush()
continue
if len(frame) != N:
sys.stderr.write('!')
sys.stderr.flush()
continue
start = 0
mx.append(struct.unpack('<h', frame[start:start+2])[0])
start += 2
my.append(struct.unpack('<h', frame[start:start+2])[0])
start += 2
mz.append(struct.unpack('<h', frame[start:start+2])[0])
start += 2
arx.append(struct.unpack('<h', frame[start:start+2])[0])
start += 2
ary.append(struct.unpack('<h', frame[start:start+2])[0])
start += 2
arz.append(struct.unpack('<h', frame[start:start+2])[0])
start += 2
gx.append(struct.unpack('<h', frame[start:start+2])[0])
start += 2
gy.append(struct.unpack('<h', frame[start:start+2])[0])
start += 2
gz.append(struct.unpack('<h', frame[start:start+2])[0])
start += 2
assert(start == N)
target = sys.argv[2]
# Scale data
mx = np.array(mx)
my = np.array(my)
mz = np.array(mz)
mx_max = max(mx)
mx_min = min(mx)
my_max = max(my)
my_min = min(my)
mz_max = max(mz)
mz_min = min(mz)
mx_bias = (mx_max + mx_min) / 2
my_bias = (my_max + my_min) / 2
mz_bias = (mz_max + mz_min) / 2
mx_range = (mx_max - mx_min) / 2
my_range = (my_max - my_min) / 2
mz_range = (mz_max - mz_min) / 2
if target == 'mag-calibrated':
mx = (mx - mx_bias) / mx_range
my = (my - my_bias) / my_range
mz = (mz - mz_bias) / mz_range
mx_max = (mx_max - mx_bias) / mx_range
mx_min = (mx_min - mx_bias) / mx_range
my_max = (my_max - my_bias) / my_range
my_min = (my_min - my_bias) / my_range
mz_max = (mz_max - mz_bias) / mz_range
mz_min = (mz_min - mz_bias) / mz_range
mxy = max([abs(mx_max), abs(mx_min), abs(my_max), abs(my_min)])
myz = max([abs(my_max), abs(my_min), abs(mz_max), abs(mz_min)])
mxz = max([abs(mx_max), abs(mx_min), abs(mz_max), abs(mz_min)])
arx_mean = np.mean(arx) * K_AR
ary_mean = np.mean(ary) * K_AR
arz_mean = np.mean(arz) * K_AR
if target == 'gyro-calibrated':
arx = np.array(arx) * K_AR - arx_mean
ary = np.array(ary) * K_AR - ary_mean
arz = np.array(arz) * K_AR - arz_mean
arx_mean = 0
ary_mean = 0
arz_mean = 0
else:
arx = np.array(arx) * K_AR
ary = np.array(ary) * K_AR
arz = np.array(arz) * K_AR
gx = np.array(gx) * K_G
gy = np.array(gy) * K_G
gz = np.array(gz) * K_G
gx_mean = np.mean(gx)
gy_mean = np.mean(gy)
gz_mean = np.mean(gz)
# Plot
x = np.arange(0, len(arx)) * DT
if target == 'accel':
plt.subplot(221)
plt.plot(x, gx)
plt.plot([x[0], x[-1]], np.ones(2) * gx_mean, label='mean')
plt.xlim(round(x[0]), round(x[-1]))
plt.legend()
plt.ylabel('Acceleration (g)')
plt.title(r'$G_x$')
plt.subplot(222)
plt.plot(x, gy)
plt.plot([x[0], x[-1]], np.ones(2) * gy_mean, label='mean')
plt.xlim(round(x[0]), round(x[-1]))
plt.legend()
plt.title(r'$G_y$')
plt.subplot(223)
plt.plot(x, gz)
plt.plot([x[0], x[-1]], np.ones(2) * gz_mean, label='mean')
plt.xlim(round(x[0]), round(x[-1]))
plt.legend()
plt.xlabel('Time (s)')
plt.ylabel('Acceleration (g)')
plt.title(r'$G_z$')
g = np.sqrt(gx**2 + gy**2 + gz**2)
plt.subplot(224)
plt.plot(x, g)
plt.xlim(round(x[0]), round(x[-1]))
plt.xlabel('Time (s)')
plt.title(r'$\|G\|$')
plt.suptitle('Accelerometer data')
plt.tight_layout()
plt.subplots_adjust(top=0.88)
plt.savefig(target + '.svg')
plt.close()
if target == 'gyro' or target == 'gyro-calibrated':
plt.subplot(221)
plt.plot(x, arx)
plt.plot([x[0], x[-1]], np.ones(2) * arx_mean, label='mean')
plt.xlim(round(x[0]), round(x[-1]))
plt.legend()
plt.ylabel('Angular rate (dps)')
plt.title(r'$AR_x$')
plt.subplot(222)
plt.plot(x, ary)
plt.plot([x[0], x[-1]], np.ones(2) * ary_mean, label='mean')
plt.xlim(round(x[0]), round(x[-1]))
plt.legend()
plt.title(r'$AR_y$')
plt.subplot(223)
plt.plot(x, arz)
plt.plot([x[0], x[-1]], np.ones(2) * arz_mean, label='mean')
plt.xlim(round(x[0]), round(x[-1]))
plt.legend()
plt.xlabel('Time (s)')
plt.ylabel('Angular rate (dps)')
plt.title(r'$AR_z$')
ar = np.sqrt(arx**2 + ary**2 + arz**2)
plt.subplot(224)
plt.plot(x, ar)
plt.xlim(round(x[0]), round(x[-1]))
plt.xlabel('Time (s)')
plt.title(r'$\|AR\|$')
if target == 'gyro-calibrated':
plt.suptitle('Calibrated gyroscope data')
else:
plt.suptitle('Gyroscope data')
plt.tight_layout()
plt.subplots_adjust(top=0.88)
plt.savefig(target + '.svg')
plt.close()
if target == 'mag' or target == 'mag-calibrated':
ax = plt.subplot(221)
plt.plot(mx, my, ',')
plt.xlim(-mxy, mxy)
plt.ylim(-mxy, mxy)
ax.set_aspect(1)
plt.xlabel(r'$M_X$')
plt.ylabel(r'$M_Y$')
plt.title(r'$M_{XY}$')
ax = plt.subplot(222)
plt.plot(my, mz, ',')
plt.xlim(-myz, myz)
plt.ylim(-myz, myz)
ax.set_aspect(1)
plt.xlabel(r'$M_Y$')
plt.ylabel(r'$M_Z$')
plt.title(r'$M_{YZ}$')
ax = plt.subplot(223)
plt.plot(mx, mz, ',')
plt.xlim(-mxz, mxz)
plt.ylim(-mxz, mxz)
ax.set_aspect(1)
plt.xlabel(r'$M_X$')
plt.ylabel(r'$M_Z$')
plt.title(r'$M_{XZ}$')
m = np.sqrt(mx**2 + my**2 + mz**2)
plt.subplot(224)
plt.plot(x, m)
plt.xlim(round(x[0]), round(x[-1]))
plt.xlabel('Time (s)')
plt.title(r'$\|M\|$')
if target == 'mag-calibrated':
plt.suptitle('Calibrated magnetometer data')
else:
plt.suptitle('Magnetometer data')
plt.tight_layout()
plt.subplots_adjust(top=0.88)
plt.savefig(target + '.svg')
plt.close()
if target == 'mag-calibrated':
print()
print('X(bias =', mx_bias, ', range =', mx_range, ')')
print('Y(bias =', my_bias, ', range =', my_range, ')')
print('Z(bias =', mz_bias, ', range =', mz_range, ')')