Simon Krogedal / Async_4pin_Stepper

Dependents:   Lift_arm_control

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Async_4pin_Stepper.h Source File

Async_4pin_Stepper.h

Go to the documentation of this file.
00001 /**
00002  * @file Async_4pin_Stepper.h
00003  * @author Simon Krogedal <simon.krogedal@student.manchester.ac.uk>
00004  * @version 0.1
00005  */
00006 
00007 #ifndef ASYNC_4PIN_STEPPER_H
00008 #define ASYNC_4PIN_STEPPER_H
00009 
00010 #include "mbed.h"
00011 
00012 /** Asynchronous Stepper with 4-pin half step output class.
00013  *
00014  
00015  *
00016  * 31/05/2021
00017  * Team 9 4th Year project
00018  * 
00019  * for NUCLEO-F401RE
00020  * 
00021  
00022  *
00023  */
00024 class Async_4pin_Stepper {
00025     
00026     private:
00027     
00028         Ticker ticker;          /// Ticker object
00029         BusOut output;          /// Collection of output pins
00030         
00031         int     currentPos;     /// Current position of stepper
00032         int     targetPos;      /// Target position of stepper
00033         int     speed_;         /// Set speed for stepper movement
00034         double  stepInterval;   /// Step interval for movement at desired speed
00035         double  SPR;            /// Steps per revolution
00036         bool    running;        /// Boolean flag indicating whether the motor is running
00037         
00038         /** Do a single step, called by stepOnce functions
00039          * @param step The desired step
00040          */
00041         void    step(int step); /// Do a single step, called by step once funcs
00042         void    stepCB();       /// Callback function for stepper, called intermittedly to do a step towards the current target
00043         
00044     protected:
00045         
00046         void stepOnceCW();      /// Single step CW
00047         void stepOnceCCW();     /// Single step CCW
00048         
00049     public:
00050     
00051         typedef enum  {
00052             CW,         /// Clockwise
00053             CCW         /// Counter-clockwise
00054         } Dir;
00055     
00056         /** Constructor takes 4 pins and a steps/rev in float presicion
00057          *
00058          * @param StepperPin1 Pin 1 on stepper motor
00059          * @param StepperPin2 Pin 2 on stepper motor
00060          * @param StepperPin3 Pin 3 on stepper motor
00061          * @param StepperPin4 Pin 4 on stepper motor
00062          * @param StepsPerRev Steps per revolution. Defaults to 4075.8, for 28BYJ-48
00063          */
00064         Async_4pin_Stepper( PinName StepperPin1,
00065                             PinName StepperPin2,
00066                             PinName StepperPin3,
00067                             PinName StepperPin4,
00068                             float   StepsPerRev = 4075.7728);
00069                             
00070         /// Set current position as 0
00071         void setZeroPos();
00072         
00073         /** Set target
00074          * @param target Target step, can be positive or negative
00075          */
00076         void setTarget(int target);
00077         
00078         /// Move towards given target
00079         void goToTarget();
00080         
00081         // Run at a given speed in step/s
00082 //        void run(float speed, Dir dir);   
00083 
00084         /** Set speed
00085          * @param speed Speed in steps/seconds
00086          */
00087         void setSpeed(int speed);               
00088         
00089         /** Get speed
00090          * @returns Speed in steps/seconds
00091          */
00092         int getSpeed();
00093         
00094         /** Get position
00095          * @returns Stepper position
00096          */
00097         int getPos();
00098         
00099         /** Get target position
00100          * @returns Target step position
00101          */
00102         int getTarget();
00103         
00104         /** Get distance left to target
00105          * @returns Number of steps away from target
00106          */
00107         int distanceToGo();
00108         
00109         /** Check if motor is running
00110          * @returns
00111          *   0 Motor is not running (ticker is not attached)
00112          *   1 Motor is running (ticker is attached)
00113          */
00114         bool isRunning();
00115         
00116         /// Close all gates, reducing current but losing holding torque
00117         void relax();
00118         
00119         /// Stop the motor and detach ticker
00120         void stop();
00121 
00122 };
00123 
00124 #endif