Simple pulse direction program for testing stepper motors.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 Timeout timer;
00004 Ticker ticker;
00005 DigitalOut stepled(LED1);   // Toggles on step
00006 DigitalOut ccled(LED2);     // Counterclockwise, =Dir
00007 DigitalOut correct(LED3);   // Checks if nr. of steps made = equal to index
00008 DigitalOut toggle(LED4);    // Proves life exists
00009 
00010 // MS1 MS2 MS3     Microstep Resolution 
00011 // Low Low Low     Full step 
00012 // Hig Low Low     Half step 
00013 // Low Hig Low     Quarter step 
00014 // Hig Hig Low     Eighth step 
00015 // Hig Hig Hig     Sixteenth step 
00016 DigitalOut MS1(p11);
00017 DigitalOut MS2(p12);
00018 DigitalOut MS3(p13);
00019 
00020 //Sleep and Reset not connected.
00021 DigitalOut enable(p20);
00022 
00023 DigitalOut Step(p21);
00024 DigitalOut Dir(p22);
00025 
00026 // Motor has 200 steps per rotation and is driven Sixteenth step in comment.
00027 // So 3200 microsteps per rotation
00028 
00029 int accel=30;       // steps/s^2
00030 int speed=32000;    // steps/s  /3200   = 10 Rounds/s *60    = 600RPM
00031 int index=12800;    // steps            = 4 rotations
00032 int count;
00033 
00034 void timeout() {
00035     stepled=!stepled;
00036     Step=1;                 // Rising edge
00037     wait_us(5);             // Short high time
00038     Step=0;
00039     if(count<(index/2)) {   // Start accelerating untill the top speed is reached
00040         if(accel*count<speed) {
00041             timer.attach_us(&timeout,((1000000/((accel*count)+10))-5)); // decreasing time until next step
00042         }                           // the +10 is to avoid dividing by 0 and boosts initial speed
00043         else {      // don't go over the top speed
00044             timer.attach_us(&timeout,((1000000/speed))-5);
00045         }                           // the -5 is to compensate for the 5us high time :p
00046     }
00047     else if(count<index) {  // Halfway to the target, start decelerating
00048         if(accel*(index-count)<speed) {
00049             timer.attach_us(&timeout,((1000000/((accel*(index-count))+10))-5)); // increasing time until next stil
00050         }
00051         else {      // stay at top speed if needed
00052             timer.attach_us(&timeout,((1000000/speed))-5); 
00053         }
00054     }
00055     else {          // target reached
00056         timer.detach();
00057         stepled=0;
00058         Step=0;
00059         if(count==index) {  // Check if the right number of steps were made
00060             correct=1;
00061         }
00062         else {
00063             correct=0;
00064         }
00065     }
00066     count++;        // ***INC STEPCOUNT***
00067 }
00068         
00069 void toggler() {    // It toggles
00070     toggle=!toggle;        
00071 }
00072 
00073 int main() {
00074     // Initialize pololu drive
00075     enable=0;   // Low active
00076     MS1=1;      // Sixteenth
00077     MS2=1;
00078     MS3=1;
00079     
00080     // Initialize for movement
00081     stepled=0;
00082     count=0;
00083     correct=0;
00084     
00085     timer.attach(&timeout,1);   // Initiate a movement
00086     ticker.attach(&toggler,1);  // Toggle the live led on interrupt
00087     while(1) {
00088         wait(10); // Simulate some 10sec delay between commands
00089         
00090         // Initialize for next movement
00091         Dir=!Dir;   // Toggle Direction
00092         ccled=Dir;  // Make it obvious
00093         count=0;    // Reset count
00094         correct=0;  // Reset count=index check
00095         
00096         timer.attach(&timeout,1);   // Initiate a movement
00097     }  
00098 }