⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pid_vp_c1.frm

📁 利用VB开发的典型pid控制程序
💻 FRM
📖 第 1 页 / 共 2 页
字号:
      Height          =   300
      Left            =   2128
      TabIndex        =   6
      Top             =   1095
      Width           =   705
   End
   Begin VB.Label Label4 
      AutoSize        =   -1  'True
      BackColor       =   &H00FFFFFF&
      BorderStyle     =   1  'Fixed Single
      Caption         =   "Label4"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   10.5
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00FF0000&
      Height          =   300
      Left            =   1138
      TabIndex        =   5
      Top             =   1100
      Width           =   705
   End
   Begin VB.Label Label3 
      AutoSize        =   -1  'True
      BackStyle       =   0  'Transparent
      Caption         =   "当前温度"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   10.5
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00000000&
      Height          =   210
      Left            =   1138
      TabIndex        =   4
      Top             =   795
      Width           =   840
   End
   Begin VB.Label Label2 
      AutoSize        =   -1  'True
      BackColor       =   &H00FFFFFF&
      BorderStyle     =   1  'Fixed Single
      Caption         =   "Label2"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   10.5
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00FF0000&
      Height          =   300
      Left            =   238
      TabIndex        =   3
      Top             =   1095
      Width           =   705
   End
   Begin VB.Label Label1 
      Alignment       =   2  'Center
      AutoSize        =   -1  'True
      BackStyle       =   0  'Transparent
      Caption         =   "设定温度"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   10.5
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ForeColor       =   &H00000000&
      Height          =   210
      Left            =   148
      TabIndex        =   2
      Top             =   795
      Width           =   855
   End
   Begin VB.Menu Calc1 
      Caption         =   "文件(&F)"
      Begin VB.Menu Save1 
         Caption         =   "保存(&S)"
         Shortcut        =   ^S
      End
      Begin VB.Menu print 
         Caption         =   "打印(&P)"
         Shortcut        =   ^P
      End
      Begin VB.Menu quit 
         Caption         =   "退出(&Q)"
         Shortcut        =   ^Q
      End
   End
   Begin VB.Menu edit 
      Caption         =   "编辑(&E)"
      Begin VB.Menu refresh 
         Caption         =   "刷新(&R)"
         Shortcut        =   ^R
      End
      Begin VB.Menu pause 
         Caption         =   "暂停(&T)"
         Shortcut        =   ^T
      End
      Begin VB.Menu aixdesign 
         Caption         =   "坐标设置(&A)"
         Shortcut        =   ^A
      End
   End
   Begin VB.Menu help 
      Caption         =   "帮助(&H)"
      Begin VB.Menu aboutsystem 
         Caption         =   "关于本系统"
      End
   End
End
Attribute VB_Name = "温度控制系统"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'PID_VP_C1: a program by the PID algorithm with variable parameters. 2005/7/12.
'The variable gains are piecewise constants.
'采样周期T=5S。
'被控对象为电烙铁:
'G1(s)=1.24exp(-130s)/1+680s
'或G2(s)=1.389exp(-160s)/1+640s
'或G3(s)=1.60exp(-180s)/1+620s
'或G10(s)=1.5exp(-170s)/(680s+1)
Option Explicit
'Const Kp = 1.309, Kip = 0.00779, Kdp = 1.25  'G2,138/49.00, 50.50-50.00
'Const Kp = 1.196, Kip = 0.00779, Kdp = 1.25  'G2,167/49.00, 50.05-50.00
'Const Kp = 0.975, Kip = 0.008, Kdp = 1.25    'G3,157/49.00,50.49-50.00
'Const Kp = 0.5, Kip = 0.008, Kdp = 1.25      'G3,490/49.00,-50.00;  R,228/49.19,50.40-49.92
'Const Kp = 1.22, Kip = 0.00728, Kdp = 1.5    'G10,146,50.50-50.0,33.21
Private Kp, Kip, Kdp As Single
Const r = 50#                      '温度设定值
Const Ts = 5000                    '采样周期 Ts=5S
Private Temp, y(2), yo, u(52), e(3), Max, Min, er As Single
Private W As Long
Private MaxTemp, MaxTime, t, Tr, DataForm As Integer
Private x0, x1, y0, y1 As Single
Private mydate, mytime As Date
Private k, flag As Integer

