PID算法C语言例程

一个PID的demo:
参考:https://blog.csdn.net/u010160335/article/details/87373031
https://www.cnblogs.com/cjq0301/p/5184808.html

#include "pch.h"
#include <iostream>

//增量式PID
typedef struct {
    
    
    int SetPoint;       //目标值
    long SumError;      //误差累计

    float P;            //比例常数
    float I;            //积分常数
    float D;            //微分常数

    int LastError;      //上次偏差值
    int PrevError;      //上上次偏差值
}PID;
/*************************************************
 *函数名称:void PID_Arg_Init(PID* sptr)          *
 *功    能:参数初始化                            *
 *参    数:PID* sptr                             *
 *返 回 值:void                                  *
 *************************************************/
void PID_Arg_Init(PID* sptr)
{
    
    
    sptr->SumError = 0;         //误差累计 
    sptr->LastError = 0;        //上次偏差值
    sptr->PrevError = 0;        //上上次偏差值

    sptr->P = 0;                //比例常数
    sptr->I = 0;                //积分常数
    sptr->D = 0;                //微分常数

    sptr->SetPoint = 0;         //目标值
}
/****************************************************************
 *函数名称:int PID_Contrl(PID* sptr,int NextPoint)              *
 *功    能:PID控制                                              *
 *参    数:PID* sptr:上次参数  int NextPoint:当前实际值       *
 *返 回 值:void                                                 *
 ****************************************************************/
void PID_Arg_Set(PID* sptr, float P,float I, float D,int Aims)
{
    
    
    /*P-I-D参数初始化*/
    sptr->P = P;  /*0.6*/
    sptr->I = I;  /*0.15*/
    sptr->D = D;  /*0.1*/

    /*目标值设定*/
    sptr->SetPoint = Aims;
}
/****************************************************************
 *函数名称:int PID_Contrl(PID* sptr,int NextPoint)              *
 *功    能:PID控制                                              *
 *参    数:PID* sptr:上次参数  int NextPoint:当前实际值       *
 *返 回 值:void                                                 *
 ****************************************************************/
int PID_Contrl(PID* sptr, int NextPoint)
{
    
    
    register int iError, iIncPid;

    /*当前误差  设定的目标值和实际值的偏差*/
    iError = sptr->SetPoint - NextPoint;   

    /*增量计算*/
    iIncPid = int((sptr->P * iError) - (sptr->I * sptr->LastError) + (sptr->D * sptr->PrevError));
        
    /*存储误差  用于下次计算*/
    sptr->PrevError = sptr->LastError;
    sptr->LastError = iError;

    /*返回增量值*/
    return iIncPid;                         
}

int main()
{
    
    
    int temp = 0;
    int add = 0;

    PID PID_Temp;
    /*PID参数初始化*/
    PID_Arg_Init(&PID_Temp);
    /*PID参数设定*/
    PID_Arg_Set(&PID_Temp,0.6,0.15,0.1,50);

    /**/
    for (int i = 0; i < 100; i++)
    {
    
    
        add = PID_Contrl(&PID_Temp, temp);
        temp += add;

        printf("下次增量 add = %d\n", add);
        printf("当前增量 temp = %d\n", temp);
        printf("目标值PID_Temp.SetPoint = %d\n", PID_Temp.SetPoint);
        printf("当前误差PID_Temp.LastError = %d\n", PID_Temp.LastError);
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38158479/article/details/112049748
今日推荐