[DemoCode下载] M480利用内核DSP库函数实现PID功能

[复制链接]
1657|18
 楼主| 598330983 发表于 2022-12-20 20:58 | 显示全部楼层 |阅读模式
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * @brief
  4. *           Display how to use M4 DSP PID Controller
  5. *           compare with calcultion time without DSP
  6. * @note
  7. * Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #include <stdio.h>
  10. #include "NuMicro.h"
  11. #include "arm_math.h"

  12. /*---------------------------------------------------------------------------*/
  13. /* Global variables                                                          */
  14. /*---------------------------------------------------------------------------*/
  15. #define USE_DSP

  16. uint32_t u32CalTime;
  17. arm_pid_instance_f32 PIDS;
  18. float32_t doutput[100], derr;
  19. float fA0, fA1, fA2, afstate[3], fKp = 0.4, fKi = 0.4, fKd = 0, ftarget, fival;

  20. /*---------------------------------------------------------------------------*/
  21. /* Functions                                                                 */
  22. /*---------------------------------------------------------------------------*/
  23. float PID(float fin)
  24. {
  25.     float fout;

  26.     fA0 = fKp + fKi;
  27.     fA1 = -fKp - (2 * fKd);
  28.     fA2 = fKd;
  29.     /* y[n] = y[n-1] + A0 * x[n] + A1 * x[n-1] + A2 * x[n-2]  */
  30.     fout = (fA0 * fin) + (fA1 * afstate[0]) + (fA2 * afstate[1]) + (afstate[2]);

  31.     /* Update state */
  32.     afstate[1] = afstate[0];
  33.     afstate[0] = fin;
  34.     afstate[2] = fout;

  35.     /* return to application */
  36.     return (fout);
  37. }

  38. void SYS_Init(void)
  39. {
  40.     /*---------------------------------------------------------------------------*/
  41.     /* Init System Clock                                                         */
  42.     /*---------------------------------------------------------------------------*/
  43.     /* Unlock protected registers */
  44.     SYS_UnlockReg();

  45.     /* Set XT1_OUT(PF.2) and XT1_IN(PF.3) to input mode */
  46.     PF->MODE &= ~(GPIO_MODE_MODE2_Msk | GPIO_MODE_MODE3_Msk);

  47.     /* Enable External XTAL (4~24 MHz) */
  48.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  49.     /* Waiting for 12MHz clock ready */
  50.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  51.     /* Set core clock as PLL_CLOCK from PLL */
  52.     CLK_SetCoreClock(FREQ_192MHZ);

  53.     /* Set both PCLK0 and PCLK1 as HCLK/2 */
  54.     CLK->PCLKDIV = CLK_PCLKDIV_PCLK0DIV2 | CLK_PCLKDIV_PCLK1DIV2;

  55.     /* Enable IP clock */
  56.     CLK_EnableModuleClock(UART0_MODULE);
  57.     CLK_EnableModuleClock(TMR0_MODULE);

  58.     /* Select IP clock source */
  59.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HXT, CLK_CLKDIV0_UART0(1));
  60.     CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_HXT, 0);

  61.     /* Update System Core Clock */
  62.     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
  63.     SystemCoreClockUpdate();


  64.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  65.     SYS->GPB_MFPH &= ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk);
  66.     SYS->GPB_MFPH |= (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);

  67.     /* Lock protected registers */
  68.     SYS_LockReg();

  69. }

  70. void UART_Init(void)
  71. {
  72.     /* Configure UART0 and set UART0 Baudrate */
  73.     UART_Open(UART0, 115200);
  74. }

  75. int32_t main(void)
  76. {
  77.     uint32_t i;

  78.     /* Set PID parameters */
  79.     PIDS.Kp = 0.4;
  80.     PIDS.Ki = 0.4;
  81.     PIDS.Kd = 0;

  82.     /* Target value*/
  83.     ftarget = 500;
  84.     /* Inital value */
  85.     fival = 0;
  86.     /* Initial value and target value error */
  87.     derr = ftarget - fival;

  88.     /* Unlock protected registers */
  89.     SYS_UnlockReg();

  90.     /* Init System, peripheral clock and multi-function I/O */
  91.     SYS_Init();

  92.     /* Init UART for printf */
  93.     UART_Init();

  94.     /*
  95.         This sample code is used to show how to use ARM Cortex-M4 DSP library.
  96.         And displays function calculation time.
  97.     */

  98.     printf("\n\n");
  99.     printf("+----------------------------------------+\n");
  100.     printf("|      M480_M4_DSP_PID Sample Code       |\n");
  101.     printf("+----------------------------------------+\n");

  102.     TIMER_Open(TIMER0, TIMER_CONTINUOUS_MODE, 1);
  103.     TIMER_Start(TIMER0);

  104. #ifdef USE_DSP
  105.     /* Initial DSP PID controller function */
  106.     arm_pid_init_f32(&PIDS, 0);

  107.     /* Calculate PID controller function 100 times */
  108.     for (i = 1; i < 100; i++)
  109.     {
  110.         doutput[i] = arm_pid_f32(&PIDS, derr);
  111.         //printf("%0.2f\n",doutput[i]);
  112.         /* Update error */
  113.         derr = ftarget - doutput[i - 1];
  114.     }

  115. #else

  116.     for (i = 1; i < 100; i++)
  117.     {
  118.         doutput[i] = PID(derr);
  119.         derr = ftarget - doutput[i - 1];
  120.     }

  121. #endif

  122.     TIMER_Close(TIMER0);
  123.     u32CalTime = TIMER_GetCounter(TIMER0);

  124.     /* Display function calculation time */
  125.     printf("Calculation time is %d \n", u32CalTime);

  126.     while (1);
  127. }


 楼主| 598330983 发表于 2022-12-20 20:59 | 显示全部楼层
