MAE 211—Mechatronics Lab #3: Introduction to Using the Data

Description
MAE 211—Mechatronics Lab #3: Introduction to Using the Data Acquisition Board Objectives: ã Introduction to basic programming logic ã Familiarization with…

Please download to get full document.

View again

of 6
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Information
Category:

Computer Science

Publish on:

Views: 41 | Pages: 6

Extension: PDF | Download: 0

Share
Transcript
MAE 211—Mechatronics Lab #3: Introduction to Using the Data Acquisition Board Objectives: ã Introduction to basic programming logic ã Familiarization with basic flowchart logic ã Familiarization with basic output and functions ã Use of if, for, while, switch, case, tone, pause, and display functions Introduction: Mechanical engineers often find themselves with a need to gather data using a computer. Data acquisition is likely the most important aspect of an experiment. After all, quantitative results tend to be more useful than the qualitative results that can be observed with one’s naked eye. In this course, the data acquisition board (DAQ board) will be used to output signals to motors or relays and to collect data from various types of sensors, as could be done in a real world setting. In order to do so, some knowledge about using the DAQ board must be obtained. Fig. 3-1—DAQ Board Part 1: MATLAB MATLAB is used in this course as an interface between the DAQ board and the user. MATLAB is a user-friendly, programmable application, with a wide variety of uses beyond what will be experienced during the course of this lab. Many of these functions are contained in the many toolboxes incorporated in MATLAB. For the purposes of this course, MATLAB is a programming language with which to instruct the DAQ board. As such, some simple programming skills will be necessary. Keep in mind that for any function, you may access the help file by typing “Help” and the function in the command line. Part 2: Programming logic In order to tell the DAQ board what you want it to do, you must give it a program with a specific set of instructions for it to execute. Obviously, a computer does not understand a command like a person does. A computer needs a step by step account of exactly the tasks you want it to execute and when. Computers “think” in a logical order. As such the commands you give will be followed in order from start to finish. In order to organize your program into a logical order, it is often useful, and depending on the depth of the program, necessary, to make a flowchart. A flowchart is a logical order of processes the computer should execute during a program. This should be as detailed as necessary to explain the details of the program to someone else. See Fig. 3-2 below for an example of a flowchart. Fig. 3-1—Flowchart Shapes Fig. 3-1—Flowchart for the for-loop program in this lab Part 3: DAQ Board The DAQ board is the unit the computer uses to “talk” to sensors, gather data from them or send messages to them. The basic functionalities of the DAQ board are digital input/digital output (DIO), analog input (AI), and analog output (AO). Each of these important functions will be explained in-depth in future labs. This lab is intended only to familiarize students with accessing the DAQ board to send messages to or receive messages from it. Experiments: Experiment 1: Initializing Open MATLAB by clicking on the desktop shortcut or finding it under the Start menu. When MATLAB is ready, type “initialize” in the command window, just as it appears in the quotation marks. If this is correct, MATLAB should initialize the mechlab toolbox (the functions you will use in this lab), and then “DAQ Initialized” will appear on the screen. You will need to do this each time you open MATLAB, unless “DAQ Initialized” already appears on the screen. Experiment 2: Outputting to the screen ã Open a new file by clicking on the File Menu under MATLAB, select New, then select M-File. Aside from giving MATLAB instructions one at a time in the command window as with the initialize function, we may also write an M-File and call it from the directory to send a whole program at once. For multiple instructions, this is the better and more efficient way to send code. ã In this M-File, type “clear” on the first line. MATLAB maintains all variables and information in its memory until cleared by the user. This can be a double-edged sword when writing complex programs so always be sure to clear the memory using the “clear” command at the beginning of your programs. It may also be useful in this lab to use the “initialize” function at the beginning of each program to make sure the DAQ board is reset each time you run the program. Obviously, you will not need to do this outside of lab. ã Once the memory is cleared, it is time to begin programming. MATLAB will execute commands in the order given, logically. Type the following code into your M-File: %Simple output function disp(‘Hello world!’); rand(3) tone(1000.0, 1.0); Save this file as myfile1.m under the desktop folder “Student Documents.” Change the directory at the top of the MATLAB window to this folder by clicking on the button “…” and selecting the “Student Documents” folder. You may now use any M-File saved in this folder simply by typing its name in the command window. Do this by typing “myfile1” in the command line. Notice the way that MATLAB displays the data. If in a program a line of data should be displayed to the screen, do not place a semi-colon after it. If you do not wish to see an output displayed, place a semi-colon after it. This is called suppressing an output. The “tone” function introduced above plays a sound. The user can define this sound by first giving the frequency, then the duration in seconds inside the parenthesis. Experiment 3: Deciding statements and loops ã There are several different common statements used in programming. The common ones are “if”, “for”, “while”, and “switch”/“case”. These will be explored now. ã Type the following code: clear initialize y=1; if y 10 disp(‘y 10’); end You see that this executes the if-statement when run. Try changing the value of y to something above 10. You should see that the if statement is not executed. There are also if/else statements. Try changing your code to the following: clear initialize y=1; if y 10 disp(‘y 10’); else disp(‘y 10’); end You should see that now, no matter the value of y, the statement is executed one way or the other. ã Type the next code: clear initialize n=1; for n=1:10 tone(1000.0, 0.5); %plays a note for 0.5 seconds pause(1.0); %pauses the program for 1.0 seconds end Notice that the program beeps 10 times. This is because you defined the for-loop to continue from n=1 to n=10 at intervals of 1 unit. Pay attention to the use of the pause function. The tone function will play a note for the specified time, but unlike many languages, MATLAB will not wait for this command to be completed before continuing with the rest of the program. Therefore, to hear each note, a pause at least as long as the note must be included after the tone command. ã Enter this code: clear initialize n=1; while n =10 n n=n+1; tone(1000.0, 0.5); pause(1.0); clc end The “clc” function is useful for keeping the workspace neat. It clears all the type from the command window. This function does not clear any variables stored in memory however. Notice that the above program has virtually the same effect as the for-loop program, but written in a different way. Depending one the way one needs to define a loop will determine whether to use a for- or while-loop. Generally, a for loop is used when there is a specified number of iterations, and a while loop is used when the number of iterations is not precisely known, but a desired result is. ã Switch statements are a bit more complicated than those above. Enter this code: clear initialize; clc; n=3; switch n case 1 tone(1000,1); pause(1); case 2 tone(1000,0.5); pause (1); tone(1000,0.5); pause (1); case 3 tone(1000,0.25); pause(1); tone(1000,0.25); pause(1); tone(1000,0.25); pause(1); end Notice that when executing this statement it only runs once. Try changing the initial value of n. A switch/case statement is like a choice, depending on the case. It is equivalent to writing a series of if/if else statements, though tends to be considerably easier and faster once the user is proficient at using the switch/case. Project: ã Using the above concepts, write a program that will play a four note scale (see below for frequency breakdown) first in whole notes, then in half notes, then in quarter notes, and finally in eighth notes. Notes Frequency by octave (Hz) 4th 5th 6th 7th 8th octave octave octave octave octave A 440.00 880.00 1760.00 3520 7040 A# 466.16 932.32 1864.66 3729.31 7458.62 B 493.92 987.84 1975.53 3951.07 7902.13 C 523.28 1046.56 2093.00 4186.01 8372.02 C# 554.40 1108.80 2217.46 4434.92 8869.84 D 587.36 1174.72 2349.32 4698.64 9397.27 D# 622.24 1244.48 2489.02 4978.03 9956.06 E 659.28 1318.56 2637.02 5274.04 10548.08 F 698.48 1396.96 2793.83 5587.65 11175.30 F# 740.00 1480.00 2959.96 5919.91 11839.82 G 784.00 1568.00 3135.96 6271.93 12543.85 Ad 830.64 1661.28 3322.44 6644.88 13289.75 Questions: 1. (Experiment 1) What should happen if the DAQ board is not initialized and commands from the mechlab toolbox are used? 2. (Experiment 2) How do you display the value of a variable to the screen in Matlab? 3. (Experiment 3) Note: Do not use the examples given in this lab for your answers. Make up your own! a. Define the difference between using an if-statement and a switch case statement, and give an example of a situation in which you would use each. b. Define the difference between a for-loop and a while-loop and give an example for the use of each. 4. (Project) Include you code for this program (for proper formatting, see the lab formatting guide or ask one of the TAs). For this program, also make a flowchart. Explain why you chose to use the type of statement(s) or loop(s) you did for this program. If applicable, tell why the other options would not be satisfactory.
Related Search
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks