\documentclass[12pt]{article}

\usepackage{amssymb,latexsym,color}
\usepackage{epsfig}
\usepackage{hyperref}

%\leftfooter{Assignment 2, sfwreng 4aa3/4ga3}
%\title{Sfwr Eng 4aa3/4ga3 Assignment 2}
\textwidth 6.0in
\textheight 9.0in
%\oddsidemargin -0.5in
\topmargin -0.5in
\begin{document}
\begin{center}
\textbf{\Large{Mechtron 3TB4: Embedded Systems Design II\\Lab~1}}\\
\textbf{Control of a Stepper Motor}
\end{center}


Reports Due:\\
Part~1: No later than 11:31 hrs on Monday Jan 18, 2010, at the start of class in BSB/108.\\
Part~2: Week Starting Jan 25, 2010, at the beginning of your next tutorial sessions (to be handed over to your TA in hardcopy or available in PDF in your subversion repository).


%\vspace{0.5cm}


\noindent 
\section*{Goals:}
\begin{itemize}
\item	Learn about stepper motor basics and how to control a stepper motor using a microcontroller 
in full/half step mode and clockwise/counterclockwise direction, while controlling the speed of rotation
\item	Learn how to use AVR to interface a physical device.
\item	Practice the use of AVR timer using interrupts and polling
\end{itemize}
\textbf{Note:} This lab consists of three (3) activities. Marks allocated to each are indicated in square brackets.\\

