Skip to content

Commit

Permalink
Use conventional package aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
xioTechnologies committed Sep 2, 2024
1 parent 7756562 commit 88e3cb7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 33 deletions.
64 changes: 37 additions & 27 deletions Python/advanced_example.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import imufusion
import matplotlib.pyplot as pyplot
import numpy
import matplotlib.pyplot as plt
import numpy as np
import sys

# Import sensor data
data = numpy.genfromtxt("sensor_data.csv", delimiter=",", skip_header=1)
data = np.genfromtxt("sensor_data.csv", delimiter=",", skip_header=1)

sample_rate = 100 # 100 Hz

Expand All @@ -17,19 +17,21 @@
offset = imufusion.Offset(sample_rate)
ahrs = imufusion.Ahrs()

ahrs.settings = imufusion.Settings(imufusion.CONVENTION_NWU, # convention
0.5, # gain
2000, # gyroscope range
10, # acceleration rejection
10, # magnetic rejection
5 * sample_rate) # recovery trigger period = 5 seconds
ahrs.settings = imufusion.Settings(
imufusion.CONVENTION_NWU, # convention
0.5, # gain
2000, # gyroscope range
10, # acceleration rejection
10, # magnetic rejection
5 * sample_rate, # recovery trigger period = 5 seconds
)

# Process sensor data
delta_time = numpy.diff(timestamp, prepend=timestamp[0])
delta_time = np.diff(timestamp, prepend=timestamp[0])

euler = numpy.empty((len(timestamp), 3))
internal_states = numpy.empty((len(timestamp), 6))
flags = numpy.empty((len(timestamp), 4))
euler = np.empty((len(timestamp), 3))
internal_states = np.empty((len(timestamp), 6))
flags = np.empty((len(timestamp), 4))

for index in range(len(timestamp)):
gyroscope[index] = offset.update(gyroscope[index])
Expand All @@ -39,30 +41,38 @@
euler[index] = ahrs.quaternion.to_euler()

ahrs_internal_states = ahrs.internal_states
internal_states[index] = numpy.array([ahrs_internal_states.acceleration_error,
ahrs_internal_states.accelerometer_ignored,
ahrs_internal_states.acceleration_recovery_trigger,
ahrs_internal_states.magnetic_error,
ahrs_internal_states.magnetometer_ignored,
ahrs_internal_states.magnetic_recovery_trigger])
internal_states[index] = np.array(
[
ahrs_internal_states.acceleration_error,
ahrs_internal_states.accelerometer_ignored,
ahrs_internal_states.acceleration_recovery_trigger,
ahrs_internal_states.magnetic_error,
ahrs_internal_states.magnetometer_ignored,
ahrs_internal_states.magnetic_recovery_trigger,
]
)

ahrs_flags = ahrs.flags
flags[index] = numpy.array([ahrs_flags.initialising,
ahrs_flags.angular_rate_recovery,
ahrs_flags.acceleration_recovery,
ahrs_flags.magnetic_recovery])
flags[index] = np.array(
[
ahrs_flags.initialising,
ahrs_flags.angular_rate_recovery,
ahrs_flags.acceleration_recovery,
ahrs_flags.magnetic_recovery,
]
)


def plot_bool(axis, x, y, label):
axis.plot(x, y, "tab:cyan", label=label)
pyplot.sca(axis)
pyplot.yticks([0, 1], ["False", "True"])
plt.sca(axis)
plt.yticks([0, 1], ["False", "True"])
axis.grid()
axis.legend()


# Plot Euler angles
figure, axes = pyplot.subplots(nrows=11, sharex=True, gridspec_kw={"height_ratios": [6, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1]})
figure, axes = plt.subplots(nrows=11, sharex=True, gridspec_kw={"height_ratios": [6, 1, 1, 2, 1, 1, 1, 2, 1, 1, 1]})

figure.suptitle("Euler angles, internal states, and flags")

Expand Down Expand Up @@ -107,4 +117,4 @@ def plot_bool(axis, x, y, label):

plot_bool(axes[10], timestamp, flags[:, 3], "Magnetic recovery")

pyplot.show(block="no_block" not in sys.argv) # don't block when script run by CI
plt.show(block="no_block" not in sys.argv) # don't block when script run by CI
12 changes: 6 additions & 6 deletions Python/simple_example.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import imufusion
import matplotlib.pyplot as pyplot
import numpy
import matplotlib.pyplot as plt
import numpy as np
import sys

# Import sensor data
data = numpy.genfromtxt("sensor_data.csv", delimiter=",", skip_header=1)
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 = pyplot.subplots(nrows=3, sharex=True)
_, 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")
Expand All @@ -31,7 +31,7 @@

# Process sensor data
ahrs = imufusion.Ahrs()
euler = numpy.empty((len(timestamp), 3))
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
Expand All @@ -47,4 +47,4 @@
axes[2].grid()
axes[2].legend()

pyplot.show(block="no_block" not in sys.argv) # don't block when script run by CI
plt.show(block="no_block" not in sys.argv) # don't block when script run by CI

0 comments on commit 88e3cb7

Please sign in to comment.