# Applied Robotics/Printable version

The current, editable version of this book is available in Wikibooks, the open-content textbooks collection, at
https://en.wikibooks.org/wiki/Applied_Robotics

# Mechanisms and Actuation/DC Stepper Motor
## Stepper Motor Basics

A stepper motor is a DC motor that consists of a polyphase coil stator and a permanent magnet rotor. These motors are designed to cog into discrete commanded locations and will hold their position as current is ran through the motor. Every stepper motor will have a fixed number of steps per rotation that can be stepped through by changing the direction of the applied magnetic field in the motor. Rotation is achieved by continuously stepping the motor with a short delay in between steps. The shorter the delay, the higher the RPM.
Stepper motors work well for simple open loop position control applications where high torque and low RPM are needed. As speed increases, stepper torque greatly decreases, and excessively small step timing can lead to missed steps due to motor inertia.
Controlling a stepper motor requires a state machine that will commutate, or change the direction of the magnetic field in the motor, and apply a time delay in between steps to control motor velocity. This is typically done using a timer on a microcontroller and 4 I/O lines to control the current and therefore the magnetic field direction.
== Unipolar stepper motors == utilize center-tapped coils and switching elements that only draw current in one direction to change the current direction in the motor. These motors require twice the winding length for unipolar drive compared to a comparable bipolar stepper, and are thus less powerful for their weight than bipolar steppers. These motors typically have two center tapped phases.
== Bipolar stepper motors == utilize a single coil per phase and require the control electronics to switch current directions through the coil, typically with a single H-bridge per phase. Unipolar motors can also be driven in a bipolar fashion by leaving the center tap of each phase disconnected. These motors typically have two phases.

## Unipolar Stepper Driver

An efficient unipolar stepper driver can be built from 4 N-channel MOSFETs controlled by microcontroller I/O pins. Each of the coil center taps is connected to a positive voltage that the motor is rated for and each of the phase coil ends is connected to a different MOSFET drain. Each step is controlled by turning on one of the MOSFETs at a time. Note: If the MOSFETs will be directly driven off of microcontroller I/O levels, it is critically important that they are logic-level FETs that will turn on at 5V.

## Bipolar Stepper Driver

A bipolar stepper driver can be built using a 4-channel high current driver, such as an L293, SN754410, or L298 motor driver IC. Each of the outputs should have a pair of fast acting catch diodes to prevent damage to the driver IC from flyback voltages that occur as a result of motor inductance during switching. Each phase of the stepper motor is connected to two of the output drivers, forming two H-bridges and allowing for current to be driven bidirectionally through the two phases. Only one phase should be active at a time for a standard control scheme, so the output driver enables will have to be turned on and off in addition to controlling the direction of the output drivers.

## Stepper Control Code

Stepper control can be done on an AVR microcontroller using either a delay statement (good for simple testing, but terrible for accurate timing and program efficiency), or with a timer/counter and an interrupt (slightly more complicated, but much better efficiency wise). The following code shows how to utilize a timer counter on an ATmega device along with a simple state machine and I/O lines. Reversing direction just involves stepping in the opposite direction. This code can be used to control an L29x type motor driver to drive a bipolar stepper or a pull-down driver to control a unipolar stepper.
The basic step sequence is as follows for both types of two-phase stepper motor.
| Step | A Current | B Current |
| Step 1 | + | 0 |
| Step 2 | 0 | + |
| Step 3 | - | 0 |
| Step 4 | 0 | - |

## Off the Shelf Stepper Solutions

As an alternative to creating your own stepper driver/controller, a number of low-cost off the shelf products exist for controlling small steppers. Texas Instruments, Allegro, ST Microelectronics, and ON Semiconductor all make specialty microstepping driver ICs that operate on a simplified logic interface and provide a finer granularity of stepper control than the basic concept outlined above. A number of hobbyist vendors, such as Sparkfun and Pololu, carry breakout boards for these types of stepper drives.

# Sensors and Perception/Open CV
## What is OpenCV

OpenCV is a powerful free open source computer vision library maintained by Willow Garage that is widely used in image processing and recognition applications. OpenCV is a framework containing routines for image storage and retrieval, video capture, image processing, computer vision algorithms, GUI and windowing, and many other advanced features. From basic image thresholding to 3D stereo vision, OpenCV usually has a solution for most computer vision needs. OpenCV currently supports many platforms (Linux, BSD, Windows, and MacOSX) and supports bindings for the C, C++, and Python languages. OpenCV is notably faster at image processing than MATLAB in most cases but requires an application built around using the OpenCV library. OpenCV is also available as a ROSpackage in Robot Operating System. More information is available at the OpenCV website.

