FreeRTOS practice: splitting sensors, communication and control into tasks
An example structure that separates IMU reading, control and UART logging into tasks with FreeRTOS on STM32. The order to follow when you set it up for the first time.
Example structure: a balancing robot
| Task / ISR | Period | Priority | What it does |
|---|---|---|---|
| Timer ISR | 1 kHz | (interrupt) | Notifies the control task |
| control | 1 ms | 4 | Read IMU → filter → PID → PWM |
| comm | On events | 3 | Receive Bluetooth commands, change gains |
| telemetry | 20 ms | 2 | Take angle and output values from a queue and send them |
| ui | 100 ms | 1 | LED status, battery check |
Connections
- Timer ISR → wakes control with
vTaskNotifyGiveFromISR(an exact period) - control → telemetry: samples passed through a queue (dropped when full)
- comm → control: new gains passed through a queue and applied by control at the start of its loop (no race on shared variables)
Order for the first set-up
- Enable FreeRTOS (CMSIS-RTOS v2) in CubeMX and change the HAL timebase to a timer other than SysTick (the RTOS uses SysTick).
- Blink two LEDs at different periods with two empty tasks to check scheduling
- Turn on stack monitoring and the overflow hook
- Move features into tasks one at a time, checking the high water mark each time
- Toggle a GPIO per task and capture them on a logic analyzer to check the real execution timing
This last step is the check that separates "it seems to run" from "it keeps its period".