Private Sub Form_Load()
  MaxTime = 2400                                 '2400,3600
  MaxTemp = 100
  Call DrawXY(MaxTime, MaxTemp)                  '!!!
  MSFlexGrid1.ColAlignment(0) = 0
  MSFlexGrid1.ColAlignment(1) = 0
  MSFlexGrid1.ColAlignment(2) = 0
  MSFlexGrid1.TextMatrix(0, 0) = "时间(拍)"    '"时间(秒)"
  MSFlexGrid1.TextMatrix(0, 1) = "控制量"
  MSFlexGrid1.TextMatrix(0, 2) = "温度(℃)"
  MSFlexGrid1.Rows = MSFlexGrid1.Rows + 1
 
  t = 0
  Tr = 0
  Label2.Caption = r & " ℃"
  'Label12.Caption = Kp & ", " & Kip & ", " & Kdp
  Label16.Left = 3000       '0
  Label15.Left = Me.Width   '-Me.Width

  'Timer3.Interval = Ts \ 25    '200ms!!!
  Timer3.Interval = Ts \ 100    '50ms!!!
  'Timer3.Interval = Ts \ 250   '20ms!!!
  Timer3.Enabled = True
  k = 0
  flag = 0
  Min = r
  Call Simulating
  Call PID
  Call MMI
End Sub

Private Sub Quiting_Click()
  Timer3.Enabled = False
  End
End Sub

Private Sub Simulating()
   'y(0) = 0.9922 * y(1) + 0.0108 * u(33)   '被控对象G2(s)=1.389exp(-160s)/1+640s
   'y(0) = 0.992 * y(1) + 0.0128 * u(37)    '被控对象G3(s)=1.60exp(-180s)/1+620s
   y(0) = 0.9927 * y(1) + 0.01099 * u(35)  '被控对象G10(s)=1.5exp(-170s)/(680s+1)
   y(1) = y(0)
   If k <= 1 Then yo = y(0)
End Sub

Private Sub PID()
  Dim i As Integer
  e(0) = r - y(0)
  er = e(0) / (r - yo)
  If er > 0.99999 Then                     '在第一阶段减小Kp,可减小上超调; 改变Kp可改变拐点.
     Kp = 2.77: Kip = 0.0018: Kdp = 1#
     'Kp = 2.76: Kip = 0.0018: Kdp = 1.25
     'Kp = 2.6: Kip = 0.0021: Kdp = 3#
  ElseIf er > 0.88 Then                    '在第二阶段增大Kp或减小Kip,可减小上超调; 但Kp太大,会增大下超调.
     Kp = 21.47: Kip = 0.009: Kdp = 1#
     'Kp = 18.75: Kip = 0.009: Kdp = 1.25
     'Kp = 4.55: Kip = 0.001: Kdp = 3#
  Else                                     '在第三阶段增大Kip或减小Kp,可减小下超调;
     Kp = 0.11: Kip = 0.04: Kdp = 1#
     'Kp = 0.11: Kip = 0.04: Kdp = 1.25
     'Kp = 0.11: Kip = 0.007: Kdp = 3#
  End If
  u(0) = u(1) + Kp * ((e(0) - e(1)) + Kip * e(0) + Kdp * (e(0) - 2 * e(1) + e(2)))
  If e(0) <= 0.004 Then flag = flag + 1   'i.e. y(0) >= r
  For i = 0 To 50
     u(51 - i) = u(50 - i)
  Next i
  For i = 0 To 1
     e(2 - i) = e(1 - i)
  Next i
End Sub

Private Sub timer3_timer()
   k = k + 1
   If k >= 32767 Then k = 0
   Call Simulating
   Call PID
   Call MMI
End Sub
   
Private Sub MMI()
  y(0) = Format(y(0), "###00.00")         '数据显示格式
  u(0) = Format(u(0), "###000.00")
  Call CurveGenerator(t, y(0))               '!!!
  MSFlexGrid1.TextMatrix(Tr + 1, 0) = k
  MSFlexGrid1.TextMatrix(Tr + 1, 1) = u(0)
  MSFlexGrid1.TextMatrix(Tr + 1, 2) = y(0)
  MSFlexGrid1.Rows = MSFlexGrid1.Rows + 1
  t = t + Ts \ 1000                          '!!!
  If t >= MaxTime Then Call goon             'Refresh!!!
  Tr = Tr + 1
  Label4.Caption = y(0) & "℃"
  If (y(0) > 1.02 * r Or y(0) < 0.98 * r) Then Label10.Caption = k + 1 & " 拍" '!!!
  If y(0) > Max Then Max = y(0)
  Label6.Caption = Max & "℃"                '!!!
  If flag = 1 Then Min = y(0)
  If (flag >= 1 And y(0) <= Min) Then Min = y(0)
  Label8.Caption = Min & "℃"                '!!!
  'Label19.Caption = flag
  Label12.Caption = Kp & ", " & Kip & ", " & Kdp