EC_M480_M4_DSP_PID_V1.00.zip (6.74 MB, 下载次数: 7)
奉上代码
稳稳の幸福 发表于 2022-12-21 10:31 | 显示全部楼层
  typedef struct
  {
    float32_t A0;          /**< The derived gain, A0 = Kp + Ki + Kd . */
    float32_t A1;          /**< The derived gain, A1 = -Kp - 2Kd. */
    float32_t A2;          /**< The derived gain, A2 = Kd . */
    float32_t state[3];    /**< The state array of length 3. */
    float32_t Kp;          /**< The proportional gain. */
    float32_t Ki;          /**< The integral gain. */
    float32_t Kd;          /**< The derivative gain. */
  } arm_pid_instance_f32;
稳稳の幸福 发表于 2022-12-21 10:31 | 显示全部楼层
下载了,看了一下,使用好方便啊
稳稳の幸福 发表于 2022-12-21 10:34 | 显示全部楼层
只需要自己定义好PID三个参数的值,然后设定目标值,每次传递误差进去,返回值继续做差。666,好简单。
phoenixwhite 发表于 2023-2-2 11:02 | 显示全部楼层
如何用c语言实现PID算法的参数计算
ingramward 发表于 2023-2-2 12:51 | 显示全部楼层
已知传递函数怎么进行pid参数整定
iyoum 发表于 2023-2-2 16:29 | 显示全部楼层
PID控制器是如何实现的               
dspmana 发表于 2023-2-4 16:03 | 显示全部楼层
如何使用DSP库进行FFT              
robertesth 发表于 2023-2-4 17:45 | 显示全部楼层
库函数的说明文档在哪儿               
elsaflower 发表于 2023-2-4 23:20 | 显示全部楼层
单神经元PID双闭环调速如何实现
bartonalfred 发表于 2023-2-5 13:41 | 显示全部楼层
这个代码是自整定参数吗?              
juliestephen 发表于 2023-2-6 13:40 | 显示全部楼层
如何编写进程PID查找函数               
hilahope 发表于 2023-2-7 20:37 | 显示全部楼层
如何在DSP中添加现成的函数库
OKAKAKO 发表于 2023-2-9 09:23 | 显示全部楼层
dsp库做FFT运算的具体讲解有吗?
usysm 发表于 2023-4-18 19:45 | 显示全部楼层
在实际应用中,还需要考虑PID控制器的稳定性、响应时间、抗干扰能力等因素。
1988020566 发表于 2023-4-18 20:08 | 显示全部楼层
可以使用 Keil 内核 DSP 库函数来实现 PID 控制算法。
timfordlare 发表于 2023-4-18 21:29 | 显示全部楼层
M480 是一种基于 Arm Cortex-M4 内核的高性能单片机,配备了丰富的外设和内置 DSP 功能模块。
Jacquetry 发表于 2023-4-18 22:52 | 显示全部楼层
可以实现自动调节PID吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

267

主题

5575

帖子

22

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