【#文档大全网# 导语】以下是®文档大全网的小编为您整理的《用VB编写PID程序 一款很好的PID学习实例》,欢迎阅读!
vb PID 调节算法
Public Type PID
ParP As Single '比例系数 ParI As Single '积分系数 ParD As Single '微分系数 OP As Double
PvMin As Double '给定值下限 PvMax As Double '给定值上限 SvMin As Double '设定值下限 SvMax As Double '设定值上限 OpMin As Double '输出值下限 OpMax As Double '输出值上限 ChangeMin As Double '死区范围 ChangeMax As Double '调节范围 OutPutMax As Double '超调时输出 CtlDirection As Long '调节方向
ControlT As Double 'PID设备调控中期 PvRange As Double 'PV值的量程
OutDx As Double 'PID计算出的输出增加值 SvPvdx As Double SvPvdx1 As Double SvPvdx2 As Double Lvbo As Integer End Type
Public Function PIDCALC(ByVal Sv As Double, ByVal Pv As Double, PIDC As PID) As Double On Error Resume Next
If PIDC.ControlT = 0 Then PIDC.ControlT = 1 '确保控制周期不为零 PIDC.PvRange = PIDC.PvMax - PIDC.PvMin
If PIDC.PvRange <= 0 Then PIDC.PvRange = 1 '计算pv值的量程,并确保其不为零
PIDC.ChangeMax = PIDC.PvRange * PIDC.ChangeMax / 100 '把超调值由百分比转换成实际物理量
If Pv < PIDC.PvMin Then Pv = PIDC.PvMin 'pv值和sv值必须设定在设定的限值范围内 If Pv > PIDC.PvMax Then Pv = PIDC.PvMax If Sv < PIDC.PvMin Then Sv = PIDC.PvMin If Sv > PIDC.SvMax Then Sv = PIDC.SvMax
'计算sv和pv的差值,第一次计算时,取SvPvdx1、SvPvdx2值和SvPvdx相同 PIDC.SvPvdx = Sv - Pv
'初始设定SvPvdx1的初值为9999;标记是否第一次进行pid计防止SvPvdx和SvPvdx2的值为0
If PIDC.SvPvdx1 = 9999 Then PIDC.SvPvdx1 = PIDC.SvPvdx PIDC.SvPvdx2 = PIDC.SvPvdx
End If
'进行PID运算,ParI=0 和 ParI<>0的算法不一样
'ControlT :调节周期,和设备的采集周期相同(单位为:秒) If PIDC.ParI <> 0 Then
PIDC.OutDx = PIDC.ParP * (PIDC.SvPvdx - PIDC.SvPvdx1 + PIDC.SvPvdx * PIDC.ControlT / PIDC.ParI + _
(PIDC.SvPvdx - 2 * PIDC.SvPvdx1 + PIDC.SvPvdx2) * PIDC.ParD / PIDC.ControlT) Else
PIDC.OutDx = PIDC.ParP * (PIDC.SvPvdx - PIDC.SvPvdx1 + (PIDC.SvPvdx - 2 * PIDC.SvPvdx1 + PIDC.SvPvdx2) * PIDC.ParD / PIDC.ControlT) End If
PIDC.SvPvdx2 = PIDC.SvPvdx1 PIDC.SvPvdx1 = PIDC.SvPvdx '处在死区输出值保持不变
If Abs(PIDC.SvPvdx) < Abs(PIDC.ChangeMin) Then PIDC.OutDx = 0 End If
If PIDC.CtlDirection = 1 Then
PIDC.OP = PIDC.OP + PIDC.OutDx '正向控制时的超调处理
If Abs(PIDC.SvPvdx) > PIDC.ChangeMax And PIDC.ChangeMax > 0 Then If PIDC.SvPvdx > 0 Then PIDC.OP = PIDC.OutPutMax End If End If Else
PIDC.OP = PIDC.OP - PIDC.OutDx '反向控制时的超调处理
If Abs(PIDC.SvPvdx) >= PIDC.ChangeMax And PIDC.ChangeMax > 0 Then If PIDC.SvPvdx < 0 Then PIDC.OP = PIDC.OutPutMax End If End If End If
'OP值必须在设定的限值范围之内 If PIDC.OP < PIDC.OpMin Then PIDC.OP = PIDC.OpMin End If
If PIDC.OP > PIDC.OpMax Then PIDC.OP = PIDC.OpMax End If
End Function
函数 PIDCALC 计算PID增量OP
本文来源:https://www.wddqxz.cn/348f9a29bd64783e09122b28.html