Issue in PID control formula & assigning output to 16 bit timer PWM - Page 1
Forum section: electronics / beginners

author: Vindhyachal.takniki
author_rank: Frequent Contributor
timestamp: December 05, 2019, 01:11:53 pm
content: 1. Writing PID control for temperature control. Took kp,ki,kd values from Delta Electronics PID which calculates from Autotune.2. Below is code. Is below code ok for PID?Code:float previous_err_4 =0;float integral_4 = 0;float error_4;float dt_4 = 1;float derivative_4;float output_4;float kp=30.6f,ki=354.0f,kd=88;void pid_fxc_4(void){error_4 = Setpoint_Temp - temperature;integral_4 = integral_4 + (error_4 * dt_4);derivative_4 = (error_4 - previous_err_4)/dt_4;output_4 = (kp * error_4) + (ki * integral_4) + (-kd * derivative_4);previous_err_4 = error_4;TIMER_16_PWM = output_4; // put into timer 16 bit PWM register}3. Issue is I am getting very random output_4 values. There is no decrease in output values when temperature comes close to set point.4.Also how to map output values to 16bit pwm values which in turn control the ssr to turn heater on/off

author: Andy Watson
author_rank: Super Contributor
timestamp: December 05, 2019, 01:51:21 pm
content: How have you defined the sampling time - i.e. how often does your code run? The sampling time effectively scales "dt" - which will significantly affect the control loop parameters.

author: Tomorokoshi
author_rank: Super Contributor
timestamp: December 05, 2019, 02:00:13 pm
content: The line:Code:TIMER_16_PWM = output_4; // put into timer 16 bit PWM registerassigns a float to a 16 bit integer. Do range checking before the assignment to check for overflow / underflow.

author: pwlps
author_rank: Frequent Contributor
timestamp: December 06, 2019, 08:57:05 am
content: It is much easier to work with unitless quantities, expressing the error as a fraction. Otherwise it is more difficult to interpret the values of the coefficients Ki,Kd; then also your ouput variable will contain temperature units and as Tomorokoshi pointed out you cannot assign it directly to a pwm.Here is a modified version:Code:error_4 = (Setpoint_Temp - temperature)/temperature; //normalized error (-1 to +1)integral_4 = integral_4 + (error_4 * dt_4*ki); // 1/ki is the integration characteristic timeif (integral_4>1) integral_4=1; //integral windup protectionif (integral_4<-1) integral_4=-1;derivative_4 = - (error_4 - previous_err_4)*kd/dt_4;output_4 = (kp * error_4) + integral_4 + derivative_4;previous_err_4 = error_4;if (output_4>1) output_4=1; //limit the output to 0-1 intervalif (output_4<0) output_4=0;TIMER_16_PWM = 0xFFFF * output_4; // put into timer 16 bit PWM registerHere kp,ki, kd have clear definitions. kp gives the percentage of the output for 100% error, if dt_4 is the loop time then ki is the inverse of the time needed for the integral to reach 1 for a 100% error. I have integrated ki in the integral definition so the integral is now unitless too. Limiting the integral to +/-1 will help at startup (https://en.wikipedia.org/wiki/Integral_windup).Edit: My controllers work in Kelvin, if you use a different unit it will be better to normalize the error in a different way, something likeerror_4 = (Setpoint_Temp - temperature)/max_temperature_error

author: Yansi
author_rank: Super Contributor
timestamp: December 06, 2019, 09:01:48 am
content: Why don't you use the industry standard incremental form of the PSD regulator? (indeed, it is PSD, not PID here).In the incremental form, you can avoid windup easily, and is also much more efficient computationally.https://www.cds.caltech.edu/~murray/courses/cds101/fa02/caltech/astrom-ch6.pdf

author: Vindhyachal.takniki
author_rank: Frequent Contributor
timestamp: December 07, 2019, 05:09:38 am
content: @pwlps ,thanks for ur reply.Can you please tell more about kp,ki & kd values.I have connected Delta Electroncis Autotune PID with heater& itgave P=30.6,I=354 & D=88.In code given by you, what values will put in kp,ki & kd values?After sucessfully running PID code, will move to Autotune also & try that too.