SBB Micro
Source code for the self-balancing-bike microcontroller unit (Teensy 4.1-based). 🚀️
|
Functions and definitions for signal logging. More...
Namespaces | |
namespace | LedMode |
Led mode constants. | |
Macros | |
#define | DOLOG 1 |
Enable or disable logging (1/0). | |
#define | LOG_SAMPLING_FAC 1 |
Sampling factor of data logging (expressed in units of SAMPLING_TIME). | |
#define | sd SD.sdfs |
SD fat object (uses sd fat included in SD library). | |
#define | SD_CONFIG SdioConfig(FIFO_SDIO) |
Configuration type of sd card. | |
#define | PREALLOC 1024L*1024L*1024L |
Log file preallocation (in bytes). | |
#define | DATAPACKET_SIZE sizeof(float)*64 |
Size of logged data packet (in bytes). | |
#define | RING_BUF_SIZE DATAPACKET_SIZE*10 |
Size of the ring buffer. | |
#define | SDSECTOR_SIZE 512 |
Size of the SD sector (in bytes). | |
#define | FILENAME_MAXSIZE 32 |
Max length of the name of the log file. | |
#define | FILENAME_FORMAT "log_%04d-%02d-%02d_%02d.%02d.%02d.sbb" |
Log filename format. | |
#define | FILENAME_ENTRIES year(), month(), day(), hour(), minute(), second() |
Entries of the log filename format. | |
Functions | |
void | print_report (int mode) |
Print the report. | |
void | start_logger (void) |
Initialize the logger. | |
void | log_closer (void) |
Close the log file. | |
void | do_logger (void) |
Log signals. | |
Variables | |
boolean | doLog = false |
True when logging. | |
char | filename [FILENAME_MAXSIZE] = { 0 } |
Log filename. | |
float | fill_packet [(DATAPACKET_SIZE - sizeof(ControlClass::controlModel_U) - sizeof(ControlClass::controlModel_Y))/sizeof(float)] |
Fill the data packet. | |
uint64_t | logSampleWritten = 0 |
Number of logged bytes. | |
FsFile | logFile |
Log file object. | |
FsFile | reportFile |
File for report log. | |
RingBuf< FsFile, RING_BUF_SIZE > | log_buf |
Ring buffer object. | |
Functions and definitions for signal logging.
Definition of the logger functions, variables, and macros. This include functions to initialize the logger, log the signals, and close the log when the microcontroller turns off. The signal logging makes use of the built-in SD card using SDIO card configuration for best performances. The logger creates a *.sbb file where signals are saved in binary (as float types, i.e. 4 bytes per value) in group of DATAPACKET_SIZE bytes per sample. This means a maximum number of signal logged of DATAPACKET_SIZE/sizeof(float). The data packet is filled with NaN values untill the defined size is reached.
A ring buffer is used to write to the SD card only when free, in order to have a low writing time. Logging rate is defined by LOG_SAMPLING_FAC, while the maximum log file dimension is PREALLOC (in bytes). During logging the LEDmode is LedMode::LOG, and status LED blinks depending on LOG_LED. Data logging is not active in MTP mode.
#define DATAPACKET_SIZE sizeof(float)*64 |
Size of logged data packet (in bytes).
Note that sizeof(float)=4.
#define DOLOG 1 |
Enable or disable logging (1/0).
#define FILENAME_ENTRIES year(), month(), day(), hour(), minute(), second() |
Entries of the log filename format.
Please refer to FILENAME_FORMAT for the filename format used. \
#define FILENAME_FORMAT "log_%04d-%02d-%02d_%02d.%02d.%02d.sbb" |
Log filename format.
The current name format is log_YYYY-MM-DD_hh.mm.ss.sbb.
#define FILENAME_MAXSIZE 32 |
Max length of the name of the log file.
Standard length of the name should be 27 chars w/ format log_YYYY-MM-DD_hh.mm.ss.sbb.
#define LOG_SAMPLING_FAC 1 |
Sampling factor of data logging (expressed in units of SAMPLING_TIME).
#define PREALLOC 1024L*1024L*1024L |
Log file preallocation (in bytes).
#define RING_BUF_SIZE DATAPACKET_SIZE*10 |
Size of the ring buffer.
Bytes of data packet multiplied by the buffered samples.
#define sd SD.sdfs |
SD fat object (uses sd fat included in SD library).
#define SD_CONFIG SdioConfig(FIFO_SDIO) |
Configuration type of sd card.
#define SDSECTOR_SIZE 512 |
Size of the SD sector (in bytes).
Note that it is a multiple of DATAPACKET_SIZE
void do_logger | ( | void | ) |
Log signals.
Function to log and write the signals to the ring buffer at a sampling rate defined by LOG_SAMPLING_FAC. The ring buffer is written out to the log file every SAMPLING_TIME and when the SD card is free, for best performance in terms of writing time. The function must be called continously at a sampling rate of SAMPLING_TIME.
void log_closer | ( | void | ) |
Close the log file.
Function to flush, truncate, and close the log file. The function is called when turning off the microcntroler, or when the maximum log file dimension is reached or a logging error occurs.
void print_report | ( | int | mode | ) |
Print the report.
Function to print the report to the SD card (if available) at the startup in a log file.
mode | Operation to perform (0=print startup info, 1=print turnoff info) |
void start_logger | ( | void | ) |
Initialize the logger.
Function to initialize the logger. It initializes the SD card and create a log file with name having format log_YYYY-MM-YY_hh.mm.ss.sbb.
boolean doLog = false |
True when logging.
char filename[FILENAME_MAXSIZE] = { 0 } |
Log filename.
float fill_packet[(DATAPACKET_SIZE - sizeof(ControlClass::controlModel_U) - sizeof(ControlClass::controlModel_Y))/sizeof(float)] |
Fill the data packet.
Float array to fill the data packet with NaN for invalid values.
RingBuf<FsFile, RING_BUF_SIZE> log_buf |
Ring buffer object.
Ring buffer object with base class FsFile and size RING_BUF_SIZE.
FsFile logFile |
Log file object.
uint64_t logSampleWritten = 0 |
Number of logged bytes.
The number of the written bytes into the log file, in order to troncate the file correctly at the end.
FsFile reportFile |
File for report log.