I would like my control loop to run at 200Hz and do the following:
- 1) Gather sensor data (i2c sensors and onboard timers connected to encoders)
2) Store the collected data (32 bytes) in a buffer or buffer(s).
3) Compute the appropriate PWM for my two motors.
Additionally, I have a microSD card I would like to use to log the sensor data. I was thinking I would put the sample data in a 1024 or 2048 byte buffer and then when the buffer is full, wake up a thread that initiates a SD card write. Will SD card writes of buffers this size automatically use DMA? Should I use a raw byte array, or does ChibiOS provide a data structure designed for this purpose with some extra functionality? In addition to the sensor data, I might also want to have a log file to store when events are occurring. What sort of locking or resource management do I need to be aware of if I have two files open and they are both only being written to from one thread? The overall data collection rate isn't too high -- about 6400 bytes / second.
I have TIM2 available (TIM1, TIM3, TIM4, TIM5 are tied up with PWM and reading encoders) to use obtain a 5ms period. Should I be using this timer for the 5ms tick or is there a better/different timer? I was thinking that the interrupt handler for TIM2 could wake up a thread which would then initiate the data collection and computation of the PWM commands. The slowest part of this operation is the i2c collection of data (18 bytes @ 100kHz, around 1.4ms excluding bus/CPU overhead, though I can run the I2C clock a bit faster than 100kHz).
Also, I've already got code that configures the I2C sensors how I want them, should I just call this once I get to main but before I start any threads, or is it possible to put this code in the boardInit() function or somewhere else before main()? I'm not too clear exactly when the HAL initialization takes place, I presume my sensor configuration code needs to run after HAL has configured the I2C peripheral.
I know this is a ton of questions, but any thoughts or suggestions would be greatly appreciated.
Thanks,
Luke