|
楼主 |
发表于 2014-9-8 12:30:24
|
显示全部楼层
自己写的S7-200的pid控制算法
Network 2
// 参数配置
// TI_KC = 1 TI项 乘以 KC
//
// TD_KC = 1 TD项 乘以 KC
//
LD SM0.0
S L48.0, 1
S L48.1, 1
Network 3
// 数据类型变换
LD SM0.0
ITD LW10, AC0
DTR AC0, LD24
ITD LW12, AC0
DTR AC0, LD28
ITD LW4, AC0
DTR AC0, LD32
/R 100.0, LD32
Network 4
// e = SV - PV
LD SM0.0
-I LW2, LW0
ITD LW0, AC0
DTR AC0, LD40
Network 5
// P TEMP_OUT = e* KC 缩小100倍
LD SM0.0
MOVR LD40, LD36
*R LD32, LD36
Network 6
// TI 项初始化 PID_SumError初始化
LD SM0.0
AR= LD18, 0.0
MOVR LD40, LD18
Network 7
// TI 缩小十倍 //PID_SumError ++ E*10.0/TI
LD SM0.0
LPS
ITD LW6, AC0
DTR AC0, AC1
MOVR LD40, AC0
*R 10.0, AC0
/R AC1, AC0
A L48.0
*R LD32, AC0
+R AC0, LD18 //PID_MX ++ //乘以KC
LPP
AN L48.0
+R AC0, LD18 //PID_MX ++ //不乘以KC
Network 8
// TI 超限制控制
LD SM0.0
LPS
AR> LD18, LD28
MOVR LD28, LD18
LPP
AR< LD18, LD24
MOVR LD24, LD18 //限制幅度
Network 9
// TEMP_OUT = KC + F( TI )
LD SM0.0
+R LD18, LD36
Network 10
// TD 项初始化 PID_LastError
LD SM0.0
AR= LD14, 0.0
MOVR LD40, LD14
Network 11
// TD 缩小十倍
LD SM0.0
MOVR LD40, AC0
-R LD14, AC0
MOVR LD40, LD14
ITD LW8, AC1
DTR AC1, AC2
*R AC2, AC0 // TD*E-
/R 10.0, AC0
MOVR AC0, LD44
//默认不乘以KC
A L48.1
*R LD32, LD44 //乘以KC
Network 12
// TEMP_OUT = KC + F( TI ) + F( TD) 含超限制控制
LD SM0.0
LPS
+R LD44, LD36
AR> LD36, LD28
MOVR LD28, LD36
LPP
AR< LD36, LD24
MOVR LD24, LD36
Network 13
// 输出
LD SM0.0
ROUND LD36, AC0
DTI AC0, LW22
C语言的
//---------------------------------------------------
//---------------------------------------------------
float PIDCalc(float PV,float SV,_PIDCalc_Data * PIDCalc_Data ,_PID * PID)
{
float OutPut;
float Error = SV - PV; // e = SV - PV
OutPut = (float)Error * PID->Kc;// OutPut = e* Kc
PIDCalc_Data->MX += (float)Error * PID->Kc/PID->Ti; //
//MX 项超限制处理
if(PIDCalc_Data->MX < PIDCalc_Data->OutPut_MINI )//
{
PIDCalc_Data->MX = PIDCalc_Data->OutPut_MINI;
}
if(PIDCalc_Data->MX > PIDCalc_Data->OutPut_MAX )
{
PIDCalc_Data->MX = PIDCalc_Data->OutPut_MAX;
}
OutPut += PIDCalc_Data->MX;// OutPut = e* Kc + Kc * f(e Ti)
//TD 项初始化 PID_LastError
if(PIDCalc_Data->LastPV == 0)//初次计算,将当前PV置入LastPV.
{
PIDCalc_Data->LastPV = PV;
}
OutPut += (PV - PIDCalc_Data->LastPV) * PID->Kc * PID->Td;// OutPut = e* Kc + Kc * f(e Ti) + Kc * f(e Td)
PIDCalc_Data->LastPV = PV; //记录 PV
// OutPut 超限制处理
if(OutPut < PIDCalc_Data->OutPut_MINI )
{
OutPut = PIDCalc_Data->OutPut_MINI;
}
if(OutPut > PIDCalc_Data->OutPut_MAX )
{
OutPut = PIDCalc_Data->OutPut_MAX;
}
return OutPut;
}
|
|