Why Multi-tasking in Embedded Systems?
A real robot must simultaneously read sensors, run control loops, update displays, communicate over a network, and respond to operator commands — all within hard or soft real-time deadlines. A simple while(1) super-loop works for toy examples but quickly becomes unmanageable as complexity grows.
Multi-tasking allows the CPU to interleave work across multiple logical tasks, giving the illusion of parallel execution on a single core. Two approaches are common:
- Cooperative multitasking: tasks voluntarily yield control at defined points. Simple but vulnerable to a single task blocking the whole system.
- Preemptive multitasking (RTOS): the scheduler forcibly context-switches tasks based on priority and time slices, providing better timing guarantees.
RTOS Concepts
- 01
Task (thread): an independent unit of execution with its own stack and priority level
- 02
Scheduler: RTOS kernel component that decides which task runs next
- 03
Preemption: higher-priority task immediately takes over the CPU from a lower-priority task
- 04
Semaphore / Mutex: synchronisation primitives to protect shared resources and avoid race conditions
- 05
Message Queue: thread-safe FIFO buffer for passing data between tasks without shared memory
- 06
FreeRTOS: the most widely used open-source RTOS for MCUs — used in Amazon FreeRTOS, ESP-IDF, Arduino FreeRTOS port
- 07
Zephyr RTOS: Linux Foundation project — supports hundreds of boards including nRF52, STM32, and ESP32
PID Control
The Proportional–Integral–Derivative (PID) controller is the workhorse of feedback control. It calculates an output u(t) from the error e(t) = setpoint − measurement:
u(t) = Kp·e(t) + Ki·∫e(t)dt + Kd·de/dt
- Kp (Proportional): output proportional to current error — fast response but steady-state error remains
- Ki (Integral): accumulates past error — eliminates steady-state error but can cause overshoot ("wind-up")
- Kd (Derivative): reacts to rate of change of error — damping; reduces overshoot but amplifies noise
Discrete PID (for digital systems sampled at period T):
u[n] = Kp·e[n] + Ki·T·Σe[k] + Kd·(e[n]−e[n−1])/T
Integral wind-up — the integrator accumulating a large value when the actuator is saturated — is mitigated by clamping the integral term or using back-calculation anti-windup.
Cascade PID: an outer position loop feeds a setpoint into an inner velocity loop, which feeds into an inner current/torque loop. This is the standard architecture for high-performance servo drives.
Effect of PID Gains
| Increase | Rise Time | Overshoot | Settling Time | Steady-State Error | Stability |
|---|---|---|---|---|---|
| Kp ↑ | Decrease | Increase | Small change | Decrease | Degrade |
| Ki ↑ | Decrease | Increase | Increase | Eliminate | Degrade |
| Kd ↑ | Minor decrease | Decrease | Decrease | No effect | Improve (to a point) |
Finite State Machines in Robot Control
A Finite State Machine (FSM) is a behavioural model consisting of:
- A finite set of states (e.g., IDLE, MOVING, CHARGING, FAULT)
- Transitions triggered by events or conditions
- Actions performed on entry, exit, or during a state
FSMs are excellent for high-level robot behaviour management because they make the control flow explicit and auditable. A delivery robot might have states: WAITING → PICKING_UP → NAVIGATING → DELIVERING → RETURNING → CHARGING.
Hierarchical State Machines (HSM) add nesting — a NAVIGATING superstate can contain sub-states (AVOIDING_OBSTACLE, FOLLOWING_PATH, RECOVERING) — dramatically reducing transition complexity.
In ROS 2, the SMACH library and BehaviorTree.CPP (Behaviour Trees) are popular alternatives to FSMs for complex mission management, providing more flexibility and easier composition of behaviours.