From 19e646286ff032c3716350a28873c557b8c9d032 Mon Sep 17 00:00:00 2001 From: ggw Date: Mon, 11 May 2026 12:50:35 -0700 Subject: [PATCH] updated analysis software --- code/l452_code/packet_parser_ble.py | 10 ++- code/l452_code/packet_parser_helpers.py | 88 ++++++++++++++----------- code/l452_code/packet_parser_log.py | 16 +++++ 3 files changed, 72 insertions(+), 42 deletions(-) create mode 100644 code/l452_code/packet_parser_log.py diff --git a/code/l452_code/packet_parser_ble.py b/code/l452_code/packet_parser_ble.py index 03da5cc..6cded59 100644 --- a/code/l452_code/packet_parser_ble.py +++ b/code/l452_code/packet_parser_ble.py @@ -12,11 +12,15 @@ async def scan(): async def connect(device): async with BleakClient(device) as client: - value = b'6' - await client.write_gatt_char(TX_UUID, value, response=True) + await client.write_gatt_char(TX_UUID, b'4', response=True) + await client.write_gatt_char(TX_UUID, b'6', response=True) await client.stop_notify(RX_UUID) await client.start_notify(RX_UUID, cb) - await asyncio.sleep(10000) + await asyncio.sleep(60) + #await client.write_gatt_char(TX_UUID, b'2', response=True) + #await client.write_gatt_char(TX_UUID, b'S', response=True) + await client.stop_notify(RX_UUID) + #await asyncio.sleep(5) RX_UUID = "00000001-8E22-4541-9D4C-21EDAE82ED19" TX_UUID = "00000000-8E22-4541-9D4C-21EDAE82ED19" diff --git a/code/l452_code/packet_parser_helpers.py b/code/l452_code/packet_parser_helpers.py index befe946..9063b21 100644 --- a/code/l452_code/packet_parser_helpers.py +++ b/code/l452_code/packet_parser_helpers.py @@ -72,8 +72,9 @@ def process_ppg(d, t): accs = [] gyros = [] +imu_sparse = 0 def process_imu(d, t): - global accs, gyros + global accs, gyros, imu_sparse for e in t['elements']: block = d[e['offset']:e['offset'] + e['size']] element_size = int(len(block) / e['n_elements']) @@ -91,58 +92,67 @@ def process_imu(d, t): else: pass #assert False - if len(gyros) > 400: - gyros = gyros[-400:] - if len(accs) > 400: - accs = accs[-400:] - fig, axs = plt.subplots(2) - g = np.array(gyros) - a = np.array(accs) - axs[0].set_ylabel("dps") - axs[0].plot(g[:,0]) - axs[0].plot(g[:,1]) - axs[0].plot(g[:,2]) - axs[1].set_ylabel("g") - axs[1].plot(a[:,0]) - axs[1].plot(a[:,1]) - axs[1].plot(a[:,2]) - plt.savefig("acc_gyro.png") - plt.close() + + imu_sparse += 1 + if imu_sparse % 5 == 4: + if len(gyros) > 1600: + gyros = gyros[-1600:] + accs = accs[-1600:] + fig, axs = plt.subplots(2) + g = np.array(gyros) + a = np.array(accs) + #np.save("gyros.npy", g) + #np.save("accs.npy", a) + a -= np.mean(a, axis = 0).reshape(1,3) + axs[0].set_ylabel("dps") + axs[0].plot(g[:,0]) + axs[0].plot(g[:,1]) + axs[0].plot(g[:,2]) + axs[1].set_ylabel("g") + axs[1].plot(a[:,0]) + axs[1].plot(a[:,1]) + axs[1].plot(a[:,2]) + plt.savefig("acc_gyro.png") + plt.close() ecgs = [] t1s = [] t2s = [] strains = [] - +adc_sparse = 0 def process_adc(d, t): - global ecgs, t1s, t2s, strains + global ecgs, t1s, t2s, strains, adc_sparse for e in t['elements']: block = d[e['offset']:e['offset'] + e['size']] element_size = int(len(block) / e['n_elements']) if e['name'] == b'ekg_readings_cnts[50]': ecgs = ecgs + [(2.4 / (1<<24)) * int.from_bytes(block[4 * i : 4 * i + 4], byteorder = 'little', signed = True) for i in range(50)] - if len(ecgs) > 200: - ecgs = ecgs[-2048:] - t1s = t1s[-205:] - t2s = t2s[-205:] - strains = strains[-205:] - fig, axs = plt.subplots(4) - axs[0].set_title("ECG") - axs[1].set_title("Strain") - axs[2].set_title("oT") - axs[3].set_title("iT") - axs[0].plot(ecgs) - axs[1].plot(strains) - axs[2].plot(t1s) - axs[3].plot(t2s) - plt.savefig("adcs.png") - plt.close() + adc_sparse += 1 + if adc_sparse % 10 == 9: + if len(ecgs) > 200: + ecgs = ecgs[-2048:] + t1s = t1s[-205:] + t2s = t2s[-205:] + strains = strains[-205:] + fig, axs = plt.subplots(1) + axs.set_title("ECG") + #axs[1].set_title("Strain") + #axs[2].set_title("oT") + #axs[3].set_title("iT") + axs.plot(ecgs) + #axs[1].plot(strains) + #axs[2].plot(t1s) + #axs[3].plot(t2s) + fig.tight_layout() + plt.savefig("adcs.png") + plt.close() if e['name'] == b'str_readings_cnts[5]': - strains = strains + [(2.4 / (1<<24)) * int.from_bytes(block[4 * i : 4 * i + 4], byteorder = 'little', signed = True) for i in range(5)] + strains = strains + [int.from_bytes(block[4 * i : 4 * i + 4], byteorder = 'little', signed = True) for i in range(5)] if e['name'] == b'oT_readings_cnts[5]': - t1s = t1s + [(2.4 / (1<<24)) * int.from_bytes(block[4 * i : 4 * i + 4], byteorder = 'little', signed = True) for i in range(5)] + t1s = t1s + [int.from_bytes(block[4 * i : 4 * i + 4], byteorder = 'little', signed = True) for i in range(5)] if e['name'] == b'iT_readings_cnts[5]': - t2s = t2s + [(2.4 / (1<<24)) * int.from_bytes(block[4 * i : 4 * i + 4], byteorder = 'little', signed = True) for i in range(5)] + t2s = t2s + [int.from_bytes(block[4 * i : 4 * i + 4], byteorder = 'little', signed = True) for i in range(5)] + def read_and_process(types, cons, size): index = 0 diff --git a/code/l452_code/packet_parser_log.py b/code/l452_code/packet_parser_log.py new file mode 100644 index 0000000..054fcf2 --- /dev/null +++ b/code/l452_code/packet_parser_log.py @@ -0,0 +1,16 @@ +from packet_parser_helpers import * +import time + +log = open('first_real.LOG','rb').read() + +types = get_type_list(packet_definitions) + +index = 493 +while index < len(log): + size = int.from_bytes(log[index:index + 4], byteorder = 'little', signed = False) + print("INDEX: " + str(index) + " SIZE: " + str(size) + " FILESIZE: " + str(len(log))) + cons = log[index + 4 : index + 4 + size] + read_and_process(types, cons, size) + #time.sleep(0.1) + + index += 4 + size