Priority and preemption: urgent work first
A preemptive scheduler hands over the CPU the moment a higher-priority task becomes ready. How to decide priorities.
Preemptive scheduling
FreeRTOS by default runs "the highest-priority task that is Ready". Even while a lower task is running, the switch happens immediately when a higher task wakes. Tasks of equal priority take turns every tick (round robin).
Criteria for setting priorities
Give higher priority to shorter and stricter deadlines (the intuition behind rate-monotonic scheduling).
| Task | Period / deadline | Priority |
|---|---|---|
| Motor speed control | 1 ms | High |
| IMU read | 5 ms | Upper middle |
| CAN communication | 10 ms | Middle |
| Display update | 50 ms | Low |
| Log storage | Whenever | Lowest |
High priority goes to what "must not be late", not to what is "important". The display may look important, but nothing breaks if it is 50 ms late.
3D labRun what you have read so far→Relation to interrupts
Interrupts run before any task. Loops at 10 kHz or more, such as current control, run in an interrupt and not in a task; tasks take the slower loops above them.
Starvation
If a high-priority task keeps running without ever going Blocked, the tasks below never run. Every task needs a point where it waits (a delay, a queue wait).