T04: Embedded software, robotics, feedback, and state estimation
On this page
- A. Prerequisites and observable goals
- B. The central question: can software act correctly at the required time?
- C. Coordinates and a small kinematic model
- D. Feedback compares a target with a measurement
- E. Saturation, faults, and estimation
- F. Worked simulation and its limits
- G. Independent practice
- H. Complete solutions: T04
- I. Retention and readiness
A. Prerequisites and observable goals#
A01 coordinate transforms; A03 noise; A06 ODEs and numerical stability; C09–C10 timing and concurrency.
Analyze a sampled feedback loop, implement a simulated PID controller with saturation, derive a scalar estimation update, and distinguish simulation evidence from real-time or safety validation.
Level: Advanced-entry track. Simulated plants only; no real vehicle, robot, aircraft, or medical-device control.
B. The central question: can software act correctly at the required time?#
Embedded software interacts with limited memory, power, sensors, actuators, and timing constraints. A correct value arriving after its deadline may be useless. A hard real-time requirement treats missing a deadline as an unacceptable system failure under its specification; a soft real-time system tolerates some misses with degraded quality. Neither label follows from using C++ or an operating system marketed for real-time work.
Worst-case execution time, interrupt handling, scheduling, blocking, and communication all affect response bounds. An average loop time or a million successful runs does not prove a deadline is always met. Dynamic allocation, locks, page faults, and unbounded input can complicate timing analysis. Prioritize bounded behavior and an explicit fault response before micro-optimizing arithmetic.
A sensor measurement is not the state itself. It may contain noise, bias, delay, quantization, dropped samples, or an incorrect coordinate frame. Units and timestamps belong in the interface contract. A distance in centimeters interpreted as meters can produce a much larger error than an ordinary numerical integration approximation.
C. Coordinates and a small kinematic model#
A planar rotation by angle maps a body-frame vector into a world-frame vector using
For a robot moving forward at speed with heading and angular speed , a basic kinematic model is
This model omits slip, dynamics, actuator limits, and many physical details. For a differential-drive idealization with wheel linear speeds and track width , and . The units check: speed divided by length gives inverse seconds, consistent with angular rate in radians per second.
A transformation’s direction matters. A matrix mapping body coordinates to world coordinates cannot be used unchanged for the reverse map; for a pure rotation, the inverse is its transpose. State-estimation and control failures often originate in such representation errors rather than in sophisticated algorithms.
D. Feedback compares a target with a measurement#
For target , measured output , and error , a continuous PID form is
Proportional action responds to current error. Integral action accumulates persistent error. Derivative action responds to change and is sensitive to measurement noise. Gains have units determined by the plant and signal definitions; arbitrary copied gains are not universally meaningful.
Our plant is the nondimensional first-order system , with target and actuator range . For constant , its exact solution is , giving an analytic check independent of the controller. The simulator uses forward Euler:
With constant , the deviation from equilibrium is multiplied by , so numerical decay is stable only when , or . Closed-loop dynamics introduce additional conditions; a stable open-loop discretization does not automatically make arbitrary PID gains stable.
At each sample, the reference computes error from the latest measurement, forms a raw control using the previously accumulated integral, clamps the control, conditionally updates the integral, and advances the plant. This ordering is part of its discrete controller definition. Other discretizations can be reasonable but are not identical implementations of one universal formula.
E. Saturation, faults, and estimation#
Integral windup occurs when the integral continues growing while the actuator cannot provide the requested effort. After the error changes, the accumulated term may keep the actuator saturated in an unhelpful direction. The lab uses conditional integration: do not accumulate farther into saturation, but allow accumulation that helps leave it. This is one simple anti-windup strategy, not a universal controller design.
A dropped measurement is simulated by holding the last measurement. That makes the controller act on stale information. The policy is visible and testable but is not necessarily safe for a real plant. A real system needs freshness limits, fault detection, and a defined safe state. Noise is simulated by seeded Gaussian samples; real sensors may have non-Gaussian errors, bias, correlated drift, or adversarial interference.
For a scalar prior estimate with error variance and independent unbiased measurement with variance , consider
The posterior error variance is under the independence assumptions. Differentiate with respect to and set the result to zero:
A noisy measurement gets less weight. A dynamic Kalman filter also predicts state and uncertainty through a model, often adding process noise before measurement update. The lab implements only this scalar measurement update; it is not a complete robotics state estimator.
F. Worked simulation and its limits#
With , , , , and 1000 steps, the reference run ended at time with and . A second run with noise standard deviation , seed , and every seventh measurement dropped ended at . The slightly closer final value in the noisy run does not prove noise improves the controller; a single final sample is not a complete performance metric.
Inspect the whole trajectory, maximum error, settling behavior, saturation duration, and response under multiple fault patterns. Compare time-step refinements and analytic cases. A controller can look smooth while using the wrong units or hiding a delayed instability. None of these simulated tests establishes a real actuator’s safe operating envelope, worst-case timing, sensor calibration, or regulatory qualification.
G. Independent practice#
F foundation C standard course work T transfer S optional stretch L runnable lab or project. Attempt the complete set before opening its solutions.
For ideal differential drive with m/s, m/s, and m, compute forward speed and angular speed. At heading , compute the model’s instantaneous world-coordinate velocity.
For with constant , , and Euler step , compute the first two updates and compare the second with the exact value at .
Explain integral windup for an actuator limited to . Why can clamping the output without changing the integral update leave a problem? Propose a fault response for measurements that remain stale beyond a stated limit, without pretending the same response is safe for every plant.
For prior mean , variance , measurement , and measurement-noise variance , compute the scalar gain, posterior mean, and posterior variance. State the assumptions behind interpreting that variance.
Implement starters/control_lab.py: the sampled PID simulation with saturation, conditional integration, seeded measurement noise, periodic dropped measurements, and scalar estimation update. Run tests, reproduce the default trajectory, and compare a noisy/dropout trajectory. Keep every test simulated.
Optional small hints
For T04.01, rotate the forward velocity into the world frame. For T04.03, distinguish the raw requested control from the saturated applied control. For T04.04, derive from the variance expression rather than guessing a weight.
H. Complete solutions: T04#
Compare the first step where your work differs. A corrected answer is not yet an independent success.
T04.01Show solution
Units and orientation make the kinematics interpretable
Forward speed is m/s. Angular speed is rad/s. At heading , and m/s. These values describe an ideal no-slip kinematic model. They do not include acceleration limits or prove a real robot will follow that trajectory.
Back to T04.01T04.02Show solution
Compare numerical and analytic state
The first update is . The second is . The exact state is , so Euler overestimates by about . The difference is numerical error for the specified plant and constant input; it is not evidence of sensor noise. A smaller step should reduce the error in the stable regime.
Back to T04.02T04.03Show solution
An internal state can grow while output is capped
If positive error persists while raw control exceeds , accumulating its integral keeps increasing the controller’s request even though the actuator cannot respond further. When the plant approaches or overshoots the target, the stored integral may still force excessive positive effort. Clamping only the output does not remove this stored state. Conditional integration or another anti-windup design addresses the internal accumulation. A stale-sensor policy might stop issuing new motion commands and enter a controlled safe mode after a deadline, but the safe action depends on plant dynamics and hazards; abruptly setting all outputs to zero is not universally safe.
Back to T04.03T04.04Show solution
Combine estimates according to uncertainty
. The posterior mean is , and posterior variance is . The formula assumes the prior error and measurement noise are unbiased, independent, and have the stated variances, with a direct scalar observation of the same state. Bias, correlation, or a wrong model can make the reported variance overconfident. A small computed variance is not a substitute for checking those assumptions.
Back to T04.04T04.05Show solution
Complete simulated control laboratory
The code separates measurement, error calculation, raw control, saturation, integral update, and plant update so their order can be traced. The default experiment is deterministic for the stated seed and environment. Tests bound outputs and check selected expected behavior; they do not establish general closed-loop stability or real-time safety.
"""T04.05: simulated PID on y'=-y+u; no real actuator connections."""
import math
import random
def simulate(kp=2.0, ki=1.0, kd=0.0, dt=0.01, steps=1000,
noise=0.0, seed=17, drop_every=0):
if type(steps) is not int or steps < 1 or dt <= 0 or noise < 0:
raise ValueError("invalid simulation settings")
if any(not math.isfinite(x) for x in (kp, ki, kd, dt, noise)):
raise ValueError("finite settings required")
if type(drop_every) is not int or drop_every < 0:
raise ValueError("nonnegative fault interval required")
rng = random.Random(seed)
y = integral = last_measurement = 0.0
previous_error = 1.0
data = []
for k in range(steps):
if not drop_every or (k + 1) % drop_every:
last_measurement = y + rng.gauss(0.0, noise)
error = 1.0 - last_measurement
derivative = (error - previous_error) / dt
raw = kp * error + ki * integral + kd * derivative
u = max(-2.0, min(2.0, raw))
# Conditional integration: do not accumulate farther into saturation.
if abs(raw) <= 2.0 or (raw > 2.0 and error < 0) or (raw < -2.0 and error > 0):
integral += error * dt
y += dt * (-y + u)
if any(not math.isfinite(x) for x in (y, integral, u)):
raise ArithmeticError("nonfinite simulated state")
previous_error = error
data.append(((k + 1) * dt, y, u))
return data
def scalar_update(mean, variance, measurement, noise_variance):
if any(not math.isfinite(x) for x in (mean, variance, measurement, noise_variance)):
raise ValueError("finite measurement and state required")
if variance < 0 or noise_variance <= 0:
raise ValueError("valid positive measurement noise required")
gain = variance / (variance + noise_variance)
return (mean + gain * (measurement - mean), (1 - gain) * variance)
if __name__ == "__main__":
print("final (time, state, control):", simulate()[-1])
print("posterior (mean, variance):", scalar_update(0., 1., 2., 1.))I. Retention and readiness#
Retrieve coordinate transformations, Euler stability, and conditional probability assumptions. Readiness requires identifying units, timing boundaries, saturation, and stale-state behavior in a new control problem. Continue with signals and systems, differential equations, linear control, embedded hardware interfaces, real-time scheduling, and supervised hardware-in-the-loop validation before controlling physical equipment.