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

Fork of BipoarStepperMotor by Harsha vardan

Files at this revision

API Documentation at this revision

Comitter:
InBrewJ
Date:
Tue Feb 03 01:02:39 2015 +0000
Parent:
3:944e51dd1e4c
Child:
5:f9404f00deda
Commit message:
Made drastic changes to the stepperMotor library - this revision should perhaps be ignored...

Changed in this revision

sMotor.cpp Show annotated file Show diff for this revision Revisions of this file
sMotor.h Show annotated file Show diff for this revision Revisions of this file
--- a/sMotor.cpp	Thu Jan 29 15:50:34 2015 +0000
+++ b/sMotor.cpp	Tue Feb 03 01:02:39 2015 +0000
@@ -15,17 +15,21 @@
 
 int motorSpeed; // Steper speed
 
-sMotor::sMotor(PinName A0, PinName A1, PinName A2, PinName A3) : _A0(A0), _A1(A1), _A2(A2), _A3(A3) { // Defenition of motor pins
+sMotor::sMotor(PinName A0, PinName A1, PinName A2, PinName A3, const int maxSteps) : _A0(A0), _A1(A1), _A2(A2), _A3(A3) { // Defenition of motor pins
     _A0=0;
     _A1=0;
     _A2=0;
     _A3=0;
+    _maxSteps = maxSteps;
+    _motorPosition = maxSteps;
 }
 
 //UP
-void sMotor::anticlockwise() { // rotate the motor 4 steps UP
-    for (int i = 0; i < 4; i++) {        
-        switch (i) { // activate the ports A0, A2, A3, A3 in a binary sequence for steps
+void sMotor::anticlockwise(int num_steps) { // rotate the motor num_steps UP
+    short subStep = 0;
+    for (int i = 0; i < num_steps && _motorPosition < _maxSteps; i++) {        
+        subStep %= 4;
+        switch (subStep) { // activate the ports A0, A2, A3, A3 in a binary sequence for steps
             case 0: {
                 _A0=1;
                 _A1=0;
@@ -56,15 +60,18 @@
             break;            
         }
 
-
+        subStep++;
+        _motorPosition++;
         wait_ms(motorSpeed); // wait time defines the speed 
     }
 }
 
 // DOWN
-void sMotor::clockwise() { // rotate the motor 4 steps DOWN
-    for (int i = 0; i < 4; i++) {    
-        switch (i) {
+void sMotor::clockwise(int num_steps) { // rotate the motor num_steps DOWN
+    short subStep = 0;    
+    for (int i = 0; i < num_steps && _motorPosition > 0; i++) {    
+        subStep %= 4;
+        switch (subStep) {
             case 0: {
                 _A0=1;
                 _A1=1;
@@ -94,25 +101,26 @@
             }
             break;            
         }
-
-
+    
+        subStep++;
+        _motorPosition--;
         wait_ms(motorSpeed); // wait time defines the speed 
     }
 }
 void sMotor::step(int num_steps, int direction, int speed) {// steper function: number of steps, direction (0- right, 1- left), speed (default 1200)
-    int count=0; // initalize step count
+    //int count=0; // initalize step count
     motorSpeed=speed; //set motor speed
     if (!direction) // shaft DOWN
-        do {
-            printf("Doing Down: %d\n", count);
-            clockwise();
-            count++;
-        } while (count<num_steps/4); // turn number of steps applied 
+        //do {
+            //printf("Doing Down: %d\n", count);
+            clockwise(num_steps);
+            //count++;
+        //} while (count<num_steps); // turn number of steps applied 
     else if (direction)// Shaft UP
-        do {
-            printf("Doing Up: %d\n", count);
-            anticlockwise();
-            count++;
-        } while (count<num_steps/4);// turn number of steps applied 
+        //do {
+            //printf("Doing Up: %d\n", count);
+            anticlockwise(num_steps);
+            //count++;
+        //} while (count<num_steps);// turn number of steps applied 
 
 }
\ No newline at end of file
--- a/sMotor.h	Thu Jan 29 15:50:34 2015 +0000
+++ b/sMotor.h	Tue Feb 03 01:02:39 2015 +0000
@@ -1,11 +1,11 @@
 /*
 ##############################################
-##    Program Created by Harshavardan61     ##
+##Original Program Created by Harshavardan61##
 ##############################################
         ---- harshavardan61@gmail.com -----
-        
-This library was made for 4-Phase Stepper Motors
-I don't take any resposability for the damage caused to your equipment.
+Extended by Jason Brewer 2015
+to adapt to the stepper motor + linear actuator
+supplied by Selim Yilmaz
 
 */
 #ifndef MBED_SMOTOR_H
@@ -16,11 +16,11 @@
 class sMotor {
 public:
 
-    sMotor(PinName A0, PinName A1, PinName A2, PinName A3); //motor constructor
+    sMotor(PinName A0, PinName A1, PinName A2, PinName A3, const int maxSteps); //motor constructor
 
     void step(int num_steps, int direction, int speed);
-    void anticlockwise();
-    void clockwise();
+    void anticlockwise(int num_steps);
+    void clockwise(int num_steps);
 
 
 private:
@@ -29,6 +29,8 @@
     DigitalOut _A1;
     DigitalOut _A2;
     DigitalOut _A3;
+    int _motorPosition;
+    int _maxSteps;
 
 };