新聞中心

        EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 數(shù)字PID算法

        數(shù)字PID算法

        作者: 時間:2016-11-09 來源:網(wǎng)絡 收藏

        /*====================================================================================================
        這是從網(wǎng)上找來的一個比較典型的PID處理程序,在使用單片機作為控制cpu時,請稍作簡化,具體的PID
        參數(shù)必須由具體對象通過實驗確定。由于單片機的處理速度和ram資源的限制,一般不采用浮點數(shù)運算,
        而將所有參數(shù)全部用整數(shù),運算到最后再除以一個2的N次方數(shù)據(jù)(相當于移位),作類似定點數(shù)運算,可
        大大提高運算速度,根據(jù)控制精度的不同要求,當精度要求很高時,注意保留移位引起的“余數(shù)”,做好余
        數(shù)補償。這個程序只是一般常用pid算法的基本架構,沒有包含輸入輸出處理部分。
        =====================================================================================================*/
        #include
        #include
        /*====================================================================================================
        PID Function

        The PID (比例、積分、微分) function is used in mainly
        control applications. PIDCalc performs one iteration of the PID
        algorithm.

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

        While the PID function works, main is just a dummy program showing
        a typical usage.
        =====================================================================================================*/

        typedef struct PID {

        double SetPoint; // 設定目標 Desired Value

        double Proportion; // 比例常數(shù) Proportional Const
        double Integral; // 積分常數(shù) Integral Const
        double Derivative; // 微分常數(shù) Derivative Const

        double LastError; // Error[-1]
        double PrevError; // Error[-2]
        double SumError; // Sums of Errors

        } PID;

        /*====================================================================================================
        PID計算部分
        =====================================================================================================*/

        double PIDCalc( PID *pp, double NextPoint )
        {
        double dError,
        Error;

        Error = pp->SetPoint - NextPoint; // 偏差
        pp->SumError += Error; // 積分
        dError = pp->LastError - pp->PrevError; // 當前微分
        pp->PrevError = pp->LastError;
        pp->LastError = Error;
        return (pp->Proportion * Error // 比例項
        + pp->Integral * pp->SumError // 積分項
        + pp->Derivative * dError // 微分項
        );
        }

        /*====================================================================================================
        Initialize PID Structure
        =====================================================================================================*/

        void PIDInit (PID *pp)
        {
        memset ( pp,0,sizeof(PID));
        }

        /*====================================================================================================
        Main Program
        =====================================================================================================*/

        double sensor (void) // Dummy Sensor Function
        {
        return 100.0;
        }

        void actuator(double rDelta) // Dummy Actuator Function
        {}

        void main(void)
        {
        PID sPID; // PID Control Structure
        double rOut; // PID Response (Output)
        double rIn; // PID Feedback (Input)

        PIDInit ( &sPID ); // Initialize Structure
        sPID.Proportion = 0.5; // Set PID Coefficients
        sPID.Integral = 0.5;
        sPID.Derivative = 0.0;
        sPID.SetPoint = 100.0; // Set PID Setpoint

        for (;;) { // Mock Up of PID Processing

        rIn = sensor (); // Read Input
        rOut = PIDCalc ( &sPID,rIn ); // Perform PID Interation
        actuator ( rOut ); // Effect Needed Changes
        }
        }



        關鍵詞: 數(shù)字PID算

        評論


        技術專區(qū)

        關閉
        主站蜘蛛池模板: 九寨沟县| 寻乌县| 全南县| 武功县| 大厂| 宾川县| 蒙城县| 裕民县| 长汀县| 大新县| 九龙坡区| 盖州市| 忻州市| 子长县| 彰化市| 凤山市| 福海县| 甘洛县| 济南市| 响水县| 大连市| 新巴尔虎右旗| 华安县| 巴林右旗| 克拉玛依市| 玛纳斯县| 揭阳市| 磴口县| 长武县| 洛宁县| 务川| 乐安县| 长春市| 万盛区| 邢台市| 璧山县| 开阳县| 财经| 阿城市| 辉县市| 鹤峰县|