大功率PID控温

#include "rtthread.h"
#ifdef FRYER_TEMP_PID       //PID控温
#include "pid.h"
#include "string.h"

#define DBG_TAG "app.pid"
#define DBG_LVL DBG_INFO
#include 

/** 
* PID运算. 
* U(k)+KP*[E(k)-E(k-1)]+KI*E(k)+KD*[E(k)-2E(k-1)+E(k-2)].
* @param[in]   无
* @param[out]  无
* @retval  无
* @par 标识符 
*      保留 
* @par 其它 
*      无 
* @par 修改日志 
*      kun于2021-08-16创建 
*/
void pid_operation(pid_value_t *pid_ptr)
{
    if( pid_ptr->iSetVal > pid_ptr->iCurVal ){      //设定值大于实际值否
        if( pid_ptr->iSetVal - pid_ptr->iCurVal > 30 ){
            pid_ptr->iPriVal = 100;                 //偏差大于20为上限幅值全输出(全速加热)
        }
        else{                                       //否则慢慢来
            pid_ptr->Temp[0] = pid_ptr->iSetVal - pid_ptr->iCurVal;    //偏差<=20,计算E(k)
            pid_ptr->uEkFlag[1] = 0;                //E(k)为正数,因为设定值大于实际值
            //数值进行移位
            pid_ptr->liEkVal[2] = pid_ptr->liEkVal[1];
            pid_ptr->liEkVal[1] = pid_ptr->liEkVal[0];
            pid_ptr->liEkVal[0] = pid_ptr->Temp[0];
            
            /*==================================*/
            if( pid_ptr->liEkVal[0] > pid_ptr->liEkVal[1] ){
                pid_ptr->Temp[0] = pid_ptr->liEkVal[0] - pid_ptr->liEkVal[1];
                pid_ptr->uEkFlag[0] = 0;
            }
            else{
                pid_ptr->Temp[0] = pid_ptr->liEkVal[1] - pid_ptr->liEkVal[0];
                pid_ptr->uEkFlag[0] = 1;
            }
            
            /*==================================*/
            pid_ptr->Temp[2] = pid_ptr->liEkVal[1]*2;
            if( (pid_ptr->liEkVal[0] + pid_ptr->liEkVal[2]) > pid_ptr->Temp[2] ){
                pid_ptr->Temp[2] = (pid_ptr->liEkVal[0] + pid_ptr->liEkVal[2]) - pid_ptr->Temp[2];
                pid_ptr->uEkFlag[2] = 0;
            }
            else{
                pid_ptr->Temp[2] = pid_ptr->Temp[2] - (pid_ptr->liEkVal[0] + pid_ptr->liEkVal[2]);
                pid_ptr->uEkFlag[2] = 1;
            }
            
            pid_ptr->Temp[0] = (uint32_t)(pid_ptr->uKP_Coe*pid_ptr->Temp[0]);         //KP*[E(k)-E(k-1)]
            pid_ptr->Temp[1] = (uint32_t)(pid_ptr->uKI_Coe*pid_ptr->liEkVal[0]);      //KI*E(k)
//            pid_ptr->Temp[1] = (uint32_t)(0.5*pid_ptr->liEkVal[0]);      //KI*E(k)
            pid_ptr->Temp[2] = (uint32_t)(pid_ptr->uKD_Coe*pid_ptr->Temp[2]);         //KD*[E(k-2)+E(k)-2E(k-1)]
                        
            /*===========计算KP*[E(k)-E(k-1)]的值=============*/
            if( pid_ptr->uEkFlag[0] == 0 )
                pid_ptr->PostSum += pid_ptr->Temp[0];
            else
                pid_ptr->NegSum  += pid_ptr->Temp[0];
            
            /*===========计算KI*E(k)==========================*/
            if( pid_ptr->uEkFlag[1]==0 )
                pid_ptr->PostSum += pid_ptr->Temp[1];
            else{
            }
            
            /*===========计算KD*[E(k-2)+E(k)-2E(k-1)]的值======*/
            if( pid_ptr->uEkFlag[2]==0 )
                pid_ptr->PostSum += pid_ptr->Temp[2];
            else
                pid_ptr->NegSum  += pid_ptr->Temp[2];
                        
            /*============计算U(k)================*/
            if( pid_ptr->PostSum > pid_ptr->NegSum ){
                pid_ptr->Temp[0] = pid_ptr->PostSum - pid_ptr->NegSum;
                if( pid_ptr->Temp[0] < 100 )                            //小于上限幅值为计算输出
                    pid_ptr->iPriVal = (uint16_t)pid_ptr->Temp[0];
                else
                    pid_ptr->iPriVal = 100;                             //否则上限幅值输出
            }
            else                                                        //控制量输出为负数,则输出0(下限幅值输出)
                pid_ptr->iPriVal = 0;
        }
    }
    else{
        pid_ptr->iPriVal = 0;
        pid_ptr->NegSum = 0;
        pid_ptr->PostSum = 0;
        pid_ptr->liEkVal[0] = 0;
        pid_ptr->liEkVal[1] = 0;
        pid_ptr->liEkVal[2] = 0;
        pid_ptr->Temp[0] = 0;
        pid_ptr->Temp[1] = 0;
        pid_ptr->Temp[2] = 0;
    }
}

