E5 Lab 7

MatLab 4: Motors 'n MatLab
&
Mini-Project I: the laser pointer


Today we will explore moving motors with MatLab, and you will begin the laser pointer mini project.


For your report:

Download the Word document "E5Lab8.doc" and save it to your user folder or to SwatFiles (right click on the link and choose Save File As...)  There are several questions in this lab.  Fill in the answers (some will consist of cut-and-pasted MatLab code and plots) on the Word document.  Each group is to turn in one printed lab report next Thursday.

*Important*  Be sure to use your user folder or SwatFiles and not the hard drive on the computer you are using.  ITS regularly wipes all of the data from local hard drives.


Getting extra help:

If you find that you would like extra help, the following resources are available (in no particular order):


The Teams

The teams for the next three lab periods (the next 2 weeks) are shown below.

Section A 

Team Aa
Bravo, Sebastian
Hwang, Steven
Parker, Mariah
Verhasselt, Eric

Team Ab
Martin, Beth
Prymak, Eugene
Richardson, Emily
Rodriguez, Eric
Watanasutisas, Charles

Section B

Team Ba
Ammous, Ahmad
He, Heliang
Jacobs, Monica
Roelofs, Becca

Team Bb
Barton, Spencer
Shaban, Maher
Velasco, Jessi
Zhai, Xingda

Team Bc
Chhabria, Vishaal
Pontillo, John
Stambaugh, Alexandra
Yeboah, Duke

Team Bd
Donahey, Remy
Monari, Patrick
Sohn, Nick
Spiegel, Ariana

Section C

Team Ca
Call, Michelle
Felt, Nick
James, Lauren
Ozawa, Katherine
Reichert, Kieran

Team Cb
Chin, Roger
Dewald, Dinah
Gao, Eileen
Spagnolo, Dan
Serrano, Mark

Team Cc
Foster, Seth
Laughlin, Alice
Ryan, Gabriel
Welkie, Allen
Williams, Tanisha



Getting Started

Choose a name for your team, and add it to the wiki (follow example for "Team Generic" at the top of the page for the Laser Pointer project).

Get a plastic box, and put your team name on it.  You can store these in Hicks 213.

Read over the lab quickly - with such large groups, it probably makes sense to split up the tasks.


Serial ports and motor control

Servo background:

We will use servo motors to position the arms of the robot arm you will be building.  Recall from class that the width of pulses sent to the servo determines the angle of the output shaft.



To control the servo motors we will use the same I/O board you used last week with the two knobs.  To communicate with the board, and control the servos, you must open a serial port connection, and then send a string of characters to the I/O board.

To open a single serial port, you can use the following code.

s=instrfind; %Find any serial links (we can have only 1)
delete(s); %... and delete.

%Create a new serial communications link
s=serial('COM1','Baudrate',115200,'Terminator','CR');
fopen(s); %... and open it

The string of characters follow the format shown below:

#<ch>P<pw>...#<ch>P<pw>T<time>

where

So if we send the string:

#1P900T300

then ch=1, pw=900, time=300.  This will set the pulsewidth on motor 1 to 900 microseconds, and will make the change over 300 milliseconds. (900 ms ≈ -45)

Likewise, if we can control multiple motors simultaneously

#1P1200#5P2100T1500

This will set the pulsewidth on motor 1 to 900 microseconds,  the pulsewidth on motor 5 to 2100 microseconds,  and will make the change over 1500 milliseconds.

The only piece missing is how to send the string to the I/O board.  If we have already created the serial link (as described above), we can send a string with the MatLab code:

