Section 10.36

Hardware-in-the-loop testing

Definition

Hardware-in-the-loop (HIL) testing is testing in which the real controller hardware, running its real firmware, is connected to a test rig that simulates or substitutes the rest of the world. The rig produces the electrical signals the controller would receive from its sensors and communication partners, and measures the signals the controller sends to its actuators, the motors, valves or heaters it drives. In closed-loop HIL, a simulation of the controlled physical process turns those outputs into the next inputs. A test program runs scenarios and judges the results.

The idea

Take the controller of a motor that moves a carriage along a rail and must stop it before the end. Tests on a desktop computer alone miss the real processor, the machine code the compiler actually produced, the interrupts §6.11 and the electrical interfaces. Tests in the finished machine alone are slow, can damage the machine, and cannot produce a rare fault, such as a broken end-stop sensor at full speed, on demand. HIL testing keeps real the parts whose behavior is in question, here the board and its firmware. It replaces the parts that are dangerous, slow or hard to control, here the motor, the carriage and the sensors, with a simulation that can be driven into any situation.

A flight simulator is a fair picture. The pilot is real and is the one being tested; the aircraft, the weather and the failures are simulated and can be chosen at will. The picture also shows the limit. A pilot who has flown only the simulator has been tested only against the aircraft and the weather the simulator models. A HIL result is evidence about the real parts in the simulated world, and it reaches no further than the simulation is faithful.

Control-system development often makes more parts real step by step. In model-in-the-loop (MIL) testing, a model of the controller runs against a model of the process, both in simulation. In software-in-the-loop (SIL) testing, the controller’s actual code, compiled for a desktop computer, runs against the simulated process. In processor-in-the-loop (PIL) testing, the code runs on the target processor but exchanges values with the simulation over a debug or serial link instead of its real inputs and outputs. In HIL testing, the complete controller connects through its real electrical interfaces. In embedded-software practice, HIL also names any automated test of a real board driven by test equipment, even without a simulated process.

How it works

A HIL rig has these parts.

In closed-loop HIL, one cycle runs once per simulation step:

  1. The plant simulator computes sensor values from the current state of the simulated process.

  2. The interface hardware drives those values onto the controller’s input pins.

  3. The firmware reads its inputs, computes and sets its outputs, on its own schedule.

  4. The interface hardware measures the outputs.

  5. The simulator uses the measured outputs to advance the simulated process by one step.

The simulator must finish every cycle within the simulation step. A cycle that overruns delivers stale sensor values, and the test then shows a world in which the physics paused.

Open-loop HIL leaves out step 5. The rig plays a scripted or recorded sequence of inputs and records the outputs, and the outputs never change later inputs. That is enough for testing a parser or a command interface. It cannot test a control loop, whose behavior depends on the effects of its own earlier outputs.

The essential document of a HIL setup is its inventory. For every part of the product it says whether the rig uses the real part, a substitute or nothing at all. For every substitute it names the model it implements and its time step. It also states how the real and substituted parts are coupled: signal levels, timing and clocks.

Worked example

An inventory. A board’s UART §6.23 (universal asynchronous receiver-transmitter, a simple serial port) receives scripted messages from the test controller. The board answers each valid command by setting a GPIO §6.22 (general-purpose input/output) pin, which the rig measures. Real: the processor, the firmware image, the UART hardware, its interrupt handler §6.12, the message parser and the pin driver. Substituted: the sender of the messages, which is a script, and the power supply, which is a stable laboratory supply. Absent: the radio. If the product also accepts commands over BLE §7.1 (Bluetooth Low Energy), this rig does not test that path. Because the supply never fails, the rig says nothing about behavior after a power loss.

What the simulation step does to a measurement. A carriage moves at 500 mm/s toward an end stop. The requirement: once the carriage reaches the stop, the end-stop switch closes, the controller turns the motor off, and the carriage travels at most 0.3 mm further. The rig simulates the carriage with a simulation step of 1 ms and, following the cycle above, sets the switch signal and reads the motor command once per step.

