Library to run 4-wire half stepper in asynchronous mode
Async_4pin_Stepper.h@0:252d645cfc5d, 2021-06-03 (annotated)
- Committer:
- krogedal
- Date:
- Thu Jun 03 21:38:34 2021 +0000
- Revision:
- 0:252d645cfc5d
- Child:
- 1:359b46cdaf19
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
krogedal | 0:252d645cfc5d | 1 | #ifndef ASYNC_4PIN_STEPPER_H |
krogedal | 0:252d645cfc5d | 2 | #define ASYNC_4PIN_STEPPER_H |
krogedal | 0:252d645cfc5d | 3 | |
krogedal | 0:252d645cfc5d | 4 | /* Asynchronous Stepper with 4-pin output |
krogedal | 0:252d645cfc5d | 5 | * |
krogedal | 0:252d645cfc5d | 6 | * Written by Simon Krogedal |
krogedal | 0:252d645cfc5d | 7 | * 31/05/2021 |
krogedal | 0:252d645cfc5d | 8 | * Team 9 4th Year project |
krogedal | 0:252d645cfc5d | 9 | * |
krogedal | 0:252d645cfc5d | 10 | * for NUCLEO-F401RE |
krogedal | 0:252d645cfc5d | 11 | * |
krogedal | 0:252d645cfc5d | 12 | * Version 0.1 |
krogedal | 0:252d645cfc5d | 13 | * |
krogedal | 0:252d645cfc5d | 14 | */ |
krogedal | 0:252d645cfc5d | 15 | |
krogedal | 0:252d645cfc5d | 16 | #include "mbed.h" |
krogedal | 0:252d645cfc5d | 17 | |
krogedal | 0:252d645cfc5d | 18 | class Async_4pin_Stepper { |
krogedal | 0:252d645cfc5d | 19 | |
krogedal | 0:252d645cfc5d | 20 | private: |
krogedal | 0:252d645cfc5d | 21 | |
krogedal | 0:252d645cfc5d | 22 | Ticker ticker; |
krogedal | 0:252d645cfc5d | 23 | BusOut output; |
krogedal | 0:252d645cfc5d | 24 | |
krogedal | 0:252d645cfc5d | 25 | int currentPos; |
krogedal | 0:252d645cfc5d | 26 | int targetPos; |
krogedal | 0:252d645cfc5d | 27 | int speed_; |
krogedal | 0:252d645cfc5d | 28 | double stepInterval; |
krogedal | 0:252d645cfc5d | 29 | double SPR; |
krogedal | 0:252d645cfc5d | 30 | bool running; |
krogedal | 0:252d645cfc5d | 31 | |
krogedal | 0:252d645cfc5d | 32 | void step(int step); // Do a single step, called by step once funcs |
krogedal | 0:252d645cfc5d | 33 | void stepCB(); // Callback function for stepper |
krogedal | 0:252d645cfc5d | 34 | |
krogedal | 0:252d645cfc5d | 35 | protected: |
krogedal | 0:252d645cfc5d | 36 | |
krogedal | 0:252d645cfc5d | 37 | void stepOnceCW(); // Single step CW |
krogedal | 0:252d645cfc5d | 38 | void stepOnceCCW(); // Single step CCW |
krogedal | 0:252d645cfc5d | 39 | |
krogedal | 0:252d645cfc5d | 40 | public: |
krogedal | 0:252d645cfc5d | 41 | |
krogedal | 0:252d645cfc5d | 42 | typedef enum { |
krogedal | 0:252d645cfc5d | 43 | CW, // Clockwise |
krogedal | 0:252d645cfc5d | 44 | CCW // Counter-clockwise |
krogedal | 0:252d645cfc5d | 45 | } Dir; |
krogedal | 0:252d645cfc5d | 46 | |
krogedal | 0:252d645cfc5d | 47 | // Constructor takes 4 pins and a steps/rev in float presicion |
krogedal | 0:252d645cfc5d | 48 | Async_4pin_Stepper( PinName StepperPin1, |
krogedal | 0:252d645cfc5d | 49 | PinName StepperPin2, |
krogedal | 0:252d645cfc5d | 50 | PinName StepperPin3, |
krogedal | 0:252d645cfc5d | 51 | PinName StepperPin4, |
krogedal | 0:252d645cfc5d | 52 | float StepsPerRev = 4075.7728); |
krogedal | 0:252d645cfc5d | 53 | |
krogedal | 0:252d645cfc5d | 54 | |
krogedal | 0:252d645cfc5d | 55 | void setZeroPos(); // Set current position as 0 |
krogedal | 0:252d645cfc5d | 56 | void setTarget(int target); // set target |
krogedal | 0:252d645cfc5d | 57 | void goToTarget(); // Move towards given target |
krogedal | 0:252d645cfc5d | 58 | // void run(float speed, Dir dir); // Run at a given speed in step/s |
krogedal | 0:252d645cfc5d | 59 | void setSpeed(int speed); // Set speed |
krogedal | 0:252d645cfc5d | 60 | int getSpeed(); // Returns speed |
krogedal | 0:252d645cfc5d | 61 | int getPos(); // Returns current position |
krogedal | 0:252d645cfc5d | 62 | int getTarget(); // Returns target position |
krogedal | 0:252d645cfc5d | 63 | int distanceToGo(); // Returns steps left to target |
krogedal | 0:252d645cfc5d | 64 | void relax(); // Close all gates, reducing current but losing holding torque |
krogedal | 0:252d645cfc5d | 65 | bool isRunning(); // Is the motor running? |
krogedal | 0:252d645cfc5d | 66 | void stop(); |
krogedal | 0:252d645cfc5d | 67 | |
krogedal | 0:252d645cfc5d | 68 | }; |
krogedal | 0:252d645cfc5d | 69 | |
krogedal | 0:252d645cfc5d | 70 | #endif |