Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
StepperMotorUni.cpp
00001 /** Stepper Motor (Unipolar) control library 00002 * 00003 * @class StepperMotorUni 00004 * @author Dwijay.Edutech Learning Solutions 00005 * @version 1.0 00006 * @date 1-Feb-2016 00007 * 00008 * The library that controls stepper motor via motor driver chip 00009 * This is a driver for a unipolar stepper motor. 00010 * 00011 * Example: 00012 * @code 00013 * #include "mbed.h" 00014 * #include "StepperMotorUni.h" 00015 * 00016 * StepperMotorUni motor( p26, p25, p24, p23 ); 00017 * 00018 * int main() 00019 * { 00020 * motor.set_operation_mode(StepperMotorUni::STEP); 00021 * 00022 * while ( 1 ) { 00023 * motor.rotate_angle(StepperMotorUni::CLOCKWISE,90,0.02); 00024 * wait( 1 ); 00025 * 00026 * motor.rotate_angle(StepperMotorUni::COUNTER_CLOCKWISE,90,0.02); 00027 * wait( 1 ); 00028 * } 00029 * } 00030 * @endcode 00031 */ 00032 00033 #include "mbed.h" 00034 #include "StepperMotorUni.h" 00035 00036 00037 StepperMotorUni::StepperMotorUni( 00038 PinName out_A, 00039 PinName out_B, 00040 PinName out_C, 00041 PinName out_D 00042 ) : motor_out( out_A, out_B, out_C, out_D ), 00043 rot_mode( CLOCKWISE ), 00044 sync_mode( ASYNCHRONOUS ), 00045 phase_mode(STEP), 00046 current_pos( 0 ), 00047 max_msps( MAX_MSPS ), 00048 target_pos( 0 ), 00049 pause( true ) 00050 { 00051 msps = max_msps; 00052 pattern = (unsigned char *)pattern_step_cw; 00053 pat_index_mask = 0x3; 00054 // t.attach( this, &StepperMotorUni::motor_maintain, (float)msps ); 00055 } 00056 00057 00058 void StepperMotorUni::motor_maintain( void ) 00059 { 00060 if ( pause ) 00061 return; 00062 00063 current_pos = current_pos + ( (target_pos < current_pos) ? -1 : 1 ); 00064 send_sequence(current_pos); 00065 00066 // printf( "%d>>>%d\r\n", current_pos, target_pos ); 00067 00068 if ( target_pos == current_pos ) { 00069 current_pos = 0; 00070 target_pos = 0; 00071 set_pause(true); 00072 t.detach(); 00073 } 00074 }; 00075 00076 00077 void StepperMotorUni::send_sequence(int stepPos) 00078 { 00079 motor_out = pattern[stepPos & pat_index_mask]; 00080 } 00081 00082 void StepperMotorUni::set_operation_mode( OperationMode v ) 00083 { 00084 phase_mode = v; 00085 } 00086 00087 void StepperMotorUni::set_sync_mode( SyncMode m ) 00088 { 00089 sync_mode = m; 00090 } 00091 00092 int StepperMotorUni::distance( void ) 00093 { 00094 return( target_pos - current_pos ); 00095 } 00096 00097 void StepperMotorUni::set_target_pos( int p ) 00098 { 00099 target_pos = p; 00100 00101 if (sync_mode == SYNCHRONOUS) 00102 while ( distance() ) 00103 wait( 0 ); 00104 } 00105 00106 void StepperMotorUni::set_pause( int sw ) 00107 { 00108 pause = sw; 00109 } 00110 00111 void StepperMotorUni::stop( void ) 00112 { 00113 target_pos = current_pos; 00114 set_pause(true); 00115 t.detach(); 00116 } 00117 00118 00119 void StepperMotorUni::rotate_angle(RotMode StMotorDirection, int Angle, float Speed) 00120 { 00121 switch ( phase_mode ) { 00122 case StepperMotorUni::STEP : 00123 if(StMotorDirection == CLOCKWISE){ 00124 pattern = pattern_step_cw; 00125 }else if (StMotorDirection == COUNTER_CLOCKWISE) { 00126 pattern = pattern_step_acw; 00127 } 00128 pat_index_mask = 0x3; 00129 set_target_pos((int)(Angle/CAL_ANGLE)); 00130 break; 00131 00132 case StepperMotorUni::HALFSTEP : 00133 if(StMotorDirection == CLOCKWISE){ 00134 pattern = pattern_halfstep_cw; 00135 }else if (StMotorDirection == COUNTER_CLOCKWISE) { 00136 pattern = pattern_halfstep_acw; 00137 } 00138 pat_index_mask = 0x7; 00139 set_target_pos((int)(2*Angle/CAL_ANGLE)); 00140 break; 00141 00142 default: 00143 break; 00144 } 00145 00146 current_pos = 0; 00147 00148 // printf("target pos %d",target_pos); 00149 set_pause(false); 00150 00151 msps = Speed; 00152 00153 t.detach(); 00154 t.attach( this, &StepperMotorUni::motor_maintain, (float)msps ); 00155 } 00156 00157 00158 void StepperMotorUni::rotate_steps(RotMode StMotorDirection, int Steps, float Speed) 00159 { 00160 switch ( phase_mode ) { 00161 case StepperMotorUni::STEP : 00162 if(StMotorDirection == CLOCKWISE){ 00163 pattern = pattern_step_cw; 00164 }else if (StMotorDirection == COUNTER_CLOCKWISE) { 00165 pattern = pattern_step_acw; 00166 } 00167 pat_index_mask = 0x3; 00168 break; 00169 00170 case StepperMotorUni::HALFSTEP : 00171 if(StMotorDirection == CLOCKWISE){ 00172 pattern = pattern_halfstep_cw; 00173 }else if (StMotorDirection == COUNTER_CLOCKWISE) { 00174 pattern = pattern_halfstep_acw; 00175 } 00176 pat_index_mask = 0x7; 00177 break; 00178 00179 default: 00180 break; 00181 } 00182 00183 set_target_pos(Steps); 00184 current_pos = 0; 00185 00186 // printf("target pos %d",target_pos); 00187 set_pause(false); 00188 00189 msps = Speed; 00190 00191 t.detach(); 00192 t.attach( this, &StepperMotorUni::motor_maintain, (float)msps ); 00193 } 00194 00195 00196 #if GENERAL 00197 unsigned char StepperMotorUni::pattern_one_phase[ 4 ] = { 0x1, 0x2, 0x4, 0x8 }; 00198 unsigned char StepperMotorUni::pattern_two_phase[ 4 ] = { 0x3, 0x6, 0xC, 0x9 }; 00199 unsigned char StepperMotorUni::pattern_halfstep[ 8 ] = { 0x1, 0x3, 0x2, 0x6, 0x4, 0xC, 0x8, 0x9 }; 00200 #endif 00201 #if STM601 00202 unsigned char StepperMotorUni::pattern_step_cw[ 4 ] = { 0xA, 0x3, 0x5, 0xC }; 00203 unsigned char StepperMotorUni::pattern_step_acw[ 4 ] = { 0xC, 0x5, 0x3, 0xA }; 00204 unsigned char StepperMotorUni::pattern_halfstep_cw[ 8 ] = { 0xC, 0x8, 0xA, 0x2, 0x3, 0x1, 0x5, 0x4 }; 00205 unsigned char StepperMotorUni::pattern_halfstep_acw[ 8 ] = { 0x4, 0x5, 0x1, 0x3, 0x2, 0xA, 0x8, 0xC }; 00206 #endif
Generated on Sat Jul 16 2022 01:20:43 by
1.7.2