Provides an API software interface to TIMER2 to control upto four stepper motors.

Dependents:   Steppermotor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers example2.h Source File

example2.h

00001 
00002 #include "mbed.h"
00003 #include "SimpleStepperProfiler.h"
00004 
00005 SimpleStepperOutput led1(LED1);
00006 
00007 // SimpleStepperOutput is basically the same as
00008 // Mbed's DigitalOut class. However, it's more
00009 // portable to other platforms without having
00010 // to also port the entire Mbed library.
00011 SimpleStepperOutput sdir0(p17);
00012 
00013 // Create four stepper profilers.
00014 // Stepper0 has the pulse output on p8 and dir on p17
00015 SimpleStepperProfiler stepper0(p8, &sdir0);
00016 
00017 int main() {
00018     
00019     // We do not need to maintain the stepper position
00020     // for this simple example. This reduces the amount
00021     // of work the ISR has to do.
00022     stepper0.setMaintainPositionData(false);
00023     
00024     // Set all steppers to top speed of 5000 pulses/second.
00025     // Because we are using the SimpleStepperProfiler then
00026     // the accel will be 1000pps/s, so to reach 5000pps will
00027     // take 5seconds.
00028     
00029     // The accel profile can be adjusted by altering the profilers
00030     // step value and poll interval. The default step value is 10
00031     // each poll interval and the default poll interval is 10ms.
00032     // This equates to 100 * 10 = 1000pps/s. Adjust as required
00033     // using :-
00034     //   setStepPos(int i); where i is the step value +/- each poll interval accel
00035     //   setStepNeg(int i); where i is the step value +/- each poll interval decel
00036     //   setPollInterval(int i) where i is the poll interval in microseconds.
00037     
00038     stepper0.setSpeed(5000);
00039     while(stepper0.getSpeed() != 5000); // wait to reach speed.
00040     
00041     // Decel stepper to halt/stop.
00042     stepper0.setSpeed(0);
00043     while(stepper0.getSpeed() != 0); // wait to reach speed.
00044     
00045     // Set speed but peform async emergency stop while accelerating.
00046     stepper0.setSpeed(5000);
00047     wait(2.5);
00048     stepper0.eStop();
00049     
00050     while(1) {
00051         led1 = !led1;
00052         wait(0.2);        
00053     }
00054 }