This function computes the output for the PID compensator/ controller, using the P,I, and D Gains defined in the PIDdef statement for the same channel ch. The integral and differential values are stored within the channel's internal data space and will be automatically used by the PID computation routine. The PIDcompute( ) function uses the lmt (max. limit) term of PIDdef statement to limit the results of its computation. If the absolute value of the computed result is greater then "lmt", then the result will be set equal to "lmt" for +ve number and to "-lmt" for negative value. When this happens, the integral term will not accumulate the current error to prevent an "integrator windup" which is very undesirable for the system.
ch = channel number (1-16)
Err = Closed-loop Error
The controller may obtain feedback from ADC, High Speed Counters, PULSEFREQUENCY or other means. The obtained result is then scaled and subtracted from the desired (set point) value to get "Err ". All computations are performed in 32-bit integers and the function returns a 32-bit integer which can be assigned to any variable. Any scaling for actual output (DAC or PWM) will be computed by the user within the same CusFn and sent to the output.
The set point value is 10000 units, the feedback value is read from ADC channel #2 and then multiplied by 20 to convert (scale) it to the same unit as the parameter to be controlled. PID computation channel #5 (assume somewhere in the program a PIDdef for channel #5 has been executed before) is then used to compute the desired controller output value using the error signal (= set point - feedback value ADC(2) x 20).
The desired output (stored in variable A) is then added to the offset value 8000 and then scaled down by a factor of 100 before being sent out physically via PWM Channel #4.Important: In actual implementation, use a clock pulse such as 0.1s, 0.5s or 1s etc to periodically activate the PIDcompute( ) function so that digital control in discrete-time can be implemented. The PID sampling period depends on the time constant of the system. For very slow response processes such as the cooking temperature of a large body of water, the time constant is very large and even slower than 1.0 seconds clock may be sufficient. Do not use unnecessarily short sampling time because it increases computation time and slows down overall performance of the system.
Basic to TBASIC Reference Manual