1.c. PID Calculation Overview
The jrk is designed to be part of a control system in which the output (usually a motor position or speed) is constantly adjusted to match a specified target value. To achieve this, it constantly measures the state of the system and responds based on the latest information. The information processing performed by the jrk is outlined in the diagram below:
Diagram of a typical feedback system, showing quantities computed by the jrk.
In this diagram, each arrow represents a specific number measured or computed by the jrk, and the blue boxes represent the internal computations that each occur once per PID period. The PID period can be set in 1 ms increments and is one of about 50 configurable parameters that affect the behavior of the system. For more information about configuring the jrk, see Section 3. The jrk uses the following measurements to determine the output:
- The input is measured as a value from 0 to 4095. In analog voltage input mode, this represents a voltage level of 0 to 5 V. In RC mode, the number is a pulse width in units of 2/3 μs. The input is adjusted according to input scaling parameters to determine the target, also a value from 0 to 4095 (see Section 3.b).
- The feedback is measured as a value from 0 to 4095. In analog voltage feedback mode, this represents a voltage level of 0 to 5 V. In digital frequency mode, it is a representation of the output speed (see Section 3.c.) The jrk uses this value to compute the scaled feedback, which is a representation of the output of the entire control system. A scaled feedback of 0 should represent the minimum position of the system, and 4095 should represent the maximum position.
- The current through the motor is measured as a number from 0 to 255. A calibration value relates this to an actual current in amps.
Every PID cycle, the jrk performs the following computations to determine the behavior of the motor (see Section 3.d for more information):
- The error is computed as the difference of scaled feedback and target (error = scaled feedback − target).
- An implementation of the PID algorithm is applied to the error. PID stands for the three terms that are added together: proportional (proportional to the error), integral (proportional to the accumulated sum of the error over time), and derivative (proportional to the difference of the error relative to the previous PID period.) The three constants of proportionality are the most important parameters determining the behavior of the control system. The result of the PID algorithm is a number from -600 to +600 called the duty cycle target.
- The duty cycle target is reduced according to various configurable limits, including acceleration, current, and maximum duty cycle limits (Section 3.e). The limits are intended to prevent the system from causing damage to itself under most circumstances.
The resulting value becomes the duty cycle of the PWM (pulse width modulation) signal applied to the motor. A value of +600 corresponds to 100% duty cycle in the forward direction, 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.
Various parameters and commands have an effect on the steps described above. For example, feedback may be turned off so that the jrk can become a simple speed controller; in this case the PID calculation is bypassed and the duty cycle target is just equal to the target minus 2048. In this mode, limits applied to the duty cycle continue to provide a useful way of preventing damage to the system. As another example, a command to turn the system off prevents the motors from being driven, but all measurements and calculations continue to occur normally.