zoukankan      html  css  js  c++  java
  • 减法要用 signed 型

      今天调试一个程序,因为Feedback是电流采样值,Setpoint是PWM值,这两个不可能是负值。所以以为Setpoint和Feedback这两个变量都可以设置为u16型(unsigned int),结果悲催了,CPU总是跑飞。导致LED暴亮,差点烧掉。。。

      原因是两个unsigned型数据相减后不能为负值。如: PIdata->Setpoint - PIdata->Feedback

    所以,要保证其中有一个是signed 型,系统就默认相减的结果为signed型,程序可以正常运行。

    typedef struct {
        u16        Kp;
        u16        Ki;
        u8        OutMax;
        signed long    Setpoint; 
        u16            Feedback;
        signed long            Error;
        signed long            Integral;
        signed long            Output;
        u8                Saturated;
        }tPIParams;
    
    tPIParams    Current;
    void CalcPI(tPIParams *PIdata)
    {    
        PIdata->Error = PIdata->Setpoint - PIdata->Feedback;
        // Deadband -- If the magnitude of the error is less than a limit,
        // then don't calculate the PI routine at all.  This saves
        // processor time and avoids oscillation problems.
        if((PIdata->Error > 2) || (PIdata->Error < -2))
        {
            // Now, calculate the actual PI function here.
            PIdata->Output += (PIdata->Error * PIdata->Kp )/10;
    
            // Perform boundary checks on the PI controller output.  If the
            // output limits are exceeded, then set output to the limit
            // and set flag.    
            if(PIdata->Output > PIdata->OutMax)
            {
                PIdata->Output = PIdata->OutMax;    
            }else if(PIdata->Output < 0)
            {
                //PIdata->Saturated = 1;
                PIdata->Output = 0;    
            }
        }    
    }
  • 相关阅读:
    简单三步给MM美白
    人物扣图抽出法
    Photoshop用抽出法抠婚纱图技巧实例集合
    十余种漂亮照片边框简单制作技巧
    如何安装knockout 2.0
    色阶去水印法
    PS合成古典的水墨舞者
    在photoshop中,从1寸到24寸的大小是多少
    常用Smarty变量操作符
    discuz代码分析一 从common.inc.php开始
  • 原文地址:https://www.cnblogs.com/liushao/p/6439459.html
Copyright © 2011-2022 走看看