• 大小: 7.51 KB
    文件类型: .c
    金币: 1
    下载: 0 次
    发布日期: 2025-01-05
  • 语言: 其他
  • 标签: 开发技术  C  

资源简介

模糊PID源程序,C语言编写,内有详细说明,适用于初学者-Fuzzy PID source

资源截图

代码片段和文件信息

#include 
#include “systick.h“
#include “motor.h“
#include “PID.h“

__IO uint32_t CaptureNumber1;
__IO uint32_t CaptureNumber2;
__IO uint32_t CaptureNumber3;
__IO uint32_t CaptureNumber4;

typedef struct 
{
__IO int      SetPoint;                                 //设定目标 Desired Value
__IO double   Proportion;                               //比例常数 Proportional Const
__IO double   Integral;                                 //积分常数 Integral Const
__IO double   Derivative;                               //微分常数 Derivative Const
__IO int      LastError1;                                //Error[-1]
__IO int      PrevError1;                                //Error[-2]
__IO int      LastError2;                                //Error[-1]
__IO int      PrevError2;                                //Error[-2]
__IO int      LastError3;                                //Error[-1]
__IO int      PrevError3;                                //Error[-2]
__IO int      LastError4;                                //Error[-1]
__IO int      PrevError4;                                //Error[-2]
}PID;

/* 私有宏定义 ----------------------------------------------------------------*/
/*************************************/
//定义PID相关宏

/*************************************/
//定义PID相关宏
// 这三个参数设定对电机运行影响非常大
/*************************************/
#define  P_DATA      3.2                                 //P参数
#define  I_DATA      1.1                                //I参数
#define  D_DATA      -0.15                              //D参数



static PID sPID;
static PID *sptr = &sPID;

/* 私有函数原形 --------------------------------------------------------------*/
/* 函数体 --------------------------------------------------------------------*/

/**************PID参数初始化********************************/
void IncPIDInit(void) 
{
    sptr->LastError1=0;            //Error[-1]
    sptr->PrevError1=0;            //Error[-2]
sptr->LastError2=0;            //Error[-1]
sptr->PrevError2=0;            //Error[-2]
    sptr->LastError3=0;            //Error[-1]
    sptr->PrevError3=0;            //Error[-2]
sptr->LastError4=0;            //Error[-1]
sptr->PrevError4=0;            //Error[-2]
    sptr->Proportion=P_DATA;      //比例常数 Proportional Const
    sptr->Integral=I_DATA;        //积分常数  Integral Const
    sptr->Derivative=D_DATA;      //微分常数 Derivative Const
    sptr->SetPoint=100;           //设定目标Desired Value
}
/********************增量式PID控制设计************************************/
int IncPIDCalc1(int NextPoint1) 
{
  int iErroriIncpid;                                 //当前误差
  iError=sptr->SetPoint-NextPoint1;                    //增量计算
  iIncpid=(sptr->Proportion * iError)                 //E[k]项
              -(sptr->Integral * sptr->LastError1)     //E[k-1]项
              +(sptr->Derivative * sptr->PrevError1);  //E[k-2]项
              
  sptr->PrevError1=sptr->LastError1;                    //存储误差,用于下次计算
  sptr->Last

评论

共有 条评论