Lab 7 Anemometer Interfacing using VB

advertisement
ENGSCI 232 Computer Systems 1
Lab 7: Anemometer Interfacing using VB
Introduction
In this laboratory, you will develop a program to measure and plot windspeed (or ‘car rotation
speed’) and acceleration using an anemometer (or car spinner), the parallel port interface board,
and your interfacing expertise. At the end of this lab, you will write and hand in Visual Basic
code to plot speed and acceleration, and a short (2 page) report describing the design approach
you took.
Preparation
On the T drive, inside Teaching\Courses\ENGSCI232SC, you will find a folder “LPT1 Project
with Plotting”. Copy this folder onto your own hard disk. You can then either work with this new
project, or you can copy and paste the plotting code into your own existing project. See the
comments in the code for help with this.
Anemometers
For this project, you will be using either a spinning car toy or a windmeasuring device known as an anemometer. The anemometers we are
using are designed by Midas for use on boats. We are most grateful to
Marine Instruments Ltd of Auckland for making these available to us.
Reed Switches
Anemometers measure wind speed by using cups to spin
magnets that pass over a reed switch, producing an on/off
output signal whose frequency varies with the wind speed.
The two wires coming out the anemometer are connected
to the ends of the reed switch. The car spinners work in a
similar way.
magnet
Group Work:
You will be working in pairs during this lab, and will
submit one code listing per group. However, each
individual will submit their own design report (see below).
It is hoped you will use your teamwork skills learnt in this
and other courses to ensure everyone contributes to
completing the tasks. If any issues arise that you cannot
resolve within the group, please see a staff member
immediately.
A Magnetic Reed Switch.
When a magnet moves over the
switch, it changes state.
Task:
Develop a VB program that displays both windspeed (in knots) (or spinner car speed, in cm/s)
and the rate of change in this data (the acceleration). Note that one rotation of the anemometer per
Page 1
Version 1.2
second corresponds to about 4 knots windspeed. (You can work out what one rotation is in cm/s
car speed, assuming a radius of 5 cm) This calibration figure should be easily customisable by the
programmer (not user).
Notes:
1. There are at least two (essentially ‘reciprocal’) approaches to measuring speed; be sure to
outline both in your report, and justify the choice you make.
2. You may need to add code to ignore “switch bounce”. Whenever a switch opens or closes, the
electrical contacts can bounce up and down, giving a sequence of very rapid open/close
changes known as switch bounce. These bounces can confuse your algorithm. To work
around switch bounce, you can code that ensures whenever you observe the switch change,
you wait (for, say, 5 or 10ms) before you look for further changes.
3. Your program should display the current speed and acceleration as text. This value should
update at least once every 2 seconds; faster updates are better (as long as they are reasonably
accurate).
4. You should also plot this data; update the plots at the same rate as the displayed text. You will
need to make sure you are familiar with arrays; remember they are just like vectors.
5. Add acceleration after you have the basic speed plot working
6. You should display the most significant digit of your speed on the 7-segment LED.
7. The plots do not need to have axis labels (unless you are keen).
8. Numerical differentiation can be tricky; give some thought to smoothing your output values if
you think this will help.
9. Your program should be well structured using many short subroutines, each with at least a 1line descriptive comment. Also comment any tricky pieces of code, and use clear variable
names.
10. Add some extension of your own choosing. For example, you might add something useful for
Americas cup usage or to keep children more happy with their spinners, or some
improvement to something overlooked in this spec.
Submission: Due Wed 5 Oct 9am in Lab
Each student should hand in their own brief design report (no more than 2 pages) that outlines the
approach you took, discusses trade-offs in different approaches, and justifies your design choices.
This document should be suitable for an engineer wanting to build their own speed measurement
software. Be sure to include a brief algorithm (no more than half a page) that details, in a general
way, how speed and acceleration can be determined from on/off switch-based inputs such as used
in this lab. Also include a circuit diagram of how the switch is configured to drive an input line in
the PC. Each pair of students should also hand in a listing of their finished code, and a printout of
their program’s form (preferably while running; use Alt-Print Screen to get a screen grab that you
can paste into Word.). Be sure to include the names (and IDs) of both students on the printout.
Your code will need to be ready for demo’ing at the start of the lab on the hand-in date above.
You will be marked on (1) meeting the core software objectives, (2) good software design, (3)
elegance in solving the design problems, (4) your extension, and (5) quality of your report.
Page 2
Version 1.2
VB Plotting Code
Option Explicit
' This is our global data for the values to be plotted; all values are between 0 and 1
' Because this PlotData() is defined outside any subroutine or function, we can
' access PlotData() from any code in this file.
' VB will initialise this array to zero; most languages don't do this
Const NumPlotPoints As Integer = 100
Dim PlotData(NumPlotPoints) As Double
Private Sub picPlot_Paint()
' This routine draws the sequence of points in PlotData into picPlot
' It is called automatically whenever the PictureBox on the form needs to be drawn
Dim colour As Long
Dim i As Integer, PlotXStep As Double, PlotYScale As Double, PlotYOffset As Double
' We'll plot in red
colour = RGB(255, 0, 0) ' being red
' Scale the points to leave a bit of blank either side (left and right)
PlotXStep = picPlot.Width / (NumPlotPoints + 2#)
' Scale the data points (each between 0 and 1) to nearly fill the box vertically
PlotYScale = picPlot.Height * -0.9
PlotYOffset = picPlot.Height * 0.95
' Plot an empty first point, and then draw the lines
picPlot.Line (PlotXStep, PlotYOffset + PlotData(1) * PlotYScale)-(PlotXStep,
PlotYOffset + PlotData(1) * PlotYScale), colour
For i = 2 To NumPlotPoints
picPlot.Line -(i * PlotXStep, PlotYOffset + PlotData(i) * PlotYScale), colour
Next i
End Sub
Private Sub DisplayNewPlotPoint(NewValue As Double)
' This routine takes a new value (between 0 and 1) and displays it as
' the right-most point on the plot, scrolling everything left
Dim i As Integer
' Shuffle data left in the array
For i = 1 To NumPlotPoints - 1
PlotData(i) = PlotData(i + 1)
Next i
' Add new point into end of array storage
PlotData(NumPlotPoints) = NewValue
' Cause the PlotPictureBox to redraw itself (VB erases, and then calls
PlotPictureBox_Paint)
picPlot.Refresh
End Sub
'================================================================
' The following subroutine illustrates how to use the
' plotting routines above.
'================================================================
Private Sub cmdTestPlotting_Click()
' As an illustration, add 20 random points to the plot
' Note that each point must have a value between 0 and 1.
Dim i As Integer
For i = 1 To 20
DisplayNewPlotPoint Rnd
Next i
End Sub
Notes:
To draw a line in the picture box picPlot from co-ords
(x1,y1) to (x2,y2) in colour c:
Dim c as long, x1 as integer, x2 as integer, _
y1 as integer, y2 as integer
c=RGB(255,0,0)
picPlot.Line (x1, y1)-(x2, y2), c
To draw a line in the picture box picPlot from the last point
drawn to (x2,y2) in colour c:
picPlot.Line -(x2, y2), c
Page 3
Version 1.2
Download