\chapter{Stepper Motor Modeling and Control}
In this chapter, we model and then propose a controller for a permanent magnet~(PM) stepper motor. Stepper motors can be viewed as digital positioning devices. Specifically, stepper motors are electromechanical motion devices that convert input information in digital form into output that has mechanical form. The stepper motor can be driven by digital pulses, for example of the form: move clockwise or move counterclockwise. We now describe an idea of an operation of a stepper motor. In the next section, we derive equations modeling the PM stepper motor.

A simplified organization of a stepper motor is shown in Figure~\ref{fig:step_org}.
\begin{figure}
\vspace{3.5in}
%\centerbmp{4.67in}{3.5in}{c:/figures/step_org.bmp x=4.67in y=3.5in}
\hskip0.5in\special{bmp:c:/figures/step_org.bmp x=4.67in y=3.5in}
\caption{A simplified schematic of an operation of a stepper motor.}
\label{fig:step_org}
\end{figure}
The armature, or rotor, is a magnet. At any instant of the operation, exactly one pair of windings of the stator is energized. A pair of windings represents a phase. Each pair of the stator windings is wound in such a way that their magnetic fields are collinear, for example, windings A and A'. The rotor will then rotate until its magnetic field is aligned with the fields of the energized pair of the stator windings. Figure~\ref{fig:step_org} shows the situation where the pair of windings~A and A' was energized. If the rotor is at rest, as shown in Figure~\ref{fig:step_org}, and the pair~A, A' is de-energized, while the pair~B, B' is energized, the rotor will rotate to the new position so that its magnetic field is aligned with the magnetic field of the pair~A, A'. In this example of the three-phase motor, a single step results in the rotor motion of $360^{\circ}/6=60^{\circ}$. In the next section, we give a more detailed description of the electrical organization of the stepper motor and construct its model.

\section{Modeling of a Permanent Magnet~(PM) Stepper Motor}
The basic PM stepper motor consists of a slotted stator and a rotor. There can be two sets of teeth on the rotor. These two sets of teeth are out of alignment with each other by a tooth width, as shown in Figure~\ref{fig:rot_tee}.
\begin{figure}
\vspace{3.5in}
\hskip0.5in\special{bmp:c:/figures/rot_tee.bmp x=4.67in y=3.5in}
%\centerbmp{4.67in}{3.5in}{c:/figures/rot_tee.bmp x=4.67in y=3.5in}
\caption{A possible rotor structure of a PM stepper motor.}
\label{fig:rot_tee}
\end{figure}
Specifically, the rotor teeth of the left end-cap are displaced one half pitch from the teeth on the right end-cap. A magnet, or an electromagnet, that is mounted on the rotor magnetizes the iron end-caps. The left end-cap is magnetized as a north pole, while the right end-cap is magnetized as a south pole. Slotted stator is equipped with two or more individual coils. Figure~\ref{fig:pm_step} shows a four-pole stator and a five-pole rotor structure.
\begin{figure}
\vskip3.5in
\hskip.5in\special{bmp:c:/figures/pm_stepa.bmp x=4.67in y=3.5in}
\vskip3.5in
\hskip.5in\special{bmp:c:/figures/pm_stepb.bmp x=4.67in y=3.5in}
%\centerbmp{4.67in}{3.5in}{c:/figures/pm_step.bmp x=4.67in y=3.5in}
\caption{Construction features of a four-pole stator and a five-pole rotor PM stepper motor.}
\label{fig:pm_step}
\end{figure}

