Ground Embedded KO Subscribe

Notes / RTOS

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.

GROUND EMBEDDED · NOTERTOS 08FreeRTOS in practiceBUFFERhead →tail →write · read

Example structure: a balancing robot

Timer ISR1 kHzcontrolpriority 4Queuetelemetrypriority 2UART
Every 1 ms the timer wakes the control task, and the control result goes through a queue to the telemetry task.
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

  1. Enable FreeRTOS (CMSIS-RTOS v2) in CubeMX and change the HAL timebase to a timer other than SysTick (the RTOS uses SysTick).
  2. Blink two LEDs at different periods with two empty tasks to check scheduling
  3. Turn on stack monitoring and the overflow hook
  4. Move features into tasks one at a time, checking the high water mark each time
  5. 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".

FREE PDFGet the 3 free ebooksIf you want to keep reading this topic as one book: BLDC motor control, serial communication and sensors at book length. PDFs are in Korean.Get them free →