Essentials of Mechatronics  ISBN: 0-471-72341-X- ©John Billingsley 2006 - published John Wiley & Sons, Inc

3. Converting QB to Visual Basic

If you follow the link to the simulation examples, then follow more links to the first QB example, you will find the following code.  It is a very simple-minded simulation of water running into a tank, while a leak in the bottom allows water to run out at a rate proportional to the depth.

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


The 'screen' command sets graphics mode, while the 'window' line sets the coordinate scale.

When we transport the code to Visual Basic 6, the area on which a plot will appear is called a 'form'.  We can associate the code with the form, so that it will be executed when the form is loaded.  The code is located within the 'sub' Form_Load.

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


You can find this code here.

In QB, keystrokes can be used as inputs to the process as it is running, using the inkey$ function.  This does not exist in Visual Basic, but we can create the same effect by capturing the keystrokes as 'events' in the form.

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

Find this code here.