We now discuss the way in which the PM stepper motor can be used to perform precise positioning. Assume that the starting point is as represented in Figure~\ref{fig:pm_start}.
\begin{figure}
\vspace{3.5in}
%\centerbmp{4.67in}{3.5in}{c:/figures/pm_start.bmp x=4.67in y=3.5in}
\hskip0.5in\special{bmp:c:/figures/pm_start.bmp x=4.67in y=3.5in}
\caption{Coil A-A' energized for an S-N orientation, while coil B-B' de-energized.}
\label{fig:pm_start}
\end{figure}
Here, coil A-A' is energized so that the upper stator pole is given an S-pole polarity and the corresponding lower stator is given an N-pole polarity. The rotor remains in this position as long as stator coil voltage, and hence stator polarities, remain unchanged. This is because the net torque acting on the rotor is zero. If we now de-energize coil A-A' and energize coil B-B' so that the left pole becomes an S-pole and the right pole assumes an N-polarity, then this results in the new steady-state position shown in Figure~\ref{fig:pm_new}.
\begin{figure}
\vspace{3.5in}
\hskip0.5in\special{bmp:c:/figures/pm_new.bmp x=4.67in y=3.5in}
%\centerbmp{4.67in}{3.5in}{c:/figures/pm_new.bmp x=4.67in y=3.5in}
\caption{Coil A-A' de-energized, while coil B-B' energized for an S-N orientation.}
\label{fig:pm_new}
\end{figure}
We can proceed by alternatively energizing the two stator coils. The PM stepper motor can thus be controlled by digital pulses applied to its two inputs. The digital pulses are: move clockwise or move counterclockwise.

Our next objective is to devise a mathematical model of the PM stepper motor. In our discussion we use the following notation. We denote by $R_t$ the number of rotor teeth. The angular displacement between rotor teeth is called the tooth pitch and denoted by $T_p$. The angular displacement of the rotor as we change the input voltage from one pair of windings to the other is called the step length and denoted by $S_l$. The number of phases, that is, the number of pairs of stator windings, is denoted by $N_p$. In our example, we have $N_p=2$, $R_t=5$, and $T_p=2\pi/R_t=72^{\circ}$. In this example, it takes four input voltage switchings, and hence steps, to advance rotor one tooth pitch, that is,
\[
T_p=2N_pS_l=\frac{2\pi}{R_t}.
\]
Hence,
\[
S_l=\frac{\pi}{R_tN_p}=18^{\circ}.
\]
We denote the rotor angular displacement by $\theta$.

