紫外工控论坛

 找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 4877|回复: 2

[VB/VB.NET] PID 的模拟程序 VB 含源码

[复制链接]
叮叮咚咚 发表于 2011-6-2 00:47:41 | 显示全部楼层 |阅读模式
  1. Option Explicit
  2. '
  3. ' Simple PID Loop Simulator ... for educational use.
  4. '
  5. ' By Max Seim, mlseim@mmm.com
  6. ' Systems Control Technician, 3M Company, Cottage Grove, Minnesota
  7. '
  8. Dim invalve As Integer
  9. Dim outvalve As Integer
  10. Dim mode As Integer '0=manual, 1=auto
  11. Dim error As Integer
  12. Dim stability As Integer '0=stable, 1=unstable
  13. Dim supply As Integer
  14. Dim x As Integer
  15. Dim y As Integer
  16. Dim gain As Long
  17. Dim reset As Long
  18. Dim rate As Long
  19. Dim output As Integer
  20. Dim pv As Long
  21. Dim s1 As Integer
  22. Dim s2 As Integer
  23. Dim outgraph(100) As Integer
  24. Dim pvgraph(100) As Integer
  25. Dim inputd As Long
  26. Dim inputdf As Long
  27. Dim inputlast As Long
  28. Dim feedback As Long
  29. Dim dfilter As Long
  30. Dim sp As Long

  31. Private Sub Command1_Click() ' Go into MANUAL control
  32. Shape12.FillStyle = 0
  33. Shape13.FillStyle = 1
  34. mode = 0
  35. HScroll2.Enabled = True
  36. End Sub

  37. Private Sub Command2_Click() ' Go into AUTO control
  38. Shape12.FillStyle = 1
  39. Shape13.FillStyle = 0
  40. mode = 1
  41. HScroll2.Enabled = False
  42. End Sub

  43. Private Sub Command3_Click() ' Toggle the Unstable Water Supply
  44. '
  45. If stability = 0 Then
  46. Shape19.FillStyle = 0
  47. stability = 1
  48. Exit Sub
  49. End If
  50. If stability = 1 Then
  51. Shape19.FillStyle = 1
  52. stability = 0
  53. supply = 2000
  54. End If
  55. Label38.Caption = supply
  56. End Sub

  57. Private Sub Form_load()
  58. ' center the form
  59. Form1.Left = (Screen.Width / 2) - (Form1.Width / 2)
  60. Form1.Top = (Screen.Height / 2) - (Form1.Height / 2)

  61. ' Initialize the Sliders and other variables
  62. VScroll1.Value = 0
  63. Shape1(1).Top = (3100 - VScroll1.Value) + 1040
  64. Shape1(1).Height = VScroll1.Value
  65. Shape1(0).Top = (3100 - pv) + 1030
  66. Shape1(0).Height = pv
  67. Label12.Caption = VScroll1.Value

  68. supply = 2000
  69. Label38.Caption = supply
  70. HScroll1.Value = 100
  71. Label13.Caption = HScroll1.Value
  72. invalve = (HScroll1.Value * (supply / 100)) / 60

  73. outvalve = 0
  74. Label14.Caption = 0
  75. Text1.Text = pv

  76. Shape12.FillStyle = 1
  77. Shape13.FillStyle = 0
  78. mode = 1
  79. HScroll2.Enabled = False

  80. gain = Text2.Text
  81. reset = Text3.Text
  82. rate = Text4.Text
  83. VScroll1.Value = 1500
  84. Shape1(1).Top = (3100 - VScroll1.Value) + 1040
  85. Shape1(1).Height = VScroll1.Value
  86. Label12.Caption = VScroll1.Value
  87. sp = VScroll1.Value

  88. Picture1.Cls
  89. Picture1.ScaleMode = 3
  90. Picture1.ScaleHeight = 3105
  91. Picture1.ScaleWidth = 100
  92. Picture1.AutoRedraw = True
  93. Picture1.ForeColor = vbCyan
  94. Picture1.DrawStyle = 0
  95. Picture1.DrawWidth = 2

  96. Picture2.Cls
  97. Picture2.ScaleMode = 3
  98. Picture2.ScaleHeight = 105
  99. Picture2.ScaleWidth = 100
  100. Picture2.AutoRedraw = True
  101. Picture2.ForeColor = vbRed
  102. Picture2.DrawStyle = 0
  103. Picture2.DrawWidth = 2
  104. End Sub

  105. Private Sub HScroll1_Change() ' Slider control for INLET VALVE POSITION
  106. Label13.Caption = HScroll1.Value
  107. invalve = (HScroll1.Value * (supply / 100)) / 60
  108. End Sub

  109. Private Sub HScroll2_Change() ' Slider control for OUTLET VALVE POSITION
  110. Label14.Caption = HScroll2.Value
  111. outvalve = (HScroll2.Value * 30) / 60
  112. End Sub

  113. Private Sub mnExit_Click() ' Exit program
  114. Erase outgraph
  115. Erase pvgraph
  116. Unload Me
  117. End Sub

  118. Private Sub mnInstructions_Click() ' Show instruction form
  119. instructions.Show
  120. End Sub

  121. Private Sub Timer1_Timer()
  122. On Error Resume Next
  123. If Text2 < 0 Then
  124. Text2 = 0
  125. End If
  126. If Text2 > 100 Then
  127. Text2 = 100
  128. End If
  129. gain = Val(Text2)
  130. If Text3 < 0 Then
  131. Text3 = 0
  132. End If
  133. If Text3 > 120 Then
  134. Text3 = 120
  135. End If
  136. reset = Val(Text3)
  137. If Text4 < 0 Then
  138. Text4 = 0
  139. End If
  140. If Text4 > 120 Then
  141. Text4 = 120
  142. End If
  143. rate = Val(Text4)

  144. If pv < 3101 Then
  145. pv = pv + invalve
  146. End If
  147. If pv > 0 Then
  148. pv = pv - outvalve
  149. End If
  150. Text1.Text = pv
  151. error = sp - pv
  152. Label30.Caption = error
  153. Label38.Caption = supply
  154. tank
  155. If stability = 1 Then
  156. watersupply
  157. invalve = (HScroll1.Value * (supply / 100)) / 60
  158. End If
  159. If mode = 1 Then
  160. pidloop
  161. End If

  162. ' Graph the PV, Process Variable
  163. Picture1.Cls
  164. pvgraph(100) = pv
  165. For x = 0 To 99
  166. pvgraph(x) = pvgraph(x + 1)
  167. Picture1.PSet (x, 3000 - (pvgraph(x)))
  168. Next x

  169. ' Display the SP line (yellow)
  170. Picture1.Line (0, 3000 - sp)-(100, 3000 - sp), vbYellow

  171. ' Graph the OUTPUT VALVE position
  172. Picture2.Cls
  173. outgraph(100) = outvalve
  174. For x = 0 To 99
  175. outgraph(x) = outgraph(x + 1)
  176. Picture2.PSet (x, 100 - (outgraph(x) * 2))
  177. Next x
  178. End Sub

  179. Private Sub VScroll1_Change() ' Slider control for SP (setpoint)
  180. Shape1(1).Top = (3100 - VScroll1.Value) + 1040
  181. Shape1(1).Height = VScroll1.Value
  182. Label12.Caption = VScroll1.Value
  183. sp = VScroll1.Value
  184. End Sub
  185. Private Sub tank() ' Draw the water tank animation
  186. If HScroll1.Value > 0 Then
  187. Shape7.FillStyle = 0
  188. Shape8.FillStyle = 0
  189. Shape9.FillStyle = 0
  190. Else: Shape7.FillStyle = 1
  191. Shape8.FillStyle = 1
  192. Shape9.FillStyle = 1
  193. End If
  194. If pv > -1 Then
  195. Shape1(0).Top = (3100 - pv) + 1030
  196. Shape1(0).Height = pv
  197. End If
  198. If (pv > 0) Or (HScroll1.Value > 0) Then
  199. Shape10.FillStyle = 0
  200. Else: Shape10.FillStyle = 1
  201. End If
  202. If (HScroll2.Value > 0) And (pv > 0) Then
  203. Shape11.FillStyle = 0
  204. Else: Shape11.FillStyle = 1
  205. End If
  206. End Sub
  207. Private Sub watersupply() ' Create an unstable water supply
  208. Randomize
  209. s1 = Int(Rnd(1) * 20 + 1)
  210. Randomize
  211. s2 = Int(Rnd(1) * 1000 + 1)
  212. If s2 < 100 Then
  213. supply = supply + s1
  214. End If
  215. If s2 > 900 Then
  216. supply = supply - s1
  217. End If
  218. If supply < 500 Then
  219. supply = 500
  220. End If
  221. If supply > 2500 Then
  222. supply = 2500
  223. End If

  224. End Sub

  225. Private Sub pidloop()
  226. dfilter = 10 ' Filter value to scale down derivative effect.
  227. inputd = pv + (inputlast - pv) * (rate / 60)
  228. inputlast = pv
  229. inputdf = inputdf + (inputd - inputdf) * dfilter / 60
  230. output = (sp - inputdf) * (gain / 100) + feedback
  231. If output > 100 Then ' clamp output valve between 0 and 100%
  232. output = 100
  233. End If
  234. If output < 0 Then
  235. output = 0
  236. End If
  237. HScroll2.Value = 100 - output ' Change slider value (AUTO MODE)
  238. Picture4.Width = HScroll2.Value * 20
  239. Label14.Caption = HScroll2.Value
  240. feedback = feedback - (feedback - output) * reset / 60
  241. End Sub

复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
习惯 发表于 2012-8-6 11:35:35 | 显示全部楼层
没人回复呢
Mr.Zhang 发表于 2013-10-5 20:17:23 | 显示全部楼层
学学。。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则


--------------------------------------------------------------------------------------------------------------------
本站是工控技术交流站点,论坛内容均为网络收集或会员所发表,并不代表本站立场,会员拥有该内容的所有权力及责任!
本站内容如有侵犯您的版权,请按下面方式联系本站管理员,我们将及时删除处理
管理员:冰糖 QQ:5483695(请直击主题), Mail:admin#ziwai.net(#改成@) 其它非本人.
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论!

QQ|Archiver|手机版|小黑屋|紫外工控论坛. ( 苏ICP备11032118号-1 )

GMT+8, 2024-4-27 03:24 , Processed in 0.437498 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表