% set pulsewidth on motor 1 to 1000 microseconds (over 200 mS)
fprintf(s,‘#1P1000T200');


MatLab and Strings

So far we have only used MatLab to manipulate numbers, but it can also manipulate strings.  Try entering the following, one line at a time (you needn't add the comments).  If you have any questions about what is happening, please ask.

% Anything between two single quotes is a string.
a = 'this'
b = ' '
c = 'is'
d = 'ain''t'   		%Adding single quotes

x=a

x = [a b c b a]		%combining strings

x = [a b c b a '!  ' a b d ' something else.']

disp(x)			%displaying strings

ch=1;  pw=900;  T=300;
num2str(pw)		%convert a number to a string
cmd = ['#' num2str(ch) 'P' num2str(pw) 'T' num2str(300)]

pw=900.35
cmd = ['#' num2str(ch) 'P' num2str(round(pw)) 'T' num2str(300)]

Servo Calibration:

Your first task for today is to calibrate your two servos.  Start by labeling the two servos "A" and "B" using the labels provided.

Connect servo "A" to channel 1 as shown below:

Note that the yellow wire is connected to the line labeled "pulse", the red wire to the line labeled "VS1" and the black wire to the one with the ground symbol.

Download and run the script "ServoCalibrate.m" which asks for user input for the pulse width (in microseconds) and then sends the specified pulse width to channel 1.  Ask me (or a wizard if you have questions about the code).  Try to ensure that everybody on your team understands this code - there are several new things that we haven't seen before (and these will be more obvious to those that are more comfortable with MatLab).

Make sure you understand how the code works.  In particular add comments to every line of uncommented code below the point marked in the code (i.e., for every line from line 12 (pw=1500) to the end of the script).

→ Do problem 1 of the Word document. ←

Set the pulse width to 1500 microseconds.  We will call this zero degrees.   Use the protractor provided to fill in the table below with the angles for several pulse widths.  You may take more data at more pulse widths if you wish.

Pulse width (microseconds) 1500 1000 1250 1750 2000
Angle (degrees) 0        

Now plot the pulse width versus angle (you did this in task 5 of last week's lab), and find the values of slope and intercept the best fit the data

→ Do problem 2 of the Word document. ←

Repeat for a second servo motor (you will need both motors for the lab, so keep them separate).

→ Do problem 3 of the Word document. ←


gotoAngles

Use the supplied template, gotoAngles.m, and fill it in to create a function that takes as input:

The function should move the two motors to the desired angles, and it must use your getPWs function from last week (you'll have to edit the constants defined in that function in accordance with your motor calibrations).

→ Do problem 4 of the Word document. ←


Inverse Kinematics

Use the supplied template, invKin.m, and fill it in to create a function that takes as input:

and returns

You may want to review the class notes to review the geometry of the problem.

Provide evidence that your invKin function works.  To do this you should figure out at least four x, y, and z values and their associated values for theta and phi for a given arm length rho, and demonstrate that your function returns the proper angle.  Some examples locations that are easy to verify include (you might want to double check these for accuracy because I didn't):

→ Do problems 5 and 6 of the Word document. ←

The word document is due next week in lab.  
The word document is due next week in lab (Thursday 11/5) .  You should consider the tasks in the Word document to be only a part of what you need to accomplish this week.

The text below describes a two-week project.  It will be demonstrated in lab on November 12th.  Though this may seem like a long time from now, there is a lot to do.  You  should make substantial progress on the project this week.  You will need to do substantial work outside of the specified lab time.  See the note above about getting extra help.


The mini project

Resources/Ideas:

Requirements

Your task for the mini project is to write a script that conrols a laser pointer and meets the following 2 requirements.

Requirement 1:
At the top of the program you will define a y vector, a z vector and a variable Ttot which is the total time for the movement.   The origin, (y,z)=(0,0) will be marked in advance.

The script will move the laser to consecutive (y,z) locations in a total time of Ttot.

As an example, if the following code was at the top of your script:

th=linspace(0,2*pi,20);
y=3*cos(th);
z=3*sin(th);
Ttot=20;

the laser should trace out a circle with a radius of three inches centered on the origin, in 20 seconds.

The code:

x=[-1 -1 4 4 -1];
y=[-1 4 4 -1 -1];
Ttot=10; 

would trace a square 5 inches on a side in 10 seconds.

Requirement 2:
Your script must use your gotoAngles function, and your invKin function.  You may edit these in minor ways if it is helpful.

Requirement 3:
The project will be demonstrated in lab in two weeks (November 12).

Requirement 4:
You will create a wiki page that describes your design (what you did, evidence that your design functioned properly, what worked well, what didn't...) and includes a link to a zip file that contains all of your code used in the project.  The wiki is due the following week (November 19).

Other than those requirements, you are free to do as you please.


Good luck!