The rig fails a controller that meets the requirement, because the simulation step, not the controller, dominates the result. A rig can also pass a faulty controller: a simulated motor that stops in the very step in which the rig reads the motor-off command leaves out the coasting of a real carriage after its motor is switched off. The board is real in every case; the plant model decides what the number means.

Stated precisely

A HIL test supports a statement of this form: the real parts, connected to substitutes that behave as their models, meet a stated property in the scenarios that were run. The claim is complete only with its three lists: the real parts; the substitutes, each with the model it implements; and the scenarios, including the faults inserted. A claim about any part that was not real needs evidence from elsewhere.

For timing, let TsT_s be the simulation step and LL the latency of the interface hardware, the time from a value change in the simulator to the matching signal change at the controller’s pin. An event in the simulated process then reaches the pin between LL and Ts+LT_s + L after the event occurs in the model. Let tmt_m be the time the rig measures from the simulated event to the controller’s response, observed directly at the controller’s output pin, and tct_c the controller’s own response time §9.32 from pin to pin. Then tm−Ts−L≤tc≤tm−L.t_m - T_s - L \le t_c \le t_m - L. A requirement tc≤tmaxt_c \le t_{\max} is therefore shown to hold when tm−L≤tmaxt_m - L \le t_{\max}, shown to fail when tm−Ts−L>tmaxt_m - T_s - L > t_{\max}, and left undecided in between. A measurement taken from pin to pin, as in the example, excludes TsT_s and LL altogether. Effects that the plant model computes from the controller’s outputs are delayed again, by up to one more step, because the simulator reads the outputs once per step; a closed-loop result about the physical process therefore carries an error of up to 2Ts2T_s plus the interface latencies.

Limits and common mistakes

One board is one sample, not the product population. Manufacturing differences, temperature and aging change clock frequencies, voltage thresholds and timing, and one board at room temperature shows none of that spread.

Observation disturbs the device. A debug probe that halts the processor, a logging port the firmware must feed, or extra test code built into the image changes timing; this is instrumentation perturbation §18.1.7. The image tested should be the image shipped, or the difference should be stated.

The plant model sets the ceiling of fidelity. Radio interference, electrical noise, mechanical effects and faults that the simulator does not produce are not tested.

The rig itself can be wrong. A miswired channel, a wrong scaling factor in a converter, or a mismatch between the rig’s clock and the simulation’s time produces failures and passes that belong to the rig. Calibrate the rig, and run a positive control §10.30, a scenario with a known correct outcome, before trusting it.

Electrical tests and functional tests support one combined claim only if they used the same board, firmware image and rig. A timing measurement on one firmware version and a functional pass on another say nothing about either version as a whole.

“Tested on hardware” is not a certificate of realism. It answers none of the inventory’s questions by itself.

Questions answered

Which part is real and which part is simulated or substituted?

The answer depends on the rig and must be written down part by part, because a hardware-in-the-loop (HIL) result means something only together with that list. In a typical HIL rig the real parts are the controller board with its processor, memory and peripheral circuits, the firmware image built for the product, and the electrical interfaces at the pins the rig connects to. The simulated or substituted parts are usually the physical process the controller acts on; the sensors and actuators, replaced by signals from the rig’s converters; the communication peers, replaced by scripts or test equipment; the power supply, replaced by a laboratory supply; and the environment, such as temperature and radio conditions.

For each part, record whether it is real, substituted or absent; for each substitute, the model it implements and its simulation step; and how the parts are coupled in signal levels, timing and clocks. The test then supports claims about the real parts only, and only in the scenarios and faults that were actually run.

Related entries: System test (§10.3), Mock versus stub versus fake (§10.35), Experimental unit (§11.3), GPIO (§6.22), UART (§6.23), Positive control (§10.30), Instrumentation perturbation and observer effects (§18.1.7).