Moves stepper forward and reverse at a constant speed.

Dependencies:   TextLCD mbed

Files at this revision

API Documentation at this revision

Comitter:
socdough
Date:
Fri Aug 19 16:32:53 2011 +0000
Parent:
0:506d7846938a
Commit message:
Added Homing

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Aug 18 15:04:19 2011 +0000
+++ b/main.cpp	Fri Aug 19 16:32:53 2011 +0000
@@ -1,11 +1,16 @@
 /* 
 Project Stepper
 Written by Tony Cacace & Brandon Ring
-Date: 08-18-11
-Rev: AB
+Date: 08-19-11
+Rev: AD
 
 This program will use the mbed micro-controller to step a stepper motor forward and reverse
-based on the users input request.  
+based on the users input request.  It also uses Limit switches to define range of travel for
+the stage and the Home marker.
+
+Revesions....
+AB: Added menu function and forward and reverse loops.
+AD: Added homing using limit switches.
 */
 
 #include "mbed.h"
@@ -20,12 +25,18 @@
 DigitalOut STP2(p12);
 DigitalOut STP3(p13);
 DigitalOut STP4(p14);
+DigitalIn  LLT(p21);
+DigitalIn  ULT(p22);
+DigitalIn  HLT(p23);
 Serial pc(USBTX, USBRX); // tx, rx
 
+int Yes = 0;      //Yes means false, for homing loop
+int No = 1;       //No means true, for homing loop
 void moveSteps (int);       // Fuction prototype: used to define how many steps to take
 volatile int count = 1;     //keeps track of counted steps 1-4 
