Library to run 4-wire half stepper in asynchronous mode

Dependents:   Lift_arm_control

Committer:
krogedal
Date:
Sat Jun 05 23:06:24 2021 +0000
Revision:
1:359b46cdaf19
Parent:
0:252d645cfc5d
Updated for documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
krogedal 1:359b46cdaf19 1 /**
krogedal 1:359b46cdaf19 2 * @file Async_4pin_Stepper.h
krogedal 1:359b46cdaf19 3 * @author Simon Krogedal <simon.krogedal@student.manchester.ac.uk>
krogedal 1:359b46cdaf19 4 * @version 0.1
krogedal 1:359b46cdaf19 5 */
krogedal 1:359b46cdaf19 6
krogedal 0:252d645cfc5d 7 #ifndef ASYNC_4PIN_STEPPER_H
krogedal 0:252d645cfc5d 8 #define ASYNC_4PIN_STEPPER_H
krogedal 0:252d645cfc5d 9
krogedal 1:359b46cdaf19 10 #include "mbed.h"
krogedal 1:359b46cdaf19 11
krogedal 1:359b46cdaf19 12 /** Asynchronous Stepper with 4-pin half step output class.
krogedal 0:252d645cfc5d 13 *
krogedal 1:359b46cdaf19 14
krogedal 1:359b46cdaf19 15 *
krogedal 0:252d645cfc5d 16 * 31/05/2021
krogedal 0:252d645cfc5d 17 * Team 9 4th Year project
krogedal 0:252d645cfc5d 18 *
krogedal 0:252d645cfc5d 19 * for NUCLEO-F401RE
krogedal 0:252d645cfc5d 20 *
krogedal 1:359b46cdaf19 21
krogedal 0:252d645cfc5d 22 *
krogedal 0:252d645cfc5d 23 */
krogedal 0:252d645cfc5d 24 class Async_4pin_Stepper {
krogedal 0:252d645cfc5d 25
krogedal 0:252d645cfc5d 26 private:
krogedal 0:252d645cfc5d 27
krogedal 1:359b46cdaf19 28 Ticker ticker; /// Ticker object
krogedal 1:359b46cdaf19 29 BusOut output; /// Collection of output pins
krogedal 0:252d645cfc5d 30
krogedal 1:359b46cdaf19 31 int currentPos; /// Current position of stepper
krogedal 1:359b46cdaf19 32 int targetPos; /// Target position of stepper
krogedal 1:359b46cdaf19 33 int speed_; /// Set speed for stepper movement
krogedal 1:359b46cdaf19 34 double stepInterval; /// Step interval for movement at desired speed
krogedal 1:359b46cdaf19 35 double SPR; /// Steps per revolution
krogedal 1:359b46cdaf19 36 bool running; /// Boolean flag indicating whether the motor is running
krogedal 0:252d645cfc5d 37
krogedal 1:359b46cdaf19 38 /** Do a single step, called by stepOnce functions
krogedal 1:359b46cdaf19 39 * @param step The desired step
krogedal 1:359b46cdaf19 40 */
krogedal 1:359b46cdaf19 41 void step(int step); /// Do a single step, called by step once funcs
krogedal 1:359b46cdaf19 42 void stepCB(); /// Callback function for stepper, called intermittedly to do a step towards the current target
krogedal 0:252d645cfc5d 43
krogedal 0:252d645cfc5d 44 protected:
krogedal 0:252d645cfc5d 45
krogedal 1:359b46cdaf19 46 void stepOnceCW(); /// Single step CW
krogedal 1:359b46cdaf19 47 void stepOnceCCW(); /// Single step CCW
krogedal 0:252d645cfc5d 48
krogedal 0:252d645cfc5d 49 public:
krogedal 0:252d645cfc5d 50
krogedal 0:252d645cfc5d 51 typedef enum {
krogedal 1:359b46cdaf19 52 CW, /// Clockwise
krogedal 1:359b46cdaf19 53 CCW /// Counter-clockwise
krogedal 0:252d645cfc5d 54 } Dir;
krogedal 0:252d645cfc5d 55
krogedal 1:359b46cdaf19 56 /** Constructor takes 4 pins and a steps/rev in float presicion
krogedal 1:359b46cdaf19 57 *
krogedal 1:359b46cdaf19 58 * @param StepperPin1 Pin 1 on stepper motor
krogedal 1:359b46cdaf19 59 * @param StepperPin2 Pin 2 on stepper motor
krogedal 1:359b46cdaf19 60 * @param StepperPin3 Pin 3 on stepper motor
krogedal 1:359b46cdaf19 61 * @param StepperPin4 Pin 4 on stepper motor
krogedal 1:359b46cdaf19 62 * @param StepsPerRev Steps per revolution. Defaults to 4075.8, for 28BYJ-48
krogedal 1:359b46cdaf19 63 */
krogedal 0:252d645cfc5d 64 Async_4pin_Stepper( PinName StepperPin1,
krogedal 0:252d645cfc5d 65 PinName StepperPin2,
krogedal 0:252d645cfc5d 66 PinName StepperPin3,
krogedal 0:252d645cfc5d 67 PinName StepperPin4,
krogedal 0:252d645cfc5d 68 float StepsPerRev = 4075.7728);
krogedal 0:252d645cfc5d 69
krogedal 1:359b46cdaf19 70 /// Set current position as 0
krogedal 1:359b46cdaf19 71 void setZeroPos();
krogedal 1:359b46cdaf19 72
krogedal 1:359b46cdaf19 73 /** Set target
krogedal 1:359b46cdaf19 74 * @param target Target step, can be positive or negative
krogedal 1:359b46cdaf19 75 */
krogedal 1:359b46cdaf19 76 void setTarget(int target);
krogedal 1:359b46cdaf19 77
krogedal 1:359b46cdaf19 78 /// Move towards given target
krogedal 1:359b46cdaf19 79 void goToTarget();
krogedal 1:359b46cdaf19 80
krogedal 1:359b46cdaf19 81 // Run at a given speed in step/s
krogedal 1:359b46cdaf19 82 // void run(float speed, Dir dir);
krogedal 1:359b46cdaf19 83
krogedal 1:359b46cdaf19 84 /** Set speed
krogedal 1:359b46cdaf19 85 * @param speed Speed in steps/seconds
krogedal 1:359b46cdaf19 86 */
krogedal 1:359b46cdaf19 87 void setSpeed(int speed);
krogedal 1:359b46cdaf19 88
krogedal 1:359b46cdaf19 89 /** Get speed
krogedal 1:359b46cdaf19 90 * @returns Speed in steps/seconds
krogedal 1:359b46cdaf19 91 */
krogedal 1:359b46cdaf19 92 int getSpeed();
krogedal 1:359b46cdaf19 93
krogedal 1:359b46cdaf19 94 /** Get position
krogedal 1:359b46cdaf19 95 * @returns Stepper position
krogedal 1:359b46cdaf19 96 */
krogedal 1:359b46cdaf19 97 int getPos();
krogedal 1:359b46cdaf19 98
krogedal 1:359b46cdaf19 99 /** Get target position
krogedal 1:359b46cdaf19 100 * @returns Target step position
krogedal 1:359b46cdaf19 101 */
krogedal 1:359b46cdaf19 102 int getTarget();
krogedal 1:359b46cdaf19 103
krogedal 1:359b46cdaf19 104 /** Get distance left to target
krogedal 1:359b46cdaf19 105 * @returns Number of steps away from target
krogedal 1:359b46cdaf19 106 */
krogedal 1:359b46cdaf19 107 int distanceToGo();
krogedal 1:359b46cdaf19 108
krogedal 1:359b46cdaf19 109 /** Check if motor is running
krogedal 1:359b46cdaf19 110 * @returns
krogedal 1:359b46cdaf19 111 * 0 Motor is not running (ticker is not attached)
krogedal 1:359b46cdaf19 112 * 1 Motor is running (ticker is attached)
krogedal 1:359b46cdaf19 113 */
krogedal 1:359b46cdaf19 114 bool isRunning();
krogedal 1:359b46cdaf19 115
krogedal 1:359b46cdaf19 116 /// Close all gates, reducing current but losing holding torque
krogedal 1:359b46cdaf19 117 void relax();
krogedal 1:359b46cdaf19 118
krogedal 1:359b46cdaf19 119 /// Stop the motor and detach ticker
krogedal 0:252d645cfc5d 120 void stop();
krogedal 0:252d645cfc5d 121
krogedal 0:252d645cfc5d 122 };
krogedal 0:252d645cfc5d 123
krogedal 0:252d645cfc5d 124 #endif