標(biāo)題: 增量式PID算法C++源碼 [打印本頁]

作者: 1234zhang123456    時間: 2018-5-20 10:00
標(biāo)題: 增量式PID算法C++源碼
C++源程序如下:
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <Windows.h>

  5. //定義PID變量的結(jié)構(gòu)體
  6. struct _pid
  7. {
  8.         float SetSpeed;
  9.         float ActualSpeed;
  10.         float err;
  11.         float err_last;        //定義上上個偏差
  12.         float err_next;       //定義上一個偏差值
  13.         float Kp,Ki,Kd;
  14.        
  15. }pid;

  16. //初始化變量
  17. void PID_init(void)
  18. {
  19.         printf ("pid_value_init_begin\n");

  20.         pid.ActualSpeed = 0.0;
  21.         pid.SetSpeed = 0.0;

  22.         pid.err = 0.0;
  23.         pid.err_last = 0.0;
  24.         pid. err_next = 0.0;
  25.        
  26.        
  27.         pid.Kp = 0.2;
  28.         pid.Ki = 0.015;
  29.         pid.Kd = 0.2;

  30.         printf("pid_init end \n");
  31. }

  32. //增量式算法
  33. float PID_calculate(float speed)
  34. {

  35.         pid.SetSpeed = speed;
  36.         pid.err = pid.SetSpeed - pid.ActualSpeed;
  37.        
  38.         float incrementSpeed = pid.Kp * (pid.err - pid.err_next) + pid.Ki * pid.err + pid.Kd * (pid.err - 2 * pid.err_next + pid.err_last);

  39.         pid.ActualSpeed += incrementSpeed;

  40.         pid.err_last = pid.err_next;     //定義上一個偏差值
  41.         pid.err_next = pid.err;

  42.         return pid.ActualSpeed;

  43. }




  44. int main(void)
  45. {
  46.         int setcount = 0;
  47.         printf ("Please begin \n");
  48.         PID_init();

  49.         while (setcount < 100)
  50.         {
  51.                 float speed = PID_calculate(250.0);
  52.                 printf ("value is %f\n",speed);
  53.                 setcount++;
  54.                 Sleep(300);
  55.         }
  56.         system("pause");
  57.         return 0;
  58. }
復(fù)制代碼

所有資料51hei提供下載:
增量式PID.rar (13.49 MB, 下載次數(shù): 43)



作者: l1051324850    時間: 2019-5-30 14:11
運行不了啊




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1