Unipolar stepper motor operation library

Dependents:   LAB04_Oppgave1 test_stepper Stepper_Motor_Demo StepperMotorUni_Hello ... more

Unipolar stepper motor library

This library generates pulses on 4 digital output pins of the mbed. The pulses are generated by mbed's ticker function.

The mbed pins cannot drive the stepper motor directly. So it requires driver stage for the motor. The circuit may be like following diagram.
The driver stage should be chosen by requirement for the stepper motor.

/media/uploads/okano/unipolar-steppermotor-sample.png


The mbed generates pulses on 4 output pins for external driver stage.
This library can generate 3 types of pulses.

1 phase drive (wave drive) /media/uploads/okano/1phase_drive.gif

2 phase drive /media/uploads/okano/2phase_drive.gif

1-2 phase (half step) drive /media/uploads/okano/halfstep_drive.gif

Components pages

Components pages are available for bipolar and unipolar motor libraries

A bipolar stepper motor driving pulse generator

A unipolar stepper motor driving pulse generator

Committer:
okano
Date:
Mon Apr 13 21:10:56 2015 +0000
Revision:
1:5de3a9848490
Parent:
0:5d0abdf92786
Child:
2:6835e719ff96
API document correction

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okano 0:5d0abdf92786 1 /** Stepper Motor (Unipolar) control library
okano 0:5d0abdf92786 2 *
okano 0:5d0abdf92786 3 * @class StepperMotorUni
okano 0:5d0abdf92786 4 * @author Tedd OKANO
okano 1:5de3a9848490 5 * @version 1.0.1
okano 1:5de3a9848490 6 * @date 14-Apr-2015
okano 0:5d0abdf92786 7 *
okano 1:5de3a9848490 8 * Copyright: 2010, 2014, 2015 Tedd OKANO
okano 0:5d0abdf92786 9 * Released under the Apache 2 license License
okano 0:5d0abdf92786 10 *
okano 0:5d0abdf92786 11 * The library that controls stepper motor via motor driver chip
okano 0:5d0abdf92786 12 * This is a driver for a unipolar stepper motor.
okano 0:5d0abdf92786 13 *
okano 0:5d0abdf92786 14 * Example:
okano 0:5d0abdf92786 15 * @code
okano 0:5d0abdf92786 16 * #include "mbed.h"
okano 0:5d0abdf92786 17 * #include "StepperMotorUni.h"
okano 0:5d0abdf92786 18 *
okano 0:5d0abdf92786 19 * StepperMotorUni motor( p26, p25, p24, p23 );
okano 0:5d0abdf92786 20 *
okano 0:5d0abdf92786 21 * int main()
okano 0:5d0abdf92786 22 * {
okano 0:5d0abdf92786 23 * motor.set_pps( 50 );
okano 0:5d0abdf92786 24 *
okano 0:5d0abdf92786 25 * while ( 1 ) {
okano 0:5d0abdf92786 26 * motor.move_steps( 24 );
okano 0:5d0abdf92786 27 * wait( 1 );
okano 0:5d0abdf92786 28 *
okano 0:5d0abdf92786 29 * motor.move_steps( -24 );
okano 0:5d0abdf92786 30 * wait( 1 );
okano 0:5d0abdf92786 31 * }
okano 0:5d0abdf92786 32 * }
okano 0:5d0abdf92786 33 * @endcode
okano 0:5d0abdf92786 34 *
okano 0:5d0abdf92786 35 * version 0.51(27-Nov-2010) // initial version (un-published)
okano 0:5d0abdf92786 36 * version 0.6 (15-Jan-2014) // compatible to LPC1768, LPC11U24 and LPC1114 targets
okano 1:5de3a9848490 37 * version 1.0 (19-Jun-2014) // version 1.0 release
okano 1:5de3a9848490 38 * version 1.0.1 (14-Apr-2015) // API document correction
okano 0:5d0abdf92786 39 */
okano 0:5d0abdf92786 40
okano 0:5d0abdf92786 41 #ifndef MBED_STEPPERMOTOR_UNIPOLAR
okano 0:5d0abdf92786 42 #define MBED_STEPPERMOTOR_UNIPOLAR
okano 0:5d0abdf92786 43
okano 0:5d0abdf92786 44 #include "mbed.h"
okano 0:5d0abdf92786 45
okano 0:5d0abdf92786 46 #define MAX_PPS 100 // pulse per second
okano 0:5d0abdf92786 47
okano 0:5d0abdf92786 48
okano 0:5d0abdf92786 49 class StepperMotorUni
okano 0:5d0abdf92786 50 {
okano 0:5d0abdf92786 51 public:
okano 0:5d0abdf92786 52
okano 0:5d0abdf92786 53 /** Constants for motor rotate mode */
okano 0:5d0abdf92786 54 typedef enum {
okano 0:5d0abdf92786 55 ONE_PHASE, /**< 1 phase operation (default) */
okano 0:5d0abdf92786 56 TWO_PHASE, /**< 2 phase operation */
okano 0:5d0abdf92786 57 HALFSTEP /**< halfstep operation */
okano 0:5d0abdf92786 58 } OperationPhaseMode;
okano 0:5d0abdf92786 59
okano 0:5d0abdf92786 60 /** Constants for motor rotate mode */
okano 0:5d0abdf92786 61 typedef enum {
okano 0:5d0abdf92786 62 SHORTEST, /**< turn by shortest direction (default) */
okano 0:5d0abdf92786 63 NO_WRAPAROUND, /**< do not accross home position */
okano 0:5d0abdf92786 64 CLOCKWISE_ONLY, /**< one-way: clockwise turn */
okano 0:5d0abdf92786 65 COUNTER_CLOCKWISE_ONLY /**< one-way: counter clockwise turn */
okano 0:5d0abdf92786 66 } RotMode;
okano 0:5d0abdf92786 67 /** Constants for syncronization mode */
okano 0:5d0abdf92786 68 typedef enum {
okano 0:5d0abdf92786 69 ASYNCHRONOUS, /**< program does wait motor turn completion (default) */
okano 0:5d0abdf92786 70 SYNCHRONOUS /**< program doesn't wait motor turn completion */
okano 0:5d0abdf92786 71 } SyncMode;
okano 0:5d0abdf92786 72
okano 0:5d0abdf92786 73 /** Constants for position detection edge polarity */
okano 0:5d0abdf92786 74 typedef enum {
okano 0:5d0abdf92786 75 RISING_EDGE, /**< position detection done by rising edge */
okano 0:5d0abdf92786 76 FALLING_EDGE /**< position detection done by falling edge */
okano 0:5d0abdf92786 77 } PositionDetectPorarity;
okano 0:5d0abdf92786 78
okano 0:5d0abdf92786 79 /** Constants for position detection edge polarity */
okano 0:5d0abdf92786 80 typedef enum {
okano 0:5d0abdf92786 81 SOFT_BRAKE, /**< brake with slowing down */
okano 0:5d0abdf92786 82 HARD_BRAKE /**< for immedate stop */
okano 0:5d0abdf92786 83 } BrakeMode;
okano 0:5d0abdf92786 84
okano 0:5d0abdf92786 85 /** Create a stepper motor object connected to specified DigitalOut pins and a DigitalIn pin
okano 0:5d0abdf92786 86 *
okano 0:5d0abdf92786 87 * @param out_A DigitalOut pin for motor pulse signal-A
okano 0:5d0abdf92786 88 * @param out_B DigitalOut pin for motor pulse signal-B
okano 0:5d0abdf92786 89 * @param out_C DigitalOut pin for motor pulse signal-C
okano 0:5d0abdf92786 90 * @param out_D DigitalOut pin for motor pulse signal-D
okano 0:5d0abdf92786 91 * @param position_detect DigitalIn pin for home position detection (option). if not defined, "find_home_position()" function cannot be used
okano 0:5d0abdf92786 92 */
okano 0:5d0abdf92786 93 StepperMotorUni(
okano 0:5d0abdf92786 94 PinName out_A,
okano 0:5d0abdf92786 95 PinName out_B,
okano 0:5d0abdf92786 96 PinName out_C,
okano 0:5d0abdf92786 97 PinName out_D,
okano 0:5d0abdf92786 98 PinName position_detect = NC
okano 0:5d0abdf92786 99 ) ;
okano 0:5d0abdf92786 100
okano 0:5d0abdf92786 101 /** Set the pulse width (i.e. motor turning speed)
okano 0:5d0abdf92786 102 *
okano 0:5d0abdf92786 103 * @param v pulse per second (pps) : lower number makes the turn slower (default = 100)
okano 0:5d0abdf92786 104 */
okano 0:5d0abdf92786 105 float set_pps( float v );
okano 0:5d0abdf92786 106
okano 0:5d0abdf92786 107 /** Set maximum PPS (= minimum pulse width) which will be used in finding home position
okano 0:5d0abdf92786 108 *
okano 0:5d0abdf92786 109 * @param v maximum pulse per second : lower number makes the turn slower (default = 100)
okano 0:5d0abdf92786 110 */
okano 0:5d0abdf92786 111 void set_max_pps( float v );
okano 0:5d0abdf92786 112
okano 0:5d0abdf92786 113 /** Find home position: rotate the motor until the detection edge comes.
okano 0:5d0abdf92786 114 *
okano 0:5d0abdf92786 115 * Turns the motor until the home position detected.
okano 0:5d0abdf92786 116 * The "home position" is a reference point for the step and angle. It will be step=0 and angle=0.
okano 0:5d0abdf92786 117 * The detection signal edge can be defined by an argument.
okano 0:5d0abdf92786 118 * It follows the rotate mode.
okano 0:5d0abdf92786 119 * When the edge is detected, the motor will be stopped and it will be the new home position.
okano 0:5d0abdf92786 120 * If no detection signal detected, no home position update done.
okano 0:5d0abdf92786 121 *
okano 0:5d0abdf92786 122 * @param edge defines detection edge rise or fall
okano 0:5d0abdf92786 123 */
okano 0:5d0abdf92786 124 int find_home_position( PositionDetectPorarity edge );
okano 0:5d0abdf92786 125
okano 0:5d0abdf92786 126 /** Update home position
okano 0:5d0abdf92786 127 *
okano 0:5d0abdf92786 128 * Set the home position as current motor position.
okano 0:5d0abdf92786 129 */
okano 0:5d0abdf92786 130 void set_home_position( void );
okano 0:5d0abdf92786 131
okano 0:5d0abdf92786 132 /** Turn the motor to defined position (by steps from home position)
okano 0:5d0abdf92786 133 *
okano 0:5d0abdf92786 134 * Make motor move to absolute position
okano 0:5d0abdf92786 135 *
okano 0:5d0abdf92786 136 * @param v the position defined by steps from home position
okano 0:5d0abdf92786 137 */
okano 0:5d0abdf92786 138 int go_position( int v );
okano 0:5d0abdf92786 139
okano 0:5d0abdf92786 140 /** Turn the motor to defined position (by angle (degree)) from home position)
okano 0:5d0abdf92786 141 *
okano 0:5d0abdf92786 142 * Make motor move to absolute position
okano 0:5d0abdf92786 143 *
okano 0:5d0abdf92786 144 * @param v the position defined by steps from home position
okano 0:5d0abdf92786 145 */
okano 0:5d0abdf92786 146 void go_angle( float angle );
okano 0:5d0abdf92786 147
okano 0:5d0abdf92786 148 /** Turn the motor to defined position (by steps from current position)
okano 0:5d0abdf92786 149 *
okano 0:5d0abdf92786 150 * Make motor move to defined position
okano 0:5d0abdf92786 151 *
okano 0:5d0abdf92786 152 * @param v the position defined by steps from current position
okano 0:5d0abdf92786 153 */
okano 0:5d0abdf92786 154 int move_steps( int s );
okano 0:5d0abdf92786 155
okano 0:5d0abdf92786 156 /** Turn the motor to defined rotation (from current position)
okano 0:5d0abdf92786 157 *
okano 0:5d0abdf92786 158 * Make motor rotate
okano 0:5d0abdf92786 159 *
okano 0:5d0abdf92786 160 * @param r number of rotation start from current position
okano 0:5d0abdf92786 161 */
okano 0:5d0abdf92786 162 int move_rotates( float r );
okano 0:5d0abdf92786 163
okano 0:5d0abdf92786 164 /** Interface for opertion phase mode setting
okano 0:5d0abdf92786 165 *
okano 1:5de3a9848490 166 * @param v Driving phase mode change : ONE_PHASE (default), TWO_PHASE or HALFSTEP
okano 0:5d0abdf92786 167 */
okano 0:5d0abdf92786 168 void set_operation_phase_mode( OperationPhaseMode v );
okano 0:5d0abdf92786 169
okano 0:5d0abdf92786 170 /** Interface for motor rotate mode setting
okano 0:5d0abdf92786 171 *
okano 0:5d0abdf92786 172 * Example:
okano 0:5d0abdf92786 173 * @code
okano 0:5d0abdf92786 174 * StepperMotor m( p21, p22, p23, p24 );
okano 0:5d0abdf92786 175 * int main() {
okano 0:5d0abdf92786 176 * m.set_rot_mode( StepperMotor::NO_WRAPAROUND );
okano 0:5d0abdf92786 177 * ...
okano 0:5d0abdf92786 178 * @endcode
okano 0:5d0abdf92786 179 *
okano 0:5d0abdf92786 180 * @param m motor rotate mode : SHORTEST (default), NO_WRAPAROUND, CLOCKWISE_ONLY or COUNTER_CLOCKWISE_ONLY
okano 0:5d0abdf92786 181 */
okano 0:5d0abdf92786 182
okano 0:5d0abdf92786 183 void set_rot_mode( RotMode m );
okano 0:5d0abdf92786 184
okano 0:5d0abdf92786 185 /** Interface for syncronization mode setting
okano 0:5d0abdf92786 186 *
okano 0:5d0abdf92786 187 * Example:
okano 0:5d0abdf92786 188 * @code
okano 0:5d0abdf92786 189 * StepperMotor m( p21, p22, p23, p24 );
okano 0:5d0abdf92786 190 * int main() {
okano 0:5d0abdf92786 191 * m.set_sync_mode( StepperMotor::NO_WRAPAROUND );
okano 0:5d0abdf92786 192 * ...
okano 0:5d0abdf92786 193 * @endcode
okano 0:5d0abdf92786 194 *
okano 0:5d0abdf92786 195 * @param m motor rotate mode : ASYNCHRONOUS (default) or SYNCHRONOUS
okano 0:5d0abdf92786 196 */
okano 0:5d0abdf92786 197 void set_sync_mode( SyncMode m );
okano 0:5d0abdf92786 198
okano 0:5d0abdf92786 199 /** Check remaining distance that motor need to move
okano 0:5d0abdf92786 200 *
okano 0:5d0abdf92786 201 * software can check if the motor action completed in asynchronous mode
okano 0:5d0abdf92786 202 *
okano 0:5d0abdf92786 203 * @return remaining steps that motor need to go
okano 0:5d0abdf92786 204 */
okano 0:5d0abdf92786 205 int distance( void );
okano 0:5d0abdf92786 206
okano 0:5d0abdf92786 207 /** Pause/Resume the motor action
okano 0:5d0abdf92786 208 *
okano 0:5d0abdf92786 209 * @param sw use "true" for pause, "false" (default) for resume
okano 0:5d0abdf92786 210 */
okano 0:5d0abdf92786 211 void set_pause( int sw );
okano 0:5d0abdf92786 212
okano 0:5d0abdf92786 213 /** Pause/Resume the motor action
okano 0:5d0abdf92786 214 *
okano 0:5d0abdf92786 215 * @param sw use "true" for pause, "false" (default) for resume
okano 0:5d0abdf92786 216 */
okano 0:5d0abdf92786 217 void brake( void );
okano 0:5d0abdf92786 218 void brake( BrakeMode mode );
okano 0:5d0abdf92786 219
okano 0:5d0abdf92786 220 /** Auto power control enable
okano 0:5d0abdf92786 221 *
okano 0:5d0abdf92786 222 * If the auto power control is enabled, the motor power will be turned-off when it stays same place
okano 0:5d0abdf92786 223 *
okano 0:5d0abdf92786 224 * @param sw use "true" for pause, "false" (default) for resume
okano 0:5d0abdf92786 225 */
okano 0:5d0abdf92786 226 void set_power_ctrl( int sw );
okano 0:5d0abdf92786 227
okano 0:5d0abdf92786 228 /** Setting for steps/rotate
okano 0:5d0abdf92786 229 *
okano 0:5d0abdf92786 230 * This parameter is required if program want to use the "go_angle()" interface.
okano 0:5d0abdf92786 231 * The angle will be calculated from this parameter.
okano 0:5d0abdf92786 232 *
okano 0:5d0abdf92786 233 * @param steps per rotate
okano 0:5d0abdf92786 234 */
okano 0:5d0abdf92786 235 void set_steps_per_rotate( int steps );
okano 0:5d0abdf92786 236
okano 0:5d0abdf92786 237 void set_ramp_control( float initial_speed_rate, int ramp_steps );
okano 0:5d0abdf92786 238 private:
okano 0:5d0abdf92786 239
okano 0:5d0abdf92786 240 Ticker t;
okano 0:5d0abdf92786 241 BusOut motor_out;
okano 0:5d0abdf92786 242 DigitalIn position_detect_pin;
okano 0:5d0abdf92786 243
okano 0:5d0abdf92786 244 static unsigned char pattern_one_phase[ 4 ]; // 1 phase pulse pattern for motor control
okano 0:5d0abdf92786 245 static unsigned char pattern_two_phase[ 4 ]; // 1 phase pulse pattern for motor control
okano 0:5d0abdf92786 246 static unsigned char pattern_halfstep[ 8 ]; // 1 phase pulse pattern for motor control
okano 0:5d0abdf92786 247 unsigned char *pattern;
okano 0:5d0abdf92786 248 int pat_index_mask;
okano 0:5d0abdf92786 249 OperationPhaseMode phase_mode;
okano 0:5d0abdf92786 250 RotMode rot_mode;
okano 0:5d0abdf92786 251 SyncMode sync_mode;
okano 0:5d0abdf92786 252 int max_pos;
okano 0:5d0abdf92786 253 int current_pos;
okano 0:5d0abdf92786 254 int pos_offset;
okano 0:5d0abdf92786 255 int target_pos;
okano 0:5d0abdf92786 256 float pps;
okano 0:5d0abdf92786 257 float max_pps;
okano 0:5d0abdf92786 258 int init_done;
okano 0:5d0abdf92786 259 int pause;
okano 0:5d0abdf92786 260 int power_ctrl;
okano 0:5d0abdf92786 261 float ramp_init_speed_rate;
okano 0:5d0abdf92786 262 int ramp_control_steps;
okano 0:5d0abdf92786 263 float ramp_rate;
okano 0:5d0abdf92786 264
okano 0:5d0abdf92786 265 void set_target_pos( int p ); // target position setting interface
okano 0:5d0abdf92786 266 void motor_maintain( void ); // this function is called periodically by Ticker
okano 0:5d0abdf92786 267 };
okano 0:5d0abdf92786 268
okano 0:5d0abdf92786 269
okano 0:5d0abdf92786 270 #endif