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

QQ登錄

只需一步,快速開始

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

比較典型的PID處理程序

  [復(fù)制鏈接]
ID:793610 發(fā)表于 2020-6-30 11:55 | 顯示全部樓層 |閱讀模式
這是從網(wǎng)上找來的一個(gè)比較典型的PID處理程序,在使用單片機(jī)作為控制cpu時(shí),請(qǐng)稍作簡化,具體的PID
參數(shù)必須由具體對(duì)象通過實(shí)驗(yàn)確定。由于單片機(jī)的處理速度和ram資源的限制,一般不采用浮點(diǎn)數(shù)運(yùn)算,
而將所有參數(shù)全部用整數(shù),運(yùn)算到最后再除以一個(gè)2的N次方數(shù)據(jù)(相當(dāng)于移位),作類似定點(diǎn)數(shù)運(yùn)算,可
大大提高運(yùn)算速度,根據(jù)控制精度的不同要求,當(dāng)精度要求很高時(shí),注意保留移位引起的“余數(shù)”,做好余
數(shù)補(bǔ)償。這個(gè)程序只是一般常用pid算法的基本架構(gòu),沒有包含輸入輸出處理部分。


  1. #include <string.h>
  2. #include <stdio.h>
  3. /*====================================================================================================
  4. PID Function
  5. The PID (比例、積分、微分) function is used in mainly
  6. control applications. PIDCalc performs one iteration of the PID
  7. algorithm.
  8. While the PID function works, main is just a dummy program showing
  9. a typical usage.
  10. =====================================================================================================*/
  11. typedef struct PID {
  12. double SetPoint; // 設(shè)定目標(biāo) Desired Value
  13. double Proportion; // 比例常數(shù) Proportional Const
  14. double Integral; // 積分常數(shù) Integral Const
  15. double Derivative; // 微分常數(shù) Derivative Const
  16. double LastError; // Error[-1]
  17. double PrevError; // Error[-2]
  18. double SumError; // Sums of Errors
  19. } PID;
  20. /*====================================================================================================
  21. PID計(jì)算部分
  22. =====================================================================================================*/
  23. double PIDCalc( PID *pp, double NextPoint )
  24. {
  25. double dError,
  26. Error;
  27. Error = pp->SetPoint - NextPoint; // 偏差
  28. pp->SumError += Error; // 積分
  29. dError = pp->LastError - pp->PrevError; // 當(dāng)前微分
  30. pp->PrevError = pp->LastError;
  31. pp->LastError = Error;
  32. return (pp->Proportion * Error // 比例項(xiàng)
  33. + pp->Integral * pp->SumError // 積分項(xiàng)
  34. + pp->Derivative * dError // 微分項(xiàng)
  35. );
  36. }
  37. /*====================================================================================================
  38. Initialize PID Structure
  39. =====================================================================================================*/
  40. void PIDInit (PID *pp)
  41. {
  42. memset ( pp,0,sizeof(PID));
  43. }
  44. /*====================================================================================================
  45. Main Program
  46. =====================================================================================================*/
  47. double sensor (void) // Dummy Sensor Function
  48. {
  49. return 100.0;
  50. }
  51. void actuator(double rDelta) // Dummy Actuator Function
  52. {}
  53. void main(void)
  54. {
  55. PID sPID; // PID Control Structure
  56. double rOut; // PID Response (Output)


  57. double rIn; // PID Feedback (Input)
  58. PIDInit ( &sPID ); // Initialize Structure
  59. sPID.Proportion = 0.5; // Set PID Coefficients
  60. sPID.Integral = 0.5;
  61. sPID.Derivative = 0.0;
  62. sPID.SetPoint = 100.0; // Set PID Setpoint
  63. for (;;) { // Mock Up of PID Processing
  64. rIn = sensor (); // Read Input
  65. rOut = PIDCalc ( &sPID,rIn ); // Perform PID Interation
  66. actuator ( rOut ); // Effect Needed Changes
  67. }
  68. }





復(fù)制代碼



評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

ID:462629 發(fā)表于 2021-12-28 09:26 | 顯示全部樓層
這個(gè)PID有用嗎?
回復(fù)

使用道具 舉報(bào)

ID:33544 發(fā)表于 2022-8-18 16:26 | 顯示全部樓層

這個(gè)資料好,不過難度也不低呀!
回復(fù)

使用道具 舉報(bào)

ID:1046594 發(fā)表于 2022-10-9 16:15 | 顯示全部樓層
這個(gè)應(yīng)該是位置式PID,如果換成增量式 數(shù)據(jù)計(jì)算就沒有那麼大
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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