Download Week 11 Lab Project - Arduino - Part 2

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
no text concepts found
Transcript
SAN JOSÉ STATE UNIVERSITY
Department of Mechanical and Aerospace Engineering
ME 30 Computer Applications
Week 11 Lab Project: Arduino Microcontroller – Part 2
Introduction
In this lab session we’ll use the Arduino microcontroller and the Spartronics Experimenter to gain
experience controlling an actuator (a radio-controlled (RC) servo and a diode laser module and learn more
about embedded system programming and mechatronics.
Objectives
Following completion of today’s lab, you should be able to:
 Configure the Arduino to control an RC servo
 Use what you learned about digital and analog IO last week to control the angular position of
the output shaft of the servo
 Develop the software to control the orientation of a diode laser module, and turn it on or off
from buttons on the Spartronics Experimenter
Procedure
This lab experiment is intended to be done by pairs of students. Find a partner to work with, and take
turns typing code and interacting with the Arduino. You will submit an individual lab report, but you may
share the program code you generate, since you will develop it together with your partner in the lab. Make
sure that your report also includes the name of your lab partner.
Download a copy of the Lab Report Template (lab_rpt_tpt.doc) from Desire2learn or the ME 30
website. Use the template to record your answers to the lab exercises. Make sure that you fill out the
cover page completely, before turning in your report. Points will be deducted if the cover sheet is not
completely filled out. Enter your last name, first initial, and project number in the labeled boxes in the
header by double-clicking on it.
Under the section of the cover sheet labeled ‘Summary’, summarize the problem(s) you solved. You
must also include a summary of the strategy you used as well as the methods you employed, and any
observations or conclusions about what you learned from the laboratory. This is perhaps the most
important part of the lab, so do a good job on it. Note that you are not limited to fitting the summary on
the cover sheet. You can expand the summary to another sheet if needed.
Save your work on your own USB storage device or email a softcopy to yourself before you leave the
lab. The hard drives on the computers in the lab are frequently refreshed, so do not rely on them for
saving your work from session to session.
Instructions for turning in your Lab Project report:
1
Save your report using the following naming convention (all lower case):
lastname_firstinitial_sec_n_lab11_report.doc
where n is your lab section number: Tues=2, Wed=3, Thurs=4
so, if your name is John Smith, and you are in section 3, you must name your file:
smith_j_sec_3_lab11_report.doc
2
Upload your report and any related files to Desire2Learn under the Week 11 Lab
Project Dropbox before the deadline
3
Turn in a hard copy of your report at the beginning of next week’s lab session
BJ Furman | ME 30 Computer Applications | Week11Lab_Arduino_part2.doc | 08NOV2010
Page 1 of 5
SAN JOSÉ STATE UNIVERSITY
Department of Mechanical and Aerospace Engineering
ME 30 Computer Applications
Introduction
Microcontrollers are often interfaced to both sensors and actuators (i.e., devices that provide the
force or torque to move physical objects, such a motor) in mechatronic systems. In this lab you
will use a radio-controlled (RC) servo as the actuator.
An RC servo consists of a dc motor, gear train, potentiometer, and some control circuitry all
mounted compactly in a case. RC servos are commonly used in radio-controlled cars, airplanes,
and boats to provide limited rotational motion to steer, move control surfaces, etc.
RC servos are attractive for educational use in mechatronics because they are relatively
inexpensive, costing in the $9-$75 price range. Depending on the model chosen, you can expect
torques in the range of 8 oz-in to172 oz-in. Servos can easily be modified to produce continuous
shaft rotation at relatively slow speeds, and they can easily be controlled by a microcontroller.
Figure 1 shows that there are three wires, black, red, and white on the servo leading from a 3-pin
female connector to the case. These carry the ground return, power, and control signal
respectively. Most hobby servos are specified to run over the 4.8-6.0V range, with higher output
torque at the higher end of the input voltage range.
Black
Red
White or yellow
Figure 1. RC Servo. The RC servo uses three wires: white carries the control signal, red carries power (usually 4.8
V to 6 V), and black is ground. Servos usually come with a plate (called a ‘horn’) that is attached to the output shaft.
The horn has an array of holes through its thickness that allow the user to connect a link to whatever is to be
actuated.
Start up Instructions
(You may also want to refer to last week’s lab guideline to refresh your memory about how
to use the Arduino IDE. See also the ME 30 website:
http://www.engr.sjsu.edu/bjfurman/courses/ME30/ME30pdf/Arduino_Fundamentals.pdf and
the Arduino related links on the ME 30 website.)
Remember to handle the Arduino and Experimenter boards only by their edges!
a. Connect the USB cable between one of the USB ports in the back of the PC and the
Arduino. Verify that the green ‘Power’ LED is on. (You will need to look underneath the
Experimenter board to see the power LED.)
b. Download Blink, and verify that you are able to program the Arduino successfully. So
this by opening the Arduino IDE by finding the shortcut in the Start menu or by
navigating to the Arduino folder in the Programs folder. After the IDE starts up, select
from the Menu bar at the top of the screen: File | Examples | Digital | Blink. Click on the
‘Upload’1 icon to compile and write the Blink program to the Arduino. You should see
the Rx and Tx LEDs flicker rapidly as the upload progresses. After ‘uploading’, the
Status bar at the bottom of the IDE should say, ‘Done uploading’, and below that, the
results window will show the size of the uploaded file in bytes. (Note: if the upload does
not work, and you get error messages: "avrdude: stk500_getsync(): not in sync:
1
It is more common to refer to the process of sending a program originally written or stored on one computer to the
target microcontroller as ‘downloading’. The Arduino developers alternatively refer to this process as ‘uploading’.
BJ Furman | ME 30 Computer Applications | Week11Lab_Arduino_part2.doc | 08NOV2010
Page 2 of 5
SAN JOSÉ STATE UNIVERSITY
Department of Mechanical and Aerospace Engineering
ME 30 Computer Applications
resp=0x00",
check the serial port from the Tools | Serial Port drop-down menu. COM3
should be checked, but you may need to find an alternate COM port that works. You can
find some discussion of other error messages and their probable cause at:
http://www.ladyada.net/learn/arduino/help.html )
c. Remember to use File | Save As to save the programs that you write to your USB drive or
personal desktop on the lab computer (you will need to navigate to the directory you
want, because the IDE will try to save your file to a default directory on the PC, which
you don’t want). Note: do not include a file extension in the name dialog box. The
Arduino IDE will append ‘.pde’ to the name you enter.
1. Exercise 1 – Getting familiar with the servo
1.1. Connecting the servo to the Spartronics Experimenter board.
It is REALLY important to connect the servo properly, so pay close attention to get the
next step right!
Connect the three-pin female connector of the servo to the three-pin male header on the
Spartronics Experimenter, so that the white wire on the servo lines up directly above the header
pin marked, ‘ctl’ (the header is labeled, ‘SERVO’, and is located at the top edge of the board
about in the middle). The black and red wires of the servo should line up directly over the – and
+ header pins respectively. Have your partner double-check that the connection is correct.
1.2. Download the sketch titled, ‘Servo_Sweep.pde’ from the ME 30 website under
Laboratory Source Code
(http://www.engr.sjsu.edu/bjfurman/courses/ME30/source_code.htm#Lab_Source)
Download Servo_Sweep.pde to the Arduino and verify that it sweeps the servo. Study
the program, so that you can explain how it works in your report. Try some different
delay periods. What happens?
If you start to hear a clicking/grinding sound from the servo, PULL THE 3-PIN
CONNECTOR OUT IMMEDIATELY! If the servo is commanded to go beyond its
physical limitation, it can be damaged, so please be careful.
You can get more information about the servo library at:
http://www.arduino.cc/en/Reference/Servo
1.3. (10 pts) Write a program that will rotate the servo to 0° when the S0 button is pressed
and held down, or to 180° when the S3 button is pressed and held down, or else will
return to 90° if no button is pressed.
2. Exercise 2 – Controlling the orientation of the servo shaft
2.1. Download the sketch titled, ‘Servo_Knob.pde from the ME 30 website under
Laboratory Source Code
(http://www.engr.sjsu.edu/bjfurman/courses/ME30/source_code.htm#Lab_Source)
Download Servo_Knob.pde to the Arduino and verify that you can control the
orientation of the servo horn by rotating the knob of the pot. Study the program, so that
you can explain how it works in your report.
2.2. (10 pts) Modify Servo_Knob sketch from 2.1, so that you can print to the monitor both
the pot voltage and the scaled value. Determine what the maximum and minimum
BJ Furman | ME 30 Computer Applications | Week11Lab_Arduino_part2.doc | 08NOV2010
Page 3 of 5
SAN JOSÉ STATE UNIVERSITY
Department of Mechanical and Aerospace Engineering
ME 30 Computer Applications
values are for the pot voltage when you turn the knob to its extreme CW and CCW
directions.
2.3. Change the 1023 argument value in the map() function to correspond with the
maximum pot voltage value that you determined in the previous step. Remember, if
you start to hear a clicking/grinding sound from the servo, PULL THE 3-PIN
CONNECTOR OUT IMMEDIATELY, so that the servo does not get damaged!
2.4. (10 pts) Write a program that will light up one of the four red ‘gumdrop’ LEDs
depending on where the potentiometer knob is turned:
o if within 2% of the extreme CCW or CW location then LED0 or LED3 is on
respectively
o if between fully CCW and half way, then LED1 is on
o if between fully CW and half way, then LED2 is on
(Don’t forget to change the LED jumper to RED. It might be on RGB from the last lab.)
3. Exercise 3 – Controlling the laser diode module
3.1. Connecting the laser diode module to the Spartronics Experimenter board.
It is REALLY important to connect the laser module properly, so pay close attention to get
the next step right!
Refer to Figure 2 below, and make sure that the red wire coming from the laser module and the
header pin it is soldered to plugs into the ANALOG IN female socket A3. The small circuit
board is constructed so that the black wire from the laser module will then be properly aligned
with the female socket labeled GND. Have your partner double-check that the connection is
correct. The laser will turn on when you use the digitalWrite() function to make pin 3 HIGH.
Figure 2. Connection of the laser module to the Spartronics Experimenter board. The dotted white lines show
where the pins connected to the red and white wires must plug into the Arduino. There is also a ‘gap’ between the
male header pins which will align over the gap between the female sockets to help you align the circuit board
properly.
3.2. (10 pts) Write a program that turn on the laser and LED3 when you press button S3 and
hold it down. The laser should turn off when S3 is not pressed
BJ Furman | ME 30 Computer Applications | Week11Lab_Arduino_part2.doc | 08NOV2010
Page 4 of 5
SAN JOSÉ STATE UNIVERSITY
Department of Mechanical and Aerospace Engineering
ME 30 Computer Applications
4. Exercise 4 – Move ‘n Shoot
4.1. (10 pts) Write a program that will move the servo horn an increment of rotation CCW
when you press S0, or will move the servo horn an increment of rotation CW when you
press S3, and will let you turn the laser on when you press either S1 or S2
Optional Extra Credit 1: Move ‘n Shoot with sound
(10 pts) Add an appropriate sound effect using the speaker when the laser is fired. You can
play a tone using the tone() function. See the Arduino Reference for more information on
tone(), http://arduino.cc/en/Reference/Tone
BJ Furman | ME 30 Computer Applications | Week11Lab_Arduino_part2.doc | 08NOV2010
Page 5 of 5