Michael Dushkoff / mMotor

Dependents:   RaceTimer

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mMotor.cpp Source File

mMotor.cpp

00001 /* * * * * * * * * * * * * * * * * * * * * * * * * * *
00002  * This is a stepper motor library for testing       *
00003  * 4 step stepper motors.  The speed of stepping is  *
00004  * variable as well as the amount of steps.          *
00005  * ------------------------------------------------- *
00006  * This library is based on the sMotor library by    *
00007  * Samuel Matildes (sam.naeec@gmail.com).            *
00008  * ------------------------------------------------- *
00009  *                                                   *
00010  * Created by: Michael Dushkoff (mad1841@rit.edu)    *
00011  * * * * * * * * * * * * * * * * * * * * * * * * * * */
00012 
00013 #include "mbed.h"
00014 #include "mMotor.h"
00015 
00016 int motorSpeed; // Steper speed
00017 
00018 mMotor::mMotor(PinName M0, PinName M1, PinName M2, PinName M3) : _M0(M0), _M1(M1), _M2(M2), _M3(M3) { // Defenition of motor pins
00019     _M0=0;
00020     _M1=0;
00021     _M2=0;
00022     _M3=0;
00023 }
00024 
00025 
00026 void mMotor::counterclockwise() { // rotate the motor 1 step anticlockwise 
00027     for (int i = 0; i < 4; i++) {
00028         switch (i) { // activate the ports A0, A2, A3, A3 in a binary sequence for steps
00029             case 0: {
00030                 _M0=0;
00031                 _M1=0;
00032                 _M2=0;
00033                 _M3=1;
00034             }
00035             break;
00036             case 1: {
00037                 _M0=0;
00038                 _M1=0;
00039                 _M2=1;
00040                 _M3=0;
00041             }
00042             break;
00043             case 2: {
00044                 _M0=0;
00045                 _M1=1;
00046                 _M2=0;
00047                 _M3=0;
00048             }
00049             break;
00050             case 3: {
00051                 _M0=1;
00052                 _M1=0;
00053                 _M2=0;
00054                 _M3=0;
00055             }
00056             break;
00057         }
00058 
00059         wait_us(motorSpeed); // wait time defines the speed 
00060     }
00061     _M0 = 0;
00062     _M1 = 0;
00063     _M2 = 0;
00064     _M3 = 0;
00065 }
00066 
00067 void mMotor::clockwise() { // rotate the motor 1 step clockwise 
00068     for (int i = 0; i < 4; i++) {
00069         switch (i) { // activate the ports A0, A2, A3, A3 in a binary sequence for steps
00070             case 0: {
00071                 _M0=1;
00072                 _M1=0;
00073                 _M2=0;
00074                 _M3=0;
00075             }
00076             break;
00077             case 1: {
00078                 _M0=0;
00079                 _M1=1;
00080                 _M2=0;
00081                 _M3=0;
00082             }
00083             break;
00084             case 2: {
00085                 _M0=0;
00086                 _M1=0;
00087                 _M2=1;
00088                 _M3=0;
00089             }
00090             break;
00091             case 3: {
00092                 _M0=0;
00093                 _M1=0;
00094                 _M2=0;
00095                 _M3=1;
00096             }
00097             break;
00098         }
00099 
00100         wait_us(motorSpeed); // wait time defines the speed 
00101     }
00102     _M0 = 0;
00103     _M1 = 0;
00104     _M2 = 0;
00105     _M3 = 1;
00106 }
00107 
00108 void mMotor::ht_clockwise() { // rotate the motor 1 step clockwise 
00109     for (int i = 0; i < 4; i++) {
00110         switch (i) { // activate the ports A0, A2, A3, A3 in a binary sequence for steps
00111             case 0: {
00112                 _M0=1;
00113                 _M1=0;
00114                 _M2=0;
00115                 _M3=0;
00116             }
00117             break;
00118             case 1: {
00119                 _M0=0;
00120                 _M1=1;
00121                 _M2=0;
00122                 _M3=0;
00123             }
00124             break;
00125             case 2: {
00126                 _M0=0;
00127                 _M1=0;
00128                 _M2=1;
00129                 _M3=0;
00130             }
00131             break;
00132             case 3: {
00133                 _M0=0;
00134                 _M1=0;
00135                 _M2=0;
00136                 _M3=1;
00137             }
00138             break;
00139         }
00140 
00141         wait_us(motorSpeed); // wait time defines the speed 
00142     }
00143 }
00144 
00145 void mMotor::ht_counterclockwise() { // rotate the motor 1 step anticlockwise 
00146     for (int i = 0; i < 4; i++) {
00147         switch (i) { // activate the ports A0, A2, A3, A3 in a binary sequence for steps
00148             case 0: {
00149                 _M0=0;
00150                 _M1=0;
00151                 _M2=0;
00152                 _M3=1;
00153             }
00154             break;
00155             case 1: {
00156                 _M0=0;
00157                 _M1=0;
00158                 _M2=1;
00159                 _M3=0;
00160             }
00161             break;
00162             case 2: {
00163                 _M0=0;
00164                 _M1=1;
00165                 _M2=0;
00166                 _M3=0;
00167             }
00168             break;
00169             case 3: {
00170                 _M0=1;
00171                 _M1=0;
00172                 _M2=0;
00173                 _M3=0;
00174             }
00175             break;
00176         }
00177 
00178         wait_us(motorSpeed); // wait time defines the speed 
00179     }
00180 }
00181 
00182 
00183 void mMotor::step(int num_steps, int direction, int speed) {// steper function: number of steps, direction (0- right, 1- left), speed (default 1200)
00184     int count=0; // initalize step count
00185     motorSpeed=speed; //set motor speed
00186     if (direction==0) // turn clockwise
00187         do {
00188             clockwise();
00189             count++;
00190         } while (count<num_steps); // turn number of steps applied 
00191     else if (direction==1)// turn anticlockwise
00192         do {
00193             counterclockwise();
00194             count++;
00195         } while (count<num_steps);// turn number of steps applied 
00196 
00197 }
00198 
00199 void mMotor::ht_step(int num_steps, int direction, int speed) {// steper function: number of steps, direction (0- right, 1- left), speed (default 1200)
00200     int count=0; // initalize step count
00201     motorSpeed=speed; //set motor speed
00202     if (direction==0) // turn clockwise
00203         do {
00204             ht_clockwise();
00205             count++;
00206         } while (count<num_steps); // turn number of steps applied 
00207     else if (direction==1)// turn anticlockwise
00208         do {
00209             ht_counterclockwise();
00210             count++;
00211         } while (count<num_steps);// turn number of steps applied 
00212 
00213 }