Simple pulse direction program for testing stepper motors.

Dependencies:   mbed

Committer:
OTBsolar
Date:
Fri Apr 29 09:15:32 2011 +0000
Revision:
0:999962b66e03

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
OTBsolar 0:999962b66e03 1 #include "mbed.h"
OTBsolar 0:999962b66e03 2
OTBsolar 0:999962b66e03 3 Timeout timer;
OTBsolar 0:999962b66e03 4 Ticker ticker;
OTBsolar 0:999962b66e03 5 DigitalOut stepled(LED1); // Toggles on step
OTBsolar 0:999962b66e03 6 DigitalOut ccled(LED2); // Counterclockwise, =Dir
OTBsolar 0:999962b66e03 7 DigitalOut correct(LED3); // Checks if nr. of steps made = equal to index
OTBsolar 0:999962b66e03 8 DigitalOut toggle(LED4); // Proves life exists
OTBsolar 0:999962b66e03 9
OTBsolar 0:999962b66e03 10 // MS1 MS2 MS3 Microstep Resolution
OTBsolar 0:999962b66e03 11 // Low Low Low Full step
OTBsolar 0:999962b66e03 12 // Hig Low Low Half step
OTBsolar 0:999962b66e03 13 // Low Hig Low Quarter step
OTBsolar 0:999962b66e03 14 // Hig Hig Low Eighth step
OTBsolar 0:999962b66e03 15 // Hig Hig Hig Sixteenth step
OTBsolar 0:999962b66e03 16 DigitalOut MS1(p11);
OTBsolar 0:999962b66e03 17 DigitalOut MS2(p12);
OTBsolar 0:999962b66e03 18 DigitalOut MS3(p13);
OTBsolar 0:999962b66e03 19
OTBsolar 0:999962b66e03 20 //Sleep and Reset not connected.
OTBsolar 0:999962b66e03 21 DigitalOut enable(p20);
OTBsolar 0:999962b66e03 22
OTBsolar 0:999962b66e03 23 DigitalOut Step(p21);
OTBsolar 0:999962b66e03 24 DigitalOut Dir(p22);
OTBsolar 0:999962b66e03 25
OTBsolar 0:999962b66e03 26 // Motor has 200 steps per rotation and is driven Sixteenth step in comment.
OTBsolar 0:999962b66e03 27 // So 3200 microsteps per rotation
OTBsolar 0:999962b66e03 28
OTBsolar 0:999962b66e03 29 int accel=30; // steps/s^2
OTBsolar 0:999962b66e03 30 int speed=32000; // steps/s /3200 = 10 Rounds/s *60 = 600RPM
OTBsolar 0:999962b66e03 31 int index=12800; // steps = 4 rotations
OTBsolar 0:999962b66e03 32 int count;
OTBsolar 0:999962b66e03 33
OTBsolar 0:999962b66e03 34 void timeout() {
OTBsolar 0:999962b66e03 35 stepled=!stepled;
OTBsolar 0:999962b66e03 36 Step=1; // Rising edge
OTBsolar 0:999962b66e03 37 wait_us(5); // Short high time
OTBsolar 0:999962b66e03 38 Step=0;
OTBsolar 0:999962b66e03 39 if(count<(index/2)) { // Start accelerating untill the top speed is reached
OTBsolar 0:999962b66e03 40 if(accel*count<speed) {
OTBsolar 0:999962b66e03 41 timer.attach_us(&timeout,((1000000/((accel*count)+10))-5)); // decreasing time until next step
OTBsolar 0:999962b66e03 42 } // the +10 is to avoid dividing by 0 and boosts initial speed
OTBsolar 0:999962b66e03 43 else { // don't go over the top speed
OTBsolar 0:999962b66e03 44 timer.attach_us(&timeout,((1000000/speed))-5);
OTBsolar 0:999962b66e03 45 } // the -5 is to compensate for the 5us high time :p
OTBsolar 0:999962b66e03 46 }
OTBsolar 0:999962b66e03 47 else if(count<index) { // Halfway to the target, start decelerating
OTBsolar 0:999962b66e03 48 if(accel*(index-count)<speed) {
OTBsolar 0:999962b66e03 49 timer.attach_us(&timeout,((1000000/((accel*(index-count))+10))-5)); // increasing time until next stil
OTBsolar 0:999962b66e03 50 }
OTBsolar 0:999962b66e03 51 else { // stay at top speed if needed
OTBsolar 0:999962b66e03 52 timer.attach_us(&timeout,((1000000/speed))-5);
OTBsolar 0:999962b66e03 53 }
OTBsolar 0:999962b66e03 54 }
OTBsolar 0:999962b66e03 55 else { // target reached
OTBsolar 0:999962b66e03 56 timer.detach();
OTBsolar 0:999962b66e03 57 stepled=0;
OTBsolar 0:999962b66e03 58 Step=0;
OTBsolar 0:999962b66e03 59 if(count==index) { // Check if the right number of steps were made
OTBsolar 0:999962b66e03 60 correct=1;
OTBsolar 0:999962b66e03 61 }
OTBsolar 0:999962b66e03 62 else {
OTBsolar 0:999962b66e03 63 correct=0;
OTBsolar 0:999962b66e03 64 }
OTBsolar 0:999962b66e03 65 }
OTBsolar 0:999962b66e03 66 count++; // ***INC STEPCOUNT***
OTBsolar 0:999962b66e03 67 }
OTBsolar 0:999962b66e03 68
OTBsolar 0:999962b66e03 69 void toggler() { // It toggles
OTBsolar 0:999962b66e03 70 toggle=!toggle;
OTBsolar 0:999962b66e03 71 }
OTBsolar 0:999962b66e03 72
OTBsolar 0:999962b66e03 73 int main() {
OTBsolar 0:999962b66e03 74 // Initialize pololu drive
OTBsolar 0:999962b66e03 75 enable=0; // Low active
OTBsolar 0:999962b66e03 76 MS1=1; // Sixteenth
OTBsolar 0:999962b66e03 77 MS2=1;
OTBsolar 0:999962b66e03 78 MS3=1;
OTBsolar 0:999962b66e03 79
OTBsolar 0:999962b66e03 80 // Initialize for movement
OTBsolar 0:999962b66e03 81 stepled=0;
OTBsolar 0:999962b66e03 82 count=0;
OTBsolar 0:999962b66e03 83 correct=0;
OTBsolar 0:999962b66e03 84
OTBsolar 0:999962b66e03 85 timer.attach(&timeout,1); // Initiate a movement
OTBsolar 0:999962b66e03 86 ticker.attach(&toggler,1); // Toggle the live led on interrupt
OTBsolar 0:999962b66e03 87 while(1) {
OTBsolar 0:999962b66e03 88 wait(10); // Simulate some 10sec delay between commands
OTBsolar 0:999962b66e03 89
OTBsolar 0:999962b66e03 90 // Initialize for next movement
OTBsolar 0:999962b66e03 91 Dir=!Dir; // Toggle Direction
OTBsolar 0:999962b66e03 92 ccled=Dir; // Make it obvious
OTBsolar 0:999962b66e03 93 count=0; // Reset count
OTBsolar 0:999962b66e03 94 correct=0; // Reset count=index check
OTBsolar 0:999962b66e03 95
OTBsolar 0:999962b66e03 96 timer.attach(&timeout,1); // Initiate a movement
OTBsolar 0:999962b66e03 97 }
OTBsolar 0:999962b66e03 98 }