This is a library that drives a stepper motor with given parameters for speed and direction.
About
This is a stepper motor library that is based on the sMotor library by Samuel Matildes (sam.naeec@gmail.com). It was designed to drive specifically a 5718m-05e-05 stepper motor in conjunction with an L293NE quad h-bridge chip. I am not responsible for any damage caused due to the execution of this program on your hardware as I can only confirm that it works for the above specified model. High torque mode is currently not complete.
mMotor.cpp@0:acf5b8fc382b, 2013-04-24 (annotated)
- Committer:
- mdu7078
- Date:
- Wed Apr 24 17:39:30 2013 +0000
- Revision:
- 0:acf5b8fc382b
- Child:
- 1:6e0a307d0f9b
First commit to motor library.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mdu7078 | 0:acf5b8fc382b | 1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * |
mdu7078 | 0:acf5b8fc382b | 2 | * This is a stepper motor library for testing * |
mdu7078 | 0:acf5b8fc382b | 3 | * 4 step stepper motors. The speed of stepping is * |
mdu7078 | 0:acf5b8fc382b | 4 | * variable as well as the amount of steps. * |
mdu7078 | 0:acf5b8fc382b | 5 | * ------------------------------------------------- * |
mdu7078 | 0:acf5b8fc382b | 6 | * This library is based on the sMotor library by * |
mdu7078 | 0:acf5b8fc382b | 7 | * Samuel Matildes (sam.naeec@gmail.com). * |
mdu7078 | 0:acf5b8fc382b | 8 | * ------------------------------------------------- * |
mdu7078 | 0:acf5b8fc382b | 9 | * * |
mdu7078 | 0:acf5b8fc382b | 10 | * Created by: Michael Dushkoff (mad1841@rit.edu) * |
mdu7078 | 0:acf5b8fc382b | 11 | * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
mdu7078 | 0:acf5b8fc382b | 12 | |
mdu7078 | 0:acf5b8fc382b | 13 | #include "mbed.h" |
mdu7078 | 0:acf5b8fc382b | 14 | #include "mMotor.h" |
mdu7078 | 0:acf5b8fc382b | 15 | |
mdu7078 | 0:acf5b8fc382b | 16 | int motorSpeed; // Steper speed |
mdu7078 | 0:acf5b8fc382b | 17 | |
mdu7078 | 0:acf5b8fc382b | 18 | mMotor::mMotor(PinName M0, PinName M1, PinName M2, PinName M3) : _M0(M0), _M1(M1), _M2(M2), _M3(M3) { // Defenition of motor pins |
mdu7078 | 0:acf5b8fc382b | 19 | _M0=0; |
mdu7078 | 0:acf5b8fc382b | 20 | _M1=0; |
mdu7078 | 0:acf5b8fc382b | 21 | _M2=0; |
mdu7078 | 0:acf5b8fc382b | 22 | _M3=0; |
mdu7078 | 0:acf5b8fc382b | 23 | } |
mdu7078 | 0:acf5b8fc382b | 24 | |
mdu7078 | 0:acf5b8fc382b | 25 | |
mdu7078 | 0:acf5b8fc382b | 26 | void mMotor::counterclockwise() { // rotate the motor 1 step anticlockwise |
mdu7078 | 0:acf5b8fc382b | 27 | for (int i = 0; i < 4; i++) { |
mdu7078 | 0:acf5b8fc382b | 28 | switch (i) { // activate the ports A0, A2, A3, A3 in a binary sequence for steps |
mdu7078 | 0:acf5b8fc382b | 29 | case 0: { |
mdu7078 | 0:acf5b8fc382b | 30 | _M0=0; |
mdu7078 | 0:acf5b8fc382b | 31 | _M1=0; |
mdu7078 | 0:acf5b8fc382b | 32 | _M2=0; |
mdu7078 | 0:acf5b8fc382b | 33 | _M3=1; |
mdu7078 | 0:acf5b8fc382b | 34 | } |
mdu7078 | 0:acf5b8fc382b | 35 | break; |
mdu7078 | 0:acf5b8fc382b | 36 | case 1: { |
mdu7078 | 0:acf5b8fc382b | 37 | _M0=0; |
mdu7078 | 0:acf5b8fc382b | 38 | _M1=0; |
mdu7078 | 0:acf5b8fc382b | 39 | _M2=1; |
mdu7078 | 0:acf5b8fc382b | 40 | _M3=0; |
mdu7078 | 0:acf5b8fc382b | 41 | } |
mdu7078 | 0:acf5b8fc382b | 42 | break; |
mdu7078 | 0:acf5b8fc382b | 43 | case 2: { |
mdu7078 | 0:acf5b8fc382b | 44 | _M0=0; |
mdu7078 | 0:acf5b8fc382b | 45 | _M1=1; |
mdu7078 | 0:acf5b8fc382b | 46 | _M2=0; |
mdu7078 | 0:acf5b8fc382b | 47 | _M3=0; |
mdu7078 | 0:acf5b8fc382b | 48 | } |
mdu7078 | 0:acf5b8fc382b | 49 | break; |
mdu7078 | 0:acf5b8fc382b | 50 | case 3: { |
mdu7078 | 0:acf5b8fc382b | 51 | _M0=1; |
mdu7078 | 0:acf5b8fc382b | 52 | _M1=0; |
mdu7078 | 0:acf5b8fc382b | 53 | _M2=0; |
mdu7078 | 0:acf5b8fc382b | 54 | _M3=0; |
mdu7078 | 0:acf5b8fc382b | 55 | } |
mdu7078 | 0:acf5b8fc382b | 56 | break; |
mdu7078 | 0:acf5b8fc382b | 57 | } |
mdu7078 | 0:acf5b8fc382b | 58 | |
mdu7078 | 0:acf5b8fc382b | 59 | wait_us(motorSpeed); // wait time defines the speed |
mdu7078 | 0:acf5b8fc382b | 60 | } |
mdu7078 | 0:acf5b8fc382b | 61 | } |
mdu7078 | 0:acf5b8fc382b | 62 | |
mdu7078 | 0:acf5b8fc382b | 63 | void mMotor::clockwise() { // rotate the motor 1 step clockwise |
mdu7078 | 0:acf5b8fc382b | 64 | for (int i = 0; i < 4; i++) { |
mdu7078 | 0:acf5b8fc382b | 65 | switch (i) { // activate the ports A0, A2, A3, A3 in a binary sequence for steps |
mdu7078 | 0:acf5b8fc382b | 66 | case 0: { |
mdu7078 | 0:acf5b8fc382b | 67 | _M0=1; |
mdu7078 | 0:acf5b8fc382b | 68 | _M1=0; |
mdu7078 | 0:acf5b8fc382b | 69 | _M2=0; |
mdu7078 | 0:acf5b8fc382b | 70 | _M3=0; |
mdu7078 | 0:acf5b8fc382b | 71 | } |
mdu7078 | 0:acf5b8fc382b | 72 | break; |
mdu7078 | 0:acf5b8fc382b | 73 | case 1: { |
mdu7078 | 0:acf5b8fc382b | 74 | _M0=0; |
mdu7078 | 0:acf5b8fc382b | 75 | _M1=1; |
mdu7078 | 0:acf5b8fc382b | 76 | _M2=0; |
mdu7078 | 0:acf5b8fc382b | 77 | _M3=0; |
mdu7078 | 0:acf5b8fc382b | 78 | } |
mdu7078 | 0:acf5b8fc382b | 79 | break; |
mdu7078 | 0:acf5b8fc382b | 80 | case 2: { |
mdu7078 | 0:acf5b8fc382b | 81 | _M0=0; |
mdu7078 | 0:acf5b8fc382b | 82 | _M1=0; |
mdu7078 | 0:acf5b8fc382b | 83 | _M2=1; |
mdu7078 | 0:acf5b8fc382b | 84 | _M3=0; |
mdu7078 | 0:acf5b8fc382b | 85 | } |
mdu7078 | 0:acf5b8fc382b | 86 | break; |
mdu7078 | 0:acf5b8fc382b | 87 | case 3: { |
mdu7078 | 0:acf5b8fc382b | 88 | _M0=0; |
mdu7078 | 0:acf5b8fc382b | 89 | _M1=0; |
mdu7078 | 0:acf5b8fc382b | 90 | _M2=0; |
mdu7078 | 0:acf5b8fc382b | 91 | _M3=1; |
mdu7078 | 0:acf5b8fc382b | 92 | } |
mdu7078 | 0:acf5b8fc382b | 93 | break; |
mdu7078 | 0:acf5b8fc382b | 94 | } |
mdu7078 | 0:acf5b8fc382b | 95 | |
mdu7078 | 0:acf5b8fc382b | 96 | wait_us(motorSpeed); // wait time defines the speed |
mdu7078 | 0:acf5b8fc382b | 97 | } |
mdu7078 | 0:acf5b8fc382b | 98 | } |
mdu7078 | 0:acf5b8fc382b | 99 | void mMotor::step(int num_steps, int direction, int speed) {// steper function: number of steps, direction (0- right, 1- left), speed (default 1200) |
mdu7078 | 0:acf5b8fc382b | 100 | int count=0; // initalize step count |
mdu7078 | 0:acf5b8fc382b | 101 | motorSpeed=speed; //set motor speed |
mdu7078 | 0:acf5b8fc382b | 102 | if (direction==0) // turn clockwise |
mdu7078 | 0:acf5b8fc382b | 103 | do { |
mdu7078 | 0:acf5b8fc382b | 104 | clockwise(); |
mdu7078 | 0:acf5b8fc382b | 105 | count++; |
mdu7078 | 0:acf5b8fc382b | 106 | } while (count<num_steps); // turn number of steps applied |
mdu7078 | 0:acf5b8fc382b | 107 | else if (direction==1)// turn anticlockwise |
mdu7078 | 0:acf5b8fc382b | 108 | do { |
mdu7078 | 0:acf5b8fc382b | 109 | counterclockwise(); |
mdu7078 | 0:acf5b8fc382b | 110 | count++; |
mdu7078 | 0:acf5b8fc382b | 111 | } while (count<num_steps);// turn number of steps applied |
mdu7078 | 0:acf5b8fc382b | 112 | |
mdu7078 | 0:acf5b8fc382b | 113 | } |