l452 usart working

This commit is contained in:
ggw
2026-04-29 18:20:54 -05:00
parent ab8d4e672a
commit 82048ad703
13 changed files with 356 additions and 122 deletions
+29 -19
View File
@@ -2,8 +2,7 @@ import serial
import matplotlib.pyplot as plt
import numpy as np
from scipy import signal
#should be running at 244Hz
import time
fs = 244
b, a = signal.butter(
@@ -46,13 +45,17 @@ for i in range(13):
ser.flush()
ser.timeout = 5
ys = []
adcs = []
accs = []
gyros = []
imu_readings = {}
for i in range(1000):
fig, axs = plt.subplots(2,2)
then = time.time()
for i in range(10):
ser.write(b'R')
data = ser.read(1024)
continue
index = 0
while (index < 1024):
packet_type = (data[index + 1]<<8) + data[index]
@@ -65,17 +68,23 @@ for i in range(1000):
for e in t['elements']:
block = d[e['offset']:e['offset'] + e['size']]
element_size = int(len(block) / e['n_elements'])
# if t['type_name'] == b'packet_ekg' and e['name'] == b'readings_cnts[50]':
# chunked = [(2.4 / (1<<24)) * int.from_bytes(block[i:i + element_size], byteorder='little', signed = True) for i in range(0, len(block), element_size)]
# #BB = [block[i:i + element_size] for i in range(0, len(block), element_size)]
# #if t['type_name'] == b'packet_ekg' and e['name'] == b'readings_cnts[50]':
# ys += chunked
# plt.clf()
# dat = np.array(ys[-1000:])
# plt.plot(dat, 'k.', linestyle = '--')#signal.filtfilt(b, a, dat))
# plt.pause(0.01)
# ##print(BB)
# e['readings'].append(chunked)
if t['type_name'] == b'packet_ekg' and e['name'] == b'readings_cnts[50]':
chunked = [(2.4 / (1<<24)) * int.from_bytes(block[i:i + element_size], byteorder='little', signed = True) for i in range(0, len(block), element_size)]
adcs += chunked
axs[0][0].cla()
axs[0][1].cla()
axs[1][1].cla()
dat = np.array(adcs[-1000:])
axs[0][0].set_title("ADC")
axs[0][1].set_title("ACC")
axs[1][1].set_title("GYRO")
axs[0][0].plot(dat, 'k.', linestyle = '--')#signal.filtfilt(b, a, dat))
axs[0][1].plot(accs[-30:], 'k.', linestyle = '--')#signal.filtfilt(b, a, dat))
axs[1][1].plot(gyros[-30:], 'k.', linestyle = '--')#signal.filtfilt(b, a, dat))
plt.pause(0.01)
##print(BB)
e['readings'].append(chunked)
if t['type_name'] == b'packet_imu' and e['name'] == b'readings_cnts[4]':
imu_reading_type = block[0] >> 3
imu_reading_tag_cnt = (block[0] >> 1) & 3 # tag count is sensor time slow
@@ -84,9 +93,9 @@ for i in range(1000):
int.from_bytes(block[6:8], byteorder = 'big', signed = True)]
if imu_reading_type == 1:
#print("{:02x} {:d}".format(imu_reading_type, imu_reading_tag_cnt))
print("Gyro: {:f} {:f} {:f}".format(*[250 * e / (1<<16) for e in reading_xyz]))
gyros.append([250 * e / (1<<16) for e in reading_xyz])
elif imu_reading_type == 2:
print("Acc: {:f} {:f} {:f}".format(*[4 * e / (1<<16) for e in reading_xyz]))
accs.append([4 * e / (1<<16) for e in reading_xyz])
else:
print("ERR")
#chunked = [int.from_bytes(block[i:i + element_size], byteorder='little', signed = True) for i in range(0, len(block), element_size)]
@@ -99,3 +108,4 @@ for i in range(1000):
# if t['type_name'] == b'packet_ekg':
# print(t)
print((time.time() - then) / 10)