| Electronics > Beginners |
| Issue in PID control formula & assigning output to 16 bit timer PWM |
| (1/2) > >> |
| Vindhyachal.takniki:
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 } --- End code --- 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 |
| Andy Watson:
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. |
| Tomorokoshi:
The line: --- Code: ---TIMER_16_PWM = output_4; // put into timer 16 bit PWM register --- End code --- assigns a float to a 16 bit integer. Do range checking before the assignment to check for overflow / underflow. |
| pwlps:
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 time if (integral_4>1) integral_4=1; //integral windup protection if (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 interval if (output_4<0) output_4=0; TIMER_16_PWM = 0xFFFF * output_4; // put into timer 16 bit PWM register --- End code --- Here 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 like error_4 = (Setpoint_Temp - temperature)/max_temperature_error |
| Yansi:
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 |
| Navigation |
| Message Index |
| Next page |