Stepper motor control class library

Dependents:   StepperMotor_HelloWorld

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers StepperMotor.h Source File

StepperMotor.h

00001 /** Stepper Motor control library
00002  *  
00003  *  @class   StepperMotor
00004  *  @author  Tedd OKANO
00005  *  @version 0.51(27-Nov-2010)
00006  *  
00007  *  Copyright: 2010 Tedd OKANO, Tsukimidai Communications Syndicate - Crawl Design
00008  *  The library that controls stepper motor via motor driver chip: TA7774
00009  *  The TA7774 is a driver for a bipolar stepper motor. 
00010  *  With this library, mbed will generate 2 phase pulses to operate the motor. 
00011  */
00012  
00013 #ifndef    MBED_STEPPERMOTOR
00014 #define    MBED_STEPPERMOTOR
00015 
00016 #include "mbed.h"
00017 
00018 #define MAX_PPS   100   //  pulse per second
00019 
00020 /** Stepper Motor control class
00021  *
00022  *  Example:
00023  *  @code
00024  *  #include "mbed.h"
00025  *  #include "StepperMotor.h"
00026  *
00027  *  StepperMotor m( p21, p22, p23, p24 );
00028  *
00029  *  int main() {
00030  *      m.set_steps_per_rotate( 480 );
00031  *      m.set_sync_mode( StepperMotor::SYNCHRONOUS );
00032  *      m.set_power_ctrl( true );
00033  *
00034  *      while( 1 ) {
00035  *          m.go_angle( 120 );
00036  *          wait( 0.5 );
00037  *     
00038  *          m.go_angle( 240 );
00039  *          wait( 0.5 );
00040  *   
00041  *          m.go_angle( 0 );
00042  *          wait( 0.5 ); 
00043  *
00044  *          m.go_angle( 240 );
00045  *          wait( 0.5 );
00046  *     
00047  *          m.go_angle( 120 );
00048  *          wait( 0.5 );
00049  *   
00050  *          m.go_angle( 0 );
00051  *          wait( 0.5 ); 
00052  *      }
00053  *  }
00054  *  @endcode
00055  */
00056 
00057 class StepperMotor {
00058 public:
00059 
00060     /** Constants for motor rotate mode */     
00061     typedef enum  {
00062         SHORTEST,                   /**< turn by shortest direction      */
00063         NO_WRAPAROUND,              /**< do not accross home position    */
00064         CLOCKWISE_ONLY,             /**< one-way: clockwise turn         */
00065         COUNTER_CLOCKWISE_ONLY      /**< one-way: counter clockwise turn */
00066     } RotMode;
00067 
00068     /** Constants for syncronization mode */     
00069     typedef enum  {
00070         ASYNCHRONOUS,               /**< program does wait motor turn completion     */
00071         SYNCHRONOUS                 /**< program doesn't wait motor turn completion  */
00072     } SyncMode;
00073 
00074     /** Constants for position detection edge polarity */     
00075     typedef enum  {
00076         RISING_EDGE,                /**< position detection done by rising  edge */
00077         FALLING_EDGE                /**< position detection done by falling edge */
00078     } PositionDetectPorarity;
00079 
00080     /** Create a stepper motor object connected to specified DigitalOut pins and a DigitalIn pin
00081      *
00082      *  @param out_A DigitalOut pin for motor pulse signal-A
00083      *  @param out_B DigitalOut pin for motor pulse signal-B
00084      *  @param out_PWR DigitalOut pin for TA7774's power control (option)
00085      *  @param position_detect DigitalIn pin for home position detection (option)
00086      */
00087     StepperMotor(
00088         PinName out_A   = p21,
00089         PinName out_B   = p22,
00090         PinName out_PWR = p23,
00091         PinName position_detect = p24
00092     ) ;
00093 
00094     /** Set the pulse width (i.e. motor turning speed)
00095      *
00096      *  @param v pulse per second : lower number makes the turn slower (default = 100)
00097      */
00098     float set_pps( float v );
00099 
00100     /** Set maximum PPS (= minimum pulse width) which will be used in finding home position
00101      *
00102      *  @param v maximum pulse per second : lower number makes the turn slower (default = 100)
00103      */
00104     void set_max_pps( float v );
00105 
00106     /** Find home position: rotate the motor until the detection edge comes.
00107      *
00108      *  Turns the motor until the home position detected. 
00109      *  The "home position" is a reference point for the step and angle. It will be step=0 and angle=0. 
00110      *  The detection signal edge can be defined by an argument. 
00111      *  It follows the rotate mode. 
00112      *  When the edge is detected, the motor will be stopped and it will be the new home position. 
00113      *  If no detection signal detected, no home position update done. 
00114      * 
00115      *  @param edge defines detection edge rise or fall
00116      */
00117     int find_home_position( PositionDetectPorarity edge );
00118     
00119     /** Update home position
00120      *
00121      *  Set the home position as current motor position.
00122      */     
00123     void set_home_position( void );
00124     
00125     /** Turn the motor to defined position (by steps from home position)
00126      *
00127      *  Make motor move to absolute position
00128      *
00129      *  @param v the position defined by steps from home position
00130      */     
00131     int go_position( int v );
00132 
00133     /** Turn the motor to defined position (by angle (degree)) from home position)
00134      *
00135      *  Make motor move to absolute position
00136      *
00137      *  @param v the position defined by steps from home position
00138      */     
00139     void go_angle( float angle );
00140 
00141     /** Turn the motor to defined position (by steps from current position)
00142      *
00143      *  Make motor move to defined position
00144      *
00145      *  @param v the position defined by steps from home position
00146      */     
00147     int move_steps( int s );
00148     
00149     /** Interface for motor rotate mode setting
00150      *
00151      *  Example:
00152      *  @code
00153      *  StepperMotor    m( p21, p22, p23, p24 );
00154      *  int main() {
00155      *      m.set_rot_mode( StepperMotor::NO_WRAPAROUND );
00156      *      ...
00157      *  @endcode
00158      *
00159      *  @param m motor rotate mode : SHORTEST (default), NO_WRAPAROUND, CLOCKWISE_ONLY or COUNTER_CLOCKWISE_ONLY
00160      */
00161     void set_rot_mode( RotMode m );
00162 
00163     /** Interface for syncronization mode setting
00164      *
00165      *  Example:
00166      *  @code
00167      *  StepperMotor    m( p21, p22, p23, p24 );
00168      *  int main() {
00169      *      m.set_sync_mode( StepperMotor::NO_WRAPAROUND );
00170      *      ...
00171      *  @endcode
00172      *
00173      *  @param m motor rotate mode : ASYNCHRONOUS (default) or SYNCHRONOUS
00174      */
00175     void set_sync_mode( SyncMode m );
00176 
00177     /** Check remaining distance that motor need to move
00178      *
00179      *  software can check if the motor action completed in asynchronous mode
00180      *  
00181      *  @return remaining steps that motor need to go
00182      */
00183     int distance( void );
00184 
00185     /** Pause/Resume the motor action
00186      *
00187      *  @param sw use "true" for pause, "false" (default) for resume
00188      */
00189     void set_pause( int sw );
00190 
00191     /** Auto power control enable
00192      *
00193      *  If the auto power control is enabled, the motor power will be turned-off when it stays same place
00194      *  
00195      *  @param sw use "true" for pause, "false" (default) for resume
00196      */
00197     void set_power_ctrl( int sw );
00198 
00199     /** Setting for steps/rotate
00200      *
00201      *  This parameter is required if program want to use the "go_angle()" interface. 
00202      *  The angle will be calculated from this parameter.
00203      *  
00204      *  @param steps per rotate
00205      */
00206     void set_steps_per_rotate( int steps );
00207     
00208 private:
00209 
00210     Ticker      t;
00211     BusOut      motor_out;
00212     DigitalOut  pwr_out;
00213     DigitalIn   position_detect_pin;
00214 
00215     static const unsigned char  pat[ 4 ];  //  2 phase pulse pattern for motor control
00216     RotMode     rot_mode;
00217     SyncMode    sync_mode;
00218     int         max_pos;
00219     int         current_pos;
00220     int         pos_offset;
00221     int         target_pos;
00222     float       pps;
00223     float       max_pps;
00224     int         init_done;
00225     int         pause;
00226     int         power_ctrl;
00227 
00228     void set_target_pos( int p );  //  target position setting interface
00229     void motor_maintain( void );   //  this function is called periodically by Ticker
00230 };
00231 
00232 
00233 #endif