• 大功率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; // 返回增量
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
     	pid_set_current( &fryer_ptr->pid,fryer_ptr->temp );	//设置目标温度
    	pid_operation( &fryer_ptr->pid );					//PID计算
    	ctrl = pid_get_ctrl(&fryer_ptr->pid);				//获取控制输出值百分比
    
    • 1
    • 2
    • 3

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

  • 相关阅读:
    OpenLDAP支持SAMBA特性
    FAST-LIO(二):程序运行&代码注释
    【开源】SpringBoot框架开发陕西非物质文化遗产网站
    来自北大算法课的Leetcode题解:85. 最大矩形
    JavaEE(入门)
    D23-读论文D23&算法D23
    瑞云介绍使用ZBrush和Marmoset工具包制作的风格化巨怪战斗机
    Spring注解-3.自动装配
    数据库基本增删改查语法和多表联查的方式
    实用调试技巧
  • 原文地址:https://blog.csdn.net/dmjkun/article/details/133256146