J
@robert-hh thanks for your suggestion. I have attempted to improve the minimum sapling frequency, by keeping the file open if the desired sampling freq < 100ms. Unfortunately, if I set the delta time to 50ms (updating the interrupt in via a IOT feed) the time between samples is still excessive at 90ms.
If I comment out the write operation so that the function isnt actually writing to sd, then the min delta T becomes 65ms (still long). I have found however that the bulk of this is due to the time to collect the variables (28 class attributes) and the convert to strings & concatenate.
def log(dTms):
global last_time_ms
global file_open_status
global f
if sd_mounted:
if data_file_version == 0:
setup_log_file()
last_time_ms = chrono3.read_ms()
timenow_ms = chrono3.read_ms()
timenow_s = timenow_ms/1000
ADC_mV_str = ",".join(str(inst.mV_val_WMAfilt) for inst in ADCInput._ADCregistry[1:])
ADC_engval_str = ",".join(str(inst.engineering_val) for inst in ADCInput._ADCregistry[1:])
Output_str = ",".join(str(inst.desired_duty_perc) for inst in output_class._Outregistry[1:])
CAN_str = ",".join(str(inst.engineering_value) for inst in CANSPN._registry[0:])
if dTms >= 100: # non-volatile mode if samp freq <10Hz (closes file each time)
if file_open_status:
f.close # this closes the file as switching from high samp freq to slow/non volatile mode
file_open_status = False
with open('/sd/data_log_%s.csv' % data_file_version,'a') as f:
f.write(str(timenow_ms)+","+str(timenow_s)+","+str(timenow_ms-last_time_ms)+","+ADC_mV_str+","+ADC_engval_str+","+Output_str+","+CAN_str +"\n")
else: # volatile mode which keeps file open between logs in high freq use cases
if not file_open_status:
f = open('/sd/data_log_%s.csv' % data_file_version,'a')
file_open_status = True
f.write(str(timenow_ms)+","+str(timenow_s)+","+str(timenow_ms-last_time_ms)+","+ADC_mV_str+","+ADC_engval_str+","+Output_str+","+CAN_str +"\n")
print(str(timenow_ms-last_time_ms))
last_time_ms = timenow_ms
else:
print("cannot log data, SD card not mounted succesfully on boot!")
Could you advise if my approach of collecting my variables from class instance attributes from other modules is slow practice? I cannot see a more suitable approach since I want to access many variables within multiple modules.
Kind regards