PID Tuning

From RepRap
Revision as of 12:53, 28 January 2014 by DaveX (talk | contribs) (Add a hint about interpreting reprap's PID terms in the commercial time-based parameterization.)
Jump to: navigation, search

PID tuning refers to a proportional-integral-derivative control algorithm used in some repraps for hot ends and heated beds.

PID needs to have a P, I and D value defined to control the nozzle temperature. If the temperature ramps up quickly and slows as it approaches the target temperature, or if it swings by a few degrees either side of the target temperature, then the values are incorrect.

To run PID Autotune in Marlin and other firmares, run the following gcode with the nozzle cold:

 M303 E0 S200 C8

This will heat the first nozzle (E0), and cycle around the target temperature 8 times (C8) at the given temperature (S200) and return values for P I and D. An example from http://www.soliwiki.com/PID_tuning is:

bias: 92 d: 92 min: 196.56 max: 203.75
Ku: 32.59 Tu: 54.92
Clasic PID
Kp: 19.56
Ki: 0.71
Kd: 134.26
PID Autotune finished ! Place the Kp, Ki and Kd constants in the configuration.h

For Marlin, these values indicate the counts of the soft-PWM power control (0 to PID_MAX) for each element of the control equation. The softPWM value regulates the duty cycle of the f=(FCPU/16/64/256/2) control signal for the associated heater. The proportional (P) constant Kp is in counts/C, representing the change in the softPWM output per each degree of error. The integral (I) constant Ki in counts/(C*s) represents the change per each unit of time-integrated error. The derivative (D) constant Kd in counts/(C/s) represents the change in output expected due to the current rate of change of the temperature. In the above example, the autotune routine has determined that to control for a temperature of 200C, the soft PWM should be biased to 92 + 19.56*error + 0.71 * (sum of errors*time) -134.26 * dError/dT. The 'sum of errors*time' value is limited to the range +/-PID_INTEGRAL_DRIVE_MAX as set in Configuration.h. Commercial PID controllers typically use time-based parameters, Ti=Kp/Ki and Td=Kd/Kp, to specify the integral and derivative parameters. In the example above: Ti=19.56/0.71=27.54s, meaning an adjustment to compensate for integrated error over about 28 seconds; Td=134.26/19.56=6.86s, meaning an adjustment to compensate for the projected temperature about 7 seconds in the future.

The Kp, Ki, and Kd values can be entered with:

 M301 P19.56 I0.71 D134.26 

In the case of multiple extruders (E0, E1, E2) these PID values are shared between the extruders, although the extruders may be controlled separately. If the EEPROM is enabled, save with M500. If it is not enabled, save these settings in Configuration.h.

For the bed, use:

 Bed: M303 E-1 S60 C8 

and save bed settings with:

 M304 P1 I2 D3 

For manual adjustments:

  • if it overshoots a lot and oscillates, either the integral gain needs to be increased or all gains should be reduced
  • Too much overshoot? Increase D, decrease P.
  • Response too damped? Increase P.
  • Ramps up quickly to a value below target temperature (0-160 fast) and then slows down as it approaches target (160-170 slow, 170-180 really slow, etc) temperature? Try increasing the I constant.


See also Wikipedia's PID_controller and Zeigler-Nichols tuning method. Marlin autotuning (2014-01-20, https://github.com/ErikZalm/Marlin/blob/Marlin_v1/Marlin/temperature.cpp#L250 ) uses the Ziegler-Nichols 'Classic' method, which first finds a gain which maximizes the oscillations around the setpoint, and uses the amplitude and period of these oscillations to set the proportional, integral, and derivative terms.