找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 2074|回復(fù): 0
收起左側(cè)

STM32單片機(jī)的PID算法實(shí)現(xiàn)程序無法編譯,求指導(dǎo)

[復(fù)制鏈接]
ID:776760 發(fā)表于 2021-6-30 14:55 | 顯示全部樓層 |閱讀模式
1.png
  1. #include "System_init.h"
  2. extern int U_Set;
  3. extern u16 CCR1_Val;

  4. /*====================================================================================================
  5. PID Function
  6. The PID (比例、積分、微分) function is used in mainly
  7. control applications. PIDCalc performs one iteration of the PID
  8. algorithm.
  9. While the PID function works, main is just a dummy program showing
  10. a typical usage.
  11. =====================================================================================================*/
  12. typedef struct PID {
  13.         float SetPoint; // 設(shè)定目標(biāo)Desired value
  14.         float Proportion; // 比例常數(shù)Proportional Const
  15.         float Integral; // 積分常數(shù)Integral Const
  16.         float Derivative; // 微分常數(shù)Derivative Const
  17.         float LastError; // Error[-1]       
  18.         float PrevError; // Error[-2]
  19.         float SumError; // Sums of Errors
  20. } PID;
  21. /*====================================================================================================/
  22. PID計(jì)算部分
  23. =====================================================================================================*/
  24. PID   vPID={0,0.1,0.001,0.001,0,0,0};

  25. float PIDCalc( PID *pp, float NextPoint )
  26. {
  27.         float dError,
  28.         Error;
  29.         Error = pp->SetPoint - NextPoint; // 偏差
  30.         pp->SumError += Error; // 積分
  31.         dError = pp->LastError - pp->PrevError; // 當(dāng)前微分
  32.         pp->PrevError = pp->LastError;
  33.         pp->LastError = Error;

  34.     if(pp->SumError>900)  pp->SumError=900;
  35.     else if(pp->SumError<-900)          pp->SumError=-900;

  36.         return (pp->Proportion * Error // 比例項(xiàng)
  37.         + pp->Integral * pp->SumError // 積分項(xiàng)
  38.         + pp->Derivative * dError // 微分項(xiàng)
  39.         );
  40. }

  41. float Vol(int vSet,int v3){

  42.         float vOut; // PID Response (Output)
  43.         float vIn; // PID Feedback (Input)

  44.         vPID.SetPoint = vSet; // Set PID Setpoint
  45.         vIn = (float)v3; // Read Input
  46.         vOut = PIDCalc ( &vPID,vIn ); // Perform PID Interation         
  47.         return vOut;
  48. }

復(fù)制代碼

代碼: STM32單片機(jī)的PID算法實(shí)例.zip (729.66 KB, 下載次數(shù): 12)
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表