新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > PID公式的推導過程及實現(xiàn)代碼

        PID公式的推導過程及實現(xiàn)代碼

        作者: 時間:2016-12-01 來源:網(wǎng)絡 收藏
        一、PID框圖







        n0(t)是要穩(wěn)定的值
        n(t)是當前輸出值
        e(t) = n0(t) - n(t)


        一、模擬PID控制原理
        這個公式網(wǎng)絡上很好找:

        本文引用地址:http://www.104case.com/article/201612/324482.htm

        二、數(shù)字PID控制
        由于模擬的微積分運算對應計算機來說是不太好寫代碼的,所以要利用采樣將數(shù)據(jù)離散化

        于是公式就可以轉換為:



        其中T為采樣時間,由于T之類的參數(shù)是常量,所以將Kp乘入公式中可以轉換成另一種寫法
        這個公式叫位置式算法

        由于要不斷的累加ej,增加了計算量,所以這個公式又可以轉換為增量式算法:




        然后u(k) = u(k-1) + u

        三、參數(shù)的整定
        先將Ti設置為無窮大,Td設置為0,調節(jié)Kp
        然后再調節(jié)Ti,最后是Td

        四、實現(xiàn)代碼

        typedef struct PID
        {
        int SetPoint; //設定目標 Desired Value
        longSumError; //誤差累計
        double Proportion; //比例常數(shù) Proportional Cons
        double Integral; //積分常數(shù) Integral Const
        double Derivative; //微分常數(shù) Derivative Const
        int LastError; //Error[-1]
        int PrevError; //Error[-2]
        } PID;

        /*******************************************************************************
        * 函數(shù)名稱 : IncPIDCalc
        * 函數(shù)描述 : 增量式 PID 控制計算
        * 函數(shù)輸入 : int 當前位置
        * 函數(shù)輸出 : 無
        *函數(shù)返回 : 增量式PID結果
        *******************************************************************************/
        int IncPIDCalc(int NextPoint)
        {
        int iError, iIncpid;
        //當前誤差
        iError = sptr->SetPoint - NextPoint;
        //增量計算
        iIncpid = sptr->Proportion * iError //E[k]項
        - sptr->Integral * sptr->LastError //E[k-1]項
        + sptr->Derivative * sptr->PrevError; //E[k-2]項
        //存儲誤差,用于下次計算
        sptr->PrevError = sptr->LastError;
        sptr->LastError = iError;
        //返回增量值
        return(iIncpid);
        }
        /*******************************************************************************
        * 函數(shù)名稱 : LocPIDCalc
        * 函數(shù)描述 : 位置式 PID 控制計算
        * 函數(shù)輸入 : int 當前位置
        * 函數(shù)輸出 : 無
        *函數(shù)返回 : 位置式PID結果
        *******************************************************************************/
        int LocPIDCalc(int NextPoint)
        {
        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); //微分項
        }


        評論


        技術專區(qū)

        關閉
        主站蜘蛛池模板: 宜黄县| 库伦旗| 连云港市| 台北县| 武邑县| 包头市| 罗平县| 双鸭山市| 乌恰县| 巴彦淖尔市| 安岳县| 瑞安市| 金川县| 甘肃省| 武义县| 新乡市| 益阳市| 安龙县| 罗平县| 银川市| 弥渡县| 永城市| 临沂市| 浦东新区| 莱州市| 卓资县| 青海省| 平乡县| 赫章县| 广宁县| 黎城县| 荔浦县| 枞阳县| 彰化市| 长沙县| 班玛县| 神池县| 金溪县| 贺州市| 青海省| 峨边|