发表于:2005-04-23 20:49:00
楼主
Option Explicit
Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal crColor As Long) As Long
Dim EK1 As Single, EK2 As Single, Aa As Integer
Dim U As Single
Dim Uk As Single, Uk1 As Single, A As Single, B As Single, C As Single, Ek As Single
Dim Ti As Integer, Td As Integer, Kp As Integer
Dim SP As Single, PV As Single
Const Yellow = &HFF&
Dim Cnt1 As Byte, Cnt2 As Byte
Private Sub Command1_Click()
Timer1.Enabled = True
End Sub
Private Sub Command2_Click()
Timer1.Enabled = False
Feedback.Text = 0
Uk = 0
Aa = 0
Text7.Text = 0
Form1.Refresh
End Sub
Private Sub Timer1_Timer()
Aa = Aa + 1
Ti = Val(Integral) '读取积分时间值
Td = Val(Deviate) '读取微分时间值
Kp = Val(Proportion) '读取比例值
SP = Val(Objvalue) '设定值
PV = Val(Feedback) '模拟的反馈值
Ek = SP - PV '差值
If Ti = 0 Then
A = Kp * (1 + Td)
Else
A = Kp * (1 + 1 / Ti + Td)
End If
B = Kp * (1 + 2 * Td)
C = Kp * Td
Uk = Uk1 + A * Ek - B * EK1 + C * EK2 '输出,如进行电机控制
If Int(Uk) <= 0 Then Uk = 0 '对输出进行调理,实际使用时灵活掌握。
If Int(Uk) > 1000 Then Uk = 1000
Uk1 = Uk
EK2 = EK1 '上上次偏差
EK1 = Ek '上次偏差
Text7.Text = Uk '输出显示
SetPixel Me.hdc, Aa, Val(Feedback), Yellow'绘制响应曲线
End Sub