找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1550|回复: 0
打印 上一主题 下一主题
收起左侧

pid程序的应用

[复制链接]
跳转到指定楼层
楼主
ID:365616 发表于 2018-7-5 14:57 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

typedef struct PID
{
        int  SetPoint;                         //  设定目标 Desired Value
        long SumError;                        //        误差累计
               
        double  Proportion;     //  比例常数 Proportional Const
        double  Integral;       //  积分常数 Integral Const
        double  Derivative;     //  微分常数 Derivative Const

        int LastError;                 //  Error[-1]
        int PrevError;          //  Error[-2]

} PID;

static PID         sPID;
static PID         *sptr = &sPID;
//=============================================================
// Function: IncPIDInit()
// Syntax: void IncPIDInit(void);
// Description: Initialization PID parameter.
// Notes:
// parameters: none
// returns: none
//=============================================================
void PID_Init(void)
{
    sptr->LastError  = 0;                        //Error[-1]
    sptr->PrevError  = 0;                        //Error[-2]
        sptr->Proportion = 0.3;                        //比例常数 Proportional Const
    sptr->Integral   = 0.2;                        //3.5积分常数 Integral Const
    sptr->Derivative = 0.1;                        //1.25微分常数 Derivative Const
    sptr->SetPoint   = 0;
        sptr->SumError   = 0;
}

//=============================================================
// ----Function: IncPIDSetPoint()
// ------Syntax: void IncPIDSetPoint(unsigned int setpoint);
// -Description: Set PID Desired Value
// -------Notes:
// --parameters: Desired Value
// -----returns: none
//=============================================================
void PIDSetPoint(int setpoint)
{        sptr->SetPoint = setpoint;        }

int PIDGetSetpoint(void)
{        return(sptr->SetPoint);        }
//=============================================================
// ----Function: IncPIDKp()
// ------Syntax: void IncPIDKp(double dKp);
// -Description: Set PID Proportion coefficient
// -------Notes:
// --parameters: Proportion Const
// -----returns: none
//=============================================================
void PIDSetKp(double dKpp)
{        sptr->Proportion = dKpp;        }
//===================================//
// Get Proportion
//===================================//
double PIDGetKp(void)
{        return(sptr->Proportion);        }
//=============================================================
// ----Function: IncPIDKi()
// ------Syntax: void IncPIDKi(double dKi);
// -Description: Set PID Integral coefficient
// -------Notes:
// --parameters: Integral Const
// -----returns: none
//=============================================================
void PIDSetKi(double dKii)
{        sptr->Integral = dKii;        }
//===================================//
// Get Integral
//===================================//
double PIDGetKi(void)
{        return(sptr->Integral);        }
//=============================================================
// ----Function: IncPIDKd()
// ------Syntax: void IncPIDKd(double dKd);
// -Description: Set PID Derivative coefficient
// -------Notes:
// --parameters: Derivative Const
// -----returns: none
//=============================================================
void PIDSetKd(double dKdd)
{        sptr->Derivative = dKdd;        }
//===================================//
// Get Derivative
//===================================//
double PIDGetKd(void)
{        return(sptr->Derivative);        }

void Self_ConfigPID(double uiKp)
{
        double dKp;
        dKp = uiKp;
        PIDSetKp(2.45*dKp);
        PIDSetKi(3.50*dKp);
        PIDSetKd(1.25*dKp);
}

//=============================================================
// ----Function: IncPIDCalc()
// ------Syntax: int IncPIDCalc(unsigned int NextPoint);
// -Description: Increment Digital PID calculate
// -------Notes: Basic Increment Digital PID
// --parameters: Next Point
// -----returns: increase controls parameter
//=============================================================
int IncPIDCalc(int NextPoint)
{
        register int iError, iIncpid;

        iError = sptr->SetPoint - NextPoint;

        iIncpid = sptr->Proportion * iError                                //E[0]
            + sptr->Integral   * sptr->LastError        //E[-1]
            + sptr->Derivative * sptr->PrevError;        //E[-2]

        sptr->PrevError = sptr->LastError;
        sptr->LastError = iError;

        return(iIncpid);
}

//=============================================================
// ----Function: LocPIDCalc()
// ------Syntax: unsigned int locPIDCalc(unsigned int NextPoint);
// -Description: Location Digital PID calculate
// -------Notes: Basic Location Digital PID
// --parameters: Next Point
// -----returns: Location controls parameter
//=============================================================
unsigned int LocPIDCalc(int NextPoint)
{
    register int  iError,dError;
       
        iError = sptr->SetPoint - NextPoint;              // 偏差
        sptr->SumError += iError;                                        // 积分
        dError = iError - sptr->LastError;                         // 当前微分
        sptr->LastError = iError;

        return(sptr->Proportion * iError                   // 比例项
           + sptr->Integral * sptr->SumError         // 积分项
           + sptr->Derivative * dError);
}
       
//=============================================//
//        *END*
//=============================================//




评分

参与人数 2黑币 +55 收起 理由
热带芒果 + 5 很给力!
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

无效楼层,该帖已经被删除
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表