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.h
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 #ifndef MBED_STEPPERMOTOR_UNIPOLAR 00034 #define MBED_STEPPERMOTOR_UNIPOLAR 00035 00036 #include "mbed.h" 00037 00038 /* Default Value */ 00039 #define MAX_MSPS 0.5 // 500 millisecond per step 00040 00041 /******************************************************************************/ 00042 /* Stepper Motor Calibration */ 00043 /******************************************************************************/ 00044 #define CAL_ANGLE 1.8 // Stepper motor calibration angle 00045 00046 00047 /******************************************************************************/ 00048 /* Stepper Motor Selection */ 00049 /******************************************************************************/ 00050 #define GENERAL false 00051 #define STM601 true 00052 00053 class StepperMotorUni 00054 { 00055 public: 00056 00057 /** Constants for motor rotate mode */ 00058 typedef enum { 00059 STEP, /**< Single step operation (default) */ 00060 HALFSTEP /**< half step operation */ 00061 } OperationMode; 00062 00063 /** Constants for motor rotate mode */ 00064 typedef enum { 00065 CLOCKWISE, /**< one-way: clockwise turn */ 00066 COUNTER_CLOCKWISE /**< one-way: counter clockwise turn */ 00067 } RotMode; 00068 00069 /** Constants for syncronization mode */ 00070 typedef enum { 00071 ASYNCHRONOUS, /**< program does wait motor turn completion (default) */ 00072 SYNCHRONOUS /**< program doesn't wait motor turn completion */ 00073 } SyncMode; 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_C DigitalOut pin for motor pulse signal-C 00080 * @param out_D DigitalOut pin for motor pulse signal-D 00081 */ 00082 StepperMotorUni( 00083 PinName out_A, 00084 PinName out_B, 00085 PinName out_C, 00086 PinName out_D 00087 ); 00088 00089 /** 00090 * @brief Sends Sequence to turn motor 00091 * @param stepPos pattern index value/ sequence number 00092 */ 00093 void send_sequence(int stepPos); 00094 00095 /** 00096 * @brief Set Stepper operation mode 00097 * @param v @arg STEP Single Step operation 00098 * @arg HALFSTEP Half Step operation 00099 */ 00100 void set_operation_mode( OperationMode v ); 00101 00102 /** 00103 * @brief Rotate motor at specified angle with given speed 00104 * @param StMotorDirection Sets motor direction 00105 * @arg CLOCKWISE 00106 * @arg COUNTER_CLOCKWISE 00107 * @param Angle Specify rotation angle 00108 * @param Speed Specify Speed in Millisecond Per Step 00109 * 50msps = 0.050 (It will take 50ms for 1 step) 00110 * 00111 * Calculations: 00112 * Move 360 degrees in 2 seconds 00113 * 00114 * operation mode = STEP operation mode = HALFSTEP 00115 * Time (in ms) 00116 * speed = ------------------------ 00117 * degrees/deg_per_step 00118 * 00119 * 2000 2000 2000 2000 00120 * speed =-------= ----= 10 = 0.010msps speed =-------=----= 5 = 0.005msps 00121 * 360/1.8 200 360/0.9 400 00122 * 00123 */ 00124 void rotate_angle(RotMode StMotorDirection, int Angle, float Speed); 00125 00126 /** 00127 * @brief Rotate motor with given steps with given time 00128 * @param StMotorDirection Sets motor direction 00129 * @arg CLOCKWISE 00130 * @arg COUNTER_CLOCKWISE 00131 * @param Steps Specify Steps to rotate 00132 * @param Speed Specify Speed in Millisecond Per Step 00133 * 50msps = 0.050 (It will take 50ms for 1 step) 00134 * 00135 * Calculations: 00136 * Move 200 steps in 2 seconds 00137 * Time (in ms) 2000 00138 * speed = -------------= ------ = 10 = 0.010msps 00139 * Steps 200 00140 * 00141 */ 00142 void rotate_steps(RotMode StMotorDirection, int Steps, float Speed); 00143 00144 /** Interface for syncronization mode setting 00145 * 00146 * Example: 00147 * @code 00148 * StepperMotor m( p21, p22, p23, p24 ); 00149 * int main() { 00150 * m.set_sync_mode( StepperMotor::SYNCHRONOUS ); 00151 * ... 00152 * @endcode 00153 * 00154 * @param m motor rotate mode : ASYNCHRONOUS (default) or SYNCHRONOUS 00155 */ 00156 void set_sync_mode( SyncMode m ); 00157 00158 /** 00159 * @brief Check remaining distance that motor need to move 00160 * software can check if the motor action completed in asynchronous mode 00161 * @return remaining steps that motor need to go 00162 */ 00163 int distance( void ); 00164 00165 /** 00166 * @brief Pause/Resume the motor action 00167 * @param sw use "true" for pause, "false" (default) for resume 00168 */ 00169 void set_pause( int sw ); 00170 00171 /** 00172 * @brief Stop motor and reset values to default 00173 */ 00174 void stop( void ); 00175 00176 private: 00177 Ticker t; 00178 BusOut motor_out; 00179 00180 static unsigned char pattern_step_cw[ 4 ]; // 1 phase pulse pattern for motor control 00181 static unsigned char pattern_step_acw[ 4 ]; // 1 phase pulse pattern for motor control 00182 static unsigned char pattern_halfstep_cw[ 8 ]; // 1 phase pulse pattern for motor control 00183 static unsigned char pattern_halfstep_acw[ 8 ]; // 1 phase pulse pattern for motor control 00184 unsigned char *pattern; 00185 int pat_index_mask; 00186 OperationMode phase_mode; 00187 RotMode rot_mode; 00188 SyncMode sync_mode; 00189 int current_pos; 00190 int target_pos; 00191 float msps; 00192 float max_msps; 00193 int pause; 00194 00195 void set_target_pos( int p ); // target position setting interface 00196 void motor_maintain( void ); // this function is called periodically by Ticker 00197 }; 00198 00199 00200 #endif
Generated on Sat Jul 16 2022 01:20:43 by
1.7.2