## How to Install/Build OpenCV for C/C++

Windows: In a Windows environment, OpenCV can be installed for both the Microsoft Visual C++ compiler or MinGW. The VC++ package is easiest to install and comes in a precompiled binary available: http://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.4.4/OpenCV-2.4.4.exe/download. This install requires that Microsoft Visual Studio be installed, which is available as a free download from Microsoft (Express Edition): http://www.microsoft.com/visualstudio/eng/products/visual-studio-express-for-windows-desktop
The full version of MS Visual Studio is available via student Dreamspark accounts accessible via the College of Engineering T.E.A.C.H. webpage.
Linux, Android, MacOSX, non VC++ Windows Compilers: Most other supported platforms require building OpenCV from source files. Full instructions and methods of obtaining the source code can be found in the official OpenCV install guide.

# Sensors and Perception/Open CV/Basic OpenCV Tutorial

This post explains the basics of using OpenCV for computer vision in Python.

## Installation

On Ubuntu (and other flavors of Debian), you can install all the necessary packages for OpenCV, Python, and the OpenCV-Python bindings by running: sudo apt-get install python-opencv This will install a somewhat outdated version of OpenCV (2.3.1), but it should be sufficient for what you need in this class. If you don't use a Debian flavor, consult your package manager or Google.

## Simple Image Capture

This is a very simple OpenCV program. It opens your video camera, reads an image, displays it, and repeats until you press a key. It's a good test to verify that your OpenCV setup is functional:

### Template OpenCV Program

This program implements the base functionality of any OpenCV application:

## Image Thresholding

A common way to simplify your computer vision problem is to convert your image into a black and white ("binary") image. The threshold used to decide what becomes black and what becomes white determines what features are ignored an which ones are important to your algorithm. Many thresholding algorithms expect the image input to be in grayscale. The following functions will take an image, convert it to grayscale, and then convert it to a binary image at the specified threshold. Be sure to include the functions from the template program (above) in here as well:

### Contour Detection

