Consider a PID controller that needs to regulate the temperature of a small object. This will be implemented in software in a microcontroller.
A thermistor is closely bonded to a heater (a small 3 watt power resistor driven by an 8-bit PWM through a suitable MOSFET.) The system is able to correctly read the temperature and is able to modulate the heating output of the resistor. All the features work in isolation but now I need to bring it all together using a PID controller.
The ultimate output needs to be an integer in the range 0 - 255 for use with the PWM generator, where 0 is the heater turned off and 255 is the heater at maximum output.
It is not clear to me how to scale the values so they fit into 0 - 255. Is that something that the PID math is able to do if you choose the right P, I and D coefficient values, or should I have the PID system output values such as -1 to +1 and then just scale that linearly so it maps to 0 - 255?
Basically the point where the clever math ends and the tangible hardware begins. How do people usually set this up?
I'm after a "best practice" type of answer, or perhaps a rule-of-thumb.
The ADC that reads the thermistor is of 10-bit resolution, but I'm taking an average of 25 samples to mitigate noise effects (this sampling is interrupt driven and at a fixed frequency). The averaged temperature is updated at about 4.3 Hz. The heater PWM frequency is about 136 Hz.
With the accepted answer below I was able to implement a full PID control system. I'm using a few simple checks to ensure that the integral term doesn't become unmanageable: setting the integral to zero when the error is very small and also an upper limit on its absolute value. I'm simply clamping the final PWM value between 0 and 255.
The preliminary PID constants that produced the image below are P=50, I=3 and D=5. My set point is 60 Celsius and the system seems to stabilise at about 59.4 Celsius. It's still a bit sensitive and there is a lot of latency between the heater and the thermistor, but on the whole I'm pretty happy with the outcome. I'll continue to tweak the variables and see how good it can get.