Library to run 4-wire half stepper in asynchronous mode
Diff: Async_4pin_Stepper.h
- Revision:
- 0:252d645cfc5d
- Child:
- 1:359b46cdaf19
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Async_4pin_Stepper.h Thu Jun 03 21:38:34 2021 +0000 @@ -0,0 +1,70 @@ +#ifndef ASYNC_4PIN_STEPPER_H +#define ASYNC_4PIN_STEPPER_H + +/* Asynchronous Stepper with 4-pin output + * + * Written by Simon Krogedal + * 31/05/2021 + * Team 9 4th Year project + * + * for NUCLEO-F401RE + * + * Version 0.1 + * + */ + +#include "mbed.h" + +class Async_4pin_Stepper { + + private: + + Ticker ticker; + BusOut output; + + int currentPos; + int targetPos; + int speed_; + double stepInterval; + double SPR; + bool running; + + void step(int step); // Do a single step, called by step once funcs + void stepCB(); // Callback function for stepper + + protected: + + void stepOnceCW(); // Single step CW + void stepOnceCCW(); // Single step CCW + + public: + + typedef enum { + CW, // Clockwise + CCW // Counter-clockwise + } Dir; + + // Constructor takes 4 pins and a steps/rev in float presicion + Async_4pin_Stepper( PinName StepperPin1, + PinName StepperPin2, + PinName StepperPin3, + PinName StepperPin4, + float StepsPerRev = 4075.7728); + + + void setZeroPos(); // Set current position as 0 + void setTarget(int target); // set target + void goToTarget(); // Move towards given target +// void run(float speed, Dir dir); // Run at a given speed in step/s + void setSpeed(int speed); // Set speed + int getSpeed(); // Returns speed + int getPos(); // Returns current position + int getTarget(); // Returns target position + int distanceToGo(); // Returns steps left to target + void relax(); // Close all gates, reducing current but losing holding torque + bool isRunning(); // Is the motor running? + void stop(); + +}; + +#endif \ No newline at end of file