log fix (added f_extend), other changes

This commit is contained in:
ggw
2026-05-24 09:37:56 -05:00
parent 19e646286f
commit 7d7297b12f
54 changed files with 294 additions and 7603 deletions
+1 -1
View File
@@ -59,7 +59,7 @@ struct dbl_buff {
extern struct dbl_buff<1024, usb_send> usb_buff;
extern struct dbl_buff<1024, uart_send> ble_buff;
extern struct dbl_buff<1024, sd_send> sd_buff;
extern struct dbl_buff<4096, sd_send> sd_buff;
volatile extern bool send_imu_usb;
volatile extern bool send_imu_ble;
+2 -1
View File
@@ -31,7 +31,7 @@ void initialize_ads131(SPI_HandleTypeDef *hspi1) {
cmd[2] = 0x00;
cmd[3] = 0b00001111;
cmd[4] = 0b00011111;
cmd[4] = 0b00011011;
cmd[5] = 0x00;
HAL_GPIO_WritePin(GPIOA, ADC_CS_Pin, GPIO_PIN_RESET);
@@ -67,6 +67,7 @@ void initialize_ads131(SPI_HandleTypeDef *hspi1) {
struct packet_adc p_adc;
// With 5MHz SPI ~30uS for transfer
void read_ads131(SPI_HandleTypeDef *hspi1) {
if (HAL_GPIO_ReadPin(GPIOA, ADC_DRDY_Pin) != GPIO_PIN_RESET) {
+13 -4
View File
@@ -5,9 +5,16 @@ void sd_send(uint8_t data[], uint32_t len) {
extern bool sd_ready;
extern FIL file;
if (sd_ready) {
//char buff[80];
//CDC_Transmit_FS((uint8_t*)"W\n\r", 3);
UINT bytes_written;
FRESULT res = f_write(&file, data, len, &bytes_written);
//if (res != FR_OK || bytes_written != db_size)) {}
//f_sync(&file);
//int cx = snprintf(buff, 80, "%d %d %ld %ld\n\r", res, bytes_written, len, *((uint32_t*)data));
//CDC_Transmit_FS((uint8_t*)buff, cx);
if (res != FR_OK || bytes_written != len) {
}
}
}
@@ -22,11 +29,13 @@ void usb_send(uint8_t data[], uint32_t len) {
void uart_send(uint8_t data[], uint32_t len) {
extern bool wb1mmc_ready;
if (wb1mmc_ready) {
HAL_UART_Transmit_DMA(&huart1, data, len);
wb1mmc_ready = false;
if (huart1.gState == HAL_UART_STATE_READY) {
HAL_UART_Transmit_DMA(&huart1, data, len);
wb1mmc_ready = false;
}
}
}
struct dbl_buff<1024, usb_send> usb_buff;
struct dbl_buff<1024, uart_send> ble_buff;
struct dbl_buff<1024, sd_send> sd_buff;
struct dbl_buff<4096, sd_send> sd_buff;
+6 -5
View File
@@ -39,20 +39,20 @@ void initialize_lsm6dsv(SPI_HandleTypeDef *hspi1) {
tx_buff[1] = 0x01; // I2C and I3C disabled
send_receive_lsm6dsv(tx_buff, rx_buff, 2);
tx_buff[0] = 0x09; // Set 120Hz data rate for fifo
tx_buff[1] = 0b01100110;
tx_buff[0] = 0x09; // Set 240Hz data rate for fifo
tx_buff[1] = 0b01110111;
send_receive_lsm6dsv(tx_buff, rx_buff, 2);
tx_buff[0] = 0x0D; // Set drdy based on fifo
tx_buff[1] = 0b00111000;
send_receive_lsm6dsv(tx_buff, rx_buff, 2);
tx_buff[0] = 0x10; // Set 120Hz accelerometer high performance
tx_buff[0] = 0x10; // Set 240Hz accelerometer high performance
tx_buff[1] = 0b00000111;
send_receive_lsm6dsv(tx_buff, rx_buff, 2);
tx_buff[0] = 0x11; // Set 120Hz gyro high performance
tx_buff[1] = 0b00000110;
tx_buff[0] = 0x11; // Set 240Hz gyro high performance
tx_buff[1] = 0b00000111;
send_receive_lsm6dsv(tx_buff, rx_buff, 2);
tx_buff[0] = 0x07;
@@ -66,6 +66,7 @@ void initialize_lsm6dsv(SPI_HandleTypeDef *hspi1) {
struct packet_imu p_imu;
// With 5MHz SPI, the transfer should take 0.2mS
void read_lsm6dsv(SPI_HandleTypeDef *hspi1) {
if (HAL_GPIO_ReadPin(GPIOC, IMU_DRDY_Pin) == GPIO_PIN_RESET) {
+52 -11
View File
@@ -114,6 +114,7 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
volatile bool to_open_sd = false;
volatile bool to_close_sd = false;
volatile bool to_print_dir = false;
void comm_handler(uint8_t ch) {
switch (ch) {
@@ -150,7 +151,10 @@ void comm_handler(uint8_t ch) {
break;
case '?':
print_desc = true;
break;
break;
case 'l':
to_print_dir = true;
break;
}
}
@@ -181,27 +185,54 @@ void open_sd(void) {
return;
}
alignas(32) char buffer1[32];
int cx = snprintf(buffer1, 32, "%08lu.log", ((uint32_t)tim6_reloads) * (htim6.Init.Period + 1) + TIM6->CNT % 99999999);
int cx = snprintf(buffer1, 32, "%08lu.log", (((uint32_t)tim6_reloads) * (htim6.Init.Period + 1) + TIM6->CNT) % 99999999);
res = f_open(&file, buffer1, FA_WRITE | FA_CREATE_ALWAYS);
if (res != FR_OK) {
res = f_mount(NULL, "", 1);
return;
}
f_expand(&file, 1024 * 1024 * 100, 1);
unsigned int bw;
data_description(false, true);
sd_ready = true;
}
}
void close_sd(void) {
sd_ready = false;
to_close_sd = false;
if (sd_ready) {
f_sync(&file);
f_close(&file);
res = f_mount(NULL, "", 1);
void print_dirs(void) {
char buff[80];
FRESULT fr;
DIR dir;
FILINFO fno;
res = f_mount(&fs, "", 1);
fr = f_opendir(&dir, "");
while (1)
{
fr = f_readdir(&dir, &fno);
if (fr != FR_OK || fno.fname[0] == 0)
break;
// Skip directories just in case
if (fno.fattrib & AM_DIR)
continue;
int cx = snprintf(buff, 80, "%s %10lu\n\r", fno.fname, (unsigned long)fno.fsize);
CDC_Transmit_FS((uint8_t*)buff, cx);
}
f_closedir(&dir);
f_mount(NULL, "", 1);
}
void close_sd(void) {
if (!sd_ready) {
return;
}
to_close_sd = false;
res = f_sync(&file);
res = f_close(&file);
res = f_mount(NULL, "", 1);
sd_ready = false;
}
/* USER CODE END 0 */
@@ -269,6 +300,11 @@ int main(void)
read_ads131(&hspi1);
read_max30101(&hi2c1);
if (to_print_dir) {
to_print_dir = false;
print_dirs();
}
if (to_open_sd) {
open_sd();
}
@@ -303,15 +339,20 @@ int main(void)
write(p_vbatt);
}
if (usb_ready and total_tim6() - usb_buff.last_write > 4000) {
if (usb_ready and total_tim6() - usb_buff.last_write > 8000) {
extern dbl_buff<1024, usb_send> usb_buff;
usb_buff.send();
}
if (wb1mmc_ready and total_tim6() - ble_buff.last_write > 4000) {
if (wb1mmc_ready and total_tim6() - ble_buff.last_write > 8000) {
extern dbl_buff<1024, uart_send> ble_buff;
ble_buff.send();
}
if (sd_ready and total_tim6() - sd_buff.last_write > 8000) {
extern dbl_buff<4096, sd_send> sd_buff;
sd_buff.send();
}
}
/* USER CODE END 3 */
}
+1
View File
@@ -58,6 +58,7 @@ void initialize_max30101(I2C_HandleTypeDef *hi2c1) {
HAL_I2C_Mem_Read(hi2c1, 0xAE, 0x07, 1, p_spo2.bytes, 180, HAL_MAX_DELAY);
}
// 3mS transfer but shouldn't block...
void read_max30101(I2C_HandleTypeDef *hi2c1) {
if (print_ready) {
p_spo2.t = total_tim6();
+1 -1
View File
@@ -63,7 +63,7 @@
#define _USE_FASTSEEK 1
/* This option switches fast seek feature. (0:Disable or 1:Enable) */
#define _USE_EXPAND 0
#define _USE_EXPAND 1
/* This option switches f_expand function. (0:Disable or 1:Enable) */
#define _USE_CHMOD 0
+1 -1
View File
@@ -1,5 +1,5 @@
##########################################################################################################################
# File automatically-generated by tool: [projectgenerator] version: [4.9.0-B19] date: [Fri May 08 12:05:31 CDT 2026]
# File automatically-generated by tool: [projectgenerator] version: [4.9.0-B19] date: [Sat May 23 22:49:48 CDT 2026]
##########################################################################################################################
# ------------------------------------------------
+2 -1
View File
@@ -62,10 +62,11 @@ Dma.USART1_TX.1.PeriphInc=DMA_PINC_DISABLE
Dma.USART1_TX.1.Priority=DMA_PRIORITY_LOW
Dma.USART1_TX.1.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority
FATFS.BSP.number=1
FATFS.IPParameters=USE_DMA_CODE_SD,_USE_LFN,_MAX_SS,_FS_NORTC
FATFS.IPParameters=USE_DMA_CODE_SD,_USE_LFN,_MAX_SS,_FS_NORTC,_USE_EXPAND
FATFS.USE_DMA_CODE_SD=0
FATFS._FS_NORTC=1
FATFS._MAX_SS=512
FATFS._USE_EXPAND=1
FATFS._USE_LFN=0
FATFS0.BSP.STBoard=false
FATFS0.BSP.api=Unknown
+156 -72
View File
@@ -1,5 +1,6 @@
import numpy as np
import matplotlib.pyplot as plt
import scipy as sp
packet_definitions = b"""packet_rtc 1 28 uint32_t t 0 4 RTC_TimeTypeDef sTime 4 20 RTC_DateTypeDef sDate 24 4
packet_vbatt 2 8 uint32_t t 0 4 uint16_t vbatt_cnts 4 2
@@ -32,21 +33,10 @@ def get_type_list(lines):
i += 4
return types
def my_filter(arr):
inds = np.where(np.abs(np.diff(arr)) > 0.005)[0]
for ind in inds:
prev_ind = ind - 1
while prev_ind in inds:
prev_ind -= 1
next_ind = ind + 1
while next_ind in inds:
next_ind += 1
arr[ind] = 0.5 * arr[prev_ind] + 0.5 * arr[next_ind]
return arr
reds = []
irs = []
greens = []
ppg_freq_Hz = 50
def process_ppg(d, t):
global greens, reds, irs
for e in t['elements']:
@@ -56,19 +46,19 @@ def process_ppg(d, t):
reds += [int.from_bytes(block[3 * i : 3 * i + 3], byteorder = 'big') for i in range(0,60,3)]
irs += [int.from_bytes(block[3 * i : 3 * i + 3], byteorder = 'big') for i in range(1,60,3)]
greens += [int.from_bytes(block[3 * i : 3 * i + 3], byteorder = 'big') for i in range(2,60,3)]
if len(reds) > 400:
reds = reds[-400:]
irs = irs[-400:]
greens = greens[-400:]
fig, axs = plt.subplots(3)
axs[0].set_title('red')
axs[1].set_title('ir')
axs[2].set_title('green')
axs[0].plot(reds)
axs[1].plot(irs)
axs[2].plot(greens)
plt.savefig("ppg.png")
plt.close()
# if len(reds) > 400:
# reds = reds[-400:]
# irs = irs[-400:]
# greens = greens[-400:]
# fig, axs = plt.subplots(3)
# axs[0].set_title('red')
# axs[1].set_title('ir')
# axs[2].set_title('green')
# axs[0].plot(reds)
# axs[1].plot(irs)
# axs[2].plot(greens)
# plt.savefig("ppg.png")
# plt.close()
accs = []
gyros = []
@@ -93,66 +83,160 @@ def process_imu(d, t):
pass
#assert False
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()
# 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 = []
ts = []
adc_sparse = 0
# Should be running at 488Hz
freq_Hz = 488.28125
max_hr_Hz = 240 / 60
def process_adc(d, t):
global ecgs, t1s, t2s, strains, adc_sparse
global ecgs, t1s, t2s, strains, adc_sparse, ts
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't':
ts.append((1 / 2000) * int.from_bytes(block[:4], byteorder = 'little', signed = True))
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)]
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 + [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 + [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 + [int.from_bytes(block[4 * i : 4 * i + 4], byteorder = 'little', signed = True) for i in range(5)]
# adc_sparse += 1
# if adc_sparse % 10 == 9:
# if len(ecgs) > 200:
# ecgs = ecgs[-4 * 4096:]
# t1s = t1s[-4096:]
# t2s = t2s[-4096:]
# strains = strains[-4096:]
# fig, axs = plt.subplots(2)
# axs[0].set_title("ECG")
# #axs[2].plot(np.array(ts[-200:-1]), 1000 * np.diff(ts[-200:]),'k.',linestyle='--')
# #if len(ts) > 200:
# # axs[2].set_title(str((ts[-1] - ts[-200]) / 200))
# #axs[2].set_xlabel('S')
# #axs[2].set_ylabel('mS')
# #axs[1].set_title("Strain")
# #axs[2].set_title("oT")
# #axs[3].set_title("iT")
# # 0.25 * 244 Hz
# #b, a = sp.signal.butter(2, [1 / (0.5 * freq_Hz), 120 / (0.5 * freq_Hz)], btype = 'bandpass')
# #ecgs_ = sp.signal.filtfilt(b, a, ecgs)
# #beats = np.where(np.diff(ecgs_) < -0.0003)[0]
# #last_beat = -1000000
# #beats_ = []
# #for beat in beats:
# # if (beat - last_beat) / 488 > 1 / (max_hr_Hz):
# # last_beat = beat
# # beats_.append(beat)
# axs[0].plot(ecgs, 'k')
# r_peaks = sp.signal.find_peaks(ecgs, height = None, threshold = None, distance = freq_Hz / max_hr_Hz, width = 5, prominence = 0.001)[0]
# all_peaks = sp.signal.find_peaks(ecgs, height = None, threshold = None, width = 10, prominence = 0.0001)[0]
# # for peak in all_peaks:
# # ls = [e for e in r_peaks if peak - e < 0 and peak - e > -0.2 * freq_Hz]
# # if len(ls) >= 1:
# # axs.plot(peak, ecgs[peak], 'bo') # P peaks
# # for peak in all_peaks:
# # ls = [e for e in r_peaks if peak - e > 0 and peak - e < 0.4 * freq_Hz]
# # if len(ls) >= 1:
# # axs.plot(peak, ecgs[peak], 'yo') # T peaks
# for peak in r_peaks:
# axs[0].plot(peak, ecgs[peak], 'ro')
# if len(r_peaks) > 5:
# hr_bpm = np.mean(60 * freq_Hz / np.diff(r_peaks))
# #hrv_ms = 1000 * np.std(np.diff(r_peaks)) / freq_Hz
# hrv_rmssd_ms = 1000. * np.power(np.mean(np.power(np.diff(np.diff(r_peaks / freq_Hz)), 2)), 0.5)
# axs[0].set_title(f"ECG HR:{np.round(hr_bpm)}BPM HRV (RMSSD):{np.round(hrv_rmssd_ms)}ms")
# hist = np.histogram(np.diff(r_peaks), bins = 8)
# axs[1].plot(0.5 * (hist[1][1:] + hist[1][:-1]), hist[0])
# #axs[1].plot(strains)
# #axs[2].plot(t1s)
# #axs[3].plot(t2s)
# fig.tight_layout()
# plt.savefig("adcs.png")
# plt.close()
# fig, axs = plt.subplots()
# axs.plot(ts[1:], np.diff(ts), 'k.')
# plt.savefig("adc_ts.png")
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)]
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)]
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)]
def make_graphs():
fig, axs = plt.subplots(2,2)
b, a = sp.signal.butter(2, [1 / (0.5 * freq_Hz), 120 / (0.5 * freq_Hz)], btype = 'bandpass')
ecgs_ = sp.signal.filtfilt(b, a, ecgs)
r_peaks = sp.signal.find_peaks(ecgs, height = None, threshold = None, distance = freq_Hz / max_hr_Hz, width = 5, prominence = 0.001)[0]
axs[0][0].plot(np.arange(len(ecgs)) / freq_Hz, ecgs_, 'k.', linestyle='--')
for peak in r_peaks:
axs[0][0].plot(peak / freq_Hz, ecgs_[peak], 'ro')
axs[1][0].plot(r_peaks[:-1] / freq_Hz, 1 / (np.diff(r_peaks) /freq_Hz), 'k.')
b, a = sp.signal.butter(2, [0.05 / (0.5 * ppg_freq_Hz), 4 / (0.5 * ppg_freq_Hz)], btype = 'bandpass')
reds_ = sp.signal.filtfilt(b, a, reds)
#irs_ = sp.signal.filtfilt(b, a, irs)
#greens_ = sp.signal.filtfilt(b, a, greens)
P_r = np.log(np.abs(np.fft.rfft(reds_)))
P_i = np.log(np.abs(np.fft.rfft(irs)))
P_g = np.log(np.abs(np.fft.rfft(greens)))
#axs[0][1].plot(np.arange(P_r.shape[0]) * 1 / 200, P_r)
#axs[0][1].plot(P_i)
#axs[0][1].plot(P_g)
axs[0][1].plot(np.arange(len(reds)) / 50, reds_, color = 'red')
#ax2 = axs[0][1].twinx()
#ax2.plot(np.arange(len(reds)) / 50, irs_, color = 'magenta')
#ax3 = ax2.twinx()
#ax3.plot(np.arange(len(reds)) / 50, greens_, color = 'green')
#axs[1][1].plot(np.array(gyros)[:,0])
#axs[1][1].plot(np.array(gyros)[:,1])
#axs[1][1].plot(np.array(gyros)[:,2])
gs = np.array(gyros)
acs = np.array(accs)
f_gs = np.log(np.abs(np.fft.rfft(acs, axis = 0)))
axs[1][1].plot(f_gs[:,0], alpha = 0.25)
axs[1][1].plot(f_gs[:,1], alpha = 0.25)
axs[1][1].plot(f_gs[:,2], alpha = 0.25)
#axs[1][1].plot(strains)
#axs[1][1].plot(t1s)
#axs[1][1].plot(t2s)
plt.show()
def read_and_process(types, cons, size):
index = 0
+13 -6
View File
@@ -1,16 +1,23 @@
from packet_parser_helpers import *
import packet_parser_helpers
import time
log = open('first_real.LOG','rb').read()
import matplotlib.pyplot as plt
types = get_type_list(packet_definitions)
log = open('5-23-2026-first-10-min.LOG','rb').read()
types = packet_parser_helpers.get_type_list(packet_parser_helpers.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)
try:
packet_parser_helpers.read_and_process(types, cons, size)
#time.sleep(0.1)
except IndexError:
break
index += 4 + size
packet_parser_helpers.make_graphs()