msp430单片机中增量式pid算法的C实现方法

[复制链接]
744|1
 楼主| dirtwillfly 发表于 2016-12-25 21:54 | 显示全部楼层 |阅读模式
  1.  #include <stdio.h>
  2.   #include<math.h>
  3.   
  4.   struct _pid {
  5.    int pv; /*integer that contains the process value*/
  6.    int sp; /*integer that contains the set point*/
  7.    float integral;
  8.    float pgain;
  9.    float igain;
  10.    float dgain;
  11.    int deadband;
  12.    int last_error;
  13.   };
  14.   
  15.   struct _pid warm,*pid;
  16.   int process_point, set_point,dead_band;
  17.   float p_gain, i_gain, d_gain, integral_val,new_integ;;
  18.   
  19.   
  20.   
  21.   /*------------------------------------------------------------------------
  22.   pid_init
  23.   
  24.   DESCRIPTION This function initializes the pointers in the _pid structure
  25.   to the process variable and the setpoint. *pv and *sp are
  26.   integer pointers.
  27.   ------------------------------------------------------------------------*/
  28.   void pid_init(struct _pid *warm, int process_point, int set_point)
  29.   {
  30.    struct _pid *pid;
  31.   
  32.    pid = warm;
  33.    pid->pv = process_point;
  34.    pid->sp = set_point;
  35.   }
  36.   
  37.   
  38.   /*------------------------------------------------------------------------
  39.   pid_tune
  40.   
  41.   DESCRIPTION Sets the proportional gain (p_gain), integral gain (i_gain),
  42.   derivitive gain (d_gain), and the dead band (dead_band) of
  43.   a pid control structure _pid.
  44.   ------------------------------------------------------------------------*/
  45.   
  46.   void pid_tune(struct _pid *pid, float p_gain, float i_gain, float d_gain, int dead_band)
  47.   {
  48.    pid->pgain = p_gain;
  49.    pid->igain = i_gain;
  50.    pid->dgain = d_gain;
  51.    pid->deadband = dead_band;
  52.    pid->integral= integral_val;
  53.    pid->last_error=0;
  54.   }
  55.   
  56.   /*------------------------------------------------------------------------
  57.   pid_setinteg
  58.   
  59.   DESCRIPTION Set a new value for the integral term of the pid equation.
  60.   This is useful for setting the initial output of the
  61.   pid controller at start up.
  62.   ------------------------------------------------------------------------*/
  63.   void pid_setinteg(struct _pid *pid,float new_integ)
  64.   {
  65.    pid->integral = new_integ;
  66.    pid->last_error = 0;
  67.   }
  68.   
  69.   /*------------------------------------------------------------------------
  70.   pid_bumpless
  71.   
  72.   DESCRIPTION Bumpless transfer algorithim. When suddenly changing
  73.   setpoints, or when restarting the PID equation after an
  74.   extended pause, the derivative of the equation can cause
  75.   a bump in the controller output. This function will help
  76.   smooth out that bump. The process value in *pv should
  77.   be the updated just before this function is used.
  78.   ------------------------------------------------------------------------*/
  79.   void pid_bumpless(struct _pid *pid)
  80.   {
  81.   
  82.    pid->last_error = (pid->sp)-(pid->pv);
  83.   
  84.   }
  85.   
  86.   /*------------------------------------------------------------------------
  87.   pid_calc
  88.   
  89.   DESCRIPTION Performs PID calculations for the _pid structure *a. This function uses the positional form of the pid equation, and incorporates an integral windup prevention algorithim. Rectangular integration is used, so this function must be repeated on a consistent time basis for accurate control.
  90.   
  91.   RETURN VALUE The new output value for the pid loop.
  92.   
  93.   USAGE #include "control.h"*/
  94.   
  95.   
  96.   float pid_calc(struct _pid *pid)
  97.   {
  98.    int err;
  99.    float pterm, dterm, result, ferror;
  100.   
  101.    err = (pid->sp) - (pid->pv);
  102.    if (abs(err) > pid->deadband)
  103.    {
  104.    ferror = (float) err; /*do integer to float conversion only once*/
  105.    pterm = pid->pgain * ferror;
  106.    if (pterm > 100 || pterm < -100)
  107.    {
  108.    pid->integral = 0.0;
  109.    }
  110.    else
  111.    {
  112.    pid->integral += pid->igain * ferror;
  113.    if (pid->integral > 100.0)
  114.    {
  115.    pid->integral = 100.0;
  116.    }
  117.    else if (pid->integral < 0.0) pid->integral = 0.0;
  118.    }
  119.    dterm = ((float)(err - pid->last_error)) * pid->dgain;
  120.    result = pterm + pid->integral + dterm;
  121.    }
  122.    else result = pid->integral;
  123.    pid->last_error = err;
  124.    return (result);
  125.   }
  126.   
  127.   
  128.   void main(void)
  129.   {
  130.    float display_value;
  131.    int count=0;
  132.   
  133.    pid = &warm;
  134.   
  135.   // printf("Enter the values of Process point, Set point, P gain, I gain, D gain \n");
  136.   // scanf("%d%d%f%f%f", &process_point, &set_point, &p_gain, &i_gain, &d_gain);
  137.   
  138.   
  139.   
  140.    process_point = 30;
  141.    set_point = 40;
  142.    p_gain = (float)(5.2);
  143.    i_gain = (float)(0.77);
  144.    d_gain = (float)(0.18);
  145.   
  146.   
  147.   
  148.    dead_band = 2;
  149.    integral_val =(float)(0.01);
  150.   
  151.   
  152.    printf("The values of Process point, Set point, P gain, I gain, D gain \n");
  153.    printf(" %6d %6d %4f %4f %4f\n", process_point, set_point, p_gain, i_gain, d_gain);
  154.   
  155.    printf("Enter the values of Process point\n");
  156.   
  157.    while(count<=20)
  158.    {
  159.   
  160.   
  161.   
  162.    scanf("%d",&process_point);
  163.   
  164.    pid_init(&warm, process_point, set_point);
  165.    pid_tune(&warm, p_gain,i_gain,d_gain,dead_band);
  166.    pid_setinteg(&warm,0.0); //pid_setinteg(&warm,30.0);
  167.   
  168.    //Get input value for process point
  169.    pid_bumpless(&warm);
  170.   
  171.    // how to display output
  172.    display_value = pid_calc(&warm);
  173.    printf("%f\n", display_value);
  174.    //printf("\n%f%f%f%f",warm.pv,warm.sp,warm.igain,warm.dgain);
  175.    count++;
  176.   
  177.    }
  178.   
  179.   }


 楼主| dirtwillfly 发表于 2016-12-25 21:55 | 显示全部楼层

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:欢迎进入TI MCU论坛      21ic TI技术交流1群:61549143(已满),  21ic TI技术交流2群:311421422 我的博客:http://blog.timcu.com/

1199

主题

35121

帖子

1122

粉丝
快速回复 在线客服 返回列表 返回顶部