Biblioteca com go_angle

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers StepperMotor.h Source File

StepperMotor.h

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