From 1f024f477a2bc10984aa3777cb2bc93a63f37de2 Mon Sep 17 00:00:00 2001 From: Brendan Haines Date: Sat, 31 May 2025 22:39:33 -0600 Subject: [PATCH] plotting --- python/test_basic.py | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/python/test_basic.py b/python/test_basic.py index 1530919..bf5ebd0 100644 --- a/python/test_basic.py +++ b/python/test_basic.py @@ -6,6 +6,9 @@ import numpy as np from bleak import BleakClient, BleakScanner from matplotlib import pyplot as plt +# NOTE: this must be run in Jupyter. I rely on the Jupyter run loop for async + + # %% devices = await BleakScanner.discover() for d in devices: @@ -24,7 +27,7 @@ async with BleakClient(address) as client: # print("Model Number: {0}".format("".join(map(chr, model_number)))) start_time = time.time() print("Starting capture...") - while time.time() < start_time + 20: + while time.time() < start_time + 3: buffer = await client.read_gatt_char(SENSORS_UUID) sensor_values = np.frombuffer(bytes(buffer), dtype=np.double) timeseries.append(sensor_values) @@ -33,7 +36,36 @@ async with BleakClient(address) as client: timeseries = np.array(timeseries) # %% -plt.plot(timeseries) +plt.plot(timeseries, marker=".") +plt.show() + +# %% +a_x, a_y, a_z, g_x, g_y, g_z, m_x, m_y, m_z = timeseries.T +t = np.linspace(0, 2, len(timeseries)) +a = np.sqrt(a_x**2 + a_y**2 + a_z**2) +g = 9.81 # acceleration due to gravity +h = np.array([0, *np.cumsum((a - g)[1:] * np.diff(t))]) + +# %% +plt.plot(t, a_x, label="$A_x$") +plt.plot(t, a_y, label="$A_y$") +plt.plot(t, a_z, label="$A_z$") +plt.plot(t, a, color="k", label="$A_{total}$") +plt.ylabel("Acceleration [m/s^2]") +plt.xlabel("Time [s]") +plt.xlim(t[0], t[-1]) +plt.legend() +plt.grid(True) +plt.show() + +# %% +plt.plot(t, h, label="$Z$") +plt.ylabel("Displacement [m]") +plt.title("Displacement assuming only vertical motion") +plt.xlabel("Time [s]") +plt.xlim(t[0], t[-1]) +plt.legend() +plt.grid(True) plt.show() # %%