[DemoCode下载] M4利用DSP内核实现PID算法

[复制链接]
2374|7
 楼主| xinpian101 发表于 2018-3-8 09:25 | 显示全部楼层 |阅读模式
例程序演示如何使用PID控制器运算。首先用arm_pid_instance_f32宣告矩阵变量,再来对PID初始化设定。目标参考值为in输入arm_pid_f32()进行运算,得到进行PID后所输出值,再把第一次输出值与目标参考值相减为误差,将其误差输入PID进行第二次运算,以此进行下去输出值即可与目标参考值相近,其执行流程如下图。
用户可以直接使用这些函式,来实现自己的数学方程式运算。程序内也比较了有无使用DSP计算时间的差异,使用者把#define USE_DSP给取消即可不使用DSP来运算这些方程式。
QQ截图20180308092514.png

 楼主| xinpian101 发表于 2018-3-8 09:26 | 显示全部楼层
  1. /****************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.0
  4. * $Date: 15/09/02 10:04a $
  5. * @brief
  6. *         Display how to use M4 DSP PID Controller
  7. *                      and compare with calcultion without DSP
  8. * @note
  9. * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include "M451Series.h"
  14. #include "arm_math.h"
  15. #define USE_DSP
  16. int i,j,CalTime;
  17. arm_pid_instance_f32 PIDS;
  18. float32_t output[100],ee;
  19. float A0,A1,A2,state[3],Kp=0.4,Ki=0.4,Kd=0,target,ival;
  20. float PID(float in)
  21. {
  22.     float out;
  23.                 A0=Kp+Ki;
  24.                 A1 = -Kp-(2* Kd);
  25.                 A2 = Kd;
  26.     /* y[n] = y[n-1] + A0 * x[n] + A1 * x[n-1] + A2 * x[n-2]  */
  27.     out = (A0 * in) + (A1 * state[0]) + (A2 * state[1]) + (state[2]);

  28.     /* Update state */
  29.     state[1] = state[0];
  30.     state[0] = in;
  31.     state[2] = out;

  32.     /* return to application */
  33.     return (out);
  34. }
  35. void SYS_Init(void)
  36. {
  37.     /*---------------------------------------------------------------------------------------------------------*/
  38.     /* Init System Clock                                                                                       */
  39.     /*---------------------------------------------------------------------------------------------------------*/
  40.     /* Enable HIRC clock */
  41.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  42.     /* Waiting for HIRC clock ready */
  43.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  44.     /* Switch HCLK clock source to HIRC */
  45.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  46.     /* Enable HXT */
  47.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  48.     /* Waiting for clock ready */
  49.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  50.     /* Set core clock as PLL_CLOCK from PLL and SysTick source to HCLK/2*/
  51.     CLK_SetCoreClock(72000000);
  52.     CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_HCLK_DIV2);

  53.     /* Enable peripheral clock */
  54.     CLK_EnableModuleClock(UART0_MODULE);
  55.     CLK_EnableModuleClock(TMR0_MODULE);

  56.     /* Peripheral clock source */
  57.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_TMR0SEL_HXT, CLK_CLKDIV0_UART(1));
  58.     CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_HXT, 0);

  59. }
  60. void UART_Init(void)
  61. {
  62.     /* Set PD multi-function pins for UART0 RXD, TXD */
  63.     SYS->GPD_MFPL = SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD;
  64.     /* Reset UART module */
  65.     SYS_ResetModule(UART0_RST);

  66.     /* Configure UART0 and set UART0 Baudrate */
  67.     UART_Open(UART0, 115200);
  68. }

  69. /*---------------------------------------------------------------------------------------------------------*/
  70. /*  Main Function                                                                                          */
  71. /*---------------------------------------------------------------------------------------------------------*/
  72. int32_t main(void)
  73. {               
  74.           uint32_t i;
  75.                 PIDS.Kp=0.4;                               
  76.                 PIDS.Ki=0.4;
  77.                 PIDS.Kd=0;
  78.                 /* Target value*/
  79.                 target=500;
  80.                 /* Inital value */
  81.                 ival=0;
  82.                 /* Initial value and target value error */
  83.                 ee=target-ival;
  84.        
  85.                 /* Unlock protected registers */
  86.     SYS_UnlockReg();

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

  89.     /* Lock protected registers */
  90.     SYS_LockReg();

  91.     /* Init UART for printf */
  92.     UART_Init();
  93.                
  94.                 TIMER_Open(TIMER0, TIMER_CONTINUOUS_MODE, 1);
  95.                 TIMER_Start(TIMER0);
  96. #ifdef USE_DSP
  97.                 /* Initial DSP PID controller function*/
  98.                 arm_pid_init_f32(&PIDS,0);
  99.                 /* Calculate PID controller function 100 times*/
  100.                 for(i=1;i<100;i++)
  101.                 {
  102.                         output[i]=arm_pid_f32(&PIDS,ee);
  103.                         //printf("%0.2f\n",output[i]);
  104.                         /* Update error */
  105.                         ee=target-output[i-1];
  106.                 }
  107. #else
  108.                
  109.                 for(i=1;i<100;i++)
  110.                 {
  111.                         output[i]=PID(ee);
  112.                         ee=target-output[i-1];
  113.                 }
  114.                
  115. #endif
  116.           TIMER_Close(TIMER0);
  117.                 CalTime=TIMER_GetCounter(TIMER0);       
  118.                 printf("time is %d \n",CalTime);

  119.     while(1);                             /* main function does not return */
  120. }
 楼主| xinpian101 发表于 2018-3-8 10:50 | 显示全部楼层
是不是很好用
dongnanxibei 发表于 2018-3-8 15:21 | 显示全部楼层
这个参数有默认值吗
doit888 发表于 2018-3-9 13:27 | 显示全部楼层
请问 如果实现一个 多通道的 PID

比如 8个通道的 该怎么做呢?

谢谢!
doit888 发表于 2018-3-9 13:28 | 显示全部楼层
一般的 PID 采样周期 是多少?

计算周期又是多少呢?

doit888 发表于 2018-3-9 13:37 | 显示全部楼层
如果说 自平衡小车 一类的 是200HZ 的运算速度,那么采集MPU6050的速度 1KHZ 足够了吧?
yiyigirl2014 发表于 2018-3-10 08:44 | 显示全部楼层
doit888 发表于 2018-3-9 13:37
如果说 自平衡小车 一类的 是200HZ 的运算速度,那么采集MPU6050的速度 1KHZ 足够了吧? ...

肯定够了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

129

主题

1650

帖子

1

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