void pid_set_target(pid_value_t *pid_ptr, uint16_t target)
{
    pid_ptr->iSetVal = target;
}

uint16_t pid_get_ctrl(pid_value_t *pid_ptr)
{
    return pid_ptr->iPriVal;
}

void pid_set_current(pid_value_t *pid_ptr,uint16_t value)
{
    pid_ptr->iCurVal = value;
}

void pid_var_rst(pid_value_t *pid_ptr)
{
    pid_ptr->NegSum = 0;
    pid_ptr->PostSum = 0;
    pid_ptr->liEkVal[0] = 0;
    pid_ptr->liEkVal[1] = 0;
    pid_ptr->liEkVal[2] = 0;
    pid_ptr->Temp[0] = 0;
    pid_ptr->Temp[1] = 0;
    pid_ptr->Temp[2] = 0;
}

void pid_set(pid_value_t *pid_ptr,float p,float i, float d)
{
    pid_ptr->uKP_Coe = p;
    pid_ptr->uKI_Coe = i;
    pid_ptr->uKD_Coe = d;
}

void pid_init(pid_value_t *pid_ptr,float p,float i, float d)
{
    rt_memset(pid_ptr,0,sizeof(pid_value_t));
    pid_ptr->uKP_Coe = p;
    pid_ptr->uKI_Coe = i;
    pid_ptr->uKD_Coe = d;
}
#endif

typedef struct PID
{
    float P,I,D,limit;
}PID;
typedef struct Error
{
    float Current_Error;//当前误差

    float Last_Error;//上一次误差

    float Previous_Error;//上上次误差
}Error;

/** 
* 位置式PID. 
* U(k)+KP*[E(k)-E(k-1)]+KI*E(k)+KD*[E(k)-2E(k-1)+E(k-2)].
* @param[in]   sptr :误差参数,pid: PID参数,NowPlace:当前位置,Point:预期位置
* @param[out]  无
* @retval  输出控制值
* @par 标识符 
*      保留 
* @par 其它 
*      无 
* @par 修改日志 
*      kun于2022-01-14创建 
*/
int32_t pid_realize(Error *sptr,PID *pid, int32_t now_place, int32_t point)
{	
	int32_t iError, // 当前误差
    Realize;//实际输出

	iError = point - now_place; // 计算当前误差

	sptr->Current_Error += pid->I * iError; // 误差积分

	sptr->Current_Error = sptr->Current_Error > pid->limit ? pid->limit:sptr->Current_Error;//积分限幅

	sptr->Current_Error = sptr->Current_Error < -pid->limit? -pid->limit:sptr->Current_Error;

	Realize = pid->P * iError//比例P

	+ sptr->Current_Error//积分I

	+ pid->D * (iError - sptr->Last_Error);//微分D

	sptr->Last_Error = iError;// 更新上次误差

	return Realize; // 返回实际值
}

/** 
* 增量式PID. 
* U(k)+KP*[E(k)-E(k-1)]+KI*E(k)+KD*[E(k)-2E(k-1)+E(k-2)].
* @param[in]   sptr :误差参数,pid: PID参数,NowPlace:当前位置,Point:预期位置
* @param[out]  无
* @retval  输出控制值
* @par 标识符 
*      保留 
* @par 其它 
*      无 
* @par 修改日志 
*      kun于2022-01-14创建 
*/
int32_t pid_increase(Error *sptr, PID *pid, int32_t now_place, int32_t point)
{
    int32_t iError, //当前误差

    Increase; //最后得出的实际增量

    iError = point - now_place; // 计算当前误差

    Increase = pid->P * (iError - sptr->Last_Error)//比例P

    + pid->I * iError //积分I

    + pid->D * (iError - 2 * sptr->Last_Error + sptr->Previous_Error); //微分D

    sptr->Previous_Error = sptr->Last_Error; // 更新前次误差

    sptr->Last_Error = iError; // 更新上次误差

    return Increase; // 返回增量
}
 	pid_set_current( &fryer_ptr->pid,fryer_ptr->temp );	//设置目标温度
	pid_operation( &fryer_ptr->pid );					//PID计算
	ctrl = pid_get_ctrl(&fryer_ptr->pid);				//获取控制输出值百分比

ctrl为百分比输出值,用于控制大功率设置不能频率启动与停止,设置为10秒一个控制周期

你可能感兴趣的:(单片机,stm32,51单片机,嵌入式硬件,物联网)