\subsection{Stepper control}% -   -   -   -   -   -   -   -   -   -   -   -   -
\label{sec.stepper}

Because of thermal drift and mechanical instability, the distance between the tip and the surface changes significantly over time.
When the distance change exceeds the range of the piezo scanner, the stepper motor must be engaged to reposition the AFM tip relative to the sample.
The previous software lacked the ability to control the motor on its own, so it would pause roughly every half hour and prompt the operator to make the necessary manual adjustments.
Automatic motor control would allow the system to run longer without interrupts, facilitating the collection of large data sets.
As a side benefit, automatic motor control would make equipment setup easier, as getting the tip close enough to seal the fluid cell o-ring without breaking the tip off on the surface is difficult for novices.
Although automatic control of a stepper motor is a fairly simple task, difficulties in integrating the new control with the old experiment software led to the creation of the experiment server project discussed in Section~\ref{sec.server} and an increased understanding of the inner workings of our AFM.

%\subsubsection{Motor line control}

The first step in controlling the motor was to understand the way it was currently being controlled.
Our AFM is a MultiMode II with a NanoScope IIIa controller.
The MultiMode contains the piezo and motor for positioning the surface, as well as a circuit board handling some portion of the control logic and signal processing.

The motor can be controlled either by a switch on the body of the MultiMode, or programmatically via the NanoScope.
Because there was NanoScope control of the motor, the easiest entry point seemed to be the DB-25 cable connecting the MultiMode to the NanoScope (see Figure~\ref{fig.equip_layout}).
The task was to identify which signals to send down which lines in the connecting cable.

By monitoring any likely control lines with an oscilloscope while controlling the motor from the NanoScope, we were able to identify the four motor lines carrying rectangular wave signals.
This made it likely that the NanoScope control lines carried the information about whether or not to pass current through each of the four motor coils.
We built a break-out box to gain access to the relevant lines, wrote a simple test program
 to move the motor in one direction\cite{jones}, and succeeded in visibly moving the motor.

We verified the stability and reproducibility of the microscopic movement of the motor by making several approach-retreat cycles from the surface of $\sim$70 steps which resulted in the data displayed in Figure~\ref{fig.stepper_stability}.
We also measured the distance the surface moved with every step by determining the change in deflection voltage as a function of peizo position as we stepped the AFM tip closer the the surface.
Our stepsize data is displayed in Figure~\ref{fig.step_size}.
\begin{figure}[bt]
  \centering
  \subfloat[Hysteresis.]{\label{fig.stepper_stability}\includegraphics{mp/stepper.1}}
  \hspace{\stretch{1}}
  \subfloat[Stepsize.]{\label{fig.step_size}\includegraphics{mp/stepper.2}}
  \caption{
   (a) Stepper motor reproducibility, stability, and backlash.  The data are from a single continuous counterclockwise trace of 14 approach-retreat cycles.
    The jump from about (18, -6) to (17,-0.4) is the snap-off effect, where short-range attractive interactions between the tip and the sample require the tip to be actively pulled off surface.
    Signal noise is comparable to that expected by drift.
   (b) Motor step size calibration.
     The stepper gradually stepped closer to the surface, feeling forward with the piezo after each step.
     Successive motor positions yield traces $a$, $b$, $c$, $d$, $e$, and $f$.
     As the motor moves the sample closer, less piezo movement is required to approach to same deflection level.
     The average spacing between the traces is roughly 170nm.
     Traces $c$ and $d$ have regions of negative deflection because the tip no longer retracts far enough from the surface to break free of the snap-off effect.
     There is no backlash because the data were taken during a single approach.}
\end{figure}

The motor is very consistent when approaching the surface, which indicates that our control software is operating correctly.
However, the motor exhibits some hysteretic behavior on a scale of $\sim$46 steps, which is almost certainly due to \emph{backlash}, or slack in the motor-surface coupling machinery.
The first 46 steps in a new direction take the slack out of the coupling, and further steps move the tip relative to the surface.
The problem can be avoided entirely by simply replacing ``backwards motion by one step'' with ``backwards motion by 60 steps and forward motion by 59 steps''.

One issue raised by backlash is that it might be the source of some of our surface drift, as the drive-chain relaxes towards some central value and pulls the surface with it.
By oscillating into our eventual position, we could perhaps settle the system at the beginning, reducing the need for adjustments later on.
While this is not a problem for the current unfolding experiments, it could be an issue for longer unfolding-refolding experiments.

%\subsubsection{Total photodiode voltage}

Having achieved motor control, the next order of business was to automatically approach the surface at the beginning of the experiment.
As we already had access to the AFM's cantilever deflection signal, it was little trouble to implement our first algorithm, which simply stepped closer to the surface until some deflection set-point was acquired.

The first run of this program broke the tip off smashing into the surface.
We realized that when the cantilever entered the bubble of liquid on the surface, the extra diffraction was deflecting the laser away from the photodiode.
The photodiode still reported almost no deflection, but the reports were not valid.
We needed to measure the total photodiode voltage as well, in order to differentiate the valid deflection signals from the invalid.

We expected the total photodiode voltage to also pass through one of the lines on the cable connecting the MultiMode to the NanoScope but after much searching were unable to determine which line carried the signal.
The total photodiode voltage is displayed on the MultiMode LCD screen, so we changed tactics and tried to determine which line connecting the MultiMode main board to the LCD screen carried the signal.
After isolating the relevant line, we build a splitter cable to send the total photodiode voltage out through an auxiliary line in the MultiMode-NanoScope connector cable, and tapped into the auxiliary line through our break-out box.

We extended our initial approach algorithm to only trust validated deflection signals (see Appendix~\ref{sec.approach_surface} for an implementation of the adjusted algorithm), and the new algorithm successfully approached the surface, or stops and signals the user that the photodiode has lost the signal.
The process is not completely automatic, as the laser alignment knobs in the AFM head can only be adjusted manually, but laser re-alignment is only an issue during the initial setup and in cases of extreme drift, so this restriction should not be overly limiting.

%\nopagebreak[4]

