Author Topic: Issue in PID control formula & assigning output to 16 bit timer PWM  (Read 659 times)

0 Members and 1 Guest are viewing this topic.

Offline Vindhyachal.taknikiTopic starter

  • Frequent Contributor
  • **
  • Posts: 487
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: [Select]
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
 

Offline Andy Watson

  • Super Contributor
  • ***
  • Posts: 2086
Re: Issue in PID control formula & assigning output to 16 bit timer PWM
« Reply #1 on: December 05, 2019, 01:51:21 pm »
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.

 

Offline Tomorokoshi

  • Super Contributor
  • ***
  • Posts: 1212
  • Country: us
Re: Issue in PID control formula & assigning output to 16 bit timer PWM
« Reply #2 on: December 05, 2019, 02:00:13 pm »
The line:
Code: [Select]
TIMER_16_PWM = output_4;  // put into timer 16 bit PWM register
assigns a float to a 16 bit integer. Do range checking before the assignment to check for overflow / underflow.
 

Offline pwlps

  • Frequent Contributor
  • **
  • Posts: 372
  • Country: fr
Re: Issue in PID control formula & assigning output to 16 bit timer PWM
« Reply #3 on: December 06, 2019, 08:57:05 am »
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: [Select]

        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

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

 
« Last Edit: December 06, 2019, 09:21:08 am by pwlps »
 

Offline Yansi

  • Super Contributor
  • ***
  • Posts: 3893
  • Country: 00
  • STM32, STM8, AVR, 8051
Re: Issue in PID control formula & assigning output to 16 bit timer PWM
« Reply #4 on: December 06, 2019, 09:01:48 am »
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
 

Offline Vindhyachal.taknikiTopic starter

  • Frequent Contributor
  • **
  • Posts: 487
Re: Issue in PID control formula & assigning output to 16 bit timer PWM
« Reply #5 on: December 07, 2019, 05:09:38 am »
@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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf