Unipolar Stepper Motor Library, Supports Single Step and Half Step, Directions Supported, Speed Configuration supported in MSPS(MIlliseconds per step), Interrupt mode supported SYNCRONOUS.

Committer:
dwijaybane
Date:
Mon Feb 01 06:20:28 2016 +0000
Revision:
0:950f1bb3ff9f
Stepper Motor Library created;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dwijaybane 0:950f1bb3ff9f 1 /** Stepper Motor (Unipolar) control library
dwijaybane 0:950f1bb3ff9f 2 *
dwijaybane 0:950f1bb3ff9f 3 * @class StepperMotorUni
dwijaybane 0:950f1bb3ff9f 4 * @author Dwijay.Edutech Learning Solutions
dwijaybane 0:950f1bb3ff9f 5 * @version 1.0
dwijaybane 0:950f1bb3ff9f 6 * @date 1-Feb-2016
dwijaybane 0:950f1bb3ff9f 7 *
dwijaybane 0:950f1bb3ff9f 8 * The library that controls stepper motor via motor driver chip
dwijaybane 0:950f1bb3ff9f 9 * This is a driver for a unipolar stepper motor.
dwijaybane 0:950f1bb3ff9f 10 *
dwijaybane 0:950f1bb3ff9f 11 * Example:
dwijaybane 0:950f1bb3ff9f 12 * @code
dwijaybane 0:950f1bb3ff9f 13 * #include "mbed.h"
dwijaybane 0:950f1bb3ff9f 14 * #include "StepperMotorUni.h"
dwijaybane 0:950f1bb3ff9f 15 *
dwijaybane 0:950f1bb3ff9f 16 * StepperMotorUni motor( p26, p25, p24, p23 );
dwijaybane 0:950f1bb3ff9f 17 *
dwijaybane 0:950f1bb3ff9f 18 * int main()
dwijaybane 0:950f1bb3ff9f 19 * {
dwijaybane 0:950f1bb3ff9f 20 * motor.set_operation_mode(StepperMotorUni::STEP);
dwijaybane 0:950f1bb3ff9f 21 *
dwijaybane 0:950f1bb3ff9f 22 * while ( 1 ) {
dwijaybane 0:950f1bb3ff9f 23 * motor.rotate_angle(StepperMotorUni::CLOCKWISE,90,0.02);
dwijaybane 0:950f1bb3ff9f 24 * wait( 1 );
dwijaybane 0:950f1bb3ff9f 25 *
dwijaybane 0:950f1bb3ff9f 26 * motor.rotate_angle(StepperMotorUni::COUNTER_CLOCKWISE,90,0.02);
dwijaybane 0:950f1bb3ff9f 27 * wait( 1 );
dwijaybane 0:950f1bb3ff9f 28 * }
dwijaybane 0:950f1bb3ff9f 29 * }
dwijaybane 0:950f1bb3ff9f 30 * @endcode
dwijaybane 0:950f1bb3ff9f 31 */
dwijaybane 0:950f1bb3ff9f 32
dwijaybane 0:950f1bb3ff9f 33 #ifndef MBED_STEPPERMOTOR_UNIPOLAR
dwijaybane 0:950f1bb3ff9f 34 #define MBED_STEPPERMOTOR_UNIPOLAR
dwijaybane 0:950f1bb3ff9f 35
dwijaybane 0:950f1bb3ff9f 36 #include "mbed.h"
dwijaybane 0:950f1bb3ff9f 37
dwijaybane 0:950f1bb3ff9f 38 /* Default Value */
dwijaybane 0:950f1bb3ff9f 39 #define MAX_MSPS 0.5 // 500 millisecond per step
dwijaybane 0:950f1bb3ff9f 40
dwijaybane 0:950f1bb3ff9f 41 /******************************************************************************/
dwijaybane 0:950f1bb3ff9f 42 /* Stepper Motor Calibration */
dwijaybane 0:950f1bb3ff9f 43 /******************************************************************************/
dwijaybane 0:950f1bb3ff9f 44 #define CAL_ANGLE 1.8 // Stepper motor calibration angle
dwijaybane 0:950f1bb3ff9f 45
dwijaybane 0:950f1bb3ff9f 46
dwijaybane 0:950f1bb3ff9f 47 /******************************************************************************/
dwijaybane 0:950f1bb3ff9f 48 /* Stepper Motor Selection */
dwijaybane 0:950f1bb3ff9f 49 /******************************************************************************/
dwijaybane 0:950f1bb3ff9f 50 #define GENERAL false
dwijaybane 0:950f1bb3ff9f 51 #define STM601 true
dwijaybane 0:950f1bb3ff9f 52
dwijaybane 0:950f1bb3ff9f 53 class StepperMotorUni
dwijaybane 0:950f1bb3ff9f 54 {
dwijaybane 0:950f1bb3ff9f 55 public:
dwijaybane 0:950f1bb3ff9f 56
dwijaybane 0:950f1bb3ff9f 57 /** Constants for motor rotate mode */
dwijaybane 0:950f1bb3ff9f 58 typedef enum {
dwijaybane 0:950f1bb3ff9f 59 STEP, /**< Single step operation (default) */
dwijaybane 0:950f1bb3ff9f 60 HALFSTEP /**< half step operation */
dwijaybane 0:950f1bb3ff9f 61 } OperationMode;
dwijaybane 0:950f1bb3ff9f 62
dwijaybane 0:950f1bb3ff9f 63 /** Constants for motor rotate mode */
dwijaybane 0:950f1bb3ff9f 64 typedef enum {
dwijaybane 0:950f1bb3ff9f 65 CLOCKWISE, /**< one-way: clockwise turn */
dwijaybane 0:950f1bb3ff9f 66 COUNTER_CLOCKWISE /**< one-way: counter clockwise turn */
dwijaybane 0:950f1bb3ff9f 67 } RotMode;
dwijaybane 0:950f1bb3ff9f 68
dwijaybane 0:950f1bb3ff9f 69 /** Constants for syncronization mode */
dwijaybane 0:950f1bb3ff9f 70 typedef enum {
dwijaybane 0:950f1bb3ff9f 71 ASYNCHRONOUS, /**< program does wait motor turn completion (default) */
dwijaybane 0:950f1bb3ff9f 72 SYNCHRONOUS /**< program doesn't wait motor turn completion */
dwijaybane 0:950f1bb3ff9f 73 } SyncMode;
dwijaybane 0:950f1bb3ff9f 74
dwijaybane 0:950f1bb3ff9f 75 /** Create a stepper motor object connected to specified DigitalOut pins and a DigitalIn pin
dwijaybane 0:950f1bb3ff9f 76 *
dwijaybane 0:950f1bb3ff9f 77 * @param out_A DigitalOut pin for motor pulse signal-A
dwijaybane 0:950f1bb3ff9f 78 * @param out_B DigitalOut pin for motor pulse signal-B
dwijaybane 0:950f1bb3ff9f 79 * @param out_C DigitalOut pin for motor pulse signal-C
dwijaybane 0:950f1bb3ff9f 80 * @param out_D DigitalOut pin for motor pulse signal-D
dwijaybane 0:950f1bb3ff9f 81 */
dwijaybane 0:950f1bb3ff9f 82 StepperMotorUni(
dwijaybane 0:950f1bb3ff9f 83 PinName out_A,
dwijaybane 0:950f1bb3ff9f 84 PinName out_B,
dwijaybane 0:950f1bb3ff9f 85 PinName out_C,
dwijaybane 0:950f1bb3ff9f 86 PinName out_D
dwijaybane 0:950f1bb3ff9f 87 );
dwijaybane 0:950f1bb3ff9f 88
dwijaybane 0:950f1bb3ff9f 89 /**
dwijaybane 0:950f1bb3ff9f 90 * @brief Sends Sequence to turn motor
dwijaybane 0:950f1bb3ff9f 91 * @param stepPos pattern index value/ sequence number
dwijaybane 0:950f1bb3ff9f 92 */
dwijaybane 0:950f1bb3ff9f 93 void send_sequence(int stepPos);
dwijaybane 0:950f1bb3ff9f 94
dwijaybane 0:950f1bb3ff9f 95 /**
dwijaybane 0:950f1bb3ff9f 96 * @brief Set Stepper operation mode
dwijaybane 0:950f1bb3ff9f 97 * @param v @arg STEP Single Step operation
dwijaybane 0:950f1bb3ff9f 98 * @arg HALFSTEP Half Step operation
dwijaybane 0:950f1bb3ff9f 99 */
dwijaybane 0:950f1bb3ff9f 100 void set_operation_mode( OperationMode v );
dwijaybane 0:950f1bb3ff9f 101
dwijaybane 0:950f1bb3ff9f 102 /**
dwijaybane 0:950f1bb3ff9f 103 * @brief Rotate motor at specified angle with given speed
dwijaybane 0:950f1bb3ff9f 104 * @param StMotorDirection Sets motor direction
dwijaybane 0:950f1bb3ff9f 105 * @arg CLOCKWISE
dwijaybane 0:950f1bb3ff9f 106 * @arg COUNTER_CLOCKWISE
dwijaybane 0:950f1bb3ff9f 107 * @param Angle Specify rotation angle
dwijaybane 0:950f1bb3ff9f 108 * @param Speed Specify Speed in Millisecond Per Step
dwijaybane 0:950f1bb3ff9f 109 * 50msps = 0.050 (It will take 50ms for 1 step)
dwijaybane 0:950f1bb3ff9f 110 *
dwijaybane 0:950f1bb3ff9f 111 * Calculations:
dwijaybane 0:950f1bb3ff9f 112 * Move 360 degrees in 2 seconds
dwijaybane 0:950f1bb3ff9f 113 *
dwijaybane 0:950f1bb3ff9f 114 * operation mode = STEP operation mode = HALFSTEP
dwijaybane 0:950f1bb3ff9f 115 * Time (in ms)
dwijaybane 0:950f1bb3ff9f 116 * speed = ------------------------
dwijaybane 0:950f1bb3ff9f 117 * degrees/deg_per_step
dwijaybane 0:950f1bb3ff9f 118 *
dwijaybane 0:950f1bb3ff9f 119 * 2000 2000 2000 2000
dwijaybane 0:950f1bb3ff9f 120 * speed =-------= ----= 10 = 0.010msps speed =-------=----= 5 = 0.005msps
dwijaybane 0:950f1bb3ff9f 121 * 360/1.8 200 360/0.9 400
dwijaybane 0:950f1bb3ff9f 122 *
dwijaybane 0:950f1bb3ff9f 123 */
dwijaybane 0:950f1bb3ff9f 124 void rotate_angle(RotMode StMotorDirection, int Angle, float Speed);
dwijaybane 0:950f1bb3ff9f 125
dwijaybane 0:950f1bb3ff9f 126 /**
dwijaybane 0:950f1bb3ff9f 127 * @brief Rotate motor with given steps with given time
dwijaybane 0:950f1bb3ff9f 128 * @param StMotorDirection Sets motor direction
dwijaybane 0:950f1bb3ff9f 129 * @arg CLOCKWISE
dwijaybane 0:950f1bb3ff9f 130 * @arg COUNTER_CLOCKWISE
dwijaybane 0:950f1bb3ff9f 131 * @param Steps Specify Steps to rotate
dwijaybane 0:950f1bb3ff9f 132 * @param Speed Specify Speed in Millisecond Per Step
dwijaybane 0:950f1bb3ff9f 133 * 50msps = 0.050 (It will take 50ms for 1 step)
dwijaybane 0:950f1bb3ff9f 134 *
dwijaybane 0:950f1bb3ff9f 135 * Calculations:
dwijaybane 0:950f1bb3ff9f 136 * Move 200 steps in 2 seconds
dwijaybane 0:950f1bb3ff9f 137 * Time (in ms) 2000
dwijaybane 0:950f1bb3ff9f 138 * speed = -------------= ------ = 10 = 0.010msps
dwijaybane 0:950f1bb3ff9f 139 * Steps 200
dwijaybane 0:950f1bb3ff9f 140 *
dwijaybane 0:950f1bb3ff9f 141 */
dwijaybane 0:950f1bb3ff9f 142 void rotate_steps(RotMode StMotorDirection, int Steps, float Speed);
dwijaybane 0:950f1bb3ff9f 143
dwijaybane 0:950f1bb3ff9f 144 /** Interface for syncronization mode setting
dwijaybane 0:950f1bb3ff9f 145 *
dwijaybane 0:950f1bb3ff9f 146 * Example:
dwijaybane 0:950f1bb3ff9f 147 * @code
dwijaybane 0:950f1bb3ff9f 148 * StepperMotor m( p21, p22, p23, p24 );
dwijaybane 0:950f1bb3ff9f 149 * int main() {
dwijaybane 0:950f1bb3ff9f 150 * m.set_sync_mode( StepperMotor::SYNCHRONOUS );
dwijaybane 0:950f1bb3ff9f 151 * ...
dwijaybane 0:950f1bb3ff9f 152 * @endcode
dwijaybane 0:950f1bb3ff9f 153 *
dwijaybane 0:950f1bb3ff9f 154 * @param m motor rotate mode : ASYNCHRONOUS (default) or SYNCHRONOUS
dwijaybane 0:950f1bb3ff9f 155 */
dwijaybane 0:950f1bb3ff9f 156 void set_sync_mode( SyncMode m );
dwijaybane 0:950f1bb3ff9f 157
dwijaybane 0:950f1bb3ff9f 158 /**
dwijaybane 0:950f1bb3ff9f 159 * @brief Check remaining distance that motor need to move
dwijaybane 0:950f1bb3ff9f 160 * software can check if the motor action completed in asynchronous mode
dwijaybane 0:950f1bb3ff9f 161 * @return remaining steps that motor need to go
dwijaybane 0:950f1bb3ff9f 162 */
dwijaybane 0:950f1bb3ff9f 163 int distance( void );
dwijaybane 0:950f1bb3ff9f 164
dwijaybane 0:950f1bb3ff9f 165 /**
dwijaybane 0:950f1bb3ff9f 166 * @brief Pause/Resume the motor action
dwijaybane 0:950f1bb3ff9f 167 * @param sw use "true" for pause, "false" (default) for resume
dwijaybane 0:950f1bb3ff9f 168 */
dwijaybane 0:950f1bb3ff9f 169 void set_pause( int sw );
dwijaybane 0:950f1bb3ff9f 170
dwijaybane 0:950f1bb3ff9f 171 /**
dwijaybane 0:950f1bb3ff9f 172 * @brief Stop motor and reset values to default
dwijaybane 0:950f1bb3ff9f 173 */
dwijaybane 0:950f1bb3ff9f 174 void stop( void );
dwijaybane 0:950f1bb3ff9f 175
dwijaybane 0:950f1bb3ff9f 176 private:
dwijaybane 0:950f1bb3ff9f 177 Ticker t;
dwijaybane 0:950f1bb3ff9f 178 BusOut motor_out;
dwijaybane 0:950f1bb3ff9f 179
dwijaybane 0:950f1bb3ff9f 180 static unsigned char pattern_step_cw[ 4 ]; // 1 phase pulse pattern for motor control
dwijaybane 0:950f1bb3ff9f 181 static unsigned char pattern_step_acw[ 4 ]; // 1 phase pulse pattern for motor control
dwijaybane 0:950f1bb3ff9f 182 static unsigned char pattern_halfstep_cw[ 8 ]; // 1 phase pulse pattern for motor control
dwijaybane 0:950f1bb3ff9f 183 static unsigned char pattern_halfstep_acw[ 8 ]; // 1 phase pulse pattern for motor control
dwijaybane 0:950f1bb3ff9f 184 unsigned char *pattern;
dwijaybane 0:950f1bb3ff9f 185 int pat_index_mask;
dwijaybane 0:950f1bb3ff9f 186 OperationMode phase_mode;
dwijaybane 0:950f1bb3ff9f 187 RotMode rot_mode;
dwijaybane 0:950f1bb3ff9f 188 SyncMode sync_mode;
dwijaybane 0:950f1bb3ff9f 189 int current_pos;
dwijaybane 0:950f1bb3ff9f 190 int target_pos;
dwijaybane 0:950f1bb3ff9f 191 float msps;
dwijaybane 0:950f1bb3ff9f 192 float max_msps;
dwijaybane 0:950f1bb3ff9f 193 int pause;
dwijaybane 0:950f1bb3ff9f 194
dwijaybane 0:950f1bb3ff9f 195 void set_target_pos( int p ); // target position setting interface
dwijaybane 0:950f1bb3ff9f 196 void motor_maintain( void ); // this function is called periodically by Ticker
dwijaybane 0:950f1bb3ff9f 197 };
dwijaybane 0:950f1bb3ff9f 198
dwijaybane 0:950f1bb3ff9f 199
dwijaybane 0:950f1bb3ff9f 200 #endif