新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > PID算法調節(jié)C51程序(3)

        PID算法調節(jié)C51程序(3)

        作者: 時間:2016-11-23 來源:網絡 收藏
        #include
          #include
          
          struct _pid {
           int pv;
           int sp;
           float integral;
           float pgain;
           float igain;
           float dgain;
           int deadband;
           int last_error;
          };
          
          struct _pid warm,*pid;
          int process_point, set_point,dead_band;
          float p_gain, i_gain, d_gain, integral_val,new_integ;;
          
          
          
          
          void pid_init(struct _pid *warm, int process_point, int set_point)
          {
           struct _pid *pid;
          
           pid = warm;
           pid->pv = process_point;
           pid->sp = set_point;
          }
          
          
          
          
          void pid_tune(struct _pid *pid, float p_gain, float i_gain, float d_gain, int dead_band)
          {
           pid->pgain = p_gain;
           pid->igain = i_gain;
           pid->dgain = d_gain;
           pid->deadband = dead_band;
           pid->integral= integral_val;
           pid->last_error=0;
          }
          
          
          void pid_setinteg(struct _pid *pid,float new_integ)
          {
           pid->integral = new_integ;
           pid->last_error = 0;
          }
          
          
          void pid_bumpless(struct _pid *pid)
          {
          
           pid->last_error = (pid->sp)-(pid->pv);
          
          }
          
          
          
          
          float pid_calc(struct _pid *pid)
          {
           int err;
           float pterm, dterm, result, ferror;
          
           err = (pid->sp) - (pid->pv);
           if (abs(err) > pid->deadband)
           {
           ferror = (float) err;
           pterm = pid->pgain * ferror;
           if (pterm > 100 || pterm < -100)
           {
           pid->integral = 0.0;
           }
           else
           {
           pid->integral += pid->igain * ferror;
           if (pid->integral > 100.0)
           {
           pid->integral = 100.0;
           }
           else if (pid->integral < 0.0) pid->integral = 0.0;
           }
           dterm = ((float)(err - pid->last_error)) * pid->dgain;
           result = pterm + pid->integral + dterm;
           }
           else result = pid->integral;
           pid->last_error = err;
           return (result);
          }
          
          
          void main(void)
          {
           float display_value;
           int count=0;
          
           pid = &warm;
          
          // printf("Enter the values of Process point, Set point, P gain, I gain, D gain ");
          // scanf("%d%d%f%f%f", &process_point, &set_point, &p_gain, &i_gain, &d_gain);
          
          
          
           process_point = 30;
           set_point = 40;
           p_gain = (float)(5.2);
           i_gain = (float)(0.77);
           d_gain = (float)(0.18);
          
          
          
           dead_band = 2;
           integral_val =(float)(0.01);
          
          
           printf("The values of Process point, Set point, P gain, I gain, D gain ");
           printf(" %6d %6d %4f %4f %4f", process_point, set_point, p_gain, i_gain, d_gain);
          
           printf("Enter the values of Process point");
          
           while(count<=20)
           {
          
          
          
           scanf("%d",&process_point);
          
           pid_init(&warm, process_point, set_point);
           pid_tune(&warm, p_gain,i_gain,d_gain,dead_band);
           pid_setinteg(&warm,0.0); //pid_setinteg(&warm,30.0);
          
           //Get input value for process point
           pid_bumpless(&warm);
          
           // how to display output
           display_value = pid_calc(&warm);
           printf("%f", display_value);
           //printf("%f%f%f%f",warm.pv,warm.sp,warm.igain,warm.dgain);
           count++;
          
           }
          
          }


        評論


        技術專區(qū)

        關閉
        主站蜘蛛池模板: 赞皇县| 福贡县| 五家渠市| 增城市| 湘乡市| 大名县| 巧家县| 文成县| 久治县| 成都市| 海晏县| 敦化市| 吉安市| 元谋县| 哈巴河县| 溧水县| 元江| 屯门区| 南溪县| 镇康县| 长泰县| 通山县| 常山县| 乐亭县| 冷水江市| 怀远县| 彰化市| 盐边县| 虎林市| 清原| 嵊州市| 天柱县| 吉水县| 定西市| 青阳县| 孝昌县| 祥云县| 乌鲁木齐县| 石城县| 达拉特旗| 白城市|