-float delayStep = 0.5;    //Speed of motor.....0.005
-
+float delayStep = 0.005;    //Speed of motor.....0.005
+int Exit = No;           //Defines the variable exit for the HOME loop.
+int stepcount = 0;              // keeps track of how many steps were taken
 
 
 int main() {
@@ -41,25 +52,153 @@
     pc.printf("3. Move Stage Backwards.\n");
     pc.printf("4. Exit Program.\n\n");
     pc.scanf("%d",&count);
-    pc.printf("Option %i\n\n",count);
   
  switch (count) {   //Switch function to move to loop requested by user.
+ 
      case 1: // (HOME) Call function here to do the required operation
+     pc.printf("Homing Stage...\n\n",count);
      
-            pc.printf("We're sorry, this function is not available at this time!!\n\n");
+        Exit = No;
+        while ( (HLT < 1) && (Exit) && (ULT < 1) ) { //Moves stage forward until we hit the Home limit or Upper Limit
+            
+            int stepcount = 0;              // keeps track of how many steps were taken
+            inputsteps = 10;             // Sets limit in case limits never trip. (ie Home or EOT limit tripped)
+            while ( (stepcount<inputsteps) && (HLT < 1) && (ULT < 1) && (Exit) ) { // compares how many steps taken to the desired steps passed in 
+                count++;
+                
+                    if (count == 5)     // if the motor went around oncce..
+                        count=1;        // ...reset the counter
+                
+                    switch (count) {
+                
+                    case 1:     // if count equals 1 do this
+                        STP1 = 1;
+                        STP2 = 0;
+                        STP3 = 0;
+                        STP4 = 0;
+                    break;      // this skips the code below until stepcount++ 
+        
+                    case 2:
+                        STP1 = 0;
+                        STP2 = 0;
+                        STP3 = 1;
+                        STP4 = 0;
+                    break;
+
+                    case 3:
+                        STP1 = 0;
+                        STP2 = 1;
+                        STP3 = 0;
+                        STP4 = 0;
+                    break;
+    
+                    case 4:
+                        STP1 = 0;
+                        STP2 = 0;
+                        STP3 = 0;
+                        STP4 = 1;
+                    break;
+                    }
+                    stepcount++;                //Iterate loop to keep track of steps.
+                    lcd.cls();                  //Clear mbed LCD screen.
+                    lcd.printf("%i", stepcount);//Print loop count on LCD.
+                    wait(delayStep);            //Defines how fast to step the motor.
+                    
+                    if (HLT == 1) {     //If we are at the home limit sensor stop moving stage.
+                        Exit = Yes;     //If loop is true, stop moving stage.
+                        pc.printf("Stage is Homed!\n\n");
+                        }
+                        
+                    if (stepcount==inputsteps) { //If loop reaches max count, throw error
+                                Exit = Yes;
+                                pc.printf("Error Homing Stage, please check connections.\n");
+                                pc.printf("Stage failed to move within giving limits.\n");
+                                pc.printf("Possible error, Upper limit switch wasn't tripped.\n\n");
+                                }
+                        
+                    if (ULT == 1) {  //If EOT limit is reached, begin stepping stage backwards.
+                    
+                        int stepcount = 0;       // keeps track of how many steps were taken
+                        inputsteps = -10;        // Sets limit in case limits never trip. (ie Home or EOT limit tripped)
+                        while ( (stepcount>inputsteps) && (HLT < 1) && (Exit) ) { 
+                            //Keep moving stage backwards until Home limit is hit.
+
+                            count--;
+      
+                            if (count == 0) // if the motor went around oncce..
+                                count=4;    // ...reset the counter
+                    
+                            switch (count) {
+      
+                            case 1:     // if count equals 1 do this
+                                STP1 = 1;
+                                STP2 = 0;
+                                STP3 = 0;
+                                STP4 = 0;
+                            break;
+
+                            case 2:     // this skips the code below until stepcount--
+                                STP1 = 0;
+                                STP2 = 0;
+                                STP3 = 1;
+                                STP4 = 0;
+                            break;
+
+                            case 3:
+                                STP1 = 0;
+                                STP2 = 1;
+                                STP3 = 0;
+                                STP4 = 0;
+                            break;
+
+                            case 4:
+                                STP1 = 0;
+                                STP2 = 0;
+                                STP3 = 0;
+                                STP4 = 1;
+                            break;
+                            }
+                            stepcount--;            //Iterate loop to keep track of steps.
+                            lcd.cls();              //Clear mbed LCD screen.
+                            lcd.printf("%i", stepcount);//Print loop count on LCD.
+                            wait(delayStep);
+                            
+                            if (LLT == 1) { //Lower limit sensor is tripped, Exit loop and throw fault.
+                                Exit = Yes;
+                                pc.printf("Error Homing Stage, please check connections.\n");
+                                pc.printf("Lower limit sensor was tripped.\n\n");
+                                }
+                                
+                            if (HLT == 1) {  //If we are at the home limit sensor stop moving stage.
+                                Exit = Yes;     //If loop is true, stop moving stage.
+                                pc.printf("Stage is Homed!\n\n");
+                                }
+                                
+                            if (stepcount==inputsteps) { //If loop reaches max count, throw error
+                                Exit = Yes;
+                                pc.printf("Error Homing Stage, please check connections.\n");
+                                pc.printf("Stage failed to move within giving limits.\n");
+                                pc.printf("Possible error, Lower limit switch wasn't tripped.\n\n");
+                                }
+                            }
+                        }    
+                    }
+                Exit = Yes;    
+                }
             break;
             
      case 2: // (Move Forward) Call function here to do the required operation
+     pc.printf("Move Stage Forward.\n\n",count);
      
-            inputsteps=1;
-     while ( (inputsteps > 0) && (inputsteps < 101) ){       //Experimented to determine this range.
+            inputsteps=1;   //makes sure whiile loop is true once.
+     while ( (inputsteps > 0) && (inputsteps < 101) ){          //Experimented to determine this range.
             pc.printf("How many steps do you want to move?\n"); //Next two lines print to PC for user input.
             pc.printf("     Enter a number between 1 and 100.\n\n");
             pc.scanf("%i", &inputsteps);    //Reads PC terminal to take in user input.
             lcd.cls();                      //Clears LCD screen.
             lcd.printf("%i", inputsteps);   //Print User command to mbed LCD.
             
-            int stepcount = 0;              // keeps track of how many steps were taken
+            int stepcount = 0;                 // keeps track of how many steps were taken
             pc.printf("Move started....\n\n"); //Prints so user knows command was received.
             
                 while (stepcount<inputsteps) { // compares how many steps taken to the desired steps passed in 
@@ -109,9 +248,10 @@
             break;
             
      case 3: // (Move Backward) Call function here to do the required operation
+     pc.printf("Move Stage Backwards.\n\n",count);
      
-            inputsteps=1;
-     while ( (inputsteps > 0) && (inputsteps < 101) ){       //Experimented to determine this range.
+            inputsteps=1;   //makes sure while loop is true once.
+     while ( (inputsteps > 0) && (inputsteps < 101) ){          //Experimented to determine this range.
             pc.printf("How many steps do you want to move?\n"); //Next two lines print to PC for user input.
             pc.printf("     Enter a number between 1 and 100.\n\n");
             pc.scanf("%i", &inputsteps);    //Reads PC terminal to take in user input.
@@ -169,7 +309,7 @@
             break;
             
      case 4: // (End Program) Call function here to do the required operation
-            printf("Goodbye\n\n");
+            pc.printf("Goodbye\n\n");
             wait(100000); //Wait forever!
             break;