Provides an API software interface to TIMER2 to control upto four stepper motors.
inc/example2.h@4:7b7940df7865, 2011-05-20 (annotated)
- Committer:
- AjK
- Date:
- Fri May 20 09:13:31 2011 +0000
- Revision:
- 4:7b7940df7865
1.1 See ChangeLog.h
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AjK | 4:7b7940df7865 | 1 | |
AjK | 4:7b7940df7865 | 2 | #include "mbed.h" |
AjK | 4:7b7940df7865 | 3 | #include "SimpleStepperProfiler.h" |
AjK | 4:7b7940df7865 | 4 | |
AjK | 4:7b7940df7865 | 5 | SimpleStepperOutput led1(LED1); |
AjK | 4:7b7940df7865 | 6 | |
AjK | 4:7b7940df7865 | 7 | // SimpleStepperOutput is basically the same as |
AjK | 4:7b7940df7865 | 8 | // Mbed's DigitalOut class. However, it's more |
AjK | 4:7b7940df7865 | 9 | // portable to other platforms without having |
AjK | 4:7b7940df7865 | 10 | // to also port the entire Mbed library. |
AjK | 4:7b7940df7865 | 11 | SimpleStepperOutput sdir0(p17); |
AjK | 4:7b7940df7865 | 12 | |
AjK | 4:7b7940df7865 | 13 | // Create four stepper profilers. |
AjK | 4:7b7940df7865 | 14 | // Stepper0 has the pulse output on p8 and dir on p17 |
AjK | 4:7b7940df7865 | 15 | SimpleStepperProfiler stepper0(p8, &sdir0); |
AjK | 4:7b7940df7865 | 16 | |
AjK | 4:7b7940df7865 | 17 | int main() { |
AjK | 4:7b7940df7865 | 18 | |
AjK | 4:7b7940df7865 | 19 | // We do not need to maintain the stepper position |
AjK | 4:7b7940df7865 | 20 | // for this simple example. This reduces the amount |
AjK | 4:7b7940df7865 | 21 | // of work the ISR has to do. |
AjK | 4:7b7940df7865 | 22 | stepper0.setMaintainPositionData(false); |
AjK | 4:7b7940df7865 | 23 | |
AjK | 4:7b7940df7865 | 24 | // Set all steppers to top speed of 5000 pulses/second. |
AjK | 4:7b7940df7865 | 25 | // Because we are using the SimpleStepperProfiler then |
AjK | 4:7b7940df7865 | 26 | // the accel will be 1000pps/s, so to reach 5000pps will |
AjK | 4:7b7940df7865 | 27 | // take 5seconds. |
AjK | 4:7b7940df7865 | 28 | |
AjK | 4:7b7940df7865 | 29 | // The accel profile can be adjusted by altering the profilers |
AjK | 4:7b7940df7865 | 30 | // step value and poll interval. The default step value is 10 |
AjK | 4:7b7940df7865 | 31 | // each poll interval and the default poll interval is 10ms. |
AjK | 4:7b7940df7865 | 32 | // This equates to 100 * 10 = 1000pps/s. Adjust as required |
AjK | 4:7b7940df7865 | 33 | // using :- |
AjK | 4:7b7940df7865 | 34 | // setStepPos(int i); where i is the step value +/- each poll interval accel |
AjK | 4:7b7940df7865 | 35 | // setStepNeg(int i); where i is the step value +/- each poll interval decel |
AjK | 4:7b7940df7865 | 36 | // setPollInterval(int i) where i is the poll interval in microseconds. |
AjK | 4:7b7940df7865 | 37 | |
AjK | 4:7b7940df7865 | 38 | stepper0.setSpeed(5000); |
AjK | 4:7b7940df7865 | 39 | while(stepper0.getSpeed() != 5000); // wait to reach speed. |
AjK | 4:7b7940df7865 | 40 | |
AjK | 4:7b7940df7865 | 41 | // Decel stepper to halt/stop. |
AjK | 4:7b7940df7865 | 42 | stepper0.setSpeed(0); |
AjK | 4:7b7940df7865 | 43 | while(stepper0.getSpeed() != 0); // wait to reach speed. |
AjK | 4:7b7940df7865 | 44 | |
AjK | 4:7b7940df7865 | 45 | // Set speed but peform async emergency stop while accelerating. |
AjK | 4:7b7940df7865 | 46 | stepper0.setSpeed(5000); |
AjK | 4:7b7940df7865 | 47 | wait(2.5); |
AjK | 4:7b7940df7865 | 48 | stepper0.eStop(); |
AjK | 4:7b7940df7865 | 49 | |
AjK | 4:7b7940df7865 | 50 | while(1) { |
AjK | 4:7b7940df7865 | 51 | led1 = !led1; |
AjK | 4:7b7940df7865 | 52 | wait(0.2); |
AjK | 4:7b7940df7865 | 53 | } |
AjK | 4:7b7940df7865 | 54 | } |