cancel
Showing results for 
Search instead for 
Did you mean: 

SW for PID-regulator in C-language?

rolf
Associate II
Posted on January 16, 2004 at 06:13

SW for PID-regulator in C-language?

7 REPLIES 7
rolf
Associate II
Posted on May 17, 2011 at 10:16

We have to do a regulation loop for an universal motor within a ST7. The motor is driven controlled by a PWM signal and for feedback we have a tacho signal.

Does anybody already have developed a PID-regulation SW (or something similar) in C-language which could be shared with us?

Thanks a lot!

Rolf
gaetano
Associate II
Posted on May 17, 2011 at 10:16

Hi Rolf,

I have a draft of PI control routine write in C language but unfortunately the documentation regarding this routine is not yet available.

I think it's better to wait for a full debug soft before to send this one to you.

So at the beginning of next year, it could be possible for me to release this routine.

If, in the meanwhile, I found another routine already tested and functioning I will let you know.

Regards,

Tanio

n_muthukumaran
Associate II
Posted on May 17, 2011 at 10:16

Rolf

i heard u have implement the discrite version PID can send the coding to my email id:it will helpful for me..
n_muthukumaran
Associate II
Posted on May 17, 2011 at 10:16

Rolf

i heard u have implement the discrite version PID can send the coding to my email id:it will helpful for me..
n_muthukumaran
Associate II
Posted on May 17, 2011 at 10:16

Rolf

i heard u have implement the discrite version PID can send the coding to my email id:it will helpful for me..
n_muthukumaran
Associate II
Posted on May 17, 2011 at 10:16

[hi gforte

I have an idea about to implement PI controller closed loop regulation. I need the assembly code to implement.i hope it will use to implement the PID controller please send it to my address

mailto:n_muthukumaran@yahoo.com

bille.stello
Associate II
Posted on May 17, 2011 at 10:16

Hello,

here is a PI(D) code:

s16 PID_Regulator(s16 hReference, s16 hPresentFeedback, PID_Struct_t *PID_Struct)

{

  s32 wError, wProportional_Term,wIntegral_Term, houtput_32;

  s64 dwAux;

#ifdef DIFFERENTIAL_TERM_ENABLED   

  s32 wDifferential_Term;

#endif   

  // error computation

  wError= (s32)(hReference - hPresentFeedback);

 

  // Proportional term computation

  wProportional_Term = PID_Struct->hKp_Gain * wError;

  // Integral term computation

  if (PID_Struct->hKi_Gain == 0)

  {

    PID_Struct->wIntegral = 0;

  }

  else

  {

    wIntegral_Term = PID_Struct->hKi_Gain * wError;

    dwAux = PID_Struct->wIntegral + (s64)(wIntegral_Term);

   

    if (dwAux > PID_Struct->wUpper_Limit_Integral)

    {

      PID_Struct->wIntegral = PID_Struct->wUpper_Limit_Integral;

    }

    else if (dwAux < PID_Struct->wLower_Limit_Integral)

          {

            PID_Struct->wIntegral = PID_Struct->wLower_Limit_Integral;

          }

          else

          {

           PID_Struct->wIntegral = (s32)(dwAux);

          }

  }

  // Differential term computation

#ifdef DIFFERENTIAL_TERM_ENABLED

  {

  s32 wtemp;

 

  wtemp = wError - PID_Struct->wPreviousError;

  wDifferential_Term = PID_Struct->hKd_Gain * wtemp;

  PID_Struct->wPreviousError = wError;    // store value

  }

  houtput_32 = (wProportional_Term/PID_Struct->hKp_Divisor+

                PID_Struct->wIntegral/PID_Struct->hKi_Divisor +

                wDifferential_Term/PID_Struct->hKd_Divisor);

#else 

  houtput_32 = (wProportional_Term/PID_Struct->hKp_Divisor+

                PID_Struct->wIntegral/PID_Struct->hKi_Divisor);

#endif

 

    if (houtput_32 >= PID_Struct->hUpper_Limit_Output)

      {

      return(PID_Struct->hUpper_Limit_Output);         

      }

    else if (houtput_32 < PID_Struct->hLower_Limit_Output)

      {

      return(PID_Struct->hLower_Limit_Output);

      }

    else

      {

        return((s16)(houtput_32));   

      }

}     with

typedef struct

  s16 hKp_Gain;

  u16 hKp_Divisor;

  s16 hKi_Gain;

  u16 hKi_Divisor; 

  s16 hLower_Limit_Output;     //Lower Limit for Output limitation

  s16 hUpper_Limit_Output;     //Lower Limit for Output limitation

  s32 wLower_Limit_Integral;   //Lower Limit for Integral term limitation

  s32 wUpper_Limit_Integral;   //Lower Limit for Integral term limitation

  s32 wIntegral;

  // Actually used only if DIFFERENTIAL_TERM_ENABLED is enabled in

  //stm32f10x_MCconf.h

  s16 hKd_Gain;

  u16 hKd_Divisor;

  s32 wPreviousError;

} PID_Struct_t;

I let you find something smarter to avoid using 64 bit variable 😉

Ciao,

Billino