End Sub

Private Sub CurveGenerator(ByVal x1 As Single, ByVal y1 As Single)
  If x1 = 0 Then
     x0 = 0
     y0 = y1
  End If
  Picture1.DrawStyle = 0                     '线型为实线
  Picture1.DrawWidth = 2                     '控制线的粗细,1,2,3...
  If (x0 < x1) Then
    Picture1.Line (x0, y0)-(x1, y1), vbRed   'vbBlue
  End If
  x0 = x1
  y0 = y1
End Sub

Public Sub DrawXY(ByVal MaxX As Integer, ByVal MaxY As Integer)
  Dim DeltaX As Integer
  Dim DeltaY As Integer
  Dim i As Integer
  Dim pos As Integer
  
  DeltaX = Round(MaxX / 30)                 'Round(MaxX / 20)
  DeltaY = Round(MaxY / 11)
  Picture1.Scale (-DeltaX, MaxY + DeltaY)-(MaxX + DeltaX, -DeltaY)
  Picture1.DrawWidth = 2                     '控制线的粗细,1,2,3...
  Picture1.DrawStyle = 0
  Picture1.Line (0, 0)-(MaxX, 0), vbBlack
  Picture1.Line (0, 0)-(0, MaxY), vbBlack
  
  '画纵向网格线
  Picture1.DrawWidth = 1                     '控制线的粗细,1,2,3..
  Picture1.DrawStyle = 2                     '线型为点线
  For pos = 0 To MaxX
     If pos Mod Round(MaxX / 30) = 0 Then           '30或20条线
        Picture1.Line (pos, 0)-(pos, MaxY), vbBlue  '画长线
        Picture1.CurrentX = pos
        Picture1.CurrentY = 0
        Picture1.Print CStr(pos \ 60)
     ElseIf pos Mod Round(MaxX / 100) = 0 Then
        Picture1.Line (pos, 0)-(pos, Round(MaxY / 100)), vbBlue '画短线
     End If
  Next
  Picture1.CurrentX = MaxX / 1.06
  Picture1.CurrentY = -0.05 * MaxY
  Picture1.Print CStr("时间(分)")             '("时间(S)")
  
  '画横向网格线
  For pos = 1 To MaxY
     If pos Mod Round(MaxY / 20) = 0 Then
         If pos = r Then
            Picture1.DrawStyle = 2                    '线型为点线
            Picture1.Line (0, r)-(MaxX, r), vbRed     '给定温度
            Picture1.CurrentX = -MaxX / 30
            Picture1.CurrentY = pos * 1.03
            Picture1.Print CStr(pos)
         Else
           Picture1.DrawStyle = 2
           Picture1.Line (0, pos)-(MaxX, pos), vbBlue
           Picture1.CurrentX = -MaxX / 30
           Picture1.CurrentY = pos * 1.03
           Picture1.Print CStr(pos)
         End If
     End If
     If pos Mod Round(MaxY / 100) = 0 Then
          Picture1.Line (0, pos)-(Round(MaxX / 100), pos), vbBlue
     End If
  Next
  Picture1.CurrentX = -MaxX / 30
  Picture1.CurrentY = MaxY * 1.07
  Picture1.Print CStr("温度(℃)")
End Sub

Private Sub timer4_timer()
Label16.Left = Label16.Left - 40
Label15.Left = Label15.Left - 40
If Label16.Left < -Me.Width Then
  Label16.Left = Me.Width
Else
  If Label15.Left < -Me.Width Then
    Label15.Left = Me.Width
  End If
End If
End Sub

Private Sub timer5_timer()
mydate = Date
mytime = Time
Label13.Caption = mydate
Label14.Caption = mytime
End Sub

Private Sub goon()
  Picture1.Cls                '!!!
  Call DrawXY(MaxTime, MaxTemp)
  t = 0
End Sub

Private Sub Holding_Click()
  If Holding.Caption = "暂    停" Then
     Holding.Caption = "继续运行"
     Timer3.Enabled = False
  Else
    Holding.Caption = "暂    停"
    Timer3.Enabled = True
  End If
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -