ระบบปฏิบัติการเวลาจริง (RTOS)

Scheduling, task priority and the RTOSes used in flight stacks (NuttX/ChibiOS)

Last updated:

Why it matters

A flight controller does many jobs at once: read the IMU, estimate state, compute control, command the motors, receive GNSS, write logs and talk to the ground station. The jobs are not equally urgent. A log entry written a little late goes unnoticed. An attitude calculation a few milliseconds late makes the aircraft wobble or lose control.

A real-time operating system (RTOS) is a small operating system designed so that critical work runs predictably on time. "Real-time" does not mean fastest; it means always meeting deadlines. A system that responds in 1 ms on average but occasionally takes 50 ms is not real-time. One that takes longer but never exceeds 3 ms may be the better choice.

Core concepts

  • Task (or thread). A unit of work with its own stack. Control tasks usually repeat periodically, with a period T and a worst-case execution time C.
  • Deadline. When the task must finish. If a miss means system failure the task is hard real-time; if a miss only degrades quality it is soft real-time. Attitude control is the former; streaming video is the latter.
  • Scheduler. Decides which task gets the CPU. Most RTOSes use fixed-priority preemptive scheduling: the highest-priority ready task runs immediately, interrupting whatever was running. Switching tasks is a context switch.
  • Jitter. Variation in timing, such as a task meant to start every 2.5 ms that sometimes starts 0.3 ms late. Digital controllers are designed for a constant period, so large jitter degrades control.
  • Coordination primitives. A queue passes data between tasks, a semaphore signals that an event occurred, and a mutex stops two tasks using one resource, such as the SPI bus, at the same time.
  • Watchdog. A circuit that resets the system if software hangs and fails to check in on time; the last line of defence in an embedded system.

Assigning priorities

The widely used rule is rate-monotonic scheduling: shorter period, higher priority. Liu and Layland (1973) proved that if total CPU utilisation U = Σ Ci/Ti of n tasks does not exceed n(21/n − 1), every task meets its deadline. The bound is 0.828 for n = 2, 0.780 for n = 3, and approaches about 0.693 for many tasks. It is a sufficient condition: a task set above the bound may still be schedulable and needs a closer check, such as simulating the schedule. A set with U above 1 will certainly miss deadlines.

Example 1: time budget of a 400 Hz control loop

The main loop runs at 400 Hz, a 2,500 µs period. Assumed times inside the loop: IMU read 300 µs, state estimator 900 µs, attitude control 250 µs, motor output 100 µs, totalling 1,550 µs or 62% of the period. That leaves 950 µs per cycle for other work. Logging at 50 Hz takes 1,200 µs per run and GNSS processing at 10 Hz takes 800 µs per run.

  • U = 1,550/2,500 + 1,200/20,000 + 800/100,000 = 0.62 + 0.06 + 0.008 = 0.688
  • The Liu and Layland bound for 3 tasks is 0.780, so the set is guaranteed schedulable under a preemptive scheduler.

Under a cooperative scheduler, however, tasks cannot interrupt one another; each must give up the CPU itself. The 1,200 µs logging run cannot fit in the 950 µs gap and delays the next main-loop cycle. Passing on average utilisation does not mean every cycle is on time. Long jobs must be split into slices, or moved to a lower-priority task that can be pre-empted. All values are assumed, not real PX4 or ArduPilot timings.

Priority inversion

Priority inversion happens when a high-priority task waits for a low-priority task that holds a mutex the high task needs. If a medium-priority task then pre-empts the low one, the high task can wait without bound. The fix offered by many RTOSes is priority inheritance: the mutex holder temporarily takes the priority of the task waiting for it. Good practice is also to hold mutexes as briefly as possible.

Example 2: a high task waits on a low one

A low-priority task holds the SPI bus mutex for 2 ms. The attitude task (high priority, 2.5 ms deadline) needs the same bus. Meanwhile a medium task (say, telemetry message processing) needs 5 ms of CPU (assumed values).

  • Without priority inheritance: the medium task pre-empts the low one, and the high task can wait 2 + 5 = 7 ms, missing its 2.5 ms deadline.
  • With priority inheritance: the low task temporarily runs at high priority, the medium task cannot pre-empt it, and the high task waits at most 2 ms, meeting its deadline.

RTOSes in flight stacks

  • PX4. According to the PX4 architecture documentation, PX4 runs on operating systems that provide a POSIX API, such as Linux, macOS, NuttX and QuRT, with NuttX as the primary RTOS on flight-control boards. A PX4 module runs either as its own task with its own stack and priority, or on a shared work queue, which saves memory but must be cooperative because work items cannot interrupt one another. Modules communicate through a publish-subscribe bus called uORB. NuttX is an Apache Software Foundation project.
  • ArduPilot uses ChibiOS on STM32 boards. Copter's main loop runs at 400 Hz on Pixhawk, and other jobs are registered in a scheduler table with their desired rate and expected run time in microseconds, which lets the scheduler judge whether there is time to run them in the current cycle. ChibiOS/RT is licensed under GPL3 or a commercial licence.
  • FreeRTOS is popular on general-purpose MCUs, including lab boards. Its kernel is MIT-licensed.

Common pitfalls

  • Assigning priorities by feel rather than by period and deadline.
  • Looking only at average CPU load and ignoring the longest job under a cooperative scheduler.
  • Busy-waiting instead of using the RTOS's delay or event wait, which starves lower-priority tasks.
  • Holding a mutex for long, or calling blocking functions while holding one.
  • Undersized task stacks, which corrupt data at random. Measure peak stack use during testing.
  • No watchdog, or letting an unimportant task feed it, so the system never notices the main task has hung.

Continue in the lessons

Key references

  1. Liu, C. L., & Layland, J. W. (1973). Scheduling algorithms for multiprogramming in a hard-real-time environment. Journal of the ACM, 20(1), 46–61. link
  2. PX4 Autopilot. PX4 architectural overview. PX4 user guide (main). link
  3. Apache Software Foundation. Apache NuttX real-time operating system. link
  4. ArduPilot Dev Team. Scheduling code to run intermittently. ArduPilot developer documentation. link
  5. ChibiOS. Licensing (ChibiOS/RT and ChibiOS/HAL). link
  6. FreeRTOS. FreeRTOS kernel (MIT license). link

Used in courses

How to cite this page

Drone Institute, Rangsit University. (2026). ระบบปฏิบัติการเวลาจริง (RTOS). In UAS Technology Knowledge Hub (B.Tech.). https://rsu-uat.pk-research.work/en/kb/new/rtos-basics/