A simple object detection technique is to use contour finding, which finds the outline of shapes in your image. OpenCV can return different information on the contours it finds, including outer-contours, hierarchies, and simple lists. See the findContours function in the OpenCV documentation for more information (http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#findcontours).
In this example, we find the outermost contours of each shape (the outline of the pucks), and return a shape-approximation of the object:

### Finding Centroids Of Contours

Once you have found your contours, you may want to find the center of the shape. OpenCV can accomplish this by finding the moments (http://en.wikipedia.org/wiki/Image_moment) of your contour. The following functions can be used to find and display the centroids of a list of contours:

### Image Transforms

Because your cameras are (likely) not mounted directly above the center of the field, you will want to perform a perspective transform on the incoming video frames to correct for the skew in your image. Generally, this is done by choosing 4 known locations in a rectangle and finding the matrix which rotates/skews an image so the 4 points selected on the original image are placed in the correct locations. This example has the user click on 4 points during initialization in the corners of the board. A more advanced algorithm could use the fiducials (http://en.wikipedia.org/wiki/Fiduciary_marker) located on the 4 corners of the board.

## Final Puck Detector Application

Putting all of the above together, we can find pucks and their centers from a video stream. The complete source code can be downloaded from http://classes.engr.oregonstate.edu/engr/spring2013/engr421-001/code/opencv/puck_detector.py.
From Left to Right: Original Image Image after Perspective Transform Binary Image (Thresholding) Contours (red) and Centroids (blue) of Image

## Changing Camera Settings on Linux

If you have a camera which requires you to play around with its settings to get it to work on Linux, try using the "uvcdynctrl" tool. To get the Logitech webcams to work, I had to change their exposure setting to keep them from continuously adjusting their settings. Unfortunately, OpenCV can't control all aspects of webcams, so you need to use an external tool. Here's an example of using "uvcdynctrl" to show available devices, the available controls, and how to query and set control values:

# Microcontrollers/Getting Started
## Basics of Microcontrollers

A microcontroller is a programmable integrated circuit containing a microprocessor, memory, and I/O peripherals for general purpose control in embedded applications.

## Typical Peripherals
### Timer/Counters

Most microcontrollers contain timer/counter peripherals that increment a counter register on each cycle of an input clock (internally or externally generated). These peripherals are used for numerous purposes including program timing, timing external signals (i.e. encoders or pulse widths), and generating PWM signals.

### Analog-to-Digital Converters

Microcontrollers often contain analog-to-digital converters to read in analog voltages. These peripherals convert a voltage on command or periodically into a digital code that is readable by the processor. Some microcontroller ADCs contain an internal reference voltage, however some may require an external reference to operate.

### Interrupt Controllers
## Common Microcontroller Families
# Microcontrollers/Serial Communication
## Serial Communication Basics

Serial communication is a means of sending data between devices one bit at a time. This method only requires a few wires and I/O pins per device to facilitate communication. Typically, most serial devices send and receive byte-oriented data (8-bits per transaction), and have a dedicated transmit and receive line (full-duplex). Serial communication is typically handled by specialized hardware peripherals known as USARTs (Universal Synchronous-Asynchronous Receiver-Transmitters) that handle low level formatting, shifting, and recovery of data.
Most ATmega series AVR microcontrollers have 1-2 USARTs available. Some modern microcontrollers have 5-7 separate USARTs.

### Serial Frame Structure

Serial transactions are broken up into what are called frames. Each frame consists of the payload data byte being sent along with a set number of bits that "frame" or format the data. Most USARTs, including the AVR USARTs, are compatible with a frame structure such as this one.
The serial line idles at a logic high level until the transmission starts. Each transmission is started with a single logic-low bit known as a start bit. After the start bit, the payload data is shifted out, typically with the least-significant bit first. After the last data bit is shifted out, an optional parity bit is sent. The parity bit takes on a value of 0 or 1 depending on the number of logic 1 bits in the data and can be used by the receiver to detect an erroneous transmissions in which one of the bits are flipped. Finally, the serial line returns to logic high for a configurable period of 1 or 2 bit-times; these are known as stop bits. AVR USARTs are capable of sending frames with 5-9 data bits, with even, odd, or no parity, and 1 or 2 stop bits. In the frame above, a logic-high signal corresponds to a '0' while a logic-low signal corresponds to a '1'.

### Synchronous vs. Asynchronous Serial

Serial communication with a USART can be done both synchronously (data with a dedicated clock signal) or asynchronously (data without a dedicated clock signal). Using a synchronous serial line requires that extra wiring is included for the clock signals, and is often times unnecessary at typical serial data rates. Typically, most standard serial communication, especially between a PC and a microcontroller, is done asynchronously.

### Baud Rate

The baud rate specifies the frequency at which bits are sent. The baud rate can be calculated by dividing 1 over the bit time in seconds. Setting the baud rate correctly for asynchronous serial is very important as the receiver must recover a clock signal from the incoming data based on a predetermined clock frequency. Common baud rates available on a PC are 1.2k, 2.4k, 4.8k, 9.6k, 19.2k, 38.4k, 57.6k, 115.2k, 230.4k, 460.8k, and 921.6k.

### Logic Levels

There are many different standard serial logic levels used. Most PC interfaces use RS-232 serial, that specifies logic levels between -15V and -3V and +3 to +15V with idle-low lines. AVR USARTs use CMOS logic levels between 0-5V and have idle-high serial lines that are typically compatible with standard 3.3V or 5V logic outputs. The appropriate serial logic levels must be used between devices, and for that purpose, special level shifters may be required.
Texas Instruments manufactures RS-232 to CMOS/TTL level shifters for RS-232 use. These devices have a built in charge pump for providing the RS-232 levels and require only a few external components. An example of these chips is the MAX232 http://www.ti.com/product/max232.

## Setting up a USART on an ATmega Microcontroller

The following example shows how to set up a serial port on an ATmega128 using USART0 as an asynchronous serial device operating at 115200 baud. The ATmega128 is using a 16 MHz clock in this example. The program should initialize USART0, and send out the byte 0xAA on its transmitter. It will continue to check its receiver for new data and will set PB0 high if it receives the 0xAA byte. This allows for a quick loopback test and demonstrates basic USART concepts.

## USB - Serial Bridges

Typically most modern PCs do not have a dedicated RS-232 serial port available, so an alternative must be used. Many vendors sell both RS-232 and TTL or LVTTL level adapters for a low cost. Many of these devices typically require drivers, so follow all manufacturer setup instructions.
Plugable USB to RS-232 Adapter
FTDI UMFT230XB USB to 3.3V Serial Adapter
FTDI TTL-232R-5V-PCB USB to 5V (or 3.3V) Serial Adapter

### On Windows machines

Serial adapters typically enumerate as a COMx port. Check your device manager to determine the COM port number.

### On Linux machines

Serial adapters typically enumerate as ttyUSBx devices. Check your /dev/ directory for ttyUSB devices to determine the tty device number.

## RS-232 Pinouts

The following pinouts are commonly found standard RS-232 DE-9 connectors. The male DTE pinout corresponds to the pins on the PC or serial adapter side. The female DCE pinout corresponds to the end device, such as a microcontroller board with an RS-232 port. RS-232 also specifies a number of other flow control lines that were commonly used in old serial modems, however for most cases, just the TxD/RxD and ground lines are needed.

### Common Errors

Common errors include improperly wiring the DCE side connector Tx/Rx pins or accidentally using an RS-232 crossover cable rather than a straight cable. It's strongly advised to check these factors if your RS-232 communication does not work.

## Serial Terminal Software

A variety of serial terminal programs exist that allow for quick sending and receiving of serial data. These are useful for debugging and testingrial devices.

### Windows Serial Programs

Realterm - A very capable GUI based serial terminal. - http://realterm.sourceforge.net/ Linux Serial Programs: Minicom: A very popular terminal based serial interface program - Debian package name "minicom" Cutecom: A GUI based serial terminal program - Debian package name "cutecom"

### Python Serial Module (PySerial)

An easy cross platform library for PC serial communication is PySerial. Python version 2.7 is recommended over Python 3.x. Pyserial allows for easy serial port management and manages sending and receiving data over a COM or tty port in Windows or Linux respectively. Pyserial and the accompanying documentation can be found at - http://pyserial.sourceforge.net/

# Microcontrollers/Reading Sensors
## Reading Digital Inputs

Simple 1-bit digital sensors are the simplest type of input to read on a microcontroller. Most pins on microcontrollers are usable as general purpose I/O, which allows for the interfacing of numerous 1-bit digital sensors. Examples of these sensors might be limit switches, IR proximity detectors, high gain photodetectors (light sensors), or pushbuttons.
Binary on/off switching type sensors are very easy to integrate simply by having a pull-up or pull-down resistor connected to the switch output.
To read a sensor on an AVR microcontroller, two registers must be used to handle pin setup and reading. These two registers are:
* DDRx
* PINx
Where the x indicates an arbitrary I/O port (i.e. A, B, C, D, etc.)
The DDR (Data Direction Register) is used to configure the specified pin as an input or output for a given port. A "1" bit for each location specifies that that port pin is set as an output, and a "0" bit for each location specifies that the pin is configured as an input.
The PIN register is used for reading the digital state of all 8 pins on each port, returning a 1 for logic high and 0 for logic low states for each bit. Note that this is not the PORTx register. PORTx is used for setting output values, while PINx is used for reading inputs.
To use a digital input, you must configure the pin as an input, and then read the appropriate bit from the PINx register. The simplest way to extract a single bit from PINx is to use a bitmask.
In these examples, the bitmask is used to remove the state of all other pins on the port from the conditional. Like with digital outputs, you can use AVR-GCC macros to make your code easier to read:

## Reading Analog Inputs

Many sensors measure a continuous signal, such as the position of a knob, the light level in the room, or the distance of an object from a sensor. Often, the output of these sensors is a voltage which is linearly related to the measured signal. To interface with these sensors, you must use an Analog Digital Converter (ADC). Most microprocessors contain several ADCs. An ADC converts the voltage into an integer, the size of which depends on the accuracy of the ADC. Most ADCs are 8- or 10- bit, meaning they use 8 or 10 bits to represent the entire range (from 0 to Vcc). A 10-bit ADC discretizes the analog voltage into values, ranging from 0 (0 V) to 1023 (Vcc).
AVR's ADCs require several steps to configure and read an analog signal. In addition, only pins on the microcontroller can be used as analog inputs. Refer to your microcontroller's datasheet to find which pins can be used with the ADC (they are typically labeled ADCn, where n is a number). The registers involved with ADCs are:
* PINx: Configures pin as an input
* ADMUX: Selects the ADC input being read
* ADCSRA: ADC Control Register
* ADCL: Low 8 bits of the ADC conversion
* ADCH: High bits of the ADC conversion
Because ADC pins can also be used as digital inputs/outputs, they must be configured as an input to be read by the ADC. The ADMUX register is used to select which ADC is being converted, and for single-ended inputs, is simply a number between 0..N, where N is the last ADC pin on the MCU. ADCSRA stands for ADC Control And Status Register A, and is used to begin a conversion and check when it has completed. ADCL and ADCH contain the result of the conversion.