Essentials of Mechatronics ISBN: 0-471-72341-X- ©John Billingsley 2006 - published John Wiley & Sons, Inc
CLS
'Clear screen - to black
COLOR 15 'Text color white
PRINT "Plot of `Leaky Tank'"
PRINT
INPUT "Initial level - 0 to 40 (try 0 first) "; h
INPUT "Input U, 0 to 20 (try 20
first) "; u
k = .5 'The leak rate
dt = .01 'Edit to try various
values of steplength dt
SCREEN 12 'Defines a gray panel on which
to plot
VIEW (1, 1)-(638, 328), 8, 12
WINDOW (-.5, 0)-(5.5, 40) 'Sets the panel coordinate
scale
DO
h = h + (-k * h + u) * dt
'This is the simulation
t = t + dt
PSET (t,
h)
'This displays the result as dots
LOOP UNTIL t > 5
Private Sub Form_Load()
Show
'displays the form
ZOrder (0)
'puts the form on top
Scale (-0.5, 40)-(5.5, 0) 'Sets the scale for
plotting
SetFocus
'Gives 'focus' to the form
Cls
Form1.CurrentY =
35 'where
to print
Print "Plot of `Leaky Tank "
Print
h =
0
'values are written into the program
u =
20
'instead of being input from keyboard
Print "Initial level = 0 "
Print "Input U =20"
k = 0.5 'value of the leak
dt = 0.01 'Edit to try various values of
steplength dt
Do
h = h + (-k * h + u) * dt
'This is the simulation
t = t + dt
PSet (t,
h)
'This displays the result as dots
Loop Until t > 5
End Sub
Public inkey$
Private Sub Form_KeyPress(KeyAscii As Integer)
inkey$ = Chr(KeyAscii)
End Sub
Private Sub Form_Load()
Show
Cls
f = 2 'feedback
d = 2 'damping
Scale (-40, 10)-(40, -10)
Line (-40, 0)-(40, 0), vbBlue
Line (0, -10)-(0, 10), vbBlue
SetFocus
Print "Simulation of closed-loop system with `live input'"
Print "Phase-plane is shown - velocity against position"
Print
Print "Press number keys 0 to 9 to change the target, q to quit"
dt =
0.05 'Make dt
larger to speed up running
b = 0.1
x =
40
'Initial values
v = 0
t = 0
Target = 0 'until a
key is pressed
PSet (x, v)
'move to the first point
Do
a$ = inkey$ 'but
in this case inkey$ is a variable
inkey$ = "" 'so
we clear it after using it
If a$ <> "" Then Target = 4 *
Val(a$) - 20
u = f * (Target - x) - d *
v 'u is
determined by feedback
x = x + v *
dt
'This is the simulation
v = v + b * u *
dt
t = t + dt
Line -(x,
v)
'This displays the result
DoEvents
Loop Until a$ = "q"
End
End Sub