Hardware Reference
In-Depth Information
The Derivative Statement
The D in PID is the derivative, another calculus concept, which is just a snapshot of the slope of an equation. The
slope is calculated as rise over run —the rise comes from the change in the error, or the current error subtracted from
the last error; the run is the change in time. When the rise is divided by the time change, the rate at which the input is
changing is known. Code for the derivative component is
Derror = (Error - lasterror) / timechange ;
Dout = Kd * Derror ;
or
Derror = (Input - lastinput) / timechange ;
Dout = Kd * Derror ;
The derivative aids in the control of overshooting and controls the ringing that can occur from the integral. High
gain values in the derivative can have a tendency to cause an unstable system that will never reach a stable state. The
two versions of code both work, and mostly serve the same function. The code that uses the slope of the input reduces
the derivative kick caused when the setpoint is changed; this is good for systems in which the setpoint changes
regularly. By using the input instead of the calculated error, we get a better calculation on how the system is changing;
the code that is based on the error will have a greater perceived change, and thus a higher slope will be added to the
final output of the PID controller.
Adding It All Up
With the individual parts calculated, the proportion, integral, and the derivative have to be added together to achieve
a usable output. One line of code is used to produce the output:
Output = Pout + Iout + Dout ;
The output might need to be normalized for the input when the output equates to power. Some systems need
the output to be zero when the setpoint is achieved (e.g., ovens) so that no more heat will be added; and for motor
controls, the output might have to go negative to reverse the motor.
Time
PID controllers use the change in time to work out the order that data is entered and relates to when the PID is
calculated and how much time has passed since the last time the program calculated the PID. The individual system's
implementation determines the required time necessary for calculation. Fast systems like radio-controlled aircraft
may require time in milliseconds, ovens or refrigerators may have their time differences calculated in seconds, and
chemical and HVAC systems may require minutes. This is all based on the system's ability to change; just as in physics,
larger objects will move slower to a given force than a smaller ones at the same force.
There are two ways to set up time calculation. The first takes the current time and subtracts that from the last
time and uses the resulting change in the PID calculation. The other waits for a set amount of time to pass before
calculating the next iteration. The code to calculate based on time is as follows and would be in a loop:
// loop
now = millis() ;
timechage = (now - lasttime);
// pid caculations
lasttime = now;
 
Search WWH ::




Custom Search