Unipolar stepper motor operation library

Dependents:   LAB04_Oppgave1 test_stepper Stepper_Motor_Demo StepperMotorUni_Hello ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers StepperMotorUni.h Source File

StepperMotorUni.h

00001 /** Stepper Motor (Unipolar) control library
00002  *
00003  *  @class   StepperMotorUni
00004  *  @author  Tedd OKANO
00005  *  @version 1.1.3
00006  *  @date    13-Sep-2017
00007  *
00008  *  Copyright: 2010, 2014, 2015, 2017 Tedd OKANO
00009  *  Released under the Apache 2 license License
00010  *
00011  *  The library that controls stepper motor via motor driver chip
00012  *  This is a driver for a unipolar stepper motor.
00013  *
00014  *  Example:
00015  *  @code
00016  *  #include "mbed.h"
00017  *  #include "StepperMotorUni.h"
00018  *
00019  *  StepperMotorUni motor( p26, p25, p24, p23 );
00020  *
00021  *  int main()
00022  *  {
00023  *      motor.set_pps( 50 );
00024  *
00025  *      while ( 1 ) {
00026  *          motor.move_steps( 24 );
00027  *          wait( 1 );
00028  *
00029  *          motor.move_steps( -24 );
00030  *          wait( 1 );
00031  *      }
00032  *  }
00033  *  @endcode
00034  *
00035  *  version 0.51  (27-Nov-2010) //  initial version (un-published)
00036  *  version 0.6   (15-Jan-2014) //  compatible to LPC1768, LPC11U24 and LPC1114 targets
00037  *  version 1.0   (19-Jun-2014) //  version 1.0 release
00038  *  version 1.0.1 (14-Apr-2015) //  API document correction
00039  *  version 1.1   (21-Apr-2015) //  ramp control function enabled
00040  *  version 1.1.1 (22-Apr-2015) //  fixed: find_home_position compatibility with ramp control feature
00041  *  version 1.1.2 (27-Apr-2015) //  fixed: init_done behavior 
00042  *  version 1.1.3 (13-Sep-2017) //  fixed: keeping stopped position while power_ctrl is set false
00043  */
00044 
00045 #ifndef    MBED_STEPPERMOTOR_UNIPOLAR
00046 #define    MBED_STEPPERMOTOR_UNIPOLAR
00047 
00048 #include "mbed.h"
00049 
00050 #define MAX_PPS   100   //  pulse per second
00051 
00052 
00053 class StepperMotorUni
00054 {
00055 public:
00056 
00057     /** Constants for motor rotate mode */
00058     typedef enum  {
00059         ONE_PHASE,                  /**< 1 phase operation (default)    */
00060         TWO_PHASE,                  /**< 2 phase operation              */
00061         HALFSTEP                    /**< halfstep operation             */
00062     } OperationPhaseMode;
00063 
00064     /** Constants for motor rotate mode */
00065     typedef enum  {
00066         SHORTEST,                   /**< turn by shortest direction (default)   */
00067         NO_WRAPAROUND,              /**< do not accross home position           */
00068         CLOCKWISE_ONLY,             /**< one-way: clockwise turn                */
00069         COUNTER_CLOCKWISE_ONLY      /**< one-way: counter clockwise turn        */
00070     } RotMode;
00071     /** Constants for syncronization mode */
00072     typedef enum  {
00073         ASYNCHRONOUS,               /**< program does wait motor turn completion (default)  */
00074         SYNCHRONOUS                 /**< program doesn't wait motor turn completion         */
00075     } SyncMode;
00076 
00077     /** Constants for position detection edge polarity */
00078     typedef enum  {
00079         RISING_EDGE,                /**< position detection done by rising  edge */
00080         FALLING_EDGE                /**< position detection done by falling edge */
00081     } PositionDetectPorarity;
00082 
00083     /** Constants for position detection edge polarity */
00084     typedef enum  {
00085         SOFT_BRAKE,                /**< brake with slowing down */
00086         HARD_BRAKE                 /**< for immedate stop */
00087     } BrakeMode;
00088 
00089     /** Create a stepper motor object connected to specified DigitalOut pins and a DigitalIn pin
00090      *
00091      *  @param out_A DigitalOut pin for motor pulse signal-A
00092      *  @param out_B DigitalOut pin for motor pulse signal-B
00093      *  @param out_C DigitalOut pin for motor pulse signal-C
00094      *  @param out_D DigitalOut pin for motor pulse signal-D
00095      *  @param position_detect DigitalIn pin for home position detection (option). if not defined, "find_home_position()" function cannot be used
00096      */
00097     StepperMotorUni(
00098         PinName out_A,
00099         PinName out_B,
00100         PinName out_C,
00101         PinName out_D,
00102         PinName position_detect = NC
00103     ) ;
00104 
00105     /** Set the pulse width (i.e. motor turning speed)
00106      *
00107      *  @param v pulse per second (pps) : lower number makes the turn slower (default = 100)
00108      */
00109     float set_pps( float v );
00110 
00111     /** Set maximum PPS (= minimum pulse width) which will be used in finding home position
00112      *
00113      *  @param v maximum pulse per second : lower number makes the turn slower (default = 100)
00114      */
00115     void set_max_pps( float v );
00116 
00117     /** Find home position: rotate the motor until the detection edge comes.
00118      *
00119      *  Turns the motor until the home position detected.
00120      *  The "home position" is a reference point for the step and angle. It will be step=0 and angle=0.
00121      *  The detection signal edge can be defined by an argument.
00122      *  It follows the rotate mode.
00123      *  When the edge is detected, the motor will be stopped and it will be the new home position.
00124      *  If no detection signal detected, no home position update done.
00125      *
00126      *  @param edge defines detection edge rise or fall
00127      */
00128     int find_home_position( PositionDetectPorarity edge );
00129 
00130     /** Update home position
00131      *
00132      *  Set the home position as current motor position.
00133      */
00134     void set_home_position( void );
00135 
00136     /** Turn the motor to defined position (by steps from home position)
00137      *
00138      *  Make motor move to absolute position
00139      *
00140      *  @param v the position defined by steps from home position
00141      */
00142     int go_position( int v );
00143 
00144     /** Turn the motor to defined position (by angle (degree)) from home position)
00145      *
00146      *  Make motor move to absolute position
00147      *
00148      *  @param v the position defined by steps from home position
00149      */
00150     void go_angle( float angle );
00151 
00152     /** Turn the motor to defined position (by steps from current position)
00153      *
00154      *  Make motor move to defined position
00155      *
00156      *  @param v the position defined by steps from current position
00157      */
00158     int move_steps( int s );
00159 
00160     /** Turn the motor to defined rotation (from current position)
00161      *
00162      *  Make motor rotate
00163      *
00164      *  @param r number of rotation start from current position
00165      */
00166     int move_rotates( float r );
00167 
00168     /** Interface for opertion phase mode setting
00169      *
00170      *  @param v Driving phase mode change : ONE_PHASE (default), TWO_PHASE or HALFSTEP
00171      */
00172     void set_operation_phase_mode( OperationPhaseMode v );
00173 
00174     /** Interface for motor rotate mode setting
00175      *
00176      *  Example:
00177      *  @code
00178      *  StepperMotor    m( p21, p22, p23, p24 );
00179      *  int main() {
00180      *      m.set_rot_mode( StepperMotor::NO_WRAPAROUND );
00181      *      ...
00182      *  @endcode
00183      *
00184      *  @param m motor rotate mode : SHORTEST (default), NO_WRAPAROUND, CLOCKWISE_ONLY or COUNTER_CLOCKWISE_ONLY
00185      */
00186 
00187     void set_rot_mode( RotMode m );
00188 
00189     /** Interface for syncronization mode setting
00190      *
00191      *  Example:
00192      *  @code
00193      *  StepperMotor    m( p21, p22, p23, p24 );
00194      *  int main() {
00195      *      m.set_sync_mode( StepperMotor::NO_WRAPAROUND );
00196      *      ...
00197      *  @endcode
00198      *
00199      *  @param m motor rotate mode : ASYNCHRONOUS (default) or SYNCHRONOUS
00200      */
00201     void set_sync_mode( SyncMode m );
00202 
00203     /** Check remaining distance that motor need to move
00204      *
00205      *  software can check if the motor action completed in asynchronous mode
00206      *
00207      *  @return remaining steps that motor need to go
00208      */
00209     int distance( void );
00210 
00211     /** Pause/Resume the motor action
00212      *
00213      *  @param sw use "true" for pause, "false" (default) for resume
00214      */
00215     void set_pause( int sw );
00216 
00217     /** Pause/Resume the motor action
00218      *
00219      *  @param sw use "true" for pause, "false" (default) for resume
00220      */
00221     void brake( void );
00222     void brake( BrakeMode mode );
00223 
00224     /** Auto power control enable
00225      *
00226      *  If the auto power control is enabled, the motor power will be turned-off when it stays same place
00227      *
00228      *  @param sw use "true" for pause, "false" (default) for resume
00229      */
00230     void set_power_ctrl( int sw );
00231 
00232     /** Setting for steps/rotate
00233      *
00234      *  This parameter is required if program want to use the "go_angle()" interface.
00235      *  The angle will be calculated from this parameter.
00236      *
00237      *  @param steps per rotate
00238      */
00239     void set_steps_per_rotate( int steps );
00240 
00241     /** Setting for ramp control
00242      *
00243      *  This function enables the ramp-up and ramp-down of the motor behavior
00244      *
00245      *  @param initial_speed_rate speed rate. if the pps was set to 50pps and
00246      *    initial_speed_rate is given like 0.1, the ramp-up-start and ramp-dowm-end
00247      *    speed will be 5pps (= 0.1 * 50pps)
00248      *  @param ramp_steps the steps for ramp-up and ramp-down.
00249      */
00250     void set_ramp_control( float initial_speed_rate, int ramp_steps );
00251 
00252     int         init_done;
00253 
00254 private:
00255 
00256     Ticker      t;
00257     BusOut      motor_out;
00258     DigitalIn   position_detect_pin;
00259 
00260     static unsigned char  pattern_one_phase[ 4 ];  //  1 phase pulse pattern for motor control
00261     static unsigned char  pattern_two_phase[ 4 ];  //  1 phase pulse pattern for motor control
00262     static unsigned char  pattern_halfstep[ 8 ];   //  1 phase pulse pattern for motor control
00263     unsigned char               *pattern;
00264     int                         pat_index_mask;
00265     OperationPhaseMode          phase_mode;
00266     RotMode     rot_mode;
00267     SyncMode    sync_mode;
00268     int         max_pos;
00269     int         current_pos;
00270     int         pos_offset;
00271     int         target_pos;
00272     float       pps;
00273     float       max_pps;
00274     int         pause;
00275     int         power_ctrl;
00276     float       ramp_init_speed_rate;
00277     int         ramp_control_steps;
00278     float       ramp_rate;
00279 
00280     void set_target_pos( int p );  //  target position setting interface
00281     void motor_maintain( void );   //  this function is called periodically by Ticker
00282 };
00283 
00284 
00285 #endif