Support » Jrk G2 Motor Controller User’s Guide » 7. Details »
7.5. PID calculation
When feedback is enabled, the Jrk G2 performs a PID calculation once per PID period to determine its “Duty cycle target” variable. This section explains the details of that calculation and the settings that affect it.
When the system is configured properly, the PID calculation should generally result in the “Scaled feedback” variable (which represents the position or speed of the system’s output) moving towards the “Target” variable (which represents the commanded position or speed). The first step of the PID calculation is to calculate the error by subtracting the target from scaled feedback (error = scaled feedback − target).
If the “Feedback mode” is “Analog voltage” and the “Wraparound” feature in the “Feedback” tab is enabled, the Jrk will add or subtract 4096 from the error if necessary in order to get it into the −2048 to +2048 range. The error can normally be any number between −4095 and 4095.
Then, the three terms that give the PID algorithm its name are calculated:
- The proportional term is proportional to the error. The Jrk calculates this term by multiplying the error by the proportional coefficient.
- The integral term is proportional to the accumulated sum of errors over time. The Jrk calculates this term by multiplying the integral variable (described below) by the integral coefficient.
- The derivative term is proportional to the change in the error relative to the previous PID period. The Jrk calculates this term by first calculating the error minus the error from the previous PID period, and then multiplying by the derivative coefficient.
The Jrk sets the “Duty cycle target” equal to zero minus the sum of these three terms. The subtraction is what causes the Jrk to move the motor in the direction that counteracts the error instead of amplifying it. A duty cycle target of 600 or more corresponds to 100% duty cycle in the forward direction, while a value of −600 corresponds to 100% duty cycle in the reverse direction, and a value of 0 corresponds to 0% duty cycle or off.
PID coefficients
Each of the PID coefficients is specified as an integer value between 0 and 1023 divided by a power of two from 20 (1) to 218 (262,144). The coefficients are 0 by default, and can have values from 0.0000038 to 1023. Any value above 0.0003815 can be approximated within 0.5%. To get the closest approximation to a desired value, type the number into the box after the equal sign in the “PID” tab, and the software will compute the best possible numerator and denominator.
The PID coefficients are normally configured ahead of time using the configuration utility software, but you can also use the Jrk G2’s “Set RAM settings” command to change them (and other settings) on the fly over serial, I²C, or USB (see Section 11).
PID period
The “PID period” setting sets the rate at which the Jrk runs through all of its calculations. Note that a higher PID period will result in a more slowly changing integral and a higher derivative, so that the two corresponding PID coefficients might need to be adjusted whenever the PID period is changed. The Jrk still respects the PID period setting even if feedback is disabled: the Jrk will only calculate its main input and update the motor speed once per PID period.
Integral calculation
Each PID period, the Jrk adds the error divided by the “Integral divider” setting to the “Integral” variable. The Jrk stores the integral internally with higher precision in order to avoid the accumulation of rounding errors due to the division. For example, if the error is consistently 1 and the integral divider is 8, the integral variable will increase by 1 every 8 PID periods.
The absolute value of the integral is not allowed to exceed the “Integral limit” setting, which defaults to 1000 but can be has high as 32,767.
The integral variable initially starts at zero, and it is also reset to zero whenever there are errors stopping the motor, whenever the “Force duty cycle” or “Force duty cycle target” commands are in effect, whenever the “Feedback dead zone” feature is in effect, and whenever feedback is disabled. If the “Reset integral when proportional term exceeds max duty cycle” setting is enabled, the integral is also reset to zero whenever the proportional term goes outside of the −600 to +600 range.
Feedback dead zone
The “Feedback dead zone” option sets the duty cycle target to zero and resets the integral whenever the magnitude of the error is smaller than the feedback dead zone amount. This is useful for preventing the motor from driving when the target is very close to the scaled feedback. The feedback dead zone uses hysteresis to keep the system from simply riding the edge of the dead zone; once in the dead zone, the duty cycle and integral will remain zero until the magnitude of the error exceeds twice this value.