A PID control loop involves some contributions from each of the three kinds of control: proportional, integral, and derivative. The amount of contribution that each of the controls makes can be adjusted by changing their proportionality constants, Kp, Ki, and Kd. By making these constants larger or smaller, you can make the contribution of one of the controls more dominant or subtle in the system. One system might need only light integral control, some proportional and strong derivative, while another system might need strong integral and proportional controls, but not much derivative, while still another system might need roughly equal measures of each.
Here are several important things to keep in mind when working with PID control loops:
The next example program will perform this PID control loop. To make comparisons easier, Kp, Ki, and Kd are all set to 10.
Example Program - PidAlgorithm.bs2
Keep in mind that proportional always does some work when there's some error. However, integral and derivative are ready to do extra work to correct the error, integral to correct trends, and derivative to correct abrupt changes.
' PidAlgorithm.bs2 ' Demonstrates how a combination of proportional, integral, and ' derivative control influence error correction in a feedback loop. ' {$STAMP BS2} ' {$PBASIC 2.5} SetPoint CON 0 ' Set point Kp CON 10 ' Proportionality constant Ki CON 10 ' Integral constant Kd CON 10 ' Derivative constant Current CON 0 ' Array index - current error Accumulator CON 1 ' Array index - accumulated error Previous CON 2 ' Array index - previous error Delta CON 3 ' Array index - change in error sensorInput VAR Word ' Sensor input variable error VAR Word(4) ' Four different types of errors p VAR Word ' Proportional term i VAR Word ' Integral term d VAR Word ' Derivative term drive VAR Word ' Output DO DEBUG "Enter sensor input value: " DEBUGIN SDEC sensorInput ' Calculate error. error(Current) = SetPoint - sensorInput ' Calculate proportional term. p = Kp * error(current) ' Calculate integral term. error(Accumulator) = error(Accumulator) + error(Current) i = Ki * error(Accumulator) ' Calculate derivative term. error(Delta) = error(Current) - error(Previous) d = Kd * error(delta) ' Calculate output. drive = p + i + d ' Display values. DEBUG CR, CR, "ERROR", CR, SDEC ? SetPoint, SDEC ? sensorInput, SDEC ? error(Current), CR, "PROPORTIONAL", CR, SDEC ? Kp, SDEC ? error(Current), SDEC ? p, CR, "INTEGRAL", CR, SDEC ? Ki, SDEC ? error(accumulator), SDEC ? i, CR, "DERIVATIVE", CR, SDEC ? Kd, SDEC ? error(Delta), SDEC ? d, CR, "OUTPUT", CR, SDEC ? p, SDEC ? i, SDEC ? d, SDEC ? drive, CR, CR ' Save current error to previous error before next iteration. error(Previous) = error(Current) LOOP
Assume that your sensor inputs will range from -10 to 10. Adjust your constants (MIN and MAX operators.
Kp, Ki, and Kd ) so that the maximum contribution any of the controls can make to the output ranges fr om 650 to 850. To get the integral control to adhere to this requirement, you will also have to use the