%\vspace{0.5cm}
\noindent
%\textbf{\large{Implementation of PID Controls}}\\
\textbf{Before you go for your lab session, please read relevant portions of the following documents at your own convenient time, in addition to the class notes:}
\begin{itemize}
\item \href{../ref/Stepper.pdf}{Overview} of Stepper motors, their specifications and terminology.
\item \href{../ref/26M048B.pdf}{26M048B1B Stepper motor specifications}
\item 
\href{http://www.allegromicro.com/en/Products/Design/airpax/smh29.pdf}{Stepper Motor Technology background}
\item 
SN754410 quadruple high-current half-H bridge driver \href{../ref/sn754410.pdf}{Data Sheet}
\item \href{../ref/cprogramming.pdf}{How to program an 8-bit microcontroller using C.}
\item \href{../ref/atmel324p.pdf}{ATmega324p data sheet}
\end{itemize}


\section*{Equipment Arrangement:}
The stepper motor used in the lab is a 26M048B1B from Thomson Airpax Mechatronics, which is a 48 step motor. 
To drive the motor, any of the sequences of 
signals described in class can be used.


To drive a stepper motor, a signal conditioning stage is needed.  In the lab we will use a SN754410NE.  In addition, you will use 8 
diodes to protect the control circuitry from voltage spikes that would otherwise be produced by the windings (see p. 6 of the \href{../ref/sn754410.pdf}{SN754410 datasheet}).

A sequence of signals is generated through software using timer~0 of the AVR. This sequence is 
used to control appropriate switches in the H-bridge and the output of the H-bridge is connected to 
the four terminals of the motor. A block diagram of the equipment arrangement is shown in figure~\ref{lab1f2}.
\begin{figure}[htb]
\centering
\includegraphics[width=0.8\textwidth]{../figures/lab1f2}
\caption{\label{lab1f2}Block Diagram}
\end{figure}
%\clearpage

%\vspace{3.5cm}
\noindent



\section*{Pre-lab [30 marks]}
The following activities must be completed by each student \textbf{independently} and submitted as part~1 of the lab report 
at the start of class on Monday Jan 18, 2010.  Late submissions will not be accepted.
\begin{enumerate}
\item Calculate the angular resolution of the given motor, which makes 48 steps per revolution.
\item In this lab we will use the stepper motor to implement an unusual clock. 
The motor's shaft will be connected to a dial of the clock. Hence, we would like the motor to complete 
one full revolution in a very specific amount of time. The amount of time (in seconds) will be equal 
to the last two digits of your student number. To make the testing times reasonable and fairer across 
the class, you can adjust this number if it is very low or very high. Please use the following scheme 
to do that:
\begin{enumerate}

\item If the last two digits of your student number are less than 33, add 33 to it and use that number. E.g. if your student number is 654321, last two digits are 21, which is lower than 33. Then you add 33 to get 54 seconds, which is the period you should use.
\item If the last two digits are between 33 and 66 (inclusive), use the last two digits unchanged. E.g. if your student number is 123456, you should simply use 56 seconds for the period.
\item If the last two digits are greater than 66, subtract 33 from it and use that number. E.g. if your student number is 56789, last two digits are 89, which is greater than 66. Then you subtract 33 to get 56 seconds, which is the period you should use.
\end{enumerate}
\item Determine time period between two steps of the stepper motor used in the lab required for the motor to complete one revolution per period calculated in step 2 for:\\
\begin{enumerate} \item	Half-stepping sequence
\item Full-stepping sequence
\end{enumerate}
\item In the lab you will use Timer 0 of ATMega324P to measure time periods. Assuming you will use the 16 MHz clock available on the boards, calculate the counter reload number that will enable you to measure the period of time calculated in steps 3a and 3b.
\item Write AVR code that uses Timer 0 and the stepper motor to implement the clock that completes one revolution per period calculated in step 2. Implement this in the following two ways:
\begin{enumerate} \item	Use polling and full-stepping sequence
\item Use timer interrupts and half-stepping sequence
\end{enumerate}
Make sure that the direction of the motor movement (clockwise/counter-clockwise) can be easily changed in your program.
\item Read the documentation for the SN754410NE. Draw a schematic of how you will connect AVR, SN754410NE, diodes, and the motor
\end{enumerate}

\section*{In the Lab [50 marks]}

\noindent
\textbf{IMPORTANT NOTE:} IN ORDER TO AVOID EXPENSIVE DAMAGE, DO NOT TURN
ON YOUR MOTOR WITHOUT APPROVAL FROM A TA. IF YOU DO NOT GET APPROVAL
FROM THE TA AT THE APPROPRIATE POINTS, YOU MAY BE ASSIGNED
A ZERO FOR THE LAB.\\

\subsection*{Version Control}
Login to the PC at your lab station.  Open a file browser and create a directory called Labs.  Right click on it and select the TortoiseSVN \texttt{Checkout} command to check out your Lab Group's working directory.  It should be a URL of the form \texttt{https://websvn.mcmaster.ca/mt3tb4/groupX} where X is your group's number.  (See the TA if you are unsure of your group number.)


You will have different solutions from your pre-lab (pre-labs should be completed individually). In the lab you need to demonstrate only one working program/circuit per group.
\begin{enumerate}
\item Using the equipment available in the lab, determine which wires on the motor correspond to the 2 windings.
\item Connect the motor, AVR and SN754410NE according to the schematic you developed in the pre-lab. You may want to add some kind of marker to the motor shaft, to easily observe its movement. You can use a piece of tape wrapped around the shaft for this.
\item Perform any experiments required to answer the questions in the lab report section below.
\item Test the behaviour of your circuit and demonstrate its operation to one of the TAs. The TA may ask you to demonstrate different modes of operation (polling/interrupt version, clockwise/counter-clockwise rotation).
\item (Bonus: This part will NOT be marked)
Change your program so that the motor speed can be changed by pressing push buttons on the development board

\end{enumerate}


\section*{Lab Report [20 marks]}
The following report should be done as joint work in the lab group.  

Describe what you did in this lab, include the code used and answer the following questions:
\begin{enumerate}
\item How does the behaviour of the stepper change when you exchange the two sides of one winding (e.g. exchange A1 and A2), without changing the program.
\item How does the behaviour of the stepper change when you exchange the two winding (i.e. exchange A1 and A2 with B1 and B2)
\item Which registers did you have to set to enable timer interrupts? What values did you write into them and why?

\end{enumerate}



\end{document}