Chapter 8: FreeRTOS Multitasking Architecture
8.1 Key Points
- FreeRTOS task creation and core pinning on ESP32-S3
- Queue-based communication between tasks
- Mutex protection for shared data
- Separating sensor, control, and telemetry tasks
8.2 Course Content
A robot program should not place all logic inside a single loop. OSRCORE uses FreeRTOS tasks to separate RC input, sensor reading, control computation, and logging. This improves timing stability and makes the software easier to extend.
8.3 Basic Learning
Task Model
ESP32-S3 has two cores. ESP-IDF allows tasks to be pinned to a specific core:
c
xTaskCreatePinnedToCore(task_func, "task", 4096, NULL, 5, NULL, 0);A typical split is:
| Task | Responsibility |
|---|---|
| RC task | Receive and parse SBUS frames |
| Control task | Encoder feedback, PID calculation, ESC output |
| IMU task | Read and process IMU data |
| Telemetry task | Print logs and debug values |
Queue and Mutex
- A queue transfers data between tasks safely.
- A mutex protects shared state from concurrent access.
8.4 Program Study
Create a queue:
c
typedef struct {
uint16_t ch[16];
bool failsafe;
} rc_frame_t;
QueueHandle_t rc_queue = xQueueCreate(4, sizeof(rc_frame_t));Send from the SBUS task:
c
rc_frame_t frame;
if (sbus_read(&frame)) {
xQueueSend(rc_queue, &frame, 0);
}Receive in the control task:
c
rc_frame_t frame;
if (xQueueReceive(rc_queue, &frame, pdMS_TO_TICKS(20))) {
if (!frame.failsafe) {
// Convert channels to throttle and steering commands.
}
}8.5 Summary
FreeRTOS allows the robot firmware to remain responsive and modular. Queues and mutexes provide safe communication between tasks and avoid timing interference between unrelated work.