Applied Robotics/Mechanisms and Actuation/DC Stepper Motor

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Stepper Motor Basics[edit | edit source]

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[edit | edit source]

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.

Unipolar stepper driver

Bipolar Stepper Driver[edit | edit source]

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.

Bipolar stepper driver

Stepper Control Code[edit | edit source]

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 -
/**************************************************
 *
 * AVR Microcontroller Stepper Control Example
 * Cody Hyman <hymanc@onid.orst.edu>
 *
 **************************************************/
#include <avr/io.h>
#include <avr/interrupt.h>

#define STEP0 0b10001000     // Phase A Enabled, positive current
#define STEP1 0b00100100     // Phase B Enabled, positive current
#define STEP2 0b01001000     // Phase A enabled, negative current
#define STEP3 0b00010100     // Phase B Enabled, negative current

#define STEPPER_MASK 0xF0    // Upper 4 bits for DIR
#define STEPPER_EN_MASK 0x0C // Bits 2 and 3 for EN
#define STEPPER_ALL_MASK (STEPPER_MASK | STEPPER_EN_MASK) 
#define STEPPER_PORT PORTC   // Stepper I/O Port
#define DIRECTION_FWD 1
#define DIRECTION_REV -1

volatile int8_t stepper_state;  // Step state
volatile int8_t step_direction; // Step direction

/* Initialize the stepper I/O and timer */
void initialize_stepper()
{
 DDRC |= STEPPER_MASK | STEPPER_EN_MASK; // Initialize stepper outputs
 STEPPER_PORT |= STEP0;                  // Step to initial position
 // Enable time 3 for stepper timing via timer overflow interrupt
 TCCR3B = (1<<WGM33)|(1<<WGM32)|(1<<CS32);// Set WGM to CTC mode CLK/256
 ICR3 = 0xFFFF; // Default to maximum delay
 current_direction = DIRECTION_FWD;      // Set initial direction to fwd
 ETIMSK |= (1<<TOIE3);                   // Set Timer 3 Overflow Interrupt
 sei(); // Enable global interrupts
}

/* Adjusts the step delay */
void set_step_delay(uint16_t delay)
{
 ICR3 = delay; // Reset the top value
}

/* Step handler sequence */
void step(uint8_t direction)
{
 stepper_state = (stepper_state + direction) % 4; // Step to the next state
 // Handle I/O changes with the switch statement
 switch(stepper_state)
 {
  STEPPER_PORT &= ~(STEPPER_ALL_MASK);
  case(STEP0):
  {
   STEPPER_PORT |= STEP0;
   break;
  }
  case(STEP1):
  {
   STEPPER_PORT |= STEP1; 
   break;
  }
  case(STEP2):
  {
   STEPPER_PORT |= STEP2;
   break;
  }
  case(STEP3):
  {
   STEPPER_PORT |= STEP3;
   break;
  }
 }
}

/* Timer 3 overflow interrupt service routine */
void ISR(TIMER3_OVF_vect)
{
 step(current_direction); // Run step handler
};

/* Main */
int main(void)
{
 initialize_stepper();
 while(1)
 {
  // Nothing to do here, the interrupt handles motion control
 }
 return 0;
}

Off the Shelf Stepper Solutions[edit | edit source]

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.