Tim Wescott
Well-Known Member
- Joined
- Jun 3, 2018
- Messages
- 328
- Reaction score
- 99
... eg feed them into a ring buffer and take a weighted average, but that should be after trying to reduce noise electronically....
Aaaagh! My eyes, my eyes, I'll never un-see that.
Actually, it's a pretty common beginners mistake. There are some very good, very narrowly-focused reasons to put a moving-average (or other FIR) filter in a control loop, but this isn't one of them.
You should be able to make the temperature readings fairly quiet. If so, and if the pump control is on-off, just use hysteresis to prevent chatter (i.e., if the pump is on then turn off at 150 degrees, if it's off then turn on at 160 degrees).
If you're continually changing the speed of the pump then you're probably using a PI controller, in which case you may not need filtering at all.
If you do need low-pass filtering, use a first-order IIR:
temperature_state = temperature_state + some_gain * (temperature_measurement - temperature_state);
Just put the above line in your code after you read the temperature into temperature_measurement; it'll filter nicely, without using much resources, and the filter will be minimum phase, and thus won't contribute to loop instability. The number "some_gain" will need to be tuned, but it roughly has the same effect as the reciprocal of the length of your moving-average filter.