[PIC®/AVR®/dsPIC®产品] 比例积分微分(PID)控制器和闭环控制

[复制链接]
 楼主| huahuagg 发表于 2024-4-14 22:17 | 显示全部楼层 |阅读模式

  1. /**********************************************************************
  2. * � 2005 Microchip Technology Inc.
  3. *
  4. * FileName:        main.c
  5. * Dependencies:    Header (.h) files if applicable, see below
  6. * Processor:       dsPIC30Fxxxx
  7. * Compiler:        MPLAB� C30 v3.00 or higher
  8. * IDE:             MPLAB� IDE v7.52 or later
  9. * Dev. Board Used: dsPICDEM 1.1 Development Board
  10. * Hardware Dependencies: None
  11. *
  12. * SOFTWARE LICENSE AGREEMENT:
  13. * Microchip Technology Incorporated ("Microchip") retains all ownership and
  14. * intellectual property rights in the code accompanying this message and in all
  15. * derivatives hereto.  You may use this code, and any derivatives created by
  16. * any person or entity by or on your behalf, exclusively with Microchip,s
  17. * proprietary products.  Your acceptance and/or use of this code constitutes
  18. * agreement to the terms and conditions of this notice.
  19. *
  20. * CODE ACCOMPANYING THIS MESSAGE IS SUPPLIED BY MICROCHIP "AS IS".  NO
  21. * WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
  22. * TO, IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A
  23. * PARTICULAR PURPOSE APPLY TO THIS CODE, ITS INTERACTION WITH MICROCHIP,S
  24. * PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
  25. *
  26. * YOU ACKNOWLEDGE AND AGREE THAT, IN NO EVENT, SHALL MICROCHIP BE LIABLE, WHETHER
  27. * IN CONTRACT, WARRANTY, TORT (INCLUDING NEGLIGENCE OR BREACH OF STATUTORY DUTY),
  28. * STRICT LIABILITY, INDEMNITY, CONTRIBUTION, OR OTHERWISE, FOR ANY INDIRECT, SPECIAL,
  29. * PUNITIVE, EXEMPLARY, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, FOR COST OR EXPENSE OF
  30. * ANY KIND WHATSOEVER RELATED TO THE CODE, HOWSOEVER CAUSED, EVEN IF MICROCHIP HAS BEEN
  31. * ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.  TO THE FULLEST EXTENT
  32. * ALLOWABLE BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO
  33. * THIS CODE, SHALL NOT EXCEED THE PRICE YOU PAID DIRECTLY TO MICROCHIP SPECIFICALLY TO
  34. * HAVE THIS CODE DEVELOPED.
  35. *
  36. * You agree that you are solely responsible for testing the code and
  37. * determining its suitability.  Microchip has no obligation to modify, test,
  38. * certify, or support the code.
  39. *
  40. * REVISION HISTORY:
  41. *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  42. * Author            Date      Comments on this revision
  43. *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  44. * HV/SB             11/15/05  First release of source file
  45. *
  46. *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  47. *
  48. * ADDITIONAL NOTES:
  49. *
  50. *
  51. **********************************************************************/

  52. #include <dsp.h>

  53. /*
  54. Variable Declaration required for each PID controller in your application
  55. */
  56. /* Declare a PID Data Structure named, fooPID */
  57. tPID fooPID;
  58. /* The fooPID data structure contains a pointer to derived coefficients in X-space and */
  59. /* pointer to controler state (history) samples in Y-space. So declare variables for the */
  60. /* derived coefficients and the controller history samples */
  61. fractional abcCoefficient[3] __attribute__ ((section (".xbss, bss, xmemory")));
  62. fractional controlHistory[3] __attribute__ ((section (".ybss, bss, ymemory")));
  63. /* The abcCoefficients referenced by the fooPID data structure */
  64. /* are derived from the gain coefficients, Kp, Ki and Kd */
  65. /* So, declare Kp, Ki and Kd in an array */
  66. fractional kCoeffs[] = {0,0,0};

  67. /*
  68. Main function demonstrating the use of PID(), PIDInit() and PIDCoeffCalc()
  69. functions from DSP library in MPLAB C30 v3.00 and higher
  70. */
  71. int main (void)
  72. {
  73. /*
  74. Step 1: Initialize the PID data structure, fooPID
  75. */
  76.         fooPID.abcCoefficients = &abcCoefficient[0];    /*Set up pointer to derived coefficients */
  77.         fooPID.controlHistory = &controlHistory[0];     /*Set up pointer to controller history samples */
  78.         PIDInit(&fooPID);                               /*Clear the controler history and the controller output */
  79.         kCoeffs[0] = Q15(0.7);
  80.         kCoeffs[1] = Q15(0.2);
  81.         kCoeffs[2] = Q15(0.07);
  82.         PIDCoeffCalc(&kCoeffs[0], &fooPID);             /*Derive the a,b, & c coefficients from the Kp, Ki & Kd */

  83. /*
  84. Step 2: Use the PID Controller
  85. */
  86.         fooPID.controlReference = Q15(0.74) ;           /*Set the Reference Input for your controller */
  87.         fooPID.measuredOutput = Q15(0.453) ;            /*Typically the measuredOutput variable is a plant response*/
  88.                                                         /*measured from an A/D input or a sensor. */
  89.                                                         /*In this example we manually set it to some value for */
  90.                                                         /*demonstration but the user should note that this value will */
  91.                                                         /*keep changing in a real application*/
  92.         while (1)                                       /*We use a while(1) loop here for demonstration purposes.*/
  93.         {                                               /*Typically, the PID calculation may be triggered off a timer*/
  94.                                                         /*or A/D interrupt */

  95.                 PID(&fooPID);                           /*Call the PID controller using the new measured input */
  96.                                                         /*The user may place a breakpoint on "PID(&fooPID)", halt the debugger,*/
  97.                                                         /*tweak the measuredOutput variable within the watch window */
  98.                                                         /*and then run the debugger again */
  99.         }

  100. }


本帖子中包含更多资源

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

×
zhuomuniao110 发表于 2024-4-15 21:48 | 显示全部楼层
这个系列自带的有DSP库函数吧。
呐咯密密 发表于 2024-4-17 20:48 | 显示全部楼层
就一个函数吗
yiy 发表于 2024-4-17 23:28 | 显示全部楼层
库函数呢,想看。
稳稳の幸福 发表于 2024-4-23 23:19 | 显示全部楼层
头文件在哪儿下载呢?
幸福小强 发表于 2024-4-26 22:43 | 显示全部楼层
利用DSP库做是容易很多。
yangxiaor520 发表于 2024-4-27 12:10 来自手机 | 显示全部楼层
PID难就难在参数的调整优化
您需要登录后才可以回帖 登录 | 注册

本版积分规则

159

主题

1408

帖子

2

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

159

主题

1408

帖子

2

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