Stepper Motor to BrainStem Example
Last Modified: 2006-11-01
find:

basket

Acroname Robotics PDF webpage version Stepper Motor to BrainStem Example PDF

Related
Products

Product image for BrainStem Moto 1.0 Module
BrainStem Moto 1.0 Module
Product image for 1A Stepper Motor Bridge
1A Stepper Motor Bridge
Product image for H-Bridge Motor Driver Chip
H-Bridge Motor Driver Chip

Contents

Introduction

This example shows how to use the built-in bipolar stepper motor control features of a Moto board.  A bipolar stepper motor has two electromagnetic coils that control its stepping action.  Energizing the coils in the proper sequence with the proper polarity makes the motor step.  Since the coil polarity must change, each coil requires its own h-bridge.  A dual h-bridge chip such as the Texas Instruments SN754410NE makes a handy stepper motor driver. 

The standard functions of the H-Bridge Interface pins change in Stepper Mode.  The A and B encoder inputs are used as outputs for controlling one coil and the PWM and DIR outputs control the other coil.  The diagram below shows a possible design for a stepper motor driver board that will be compatible with the Moto board. 

Circuit Schematic

The following diagram shows how to wire up the H-bridge driver chip to interface with the Moto controller. 

Wiring diagram for this example.

Source Code

This program demonstrates fixed-speed motions and ramp motions with the three different stepper modes: wave, high-torque, and half-step.  The program has two stepper motor routines, move_stepper and ramp_stepper.  The first routine moves the stepper a desired number of ticks with a fixed time delay between each tick.  The second routine moves the stepper a desired number of ticks using a trapezoidal velocity profile.  It ramps up to a the desired maximum speed and ramps down at the same rate to reach the desired number of steps. 

Velocity is inversely proportional to the time between steps.  In order to get a linear rate of change in velocity, the ramp routine calculates a time delay by dividing a constant by the desired velocity.  This constant depends on the stepper motor.  This example program is for a stepper motor with 72 steps per revolution, or 5 degrees per step.  Degrees per second make convenient units for velocity.  To calculate a step delay to achieve a desired velocity, T = (5 degrees per step) / (V degrees per second).  This yields T seconds per step.  A factor of 10000 must be included since TEA programs measure time in units of 0.1ms.  The final formula is T=50000/V.  Since TEA can only handle integers in the range -32768 to 32767, this formula is altered slightly to get T=(25000/V)*2.  Before changing the formula for a different stepper motor, it will be necessary to check that it creates meaningful time delays for the range of acceptable velocity values.  An improper formula could generate time delays of 0 or overflow and cause an error during program execution. 

When you compile the code, note that the size exceeds the 1K (1024) byte limit of a normal TEA file slot.  The Moto board has an extra 16K file slot for big programs.  This is file slot 11.  You will need to use the following instructions to compile, load, and run the program:

steep "step"
load "step" 4 11
launch 4 11
/* step.tea */ #include <aCore.tea> #include <aPrint.tea> #include <aMotion.tea> #define WHOLESTEP 0x00 #define MAXTORQUE 0x04 #define HALFSTEP 0x08 #define VELCONST 25000 void init_stepper(char c, char flags) { aMotion_SetMode(c, aMOTION_MODE_STEP, flags); } void move_stepper(char c, int steps, int steptime) { int i = 0; aMotion_SetParameter(c, aMOTION_PARAM_PERIOD, steptime); aMotion_SetEnc32(c, 0, 0); aMotion_SetSetpoint(c, steps); while (i != steps) { aCore_Sleep(steptime); i = aMotion_GetEnc32L(c); } } void ramp_stepper(char c, int steps, int minvel, int maxvel, int dvel) { int t; int n; int vel; int nhalf; int ninc; int nrampdown; if (steps < 0) { ninc = -1; } else { ninc = 1; } n = 0; vel = minvel; nhalf = steps / 2; aMotion_SetEnc32(c, 0, 0); /* ramp up */ while ((vel <= maxvel) && (n != nhalf)) { aMotion_SetSetpoint(c, ninc); t = (VELCONST / vel) * 2; aCore_Sleep(t); vel = vel + dvel; n = n + ninc; } /* constant speed */ /* (skip if half the steps are already done) */ nrampdown = steps - n; if (n != nhalf) { while (n != nrampdown) { aMotion_SetSetpoint(c, ninc); aCore_Sleep(t); n = n + ninc; } } /* ramp down */ while ((vel >= minvel) && (n != steps)) { aMotion_SetSetpoint(c, ninc); vel = vel - dvel; t = (VELCONST / vel) * 2; aCore_Sleep(t); n = n + ninc; } } void main() { int i; for (i = 0; i < 3; i++) { switch (i) { case 0: { aPrint_String("Normal whole step mode\n"); init_stepper(0, WHOLESTEP); break; } case 1: { aPrint_String("Maximum torque mode\n"); init_stepper(0, MAXTORQUE); break; } case 2: { aPrint_String("Half step mode\n"); init_stepper(0, HALFSTEP); break; } } aCore_Sleep(1000); aPrint_String(" 100 ticks, 20ms per step\n"); move_stepper(0, 100, 200); aCore_Sleep(10000); aPrint_String("-100 ticks, 20ms per step\n"); move_stepper(0, -100, 200); aCore_Sleep(10000); aPrint_String(" 500 ticks, Ramp, dvel=5dps\n"); ramp_stepper(0, 500, 100, 500, 5); aCore_Sleep(10000); aPrint_String(" 100 ticks, Ramp, dvel=5dps\n"); ramp_stepper(0, 100, 100, 500, 5); aCore_Sleep(10000); aPrint_String("-500 ticks, Ramp, dvel=5dps\n"); ramp_stepper(0, -500, 100, 500, 5); aCore_Sleep(10000); aPrint_String("-100 ticks, Ramp, dvel=5dps\n"); ramp_stepper(0, -100, 100, 500, 5); aCore_Sleep(10000); } }

Revision History:

  • 2005-11-04: Example Created.
 

Related Links:

Acroname Supported H-Bridge Standard

Moto 1.0 Module Stepper Motor Mode Description

voice: 720-564-0373, email: sales@acroname.com, address: 4822 Sterling Dr., Boulder CO, 80301-2350, privacy
© Copyright 1994-2010 Acroname, Inc., Boulder, Colorado. All rights reserved.