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 #include "mbed.h"
dwijaybane 0:950f1bb3ff9f 34 #include "StepperMotorUni.h"
dwijaybane 0:950f1bb3ff9f 35
dwijaybane 0:950f1bb3ff9f 36
dwijaybane 0:950f1bb3ff9f 37 StepperMotorUni::StepperMotorUni(
dwijaybane 0:950f1bb3ff9f 38 PinName out_A,
dwijaybane 0:950f1bb3ff9f 39 PinName out_B,
dwijaybane 0:950f1bb3ff9f 40 PinName out_C,
dwijaybane 0:950f1bb3ff9f 41 PinName out_D
dwijaybane 0:950f1bb3ff9f 42 ) : motor_out( out_A, out_B, out_C, out_D ),
dwijaybane 0:950f1bb3ff9f 43 rot_mode( CLOCKWISE ),
dwijaybane 0:950f1bb3ff9f 44 sync_mode( ASYNCHRONOUS ),
dwijaybane 0:950f1bb3ff9f 45 phase_mode(STEP),
dwijaybane 0:950f1bb3ff9f 46 current_pos( 0 ),
dwijaybane 0:950f1bb3ff9f 47 max_msps( MAX_MSPS ),
dwijaybane 0:950f1bb3ff9f 48 target_pos( 0 ),
dwijaybane 0:950f1bb3ff9f 49 pause( true )
dwijaybane 0:950f1bb3ff9f 50 {
dwijaybane 0:950f1bb3ff9f 51 msps = max_msps;
dwijaybane 0:950f1bb3ff9f 52 pattern = (unsigned char *)pattern_step_cw;
dwijaybane 0:950f1bb3ff9f 53 pat_index_mask = 0x3;
dwijaybane 0:950f1bb3ff9f 54 // t.attach( this, &StepperMotorUni::motor_maintain, (float)msps );
dwijaybane 0:950f1bb3ff9f 55 }
dwijaybane 0:950f1bb3ff9f 56
dwijaybane 0:950f1bb3ff9f 57
dwijaybane 0:950f1bb3ff9f 58 void StepperMotorUni::motor_maintain( void )
dwijaybane 0:950f1bb3ff9f 59 {
dwijaybane 0:950f1bb3ff9f 60 if ( pause )
dwijaybane 0:950f1bb3ff9f 61 return;
dwijaybane 0:950f1bb3ff9f 62
dwijaybane 0:950f1bb3ff9f 63 current_pos = current_pos + ( (target_pos < current_pos) ? -1 : 1 );
dwijaybane 0:950f1bb3ff9f 64 send_sequence(current_pos);
dwijaybane 0:950f1bb3ff9f 65
dwijaybane 0:950f1bb3ff9f 66 // printf( "%d>>>%d\r\n", current_pos, target_pos );
dwijaybane 0:950f1bb3ff9f 67
dwijaybane 0:950f1bb3ff9f 68 if ( target_pos == current_pos ) {
dwijaybane 0:950f1bb3ff9f 69 current_pos = 0;
dwijaybane 0:950f1bb3ff9f 70 target_pos = 0;
dwijaybane 0:950f1bb3ff9f 71 set_pause(true);
dwijaybane 0:950f1bb3ff9f 72 t.detach();
dwijaybane 0:950f1bb3ff9f 73 }
dwijaybane 0:950f1bb3ff9f 74 };
dwijaybane 0:950f1bb3ff9f 75
dwijaybane 0:950f1bb3ff9f 76
dwijaybane 0:950f1bb3ff9f 77 void StepperMotorUni::send_sequence(int stepPos)
dwijaybane 0:950f1bb3ff9f 78 {
dwijaybane 0:950f1bb3ff9f 79 motor_out = pattern[stepPos & pat_index_mask];
dwijaybane 0:950f1bb3ff9f 80 }
dwijaybane 0:950f1bb3ff9f 81
dwijaybane 0:950f1bb3ff9f 82 void StepperMotorUni::set_operation_mode( OperationMode v )
dwijaybane 0:950f1bb3ff9f 83 {
dwijaybane 0:950f1bb3ff9f 84 phase_mode = v;
dwijaybane 0:950f1bb3ff9f 85 }
dwijaybane 0:950f1bb3ff9f 86
dwijaybane 0:950f1bb3ff9f 87 void StepperMotorUni::set_sync_mode( SyncMode m )
dwijaybane 0:950f1bb3ff9f 88 {
dwijaybane 0:950f1bb3ff9f 89 sync_mode = m;
dwijaybane 0:950f1bb3ff9f 90 }
dwijaybane 0:950f1bb3ff9f 91
dwijaybane 0:950f1bb3ff9f 92 int StepperMotorUni::distance( void )
dwijaybane 0:950f1bb3ff9f 93 {
dwijaybane 0:950f1bb3ff9f 94 return( target_pos - current_pos );
dwijaybane 0:950f1bb3ff9f 95 }
dwijaybane 0:950f1bb3ff9f 96
dwijaybane 0:950f1bb3ff9f 97 void StepperMotorUni::set_target_pos( int p )
dwijaybane 0:950f1bb3ff9f 98 {
dwijaybane 0:950f1bb3ff9f 99 target_pos = p;
dwijaybane 0:950f1bb3ff9f 100
dwijaybane 0:950f1bb3ff9f 101 if (sync_mode == SYNCHRONOUS)
dwijaybane 0:950f1bb3ff9f 102 while ( distance() )
dwijaybane 0:950f1bb3ff9f 103 wait( 0 );
dwijaybane 0:950f1bb3ff9f 104 }
dwijaybane 0:950f1bb3ff9f 105
dwijaybane 0:950f1bb3ff9f 106 void StepperMotorUni::set_pause( int sw )
dwijaybane 0:950f1bb3ff9f 107 {
dwijaybane 0:950f1bb3ff9f 108 pause = sw;
dwijaybane 0:950f1bb3ff9f 109 }
dwijaybane 0:950f1bb3ff9f 110
dwijaybane 0:950f1bb3ff9f 111 void StepperMotorUni::stop( void )
dwijaybane 0:950f1bb3ff9f 112 {
dwijaybane 0:950f1bb3ff9f 113 target_pos = current_pos;
dwijaybane 0:950f1bb3ff9f 114 set_pause(true);
dwijaybane 0:950f1bb3ff9f 115 t.detach();
dwijaybane 0:950f1bb3ff9f 116 }
dwijaybane 0:950f1bb3ff9f 117
dwijaybane 0:950f1bb3ff9f 118
dwijaybane 0:950f1bb3ff9f 119 void StepperMotorUni::rotate_angle(RotMode StMotorDirection, int Angle, float Speed)
dwijaybane 0:950f1bb3ff9f 120 {
dwijaybane 0:950f1bb3ff9f 121 switch ( phase_mode ) {
dwijaybane 0:950f1bb3ff9f 122 case StepperMotorUni::STEP :
dwijaybane 0:950f1bb3ff9f 123 if(StMotorDirection == CLOCKWISE){
dwijaybane 0:950f1bb3ff9f 124 pattern = pattern_step_cw;
dwijaybane 0:950f1bb3ff9f 125 }else if (StMotorDirection == COUNTER_CLOCKWISE) {
dwijaybane 0:950f1bb3ff9f 126 pattern = pattern_step_acw;
dwijaybane 0:950f1bb3ff9f 127 }
dwijaybane 0:950f1bb3ff9f 128 pat_index_mask = 0x3;
dwijaybane 0:950f1bb3ff9f 129 set_target_pos((int)(Angle/CAL_ANGLE));
dwijaybane 0:950f1bb3ff9f 130 break;
dwijaybane 0:950f1bb3ff9f 131
dwijaybane 0:950f1bb3ff9f 132 case StepperMotorUni::HALFSTEP :
dwijaybane 0:950f1bb3ff9f 133 if(StMotorDirection == CLOCKWISE){
dwijaybane 0:950f1bb3ff9f 134 pattern = pattern_halfstep_cw;
dwijaybane 0:950f1bb3ff9f 135 }else if (StMotorDirection == COUNTER_CLOCKWISE) {
dwijaybane 0:950f1bb3ff9f 136 pattern = pattern_halfstep_acw;
dwijaybane 0:950f1bb3ff9f 137 }
dwijaybane 0:950f1bb3ff9f 138 pat_index_mask = 0x7;
dwijaybane 0:950f1bb3ff9f 139 set_target_pos((int)(2*Angle/CAL_ANGLE));
dwijaybane 0:950f1bb3ff9f 140 break;
dwijaybane 0:950f1bb3ff9f 141
dwijaybane 0:950f1bb3ff9f 142 default:
dwijaybane 0:950f1bb3ff9f 143 break;
dwijaybane 0:950f1bb3ff9f 144 }
dwijaybane 0:950f1bb3ff9f 145
dwijaybane 0:950f1bb3ff9f 146 current_pos = 0;
dwijaybane 0:950f1bb3ff9f 147
dwijaybane 0:950f1bb3ff9f 148 // printf("target pos %d",target_pos);
dwijaybane 0:950f1bb3ff9f 149 set_pause(false);
dwijaybane 0:950f1bb3ff9f 150
dwijaybane 0:950f1bb3ff9f 151 msps = Speed;
dwijaybane 0:950f1bb3ff9f 152
dwijaybane 0:950f1bb3ff9f 153 t.detach();
dwijaybane 0:950f1bb3ff9f 154 t.attach( this, &StepperMotorUni::motor_maintain, (float)msps );
dwijaybane 0:950f1bb3ff9f 155 }
dwijaybane 0:950f1bb3ff9f 156
dwijaybane 0:950f1bb3ff9f 157
dwijaybane 0:950f1bb3ff9f 158 void StepperMotorUni::rotate_steps(RotMode StMotorDirection, int Steps, float Speed)
dwijaybane 0:950f1bb3ff9f 159 {
dwijaybane 0:950f1bb3ff9f 160 switch ( phase_mode ) {
dwijaybane 0:950f1bb3ff9f 161 case StepperMotorUni::STEP :
dwijaybane 0:950f1bb3ff9f 162 if(StMotorDirection == CLOCKWISE){
dwijaybane 0:950f1bb3ff9f 163 pattern = pattern_step_cw;
dwijaybane 0:950f1bb3ff9f 164 }else if (StMotorDirection == COUNTER_CLOCKWISE) {
dwijaybane 0:950f1bb3ff9f 165 pattern = pattern_step_acw;
dwijaybane 0:950f1bb3ff9f 166 }
dwijaybane 0:950f1bb3ff9f 167 pat_index_mask = 0x3;
dwijaybane 0:950f1bb3ff9f 168 break;
dwijaybane 0:950f1bb3ff9f 169
dwijaybane 0:950f1bb3ff9f 170 case StepperMotorUni::HALFSTEP :
dwijaybane 0:950f1bb3ff9f 171 if(StMotorDirection == CLOCKWISE){
dwijaybane 0:950f1bb3ff9f 172 pattern = pattern_halfstep_cw;
dwijaybane 0:950f1bb3ff9f 173 }else if (StMotorDirection == COUNTER_CLOCKWISE) {
dwijaybane 0:950f1bb3ff9f 174 pattern = pattern_halfstep_acw;
dwijaybane 0:950f1bb3ff9f 175 }
dwijaybane 0:950f1bb3ff9f 176 pat_index_mask = 0x7;
dwijaybane 0:950f1bb3ff9f 177 break;
dwijaybane 0:950f1bb3ff9f 178
dwijaybane 0:950f1bb3ff9f 179 default:
dwijaybane 0:950f1bb3ff9f 180 break;
dwijaybane 0:950f1bb3ff9f 181 }
dwijaybane 0:950f1bb3ff9f 182
dwijaybane 0:950f1bb3ff9f 183 set_target_pos(Steps);
dwijaybane 0:950f1bb3ff9f 184 current_pos = 0;
dwijaybane 0:950f1bb3ff9f 185
dwijaybane 0:950f1bb3ff9f 186 // printf("target pos %d",target_pos);
dwijaybane 0:950f1bb3ff9f 187 set_pause(false);
dwijaybane 0:950f1bb3ff9f 188
dwijaybane 0:950f1bb3ff9f 189 msps = Speed;
dwijaybane 0:950f1bb3ff9f 190
dwijaybane 0:950f1bb3ff9f 191 t.detach();
dwijaybane 0:950f1bb3ff9f 192 t.attach( this, &StepperMotorUni::motor_maintain, (float)msps );
dwijaybane 0:950f1bb3ff9f 193 }
dwijaybane 0:950f1bb3ff9f 194
dwijaybane 0:950f1bb3ff9f 195
dwijaybane 0:950f1bb3ff9f 196 #if GENERAL
dwijaybane 0:950f1bb3ff9f 197 unsigned char StepperMotorUni::pattern_one_phase[ 4 ] = { 0x1, 0x2, 0x4, 0x8 };
dwijaybane 0:950f1bb3ff9f 198 unsigned char StepperMotorUni::pattern_two_phase[ 4 ] = { 0x3, 0x6, 0xC, 0x9 };
dwijaybane 0:950f1bb3ff9f 199 unsigned char StepperMotorUni::pattern_halfstep[ 8 ] = { 0x1, 0x3, 0x2, 0x6, 0x4, 0xC, 0x8, 0x9 };
dwijaybane 0:950f1bb3ff9f 200 #endif
dwijaybane 0:950f1bb3ff9f 201 #if STM601
dwijaybane 0:950f1bb3ff9f 202 unsigned char StepperMotorUni::pattern_step_cw[ 4 ] = { 0xA, 0x3, 0x5, 0xC };
dwijaybane 0:950f1bb3ff9f 203 unsigned char StepperMotorUni::pattern_step_acw[ 4 ] = { 0xC, 0x5, 0x3, 0xA };
dwijaybane 0:950f1bb3ff9f 204 unsigned char StepperMotorUni::pattern_halfstep_cw[ 8 ] = { 0xC, 0x8, 0xA, 0x2, 0x3, 0x1, 0x5, 0x4 };
dwijaybane 0:950f1bb3ff9f 205 unsigned char StepperMotorUni::pattern_halfstep_acw[ 8 ] = { 0x4, 0x5, 0x1, 0x3, 0x2, 0xA, 0x8, 0xC };
dwijaybane 0:950f1bb3ff9f 206 #endif