Download Week #1 Laboratory Session

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Immunity-aware programming wikipedia , lookup

Emulator wikipedia , lookup

Virtual channel wikipedia , lookup

Opto-isolator wikipedia , lookup

Transcript
ME-191
Computer Applications in Mechanical Engineering II
Week #1 Laboratory Session—Hardware and Software Set-up
Lab Objectives:



Install LabJack drivers for MATLAB (also, Excel and LabView if desired)
Operate light emitting diodes (LED) using digital outputs
Monitor input states using digital inputs
Lab Equipment Required





Laptop with MATLAB/LabJack software installed
LabJack DAQ, DB25 cable, and break-out board
4 LEDs (red, green, yellow, and dual)
1 Hall-effect latching switch
1 Normally-open momentary push button switch
Installation of LabJack Drivers
It is strongly suggested that all files for this course be downloaded and saved on the computer’s
D: drive. If you have not yet installed the LabJack drivers, utilize the CD containing the drivers
(U12SetupV121.exe, and ME191_LabJack.zip) and install them to your computer, perhaps into a
subdirectory created for all ME191 files.
Unzip the zipped files into said directory. The LabJack files should be installed in a sub-directory
that MATLAB will find, i.e., under the MATLAB tree. Add this new directory to the search path
within MATLAB using the FileSet Path option from the menu in MATLAB. Make sure to
“Add with Subfolders…”
ME-191
Computer Applications in Mechanical Engineering II
Please note:
MATLAB will not work properly if files contain spaces or mathematical operators. Directories
and all sub-directories must also be free of spaces and operators. The spaces will not be ‘read’ by
MATLAB, and a mathematical operator such as a dash, for example, will tell MATLAB to do
the subtraction operation.
Incorrect:
d:\ME 191\Lab Files\week-1\program 1 b.m
Correct:
d:\ME191\LabFiles\week_1\program_1_b.m
The remaining LabJack drivers (LabView and Excel) can be installed using the setup routine
provide by LabJack and included on the CD, or at the LabJack website, but will not be required
for this course. If additional drivers and demos are desired, there are others freely available from
www.LabJack.com.
Introduction to calling LabJack functions from within MATLAB
Complete instructions for MATLAB related functions are included in a .pdf file found in the
directory you just created (MATLAB_Function_Calls.pdf).
LabJack can read/write either digital (1 or 0) or analog values. This exercise is focused on digital
devices. Analog will be covered next week. The syntax for digital inputs is:
[state Errorcode Idnum] = EDigitalIn(Idnum, demo, channel, readD)
where the left-hand arguments, state, Errorcode and Idnum, are parameters generated
by the input status queried in the right-hand command. The first argument called state is the
value of the device on a certain channel at the time the command is issued. Also, please note that
the left-hand arguments are not necessary in order to use the EDigitalIn function, i.e.,
EDigitalIn(Idnum, demo, channel, readD)
would work. But under normal circumstances, you probably do not need to know the
Errorcode or the Idnum, thus you could use only one left-hand argument to set a parameter
equal to whatever is read on a particular channel. For example, the commands
channel = 9;
switch9 = EDigitalIn(Idnum, demo, channel, readD);
read the state of the hardware connected to channel 9 and sets the parameter switch9 equal to
that value (either a 1 or a 0) and lets the program know whether the switch has been depressed or
not.
LabJack also allows for writing states to the outputs, for example turning lights on and off. The
syntax for the turning on a light, for example, connected to channel 3 is:
ME-191
Computer Applications in Mechanical Engineering II
channel = 3
state = 0
EDigitalOut(Idnum, demo, channel, writeD, state)
Then, to turn it back off, you could use:
channel = 3
state = 1
EDigitalOut(Idnum, demo, channel, writeD, state)
Another option to do the same job would be:
EDigitalOut(Idnum, demo, 3, writeD, 0)
EDigitalOut(Idnum, demo, 3, writeD, 1)
It is suggested that a standard bit of coding is used at the beginning of all m-files to set up the
LabJack hardware. For instance:
Idnum=-1;
% -1 for first LabJack board found,
% or provide actual serial number
demo=0;
% use 0 for normal operation,
% non-zero for demo mode
writeD=1;
% use 0 for I/O (red LabJack board),
% or 1 for DB25 (circuit board)
readD=1;
% use 0 for I/O (red board), or 1 for DB25
%(circuit board) connectors
channel=0;
% channels 0-3 for I/O (red LabJack board),
% and 0-15 for DB25 (circuit board)
state=0;
% use 0 for low, >0 for high outputs
sets all the variables for use by EDigitalIn and EDigitalOut commands. You can always
change the settings using your code, but this bit of standard code in the beginning of your file
helps you to remember the settings required.
Analog and digital input and output (I/O) commands require that different states be set, e.g., the
writeD parameter. It is convenient to use the same variable name (i.e. demo) in all of your
function calls to the LabJack device. For example, if you wish to debug your software without
having the LabJack device connected to your laptop, set demo=1, and you may then run your mfile without generating errors from the LabJack function calls. You may find this much more
efficient than finding each function call and editing each one.
ME-191
Computer Applications in Mechanical Engineering II
Reading and writing digital I/O
1. Hardware set-up
The LabJack hardware includes several methods of acquiring digital I/O. There are four
digital I/O connections on the main device, and 16 additional lines of I/O on the breakout board. For this lab you will use the digital circuit board and connect LEDs and digital
switches at the appropriate points. Wire the four LEDs using Digital I/O (DIO) channels
0-3. Wire the mechanical switch input on DIO channel 8.
The LEDs share a common ground wire (black). The four additional wires each activate
one of the LEDs when the channel to which it is connected is set to HIGH (+5V). Note:
The LEDs may be connected directly to the breakout board because the board contains
resistors in series to correctly limit the current through the LED. However, please be
certain to never place more than 5 volts across the LEDs, as damage will likely result.
Wire the hall-effect sensor/switch on channels 9 and 10. The hall-effect sensor (HI 401
on the chip), contains four wires. Red is the supply voltage and should be connected to
+5 V on the breakout board. Black is ground. The white and yellow wires are the outputs
of the sensor. This particular sensor provides a latched output, meaning that once it is
toggled HIGH or LOW, it will remain there until a magnet is again passed through the
sensor’s magnetic field. Connect the yellow wire to channel 9 and the white wire to
channel 10. Be sure to understand how it is functioning so that you can correctly use it in
your MATLAB m-file.
2. LabJack Logger Interface
Download and install the LabJack logger program (LJlogger.exe) from the LabJack files
disc. Open the LabJack data logger program—this allows control of all of the LabJack IO
and debugging of the system. Ensure the ability to light all four LEDs and the ability to
read the states of channels 8, 9, and 10 as digital inputs.
ME-191
Computer Applications in Mechanical Engineering II
The LJLogger program must be closed using the Exit button before MATLAB is initiated. Errors
occur whenever both programs are trying to operate at the same time!
3. LEDs—light shows
Close LJLogger and open MATLAB. Begin a new m-file, and write a short program to
sequentially light four LEDs. Provide code that requests the MATLAB user to input the
number of cycles that the program should run. Experiment with various delay times (use
the PAUSE command in MATLAB) within the program, e.g.,
EDigitalOut(Idnum, demo, 3, writeD, 1)
pause(0.5)
EDigitalOut(Idnum, demo, 3, writeD, 0)
pause(0.5)
EDigitalOut(Idnum, demo, 3, writeD, 1)
turns the device on channel three on for one-half second, then off for one-half second,
then on—and remains on. Try to develop the good habit of writing loops to obtain
sequential operations, e.g.,
for k=1:4
channel = (k-1)
EDigitalOut(Idnum, demo, (k-1), writeD, 1)
pause(0.5)
EDigitalOut(Idnum, demo, (k-1), writeD, 0)
pause(0.5)
end
ME-191
Computer Applications in Mechanical Engineering II
Having a good command of writing loops, both for-loops and while-loops, will certainly
help you in the exercises planned for later in this course.
4. Digital Inputs
Write a short program to monitor two digital inputs, one from a limit switch and one from
a hall-effect sensor. Have the program display the current state of each input on the
screen at an approximate frequency of 1 Hz (once per second). Use the “clc” command
in the MATLAB command window to clear the screen before the display is updated each
second. The screen output should designate whether it monitors the limit switch or the
hall-effect sensor so that the user knows the state of each individually.
5. Decision-making based on user inputs
Use the digital inputs from a limit switch and/or a hall-effect sensor (part 2) to modify the
display pattern of four LEDs (part 1). For example, the state of one of the digital inputs
could be used to set the direction of the lighting cycle and the other digital input used to
light only even or odd LEDs. Use your imagination and have fun!
Deliverables:
1. Lab Report: Provide a one page (or less!) summary of what you designed and
programmed for this experiment. Use the lab report format template provided. A group
report is optional. Attach your m-files for all three parts to the one page summary of your
solution. Please use comments liberally within each m-file to describe the function of
primary statements. Due one week from completion of lab.
2. Homework: Due at first meeting of week 5. Use the homework format template provided.

Describe, in detail, a hall-effect sensor: what it is, how it works, perhaps where it
might typically be utilized. Include appropriate references using at least one
reference from the MSOE library.

Find two different ‘sizes’ of commercially available hall-effect sensors.
Determine the parameters of interest for different sizes, e.g., voltage or current
ranges. Internet sources are acceptable, but be sure to list them in detail along
with the date accessed.