A fork of a fine man's work. simplified. No microstepping etc, just a work in progress

Fork of BipoarStepperMotor by Harsha vardan

Committer:
InBrewJ
Date:
Tue Feb 03 03:44:45 2015 +0000
Revision:
6:84c5df74391b
changed the names to "lsMotor.h" etc - for "Linear Stepper Motor"

Who changed what in which revision?

UserRevisionLine numberNew contents of line
InBrewJ 6:84c5df74391b 1 /*
InBrewJ 6:84c5df74391b 2 ##############################################
InBrewJ 6:84c5df74391b 3 ## Program Created by Harshavardan61 ##
InBrewJ 6:84c5df74391b 4 ##############################################
InBrewJ 6:84c5df74391b 5 ---- harshavardan61@gmail.com -----
InBrewJ 6:84c5df74391b 6
InBrewJ 6:84c5df74391b 7 Extended by Jason Brewer 2015
InBrewJ 6:84c5df74391b 8
InBrewJ 6:84c5df74391b 9 */
InBrewJ 6:84c5df74391b 10
InBrewJ 6:84c5df74391b 11 #include "lsMotor.h"
InBrewJ 6:84c5df74391b 12 #include "mbed.h"
InBrewJ 6:84c5df74391b 13
InBrewJ 6:84c5df74391b 14 lsMotor::lsMotor(PinName A0, PinName A1, PinName A2, PinName A3, PinName motorEnable, const int maxSteps) : _A0(A0), _A1(A1),
InBrewJ 6:84c5df74391b 15 _A2(A2), _A3(A3), _motorEnable(motorEnable) {
InBrewJ 6:84c5df74391b 16 _A0=0;
InBrewJ 6:84c5df74391b 17 _A1=0;
InBrewJ 6:84c5df74391b 18 _A2=0;
InBrewJ 6:84c5df74391b 19 _A3=0;
InBrewJ 6:84c5df74391b 20 _motorEnable = 0;
InBrewJ 6:84c5df74391b 21 _maxSteps = maxSteps;
InBrewJ 6:84c5df74391b 22 _motorPosition = maxSteps;
InBrewJ 6:84c5df74391b 23 }
InBrewJ 6:84c5df74391b 24
InBrewJ 6:84c5df74391b 25 //UP
InBrewJ 6:84c5df74391b 26 void lsMotor::up(int num_steps) { // rotate the motor num_steps UP
InBrewJ 6:84c5df74391b 27 short subStep = 0;
InBrewJ 6:84c5df74391b 28 //printf("In UP\n");
InBrewJ 6:84c5df74391b 29 for (int i = 0; i < num_steps && _motorPosition < _maxSteps; i++) {
InBrewJ 6:84c5df74391b 30 //printf("numsteps: %d Doing UP %d pos: %d\n", num_steps, i, _motorPosition);
InBrewJ 6:84c5df74391b 31 switch (subStep) { // activate the ports A0, A2, A3, A3 in a binary sequence for steps
InBrewJ 6:84c5df74391b 32 case 0: {
InBrewJ 6:84c5df74391b 33 _A0=1;
InBrewJ 6:84c5df74391b 34 _A1=0;
InBrewJ 6:84c5df74391b 35 _A2=0;
InBrewJ 6:84c5df74391b 36 _A3=1;
InBrewJ 6:84c5df74391b 37 }
InBrewJ 6:84c5df74391b 38 break;
InBrewJ 6:84c5df74391b 39 case 1: {
InBrewJ 6:84c5df74391b 40 _A0=0;
InBrewJ 6:84c5df74391b 41 _A1=0;
InBrewJ 6:84c5df74391b 42 _A2=1;
InBrewJ 6:84c5df74391b 43 _A3=1;
InBrewJ 6:84c5df74391b 44 }
InBrewJ 6:84c5df74391b 45 break;
InBrewJ 6:84c5df74391b 46 case 2: {
InBrewJ 6:84c5df74391b 47 _A0=0;
InBrewJ 6:84c5df74391b 48 _A1=1;
InBrewJ 6:84c5df74391b 49 _A2=1;
InBrewJ 6:84c5df74391b 50 _A3=0;
InBrewJ 6:84c5df74391b 51 }
InBrewJ 6:84c5df74391b 52 break;
InBrewJ 6:84c5df74391b 53 case 3: {
InBrewJ 6:84c5df74391b 54 _A0=1;
InBrewJ 6:84c5df74391b 55 _A1=1;
InBrewJ 6:84c5df74391b 56 _A2=0;
InBrewJ 6:84c5df74391b 57 _A3=0;
InBrewJ 6:84c5df74391b 58 }
InBrewJ 6:84c5df74391b 59 break;
InBrewJ 6:84c5df74391b 60 }
InBrewJ 6:84c5df74391b 61
InBrewJ 6:84c5df74391b 62 subStep++;
InBrewJ 6:84c5df74391b 63 subStep %= 4;
InBrewJ 6:84c5df74391b 64 _motorPosition++;
InBrewJ 6:84c5df74391b 65 wait_ms(_motorSpeed);
InBrewJ 6:84c5df74391b 66 }
InBrewJ 6:84c5df74391b 67 /*_A0=0;
InBrewJ 6:84c5df74391b 68 _A1=0;
InBrewJ 6:84c5df74391b 69 _A2=0;
InBrewJ 6:84c5df74391b 70 _A3=0;*/
InBrewJ 6:84c5df74391b 71 return;
InBrewJ 6:84c5df74391b 72 }
InBrewJ 6:84c5df74391b 73
InBrewJ 6:84c5df74391b 74 // DOWN
InBrewJ 6:84c5df74391b 75 void lsMotor::down(int num_steps) { // rotate the motor num_steps DOWN
InBrewJ 6:84c5df74391b 76 short subStep = 0;
InBrewJ 6:84c5df74391b 77 //printf("In DOWN\n");
InBrewJ 6:84c5df74391b 78 for (int i = 0; i < num_steps && _motorPosition > 0; i++) {
InBrewJ 6:84c5df74391b 79 //printf("numsteps: %d Doing DOWN %d pos: %d\n", num_steps, i, _motorPosition);
InBrewJ 6:84c5df74391b 80 switch (subStep) {
InBrewJ 6:84c5df74391b 81 case 0: {
InBrewJ 6:84c5df74391b 82 _A0=1;
InBrewJ 6:84c5df74391b 83 _A1=1;
InBrewJ 6:84c5df74391b 84 _A2=0;
InBrewJ 6:84c5df74391b 85 _A3=0;
InBrewJ 6:84c5df74391b 86 }
InBrewJ 6:84c5df74391b 87 break;
InBrewJ 6:84c5df74391b 88 case 1: {
InBrewJ 6:84c5df74391b 89 _A0=0;
InBrewJ 6:84c5df74391b 90 _A1=1;
InBrewJ 6:84c5df74391b 91 _A2=1;
InBrewJ 6:84c5df74391b 92 _A3=0;
InBrewJ 6:84c5df74391b 93 }
InBrewJ 6:84c5df74391b 94 break;
InBrewJ 6:84c5df74391b 95 case 2: {
InBrewJ 6:84c5df74391b 96 _A0=0;
InBrewJ 6:84c5df74391b 97 _A1=0;
InBrewJ 6:84c5df74391b 98 _A2=1;
InBrewJ 6:84c5df74391b 99 _A3=1;
InBrewJ 6:84c5df74391b 100 }
InBrewJ 6:84c5df74391b 101 break;
InBrewJ 6:84c5df74391b 102 case 3: {
InBrewJ 6:84c5df74391b 103 _A0=1;
InBrewJ 6:84c5df74391b 104 _A1=0;
InBrewJ 6:84c5df74391b 105 _A2=0;
InBrewJ 6:84c5df74391b 106 _A3=1;
InBrewJ 6:84c5df74391b 107 }
InBrewJ 6:84c5df74391b 108 break;
InBrewJ 6:84c5df74391b 109 }
InBrewJ 6:84c5df74391b 110
InBrewJ 6:84c5df74391b 111 subStep++;
InBrewJ 6:84c5df74391b 112 subStep %= 4;
InBrewJ 6:84c5df74391b 113 _motorPosition--;
InBrewJ 6:84c5df74391b 114 wait_ms(_motorSpeed); // wait time defines the speed
InBrewJ 6:84c5df74391b 115 }
InBrewJ 6:84c5df74391b 116 /*_A0=0;
InBrewJ 6:84c5df74391b 117 _A1=0;
InBrewJ 6:84c5df74391b 118 _A2=0;
InBrewJ 6:84c5df74391b 119 _A3=0;*/
InBrewJ 6:84c5df74391b 120 return;
InBrewJ 6:84c5df74391b 121 }
InBrewJ 6:84c5df74391b 122
InBrewJ 6:84c5df74391b 123 void lsMotor::setMotorPosition(int position) {
InBrewJ 6:84c5df74391b 124 _motorPosition = position;
InBrewJ 6:84c5df74391b 125 return;
InBrewJ 6:84c5df74391b 126 }
InBrewJ 6:84c5df74391b 127
InBrewJ 6:84c5df74391b 128 void lsMotor::step(int num_steps, int direction, int speed) {// steper function: number of steps, direction (0- down, 1- up), speed in ms
InBrewJ 6:84c5df74391b 129 _motorSpeed = speed; //set motor speed
InBrewJ 6:84c5df74391b 130 if (!direction){ // shaft DOWN
InBrewJ 6:84c5df74391b 131 _motorEnable = 1;
InBrewJ 6:84c5df74391b 132 down(num_steps);
InBrewJ 6:84c5df74391b 133 _motorEnable = 0;
InBrewJ 6:84c5df74391b 134 }else if (direction){// Shaft UP
InBrewJ 6:84c5df74391b 135 _motorEnable = 1;
InBrewJ 6:84c5df74391b 136 up(num_steps);
InBrewJ 6:84c5df74391b 137 _motorEnable = 0;
InBrewJ 6:84c5df74391b 138 }
InBrewJ 6:84c5df74391b 139 }