Correcting weigh-in-motion results with a small neural network
How I added edge AI to the MCLP weighing core without hardware or field data: a vehicle simulator, a baseline that exposed a flaw in the core's own rule, an int8 model trained for the FPGA, and the speed where the model stops helping.
A weigh-in-motion scale weighs a vehicle while it drives across. The platform is short, so each axle is on it only for a moment. During that moment, the vehicle bounces on its suspension. The faster the vehicle, the shorter the moment and the larger the bounce in comparison.
The MCLP core from part 1 had a simple weigh-in-motion rule. When the weight rose above a threshold, the core waited a set number of samples. Then it averaged until the weight dropped below a second threshold. I wanted to make MCLP an edge AI weighing core. A small neural network in the FPGA would look at the whole transient and correct that average.
Before any model work, I had a problem: no hardware and no field data.
Building data without a platform
I wrote a simulator of one axle that crosses a platform. The platform is 0.75 m long in the direction of travel and 3.0 m wide. It stands on eight load cells, four at the entry edge and four at the exit edge.
The axle carries 2000 to 12000 kg and rides on a quarter-car suspension model. In this model, the body sits on a spring and damper with a resonance between 1.2 and 2.5 Hz, and the wheel sits on the stiffness of the tire. The simulator generates the road before the platform from the ISO 8608 road-roughness spectrum. The platform has its own resonance between 30 and 120 Hz. The models of the load cells and the AD7190 converters go down to the noise of the converter.
The simulated forces go through a numpy model of the whole processing chain of the core: CIC, gain and offset, trim, sum, FIR and the weigh-in-motion block. I checked that model against the reference models of the RTL unit tests. Thus the training data is what the inference engine will actually see.
All of these ranges are assumptions, not measurements. But they show the effect of speed. In the simulation, the dynamic part of the tire force has a median of 0.26% at 2 km/h and 4.04% at 30 km/h. This figure is a standard deviation, as a share of the static load.
Measuring the core first
Before I trained anything, I ran the existing rule of the core on 400 crossings per speed, from 2 to 30 km/h. Here, the error is the difference between the estimate and the static axle load. I report the 95th percentile of its absolute value, which is the error that 95% of crossings stay within.
With its reset settings, the rule had a 95th-percentile error of 17.63% at 2 km/h and 68.57% at 30 km/h. Next, I tuned the settle time and a calibration factor separately for each speed. That brought the error to 4.69% at 2 km/h and 8.82% at 30 km/h.
For comparison, I also computed an ideal plateau. This is the average while the tire is fully on the platform, with the timing taken from the simulation. No hardware rule can know that timing. The ideal plateau reached 0.15% at 2 km/h and 9.41% at 30 km/h.
The gap at low speed came from the rule itself. The rule kept averaging until the weight fell below the exit threshold, so every result included part of the ramp off the platform. The amount of ramp in the result depended on the load, because the exit threshold is a fixed weight. It also depended on the contact length of the tire. No calibration factor can remove an error like that.
Above 10 km/h, the situation changed. Even the ideal plateau had several percent of error, because the axle is on the platform for less than one bounce of the body.
Fixing the rule before adding a model
The ramp error came from the rule, so I fixed the rule first. I compared four rules. Each rule had a single setting and a single calibration factor for all speeds, as a site with mixed traffic would run them. The best rule buffers the whole load. It averages only the weights at or above 93% of the peak of the load. Thus the ramps on and off the platform drop out at any speed and any load.
In the core, wim_estimator now keeps the last 1024 weights of a load in one block RAM and compares each weight with the peak of the load. A new register, WIM_LEVEL, sets the share of the peak. It replaces the old settle time. With no calibration factor at all, the 95th-percentile error became:
| Speed, km/h | 2 | 5 | 10 | 15 | 20 | 30 |
|---|---|---|---|---|---|---|
| Old rule, reset settings | 17.63% | 14.44% | 17.39% | 24.41% | 34.30% | 68.57% |
| New rule, WIM_LEVEL 0.93 | 0.31% | 1.64% | 3.78% | 5.72% | 6.57% | 9.79% |
The new rule tracks the ideal plateau at every speed. It costs one more block RAM and two DSP slices. The remaining error comes from the motion of the vehicle, and the model must correct that error.
Choosing what the model sees
My first input was the channel sum before the FIR filter, 256 samples at 600 Hz. At 2 km/h, that window is 427 ms long. The exit ramp of the tire alone takes about 360 ms, so the window held almost no plateau. The sum also carries the 30 to 120 Hz ringing of the platform. That model had 7.52% error at 2 km/h.
The second try used the filtered weight, scaled to its own peak so that it fits in 8 bits. Each step of the int8 input was then 0.8 to 1.6% of the load. At low speed, the rounding didn't average out. The error was 2.87% at 2 km/h, against 0.31% for the core alone.
The input that worked is every 4th weight up to the end of the load. These 256 values cover 1.7 s. Each value is the deviation from the result of the core, in steps of 1/1024. With this input, the plateau shows up in steps of 0.1%, and the ramps saturate at the bottom of the int8 range. The model only has to learn the correction to the result of the core, not the weight itself.
The model and its training
The network is a small one-dimensional CNN with ReLU between the layers:
- a convolution from 1 to 8 channels, with kernel 8 and stride 4
- a convolution from 8 to 16 channels, with kernel 5 and stride 2
- a convolution from 16 to 16 channels, with kernel 5 and stride 2
- a dense layer from 208 values to 16
- a dense layer to a single output
It needs 43216 multiply-accumulates per inference. It has 5328 weight bytes and 57 biases.
The training set has 40000 simulated crossings at random speeds from 2 to 30 km/h. The target is the correction: the true mass divided by the result of the core, minus 1. Training runs in three passes in PyTorch:
- A float model trains first.
- Each layer gets its activation range from the training data, at the 99.99th percentile.
- A final pass trains with int8 weights and activations in the loop (quantization-aware training). The model learns to work with the rounding that it will see in hardware.
The exported model uses integer arithmetic only. It has int8 weights and activations and int32 biases. Between layers, a requantization of the form (acc × M + 2^(s−1)) >> s scales the result, with a 15-bit M. A numpy implementation of that arithmetic is the reference for the hardware. The RTL must match it bit for bit.
Results
The test set is separate from the training data. It has 400 crossings at each speed, generated with other random seeds. On this set, the figures for the core differ a little from the previous table.
95th-percentile error on the test set, with the integer model computed exactly as the engine computes it.
| Speed, km/h | 2 | 5 | 10 | 15 | 20 | 30 |
|---|---|---|---|---|---|---|
| Core rule alone | 0.31% | 1.96% | 4.36% | 5.78% | 7.35% | 10.12% |
| With the int8 model | 0.27% | 1.06% | 2.75% | 5.12% | 6.72% | 10.16% |
The model lowers the error from 1.96% to 1.06% at 5 km/h and from 4.36% to 2.75% at 10 km/h. It helps a little at 15 and 20 km/h, and it makes no difference at 30 km/h. The float model lands within the same scatter as the int8 model, so quantization costs nothing measurable here.
Where the model stops helping
I tried two changes to improve the higher speeds. Models four times wider and eight times wider gave the same errors at 15 to 30 km/h, within the scatter. The raw 600 Hz sum as a second input didn't help either.
At 30 km/h, the tire is fully on the 0.75 m platform for 66 ms. One bounce of the body takes 400 to 830 ms. So the scale sees the bounce as a constant offset. Nothing in one short window tells which phase of the bounce it caught.
A longer platform, or several platforms in a row on more channels, would sample the bounce at several phases. Multi-sensor weigh-in-motion arrays work this way. This model covers a single platform.
Keeping the weight path legal
Scales for trade fall under legal metrology rules. Software between the sensor and the displayed weight is legally relevant. If that software changes, the instrument must be examined again. There are no published rules yet for AI modules in weighing instruments.
So the inference engine stays outside the weight path. RESULT, the stability flag and the weigh-in-motion result are the same whether the engine is on or off. The output of the model goes into its own registers, NN_CORR and NN_EST. The model doesn't learn on the device. The console reports a CRC-32 of the loaded model, so the model is an identifiable set of parameters. The correction is advisory, and the application decides whether to use it.
Running it in the FPGA
The model runs on mclp_nn, an inference engine inside the core. The engine starts after each weigh-in-motion load. It does one multiply-accumulate per cycle of the 50 MHz processing clock. It finishes the default model in 44439 cycles, 0.89 ms. Software can load a new model over AXI4-Lite while the system runs, with no new bitstream.
The system test drives a load over the simulated platform through the AD7190 models, with the engine on. It checks the input and the result of the engine against the integer reference. It passes at 1, 8 and 64 channels.
In the co-simulation from part 3, the console loads the default model and turns the engine on. Then it sends a load across the platform and reports the first inference: a correction of +2.13%, computed in 889 µs.
Where it stands
The model in MCLP 2.0 is an example, trained on synthetic data. It is for single axles on a 0.75 m platform with eight cells. It ships as three memory files in the coeff folder of the repository, and it is loaded into the engine at power-up. A real installation needs a model trained on data from that installation. Only a real platform can show the gap between synthetic and real data.
Part 6 shows how I built the engine: the integer arithmetic, the layer format, the datapath around one DSP multiply-accumulate and the bit-exact tests against the reference.