This is a library that drives a stepper motor with given parameters for speed and direction.

Dependents:   RaceTimer

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.

Committer:
mdu7078
Date:
Mon May 13 16:19:54 2013 +0000
Revision:
1:6e0a307d0f9b
Parent:
0:acf5b8fc382b
First commit of mMotor library

Who changed what in which revision?

UserRevisionLine numberNew 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 1:6e0a307d0f9b 61 _M0 = 0;
mdu7078 1:6e0a307d0f9b 62 _M1 = 0;
mdu7078 1:6e0a307d0f9b 63 _M2 = 0;
mdu7078 1:6e0a307d0f9b 64 _M3 = 0;
mdu7078 0:acf5b8fc382b 65 }
mdu7078 0:acf5b8fc382b 66
mdu7078 0:acf5b8fc382b 67 void mMotor::clockwise() { // rotate the motor 1 step clockwise
mdu7078 0:acf5b8fc382b 68 for (int i = 0; i < 4; i++) {
mdu7078 0:acf5b8fc382b 69 switch (i) { // activate the ports A0, A2, A3, A3 in a binary sequence for steps
mdu7078 0:acf5b8fc382b 70 case 0: {
mdu7078 0:acf5b8fc382b 71 _M0=1;
mdu7078 0:acf5b8fc382b 72 _M1=0;
mdu7078 0:acf5b8fc382b 73 _M2=0;
mdu7078 0:acf5b8fc382b 74 _M3=0;
mdu7078 0:acf5b8fc382b 75 }
mdu7078 0:acf5b8fc382b 76 break;
mdu7078 0:acf5b8fc382b 77 case 1: {
mdu7078 0:acf5b8fc382b 78 _M0=0;
mdu7078 0:acf5b8fc382b 79 _M1=1;
mdu7078 0:acf5b8fc382b 80 _M2=0;
mdu7078 0:acf5b8fc382b 81 _M3=0;
mdu7078 0:acf5b8fc382b 82 }
mdu7078 0:acf5b8fc382b 83 break;
mdu7078 0:acf5b8fc382b 84 case 2: {
mdu7078 0:acf5b8fc382b 85 _M0=0;
mdu7078 0:acf5b8fc382b 86 _M1=0;
mdu7078 0:acf5b8fc382b 87 _M2=1;
mdu7078 0:acf5b8fc382b 88 _M3=0;
mdu7078 0:acf5b8fc382b 89 }
mdu7078 0:acf5b8fc382b 90 break;
mdu7078 0:acf5b8fc382b 91 case 3: {
mdu7078 0:acf5b8fc382b 92 _M0=0;
mdu7078 0:acf5b8fc382b 93 _M1=0;
mdu7078 0:acf5b8fc382b 94 _M2=0;
mdu7078 0:acf5b8fc382b 95 _M3=1;
mdu7078 0:acf5b8fc382b 96 }
mdu7078 0:acf5b8fc382b 97 break;
mdu7078 0:acf5b8fc382b 98 }
mdu7078 0:acf5b8fc382b 99
mdu7078 0:acf5b8fc382b 100 wait_us(motorSpeed); // wait time defines the speed
mdu7078 0:acf5b8fc382b 101 }
mdu7078 1:6e0a307d0f9b 102 _M0 = 0;
mdu7078 1:6e0a307d0f9b 103 _M1 = 0;
mdu7078 1:6e0a307d0f9b 104 _M2 = 0;
mdu7078 1:6e0a307d0f9b 105 _M3 = 1;
mdu7078 0:acf5b8fc382b 106 }
mdu7078 1:6e0a307d0f9b 107
mdu7078 1:6e0a307d0f9b 108 void mMotor::ht_clockwise() { // rotate the motor 1 step clockwise
mdu7078 1:6e0a307d0f9b 109 for (int i = 0; i < 4; i++) {
mdu7078 1:6e0a307d0f9b 110 switch (i) { // activate the ports A0, A2, A3, A3 in a binary sequence for steps
mdu7078 1:6e0a307d0f9b 111 case 0: {
mdu7078 1:6e0a307d0f9b 112 _M0=1;
mdu7078 1:6e0a307d0f9b 113 _M1=0;
mdu7078 1:6e0a307d0f9b 114 _M2=0;
mdu7078 1:6e0a307d0f9b 115 _M3=0;
mdu7078 1:6e0a307d0f9b 116 }
mdu7078 1:6e0a307d0f9b 117 break;
mdu7078 1:6e0a307d0f9b 118 case 1: {
mdu7078 1:6e0a307d0f9b 119 _M0=0;
mdu7078 1:6e0a307d0f9b 120 _M1=1;
mdu7078 1:6e0a307d0f9b 121 _M2=0;
mdu7078 1:6e0a307d0f9b 122 _M3=0;
mdu7078 1:6e0a307d0f9b 123 }
mdu7078 1:6e0a307d0f9b 124 break;
mdu7078 1:6e0a307d0f9b 125 case 2: {
mdu7078 1:6e0a307d0f9b 126 _M0=0;
mdu7078 1:6e0a307d0f9b 127 _M1=0;
mdu7078 1:6e0a307d0f9b 128 _M2=1;
mdu7078 1:6e0a307d0f9b 129 _M3=0;
mdu7078 1:6e0a307d0f9b 130 }
mdu7078 1:6e0a307d0f9b 131 break;
mdu7078 1:6e0a307d0f9b 132 case 3: {
mdu7078 1:6e0a307d0f9b 133 _M0=0;
mdu7078 1:6e0a307d0f9b 134 _M1=0;
mdu7078 1:6e0a307d0f9b 135 _M2=0;
mdu7078 1:6e0a307d0f9b 136 _M3=1;
mdu7078 1:6e0a307d0f9b 137 }
mdu7078 1:6e0a307d0f9b 138 break;
mdu7078 1:6e0a307d0f9b 139 }
mdu7078 1:6e0a307d0f9b 140
mdu7078 1:6e0a307d0f9b 141 wait_us(motorSpeed); // wait time defines the speed
mdu7078 1:6e0a307d0f9b 142 }
mdu7078 1:6e0a307d0f9b 143 }
mdu7078 1:6e0a307d0f9b 144
mdu7078 1:6e0a307d0f9b 145 void mMotor::ht_counterclockwise() { // rotate the motor 1 step anticlockwise
mdu7078 1:6e0a307d0f9b 146 for (int i = 0; i < 4; i++) {
mdu7078 1:6e0a307d0f9b 147 switch (i) { // activate the ports A0, A2, A3, A3 in a binary sequence for steps
mdu7078 1:6e0a307d0f9b 148 case 0: {
mdu7078 1:6e0a307d0f9b 149 _M0=0;
mdu7078 1:6e0a307d0f9b 150 _M1=0;
mdu7078 1:6e0a307d0f9b 151 _M2=0;
mdu7078 1:6e0a307d0f9b 152 _M3=1;
mdu7078 1:6e0a307d0f9b 153 }
mdu7078 1:6e0a307d0f9b 154 break;
mdu7078 1:6e0a307d0f9b 155 case 1: {
mdu7078 1:6e0a307d0f9b 156 _M0=0;
mdu7078 1:6e0a307d0f9b 157 _M1=0;
mdu7078 1:6e0a307d0f9b 158 _M2=1;
mdu7078 1:6e0a307d0f9b 159 _M3=0;
mdu7078 1:6e0a307d0f9b 160 }
mdu7078 1:6e0a307d0f9b 161 break;
mdu7078 1:6e0a307d0f9b 162 case 2: {
mdu7078 1:6e0a307d0f9b 163 _M0=0;
mdu7078 1:6e0a307d0f9b 164 _M1=1;
mdu7078 1:6e0a307d0f9b 165 _M2=0;
mdu7078 1:6e0a307d0f9b 166 _M3=0;
mdu7078 1:6e0a307d0f9b 167 }
mdu7078 1:6e0a307d0f9b 168 break;
mdu7078 1:6e0a307d0f9b 169 case 3: {
mdu7078 1:6e0a307d0f9b 170 _M0=1;
mdu7078 1:6e0a307d0f9b 171 _M1=0;
mdu7078 1:6e0a307d0f9b 172 _M2=0;
mdu7078 1:6e0a307d0f9b 173 _M3=0;
mdu7078 1:6e0a307d0f9b 174 }
mdu7078 1:6e0a307d0f9b 175 break;
mdu7078 1:6e0a307d0f9b 176 }
mdu7078 1:6e0a307d0f9b 177
mdu7078 1:6e0a307d0f9b 178 wait_us(motorSpeed); // wait time defines the speed
mdu7078 1:6e0a307d0f9b 179 }
mdu7078 1:6e0a307d0f9b 180 }
mdu7078 1:6e0a307d0f9b 181
mdu7078 1:6e0a307d0f9b 182
mdu7078 0:acf5b8fc382b 183 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 184 int count=0; // initalize step count
mdu7078 0:acf5b8fc382b 185 motorSpeed=speed; //set motor speed
mdu7078 0:acf5b8fc382b 186 if (direction==0) // turn clockwise
mdu7078 0:acf5b8fc382b 187 do {
mdu7078 0:acf5b8fc382b 188 clockwise();
mdu7078 0:acf5b8fc382b 189 count++;
mdu7078 0:acf5b8fc382b 190 } while (count<num_steps); // turn number of steps applied
mdu7078 0:acf5b8fc382b 191 else if (direction==1)// turn anticlockwise
mdu7078 0:acf5b8fc382b 192 do {
mdu7078 0:acf5b8fc382b 193 counterclockwise();
mdu7078 0:acf5b8fc382b 194 count++;
mdu7078 0:acf5b8fc382b 195 } while (count<num_steps);// turn number of steps applied
mdu7078 0:acf5b8fc382b 196
mdu7078 1:6e0a307d0f9b 197 }
mdu7078 1:6e0a307d0f9b 198
mdu7078 1:6e0a307d0f9b 199 void mMotor::ht_step(int num_steps, int direction, int speed) {// steper function: number of steps, direction (0- right, 1- left), speed (default 1200)
mdu7078 1:6e0a307d0f9b 200 int count=0; // initalize step count
mdu7078 1:6e0a307d0f9b 201 motorSpeed=speed; //set motor speed
mdu7078 1:6e0a307d0f9b 202 if (direction==0) // turn clockwise
mdu7078 1:6e0a307d0f9b 203 do {
mdu7078 1:6e0a307d0f9b 204 ht_clockwise();
mdu7078 1:6e0a307d0f9b 205 count++;
mdu7078 1:6e0a307d0f9b 206 } while (count<num_steps); // turn number of steps applied
mdu7078 1:6e0a307d0f9b 207 else if (direction==1)// turn anticlockwise
mdu7078 1:6e0a307d0f9b 208 do {
mdu7078 1:6e0a307d0f9b 209 ht_counterclockwise();
mdu7078 1:6e0a307d0f9b 210 count++;
mdu7078 1:6e0a307d0f9b 211 } while (count<num_steps);// turn number of steps applied
mdu7078 1:6e0a307d0f9b 212
mdu7078 0:acf5b8fc382b 213 }