Moves stepper forward and reverse at a constant speed.

Dependencies:   TextLCD mbed

Committer:
socdough
Date:
Fri Aug 19 16:32:53 2011 +0000
Revision:
1:529333e22121
Parent:
0:506d7846938a
Added Homing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
socdough 0:506d7846938a 1 /*
socdough 0:506d7846938a 2 Project Stepper
socdough 0:506d7846938a 3 Written by Tony Cacace & Brandon Ring
socdough 1:529333e22121 4 Date: 08-19-11
socdough 1:529333e22121 5 Rev: AD
socdough 0:506d7846938a 6
socdough 0:506d7846938a 7 This program will use the mbed micro-controller to step a stepper motor forward and reverse
socdough 1:529333e22121 8 based on the users input request. It also uses Limit switches to define range of travel for
socdough 1:529333e22121 9 the stage and the Home marker.
socdough 1:529333e22121 10
socdough 1:529333e22121 11 Revesions....
socdough 1:529333e22121 12 AB: Added menu function and forward and reverse loops.
socdough 1:529333e22121 13 AD: Added homing using limit switches.
socdough 0:506d7846938a 14 */
socdough 0:506d7846938a 15
socdough 0:506d7846938a 16 #include "mbed.h"
socdough 0:506d7846938a 17 #include "TextLCD.h"
socdough 0:506d7846938a 18
socdough 0:506d7846938a 19 #define DELAYSEQ 3.0 // A wait used between the main while loop
socdough 0:506d7846938a 20
socdough 0:506d7846938a 21 TextLCD lcd(p24, p26, p27, p28, p29, p30);
socdough 0:506d7846938a 22 DigitalOut enable1(p21);
socdough 0:506d7846938a 23 DigitalOut enable2(p22);
socdough 0:506d7846938a 24 DigitalOut STP1(p11);
socdough 0:506d7846938a 25 DigitalOut STP2(p12);
socdough 0:506d7846938a 26 DigitalOut STP3(p13);
socdough 0:506d7846938a 27 DigitalOut STP4(p14);
socdough 1:529333e22121 28 DigitalIn LLT(p21);
socdough 1:529333e22121 29 DigitalIn ULT(p22);
socdough 1:529333e22121 30 DigitalIn HLT(p23);
socdough 0:506d7846938a 31 Serial pc(USBTX, USBRX); // tx, rx
socdough 0:506d7846938a 32
socdough 1:529333e22121 33 int Yes = 0; //Yes means false, for homing loop
socdough 1:529333e22121 34 int No = 1; //No means true, for homing loop
socdough 0:506d7846938a 35 void moveSteps (int); // Fuction prototype: used to define how many steps to take
socdough 0:506d7846938a 36 volatile int count = 1; //keeps track of counted steps 1-4
socdough 1:529333e22121 37 float delayStep = 0.005; //Speed of motor.....0.005
socdough 1:529333e22121 38 int Exit = No; //Defines the variable exit for the HOME loop.
socdough 1:529333e22121 39 int stepcount = 0; // keeps track of how many steps were taken
socdough 0:506d7846938a 40
socdough 0:506d7846938a 41
socdough 0:506d7846938a 42 int main() {
socdough 0:506d7846938a 43 enable1 = 1; //Turn on driver
socdough 0:506d7846938a 44 enable2 = 1; //Turn on driver
socdough 0:506d7846938a 45 // int inputdir; //Variable used to define step direction.
socdough 0:506d7846938a 46 int inputsteps; //Variable used to define step amount.
socdough 0:506d7846938a 47
socdough 0:506d7846938a 48 do { //Print menu on PC to request what to do from the user.
socdough 0:506d7846938a 49 pc.printf("Menu\n\n");
socdough 0:506d7846938a 50 pc.printf("1. Home Stage.\n");
socdough 0:506d7846938a 51 pc.printf("2. Move Stage Forward.\n");
socdough 0:506d7846938a 52 pc.printf("3. Move Stage Backwards.\n");
socdough 0:506d7846938a 53 pc.printf("4. Exit Program.\n\n");
socdough 0:506d7846938a 54 pc.scanf("%d",&count);
socdough 0:506d7846938a 55
socdough 0:506d7846938a 56 switch (count) { //Switch function to move to loop requested by user.
socdough 1:529333e22121 57
socdough 0:506d7846938a 58 case 1: // (HOME) Call function here to do the required operation
socdough 1:529333e22121 59 pc.printf("Homing Stage...\n\n",count);
socdough 0:506d7846938a 60
socdough 1:529333e22121 61 Exit = No;
socdough 1:529333e22121 62 while ( (HLT < 1) && (Exit) && (ULT < 1) ) { //Moves stage forward until we hit the Home limit or Upper Limit
socdough 1:529333e22121 63
socdough 1:529333e22121 64 int stepcount = 0; // keeps track of how many steps were taken
socdough 1:529333e22121 65 inputsteps = 10; // Sets limit in case limits never trip. (ie Home or EOT limit tripped)
socdough 1:529333e22121 66 while ( (stepcount<inputsteps) && (HLT < 1) && (ULT < 1) && (Exit) ) { // compares how many steps taken to the desired steps passed in
socdough 1:529333e22121 67 count++;
socdough 1:529333e22121 68
socdough 1:529333e22121 69 if (count == 5) // if the motor went around oncce..
socdough 1:529333e22121 70 count=1; // ...reset the counter
socdough 1:529333e22121 71
socdough 1:529333e22121 72 switch (count) {
socdough 1:529333e22121 73
socdough 1:529333e22121 74 case 1: // if count equals 1 do this
socdough 1:529333e22121 75 STP1 = 1;
socdough 1:529333e22121 76 STP2 = 0;
socdough 1:529333e22121 77 STP3 = 0;
socdough 1:529333e22121 78 STP4 = 0;
socdough 1:529333e22121 79 break; // this skips the code below until stepcount++
socdough 1:529333e22121 80
socdough 1:529333e22121 81 case 2:
socdough 1:529333e22121 82 STP1 = 0;
socdough 1:529333e22121 83 STP2 = 0;
socdough 1:529333e22121 84 STP3 = 1;
socdough 1:529333e22121 85 STP4 = 0;
socdough 1:529333e22121 86 break;
socdough 1:529333e22121 87
socdough 1:529333e22121 88 case 3:
socdough 1:529333e22121 89 STP1 = 0;
socdough 1:529333e22121 90 STP2 = 1;
socdough 1:529333e22121 91 STP3 = 0;
socdough 1:529333e22121 92 STP4 = 0;
socdough 1:529333e22121 93 break;
socdough 1:529333e22121 94
socdough 1:529333e22121 95 case 4:
socdough 1:529333e22121 96 STP1 = 0;
socdough 1:529333e22121 97 STP2 = 0;
socdough 1:529333e22121 98 STP3 = 0;
socdough 1:529333e22121 99 STP4 = 1;
socdough 1:529333e22121 100 break;
socdough 1:529333e22121 101 }
socdough 1:529333e22121 102 stepcount++; //Iterate loop to keep track of steps.
socdough 1:529333e22121 103 lcd.cls(); //Clear mbed LCD screen.
socdough 1:529333e22121 104 lcd.printf("%i", stepcount);//Print loop count on LCD.
socdough 1:529333e22121 105 wait(delayStep); //Defines how fast to step the motor.
socdough 1:529333e22121 106
socdough 1:529333e22121 107 if (HLT == 1) { //If we are at the home limit sensor stop moving stage.
socdough 1:529333e22121 108 Exit = Yes; //If loop is true, stop moving stage.
socdough 1:529333e22121 109 pc.printf("Stage is Homed!\n\n");
socdough 1:529333e22121 110 }
socdough 1:529333e22121 111
socdough 1:529333e22121 112 if (stepcount==inputsteps) { //If loop reaches max count, throw error
socdough 1:529333e22121 113 Exit = Yes;
socdough 1:529333e22121 114 pc.printf("Error Homing Stage, please check connections.\n");
socdough 1:529333e22121 115 pc.printf("Stage failed to move within giving limits.\n");
socdough 1:529333e22121 116 pc.printf("Possible error, Upper limit switch wasn't tripped.\n\n");
socdough 1:529333e22121 117 }
socdough 1:529333e22121 118
socdough 1:529333e22121 119 if (ULT == 1) { //If EOT limit is reached, begin stepping stage backwards.
socdough 1:529333e22121 120
socdough 1:529333e22121 121 int stepcount = 0; // keeps track of how many steps were taken
socdough 1:529333e22121 122 inputsteps = -10; // Sets limit in case limits never trip. (ie Home or EOT limit tripped)
socdough 1:529333e22121 123 while ( (stepcount>inputsteps) && (HLT < 1) && (Exit) ) {
socdough 1:529333e22121 124 //Keep moving stage backwards until Home limit is hit.
socdough 1:529333e22121 125
socdough 1:529333e22121 126 count--;
socdough 1:529333e22121 127
socdough 1:529333e22121 128 if (count == 0) // if the motor went around oncce..
socdough 1:529333e22121 129 count=4; // ...reset the counter
socdough 1:529333e22121 130
socdough 1:529333e22121 131 switch (count) {
socdough 1:529333e22121 132
socdough 1:529333e22121 133 case 1: // if count equals 1 do this
socdough 1:529333e22121 134 STP1 = 1;
socdough 1:529333e22121 135 STP2 = 0;
socdough 1:529333e22121 136 STP3 = 0;
socdough 1:529333e22121 137 STP4 = 0;
socdough 1:529333e22121 138 break;
socdough 1:529333e22121 139
socdough 1:529333e22121 140 case 2: // this skips the code below until stepcount--
socdough 1:529333e22121 141 STP1 = 0;
socdough 1:529333e22121 142 STP2 = 0;
socdough 1:529333e22121 143 STP3 = 1;
socdough 1:529333e22121 144 STP4 = 0;
socdough 1:529333e22121 145 break;
socdough 1:529333e22121 146
socdough 1:529333e22121 147 case 3:
socdough 1:529333e22121 148 STP1 = 0;
socdough 1:529333e22121 149 STP2 = 1;
socdough 1:529333e22121 150 STP3 = 0;
socdough 1:529333e22121 151 STP4 = 0;
socdough 1:529333e22121 152 break;
socdough 1:529333e22121 153
socdough 1:529333e22121 154 case 4:
socdough 1:529333e22121 155 STP1 = 0;
socdough 1:529333e22121 156 STP2 = 0;
socdough 1:529333e22121 157 STP3 = 0;
socdough 1:529333e22121 158 STP4 = 1;
socdough 1:529333e22121 159 break;
socdough 1:529333e22121 160 }
socdough 1:529333e22121 161 stepcount--; //Iterate loop to keep track of steps.
socdough 1:529333e22121 162 lcd.cls(); //Clear mbed LCD screen.
socdough 1:529333e22121 163 lcd.printf("%i", stepcount);//Print loop count on LCD.
socdough 1:529333e22121 164 wait(delayStep);
socdough 1:529333e22121 165
socdough 1:529333e22121 166 if (LLT == 1) { //Lower limit sensor is tripped, Exit loop and throw fault.
socdough 1:529333e22121 167 Exit = Yes;
socdough 1:529333e22121 168 pc.printf("Error Homing Stage, please check connections.\n");
socdough 1:529333e22121 169 pc.printf("Lower limit sensor was tripped.\n\n");
socdough 1:529333e22121 170 }
socdough 1:529333e22121 171
socdough 1:529333e22121 172 if (HLT == 1) { //If we are at the home limit sensor stop moving stage.
socdough 1:529333e22121 173 Exit = Yes; //If loop is true, stop moving stage.
socdough 1:529333e22121 174 pc.printf("Stage is Homed!\n\n");
socdough 1:529333e22121 175 }
socdough 1:529333e22121 176
socdough 1:529333e22121 177 if (stepcount==inputsteps) { //If loop reaches max count, throw error
socdough 1:529333e22121 178 Exit = Yes;
socdough 1:529333e22121 179 pc.printf("Error Homing Stage, please check connections.\n");
socdough 1:529333e22121 180 pc.printf("Stage failed to move within giving limits.\n");
socdough 1:529333e22121 181 pc.printf("Possible error, Lower limit switch wasn't tripped.\n\n");
socdough 1:529333e22121 182 }
socdough 1:529333e22121 183 }
socdough 1:529333e22121 184 }
socdough 1:529333e22121 185 }
socdough 1:529333e22121 186 Exit = Yes;
socdough 1:529333e22121 187 }
socdough 0:506d7846938a 188 break;
socdough 0:506d7846938a 189
socdough 0:506d7846938a 190 case 2: // (Move Forward) Call function here to do the required operation
socdough 1:529333e22121 191 pc.printf("Move Stage Forward.\n\n",count);
socdough 0:506d7846938a 192
socdough 1:529333e22121 193 inputsteps=1; //makes sure whiile loop is true once.
socdough 1:529333e22121 194 while ( (inputsteps > 0) && (inputsteps < 101) ){ //Experimented to determine this range.
socdough 0:506d7846938a 195 pc.printf("How many steps do you want to move?\n"); //Next two lines print to PC for user input.
socdough 0:506d7846938a 196 pc.printf(" Enter a number between 1 and 100.\n\n");
socdough 0:506d7846938a 197 pc.scanf("%i", &inputsteps); //Reads PC terminal to take in user input.
socdough 0:506d7846938a 198 lcd.cls(); //Clears LCD screen.
socdough 0:506d7846938a 199 lcd.printf("%i", inputsteps); //Print User command to mbed LCD.
socdough 0:506d7846938a 200
socdough 1:529333e22121 201 int stepcount = 0; // keeps track of how many steps were taken
socdough 0:506d7846938a 202 pc.printf("Move started....\n\n"); //Prints so user knows command was received.
socdough 0:506d7846938a 203
socdough 0:506d7846938a 204 while (stepcount<inputsteps) { // compares how many steps taken to the desired steps passed in
socdough 0:506d7846938a 205 count++;
socdough 0:506d7846938a 206
socdough 0:506d7846938a 207 if (count == 5) // if the motor went around oncce..
socdough 0:506d7846938a 208 count=1; // ...reset the counter
socdough 0:506d7846938a 209
socdough 0:506d7846938a 210 switch (count) {
socdough 0:506d7846938a 211
socdough 0:506d7846938a 212 case 1: // if count equals 1 do this
socdough 0:506d7846938a 213 STP1 = 1;
socdough 0:506d7846938a 214 STP2 = 0;
socdough 0:506d7846938a 215 STP3 = 0;
socdough 0:506d7846938a 216 STP4 = 0;
socdough 0:506d7846938a 217 break; // this skips the code below until stepcount++
socdough 0:506d7846938a 218
socdough 0:506d7846938a 219 case 2:
socdough 0:506d7846938a 220 STP1 = 0;
socdough 0:506d7846938a 221 STP2 = 0;
socdough 0:506d7846938a 222 STP3 = 1;
socdough 0:506d7846938a 223 STP4 = 0;
socdough 0:506d7846938a 224 break;
socdough 0:506d7846938a 225
socdough 0:506d7846938a 226 case 3:
socdough 0:506d7846938a 227 STP1 = 0;
socdough 0:506d7846938a 228 STP2 = 1;
socdough 0:506d7846938a 229 STP3 = 0;
socdough 0:506d7846938a 230 STP4 = 0;
socdough 0:506d7846938a 231 break;
socdough 0:506d7846938a 232
socdough 0:506d7846938a 233 case 4:
socdough 0:506d7846938a 234 STP1 = 0;
socdough 0:506d7846938a 235 STP2 = 0;
socdough 0:506d7846938a 236 STP3 = 0;
socdough 0:506d7846938a 237 STP4 = 1;
socdough 0:506d7846938a 238 break;
socdough 0:506d7846938a 239 }
socdough 0:506d7846938a 240 stepcount++; //Iterate loop to keep track of steps.
socdough 0:506d7846938a 241 lcd.cls(); //Clear mbed LCD screen.
socdough 0:506d7846938a 242 lcd.printf("%i", stepcount);//Print loop count on LCD.
socdough 0:506d7846938a 243 wait(delayStep); //Defines how fast to step the motor.
socdough 0:506d7846938a 244 }
socdough 0:506d7846938a 245 inputsteps=0; //Reset inputsteps so that while loop becomes false.
socdough 0:506d7846938a 246 pc.printf("%i steps completed.\n\n", stepcount); //Prints number of steps performed.
socdough 0:506d7846938a 247 }
socdough 0:506d7846938a 248 break;
socdough 0:506d7846938a 249
socdough 0:506d7846938a 250 case 3: // (Move Backward) Call function here to do the required operation
socdough 1:529333e22121 251 pc.printf("Move Stage Backwards.\n\n",count);
socdough 0:506d7846938a 252
socdough 1:529333e22121 253 inputsteps=1; //makes sure while loop is true once.
socdough 1:529333e22121 254 while ( (inputsteps > 0) && (inputsteps < 101) ){ //Experimented to determine this range.
socdough 0:506d7846938a 255 pc.printf("How many steps do you want to move?\n"); //Next two lines print to PC for user input.
socdough 0:506d7846938a 256 pc.printf(" Enter a number between 1 and 100.\n\n");
socdough 0:506d7846938a 257 pc.scanf("%i", &inputsteps); //Reads PC terminal to take in user input.
socdough 0:506d7846938a 258 lcd.cls(); //Clears LCD screen.
socdough 0:506d7846938a 259 lcd.printf("%i", inputsteps); //Print User command to mbed LCD.
socdough 0:506d7846938a 260
socdough 0:506d7846938a 261 int stepcount = 0; // keeps track of how many steps were taken
socdough 0:506d7846938a 262 inputsteps=-1*inputsteps; // reverses sign so that motor steps backwards.
socdough 0:506d7846938a 263 pc.printf("Move started....\n\n"); //Prints so user knows command was received.
socdough 0:506d7846938a 264 while (stepcount>inputsteps) {
socdough 0:506d7846938a 265
socdough 0:506d7846938a 266 count--;
socdough 0:506d7846938a 267
socdough 0:506d7846938a 268 if (count == 0) // if the motor went around oncce..
socdough 0:506d7846938a 269 count=4; // ...reset the counter
socdough 0:506d7846938a 270
socdough 0:506d7846938a 271 switch (count) {
socdough 0:506d7846938a 272
socdough 0:506d7846938a 273 case 1: // if count equals 1 do this
socdough 0:506d7846938a 274 STP1 = 1;
socdough 0:506d7846938a 275 STP2 = 0;
socdough 0:506d7846938a 276 STP3 = 0;
socdough 0:506d7846938a 277 STP4 = 0;
socdough 0:506d7846938a 278 break;
socdough 0:506d7846938a 279
socdough 0:506d7846938a 280 case 2: // this skips the code below until stepcount--
socdough 0:506d7846938a 281 STP1 = 0;
socdough 0:506d7846938a 282 STP2 = 0;
socdough 0:506d7846938a 283 STP3 = 1;
socdough 0:506d7846938a 284 STP4 = 0;
socdough 0:506d7846938a 285 break;
socdough 0:506d7846938a 286
socdough 0:506d7846938a 287 case 3:
socdough 0:506d7846938a 288 STP1 = 0;
socdough 0:506d7846938a 289 STP2 = 1;
socdough 0:506d7846938a 290 STP3 = 0;
socdough 0:506d7846938a 291 STP4 = 0;
socdough 0:506d7846938a 292 break;
socdough 0:506d7846938a 293
socdough 0:506d7846938a 294 case 4:
socdough 0:506d7846938a 295 STP1 = 0;
socdough 0:506d7846938a 296 STP2 = 0;
socdough 0:506d7846938a 297 STP3 = 0;
socdough 0:506d7846938a 298 STP4 = 1;
socdough 0:506d7846938a 299 break;
socdough 0:506d7846938a 300 }
socdough 0:506d7846938a 301 stepcount--; //Iterate loop to keep track of steps.
socdough 0:506d7846938a 302 lcd.cls(); //Clear mbed LCD screen.
socdough 0:506d7846938a 303 lcd.printf("%i", stepcount);//Print loop count on LCD.
socdough 0:506d7846938a 304 wait(delayStep);
socdough 0:506d7846938a 305 }
socdough 0:506d7846938a 306 inputsteps=0; //Reset inputsteps so that while loop becomes false.
socdough 0:506d7846938a 307 pc.printf("%i steps completed.\n\n", stepcount); //Prints number of steps performed.
socdough 0:506d7846938a 308 }
socdough 0:506d7846938a 309 break;
socdough 0:506d7846938a 310
socdough 0:506d7846938a 311 case 4: // (End Program) Call function here to do the required operation
socdough 1:529333e22121 312 pc.printf("Goodbye\n\n");
socdough 0:506d7846938a 313 wait(100000); //Wait forever!
socdough 0:506d7846938a 314 break;
socdough 0:506d7846938a 315
socdough 0:506d7846938a 316 default: // (Invaild Input)
socdough 0:506d7846938a 317 pc.printf("Invaild Choice. Try again...\n\n");
socdough 0:506d7846938a 318 wait(1);
socdough 0:506d7846938a 319 break;
socdough 0:506d7846938a 320 }
socdough 0:506d7846938a 321 }
socdough 0:506d7846938a 322
socdough 0:506d7846938a 323 while (1); //Program won't run without this while loop!
socdough 0:506d7846938a 324
socdough 0:506d7846938a 325 }
socdough 0:506d7846938a 326
socdough 0:506d7846938a 327