We now trace the (main) path of flux linking the A-A' windings for the rotor position shown in Figure~\ref{fig:pm_flux}.
\begin{figure}
\vskip3.5in
\hskip.5in\special{bmp:c:/figures/pm_fluxa.bmp x=4.67in y=3.5in}
\vskip3.5in
\hskip.5in\special{bmp:c:/figures/pm_fluxb.bmp x=4.67in y=3.5in}
%\centerbmp{4.67in}{3.5in}{c:/figures/pm_flux.bmp x=6in y=4.5in}
\caption{The main path of flux linking the A-A' winding.}
\label{fig:pm_flux}
\end{figure}
Flux leaves the left end-cap through the rotor tooth at the top that is aligned with the stator tooth having the A part of the A-A' winding. The flux then travels up through the stator tooth in the stator iron. Next, the flux splits and travels around the circumference of the stator, as shown in Figure~\ref{fig:pm_flux}(a). The flux returns to the south pole of the rotor through the stator tooth positioned at the bottom of the A' part of the A-A' winding, as shown in Figure~\ref{fig:pm_flux}(b). We denote the peak flux linkage produced by the rotor permanent magnet, in the A-A' stator winding by $n\phi_m$, where $n$ is the number of turns of the winding. For $\theta=0^{\circ}$, corresponding to the rotor position in Figure~\ref{fig:pm_flux}, the flux linkage in the A-A' winding of the stator is maximal and is approximately equal to $n\phi_m$. For $\theta=90^{\circ}/R_t$, corresponding to the rotor position in Figure~\ref{fig:pm_new}, the flux linkage in the A-A' winding of the stator is minimal, approximately equal to zero. We model the flux linkage, $\psi_a$, produced by the permanent magnet, in the A-A' stator winding as
\[
\psi_a=n\phi_m\cos(R_t\theta).
\]
The (main) flux, linking the B-B' winding for the rotor position shown in Figure~\ref{fig:pm_new}, would enter the stator tooth on which the B part of the B-B' winding is located from the rotor tooth on the left. The flux then would travel around the circumference of the stator and return to the rotor through the stator pole on which the B' part of the B-B' winding is wound. For $\theta=90^{\circ}/R_t$, corresponding to the rotor position in Figure~\ref{fig:pm_new}, the flux linkage in the B-B' winding of the stator is maximal and is approximately equal to $n\phi_m$. For $\theta=0^{\circ}$, corresponding to the rotor position in Figure~\ref{fig:pm_new}, the flux linkage in the B-B' winding is minimal, approximately equal to zero.  We model the flux linkage in the B-B' phase of the stator by
\[
\psi_b=n\phi_m \sin(R_t\theta).
\]
In the following development we use symbols that are described in Table~\ref{table:pm_step}
\begin{table}
\caption{Symbols used in modeling equations of the PM stepper motor.}
\begin{center}
\begin{tabular}{l}\hline
$T_p$ = tooth pitch\\
$S_l$ = step length\\
$R_t$ = number of teeth on rotor\\
$N_p$ = number of phases, that is, stator winding pairs\\
$\theta$ = angular displacement of the rotor\\
$\omega$ = angular velocity of the rotor\\
$v_a(t)$ = voltage applied to phase $a$, that is, A-A' stator winding\\
$v_b(t)$ = voltage applied to phase $b$, that is, B-B' stator winding\\
$i_a(t)$ = current in phase $a$\\
$i_b(t)$ = current in phase $b$\\
$R$ = stator winding resistance per phase\\
$L$ = stator winding inductance per phase\\
$B$ = viscous friction coefficient\\
$J$ = rotor inertia\\
$K_b$ = back emf constant\\\hline
\end{tabular}
\end{center}
\label{table:pm_step}
\end{table}
Applying Kirchhoff's voltage law to the circuit of phase $a$ yields
\[
v_a(t)= R i_a(t)+L\frac{d i_a(t)}{dt}+e_a(t),
\]
where $e_a$ is the back emf induced in phase $a$, that is,
\begin{eqnarray*}
e_a &=& \frac{d\psi_a}{dt}\\
&=& \frac{d\psi_a}{dt\theta}\frac{d\theta}{dt}\\
&=& -nR_t\phi_m \sin(R_t \theta)\frac{d\theta}{dt}\\
&=&-K_b \,\omega \sin(R_t \theta),
\end{eqnarray*}
where $K_b=nR_t\phi_m$. Hence, the voltage equation for phase $a$ can be represented as
\[
v_a=Ri_a+L\frac{di_a}{dt}-K_b \,\omega \sin(R_t \theta).
\]
Applying now Kirchhoff's voltage law to the circuit of phase $b$ yields
\[
v_b(t)= R i_b(t)+L\frac{d i_b(t)}{dt}+e_b(t),
\]
where $e_b$ is the back emf induced in phase $b$, that is,
\begin{eqnarray*}
e_b &=& \frac{d\psi_b}{dt}\\
&=& \frac{d\psi_b}{dt\theta}\frac{d\theta}{dt}\\
&=& nR_t\phi_m \cos(R_t \theta)\frac{d\theta}{dt}\\
&=&K_b \,\omega \cos(R_t \theta),
\end{eqnarray*}
where as before $K_b=nR_t\phi_m$. Hence, the voltage equation for phase $b$ can be represented as
\[
v_b=Ri_b+L\frac{di_b}{dt}+K_b \,\omega \cos(R_t \theta).
\]
Let $T_a$ and $T_b$ denote torques produced by the currents $i_a$ and $i_b$, respectively. Then, applying Newton's second law to the rotor yields
\[
J\frac{d^2 \theta}{dt^2}+B\frac{d\theta}{dt}=T_a+T_b.
\] 
The PM stepper motor, like the previously considered armature-controlled DC motor, is an electromechanical device in which electric power is transformed into mechanical power. The mechanical power, $P_a$, due to torque $T_a$ is
\[
P_a=T_a\frac{d\theta}{dt}.
\]
Assuming that there are no losses, this mechanical power is equal to the power developed in phase $a$, that is,
\[
P_a=i_a\frac{d\psi_a}{dt}=i_a\frac{d\psi_a}{d\theta}\frac{d\theta}{dt}.
\]
Equating the right hand sides of the above equations and performing simple manipulations, we obtain
\begin{eqnarray*}
T_a &=& i_a\frac{d\psi_a}{d \theta}\\
&=& -i_aK_b \sin(R_t\theta).
\end{eqnarray*}
Applying similar arguments to phase $b$, we get
\begin{eqnarray*}
T_b &=& i_b\frac{d \psi_b}{d \theta}\\
&=& i_b K_b \cos(R_t\theta).
\end{eqnarray*}
Taking the above equations into account, we represent the equation of motion of rotor as
\begin{eqnarray*}
J\frac{d^2 \theta}{dt^2}+B\frac{d\theta}{dt}&=&T_a+T_b\\
&=&-i_aK_b \sin(R_t\theta)+i_b K_b \cos(R_t\theta).
\end{eqnarray*}
We now combine the obtained above equations to obtain a model of a two-phase, that is, two input PM stepper motor. We have
\begin{eqnarray*}
\frac{d\theta}{dt}&=&\omega\\
\frac{d\omega}{dt}&=& -\frac{K_b}{J}i_a \sin(R_t\theta) + \frac{K_b}{J}i_b \cos(R_t\theta)-\frac{B}{J}\omega\\
\frac{di_a}{dt} &=& -\frac{R}{L}i_a+\frac{K_b}{L}\omega \sin(R_t\theta)+\frac{1}{L}v_a\\
\frac{di_b}{dt} &=& -\frac{R}{L}i_b-\frac{K_b}{L}\omega \cos(R_t\theta)+\frac{1}{L}v_b.
\end{eqnarray*}
We now define the state variables: $x_1=\theta$, $x_2=\omega$, $x_3=i_a$, and $x_4=i_b$. We let $K_1=B/J$, $K_2=K_b/J$, $K_3=R_t$, $K_4=K_b/L$, $K_5=R/L$, $u_1=\frac{1}{L}v_a$, and $u_2=\frac{1}{L}v_b$. Using the above notation, we represent the PM stepper motor model as
\begin{eqnarray*}
\dot{x}_1 &=& x_2\\
\dot{x}_2 &=& -K_1x_2-K_2x_3\sin(K_3x_1)+K_2x_4\cos(K_3x_1)\\
\dot{x}_3 &=& K_4x_2\sin(K_3x_1)-K_5x_3 + u_1\\
\dot{x}_4 &=& -K_4x_2\cos(K_3x_1)-K_5x_4 + u_2.
\end{eqnarray*}
In vector-matrix notation, the above model takes the form:
\[
\left[\begin{array}{c}
\dot{x}_1\\
\dot{x}_2\\
\dot{x}_3\\
\dot{x}_4
\end{array}\right]=\left[\begin{array}{c}
x_2\\
-K_1x_2-K_2x_3\sin(K_3x_1)+K_2x_4\cos(K_3x_1)\\
K_4x_2\sin(K_3x_1)-K_5x_3\\
-K_4x_2\cos(K_3x_1)-K_5x_4
\end{array}\right]+\left[\begin{array}{cc}
0 & 0\\
0 & 0\\
1 & 0\\
0 & 1
\end{array}\right]\left[\begin{array}{c}
u_1\\
u_2
\end{array}\right].
\]
The above model is in a regular form which is very convenient when designing controllers.
