-
Notifications
You must be signed in to change notification settings - Fork 257
/
simple_example.py
50 lines (41 loc) · 1.55 KB
/
simple_example.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
import imufusion
import matplotlib.pyplot as plt
import numpy as np
import sys
# Import sensor data
data = np.genfromtxt("sensor_data.csv", delimiter=",", skip_header=1)
timestamp = data[:, 0]
gyroscope = data[:, 1:4]
accelerometer = data[:, 4:7]
# Plot sensor data
_, axes = plt.subplots(nrows=3, sharex=True)
axes[0].plot(timestamp, gyroscope[:, 0], "tab:red", label="X")
axes[0].plot(timestamp, gyroscope[:, 1], "tab:green", label="Y")
axes[0].plot(timestamp, gyroscope[:, 2], "tab:blue", label="Z")
axes[0].set_title("Gyroscope")
axes[0].set_ylabel("Degrees/s")
axes[0].grid()
axes[0].legend()
axes[1].plot(timestamp, accelerometer[:, 0], "tab:red", label="X")
axes[1].plot(timestamp, accelerometer[:, 1], "tab:green", label="Y")
axes[1].plot(timestamp, accelerometer[:, 2], "tab:blue", label="Z")
axes[1].set_title("Accelerometer")
axes[1].set_ylabel("g")
axes[1].grid()
axes[1].legend()
# Process sensor data
ahrs = imufusion.Ahrs()
euler = np.empty((len(timestamp), 3))
for index in range(len(timestamp)):
ahrs.update_no_magnetometer(gyroscope[index], accelerometer[index], 1 / 100) # 100 Hz sample rate
euler[index] = ahrs.quaternion.to_euler()
# Plot Euler angles
axes[2].plot(timestamp, euler[:, 0], "tab:red", label="Roll")
axes[2].plot(timestamp, euler[:, 1], "tab:green", label="Pitch")
axes[2].plot(timestamp, euler[:, 2], "tab:blue", label="Yaw")
axes[2].set_title("Euler angles")
axes[2].set_xlabel("Seconds")
axes[2].set_ylabel("Degrees")
axes[2].grid()
axes[2].legend()
plt.show(block="no_block" not in sys.argv) # don't block when script run by CI