How do you test engine software without an engine?
An interactive look at software-in-the-loop testing: a small engine model, an idle-speed controller, faults you can inject, and requirements you can run in your browser.
An engine control unit runs software that decides, many times a second, how much air and fuel the engine gets. That software changes constantly, and almost none of those changes can wait for an engine to be free on a test bed. Most of the testing has to happen without one.
The usual answer is to give the software a pretend engine: a model that reacts the way the real one would. The software reads the model's sensors, the model responds to the software's commands, and the two run in a loop. That is software-in-the-loop testing. Testing powertrain software this way, with virtual ECUs and automated tests, is a large part of my day job.
The demos on this page are a toy version you can play with. The engine is a few lines of textbook physics, and the controller is far simpler than anything in production. It is not any manufacturer's software. The loop, though, is the real idea.
The model
Start with the thing being controlled. Control engineers call it the plant. At idle, engine speed comes down to a balance of torques: what combustion produces, minus friction, minus whatever the engine is driving. Here that is an air-conditioning compressor, a common load at idle.
The model is one equation. Net torque divided by the inertia of the crankshaft and flywheel gives the change in speed. The throttle sets how much torque combustion produces, with a short lag because air takes time to fill the intake.
There is no controller yet. You are the controller. Hold 800 rpm, then switch the compressor on.
- Engine speed
- 800 rpm
- Throttle
- 27 %
- Target
- 800 rpm
With the throttle fixed, the compressor's extra 12 Nm is more than the engine has to spare, and it stalls in well under a second. You can save it by opening the throttle quickly, but you have to be watching, and you have to be fast.
Closing the loop
The idle-speed controller does the watching. Every 10 ms it reads engine speed, compares it with the 800 rpm target, and adjusts the throttle. This one is a PI controller with three parts:
- A feedforward term: the throttle that roughly holds idle with no load. It does most of the work before any error appears.
- A proportional term, Kp: react in proportion to how far off the speed is right now.
- An integral term, Ki: accumulate the error over time and keep pushing until it is gone.
It also smooths the speed signal over roughly one crank revolution, and it stops the integral from growing while the throttle is already at a limit.
- Engine speed
- 800 rpm
- Throttle
- 27 %
- Target
- 800 rpm
Switch the compressor on and off with the default gains. Then try the extremes. With Ki at zero, the engine survives the load but settles well below target, because a proportional term alone needs a standing error to hold the throttle open. Push Ki up and the speed starts to swing. Push Kp up and the response gets sharper while the throttle gets twitchy.
None of that is visible from reading the code. The code that produces a stable idle and the code that produces an oscillating one differ by a single number.
Turning behaviour into tests
"It looks fine on the chart" is not a test. A test needs a requirement precise enough to fail:
- After the compressor engages, speed must not dip below 650 rpm, and it must be back within 25 rpm of target within two seconds.
- When the compressor disengages, speed must never flare above 950 rpm.
- With ±25 rpm of noise on the speed sensor, the throttle command must stay steady.
Each one becomes a script: start the model, switch the load at a known time, record everything, check the numbers. Because the engine is software, a scenario that would take minutes to set up on a test bed runs in milliseconds, exactly the same way every time, on every change.
Breaking it on purpose
Requirements for normal operation are the easy part. The harder question is what the software does when something else fails.
The bench below adds three faults you can inject: a noisy speed sensor, a speed sensor that drops out and reads zero, and a throttle that sticks. It also has a diagnostics switch, off by default.
- Engine speed
- 800 rpm
- Throttle
- 27 %
- Target
- 800 rpm
Runs seven scripted scenarios against the model with the current gains and diagnostics, in about the time it takes to click.
Start with the defaults and press Run test suite. The controller meets its control requirements and still fails two tests.
Now inject the sensor dropout while the engine is running. The controller believes the engine has stopped, opens the throttle fully, and the engine races to the rev limiter. Nothing in the control logic is wrong. It does exactly what it was designed to do with the information it has. The information is just false.
That is what diagnostics are for. With them on, the software checks its inputs before trusting them:
- Plausibility. A crankshaft cannot change speed by 300 rpm in 10 ms. A reading that does is treated as a sensor fault, and the controller falls back to a fixed limp-home throttle instead of chasing a number it cannot trust.
- Expected against actual. The throttle reports its own position. The software compares that with where a model of a healthy actuator says it should be. A throttle that stays put when it should be moving is reported, and the compressor is switched off so the engine is not asked for torque it cannot produce.
Turn diagnostics on and run the suite again.
The seventh test matters as much as the two that now pass. An earlier version of this demo compared the throttle position with the raw command instead. A healthy actuator always lags its command a little, so during an ordinary load change that check fired, switched the compressor off, and reported a fault that did not exist. A diagnostic that raises false alarms is its own kind of failure, so one requirement is silence when nothing is wrong.
From laptop to engine
A model this simple can only catch certain mistakes. In practice the same software climbs a ladder of increasingly real environments:
- Model-in-the-loop. The control design runs as a model, before production code exists.
- Software-in-the-loop. The production code is compiled for an ordinary computer and run against a plant model. The demos above sit here.
- Virtual ECU. The application software runs together with a simulated version of the control unit's platform software, so communication, scheduling and diagnostics behave much closer to the real unit, still without hardware.
- Hardware-in-the-loop. The real control unit is wired to a real-time simulator that produces electrical sensor signals and reads the actuator outputs.
- Test cell and vehicle. A real engine.
Each step costs more per test and catches problems the previous one cannot. The early steps can run on every change, automatically, in a CI pipeline. By the time the software reaches an engine, the question should not be whether it survives a dead sensor. That should already have been answered thousands of times.
What the toy leaves out
Real plant models are calibrated against measurements and cover far more than one equation: combustion, temperatures, gas flow, many operating points. Real idle control uses more than the throttle, adapts as the engine ages, and often receives a request before the compressor engages so it can prepare. Real diagnostics follow strict rules for how a fault is confirmed, stored and reported.
The structure is the same: a model that behaves like the world, software that only sees the world through its sensors, requirements precise enough to fail, and faults injected on purpose instead of discovered later. If one thing from this page sticks, make it the dropout. Software that is right about everything except whether its inputs are true can still send an engine to the rev limiter.