A class for driving a stepper motor via driver with indexer.

Dependents:   StepperTest

Committer:
tbjazic
Date:
Fri Dec 02 08:35:20 2016 +0000
Revision:
1:9888802e71b9
Parent:
0:12be56dc6182
Child:
2:70f593d34039
Number of sine periods set to 10.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tbjazic 0:12be56dc6182 1 #ifndef STEPPER_DRIVER_H
tbjazic 0:12be56dc6182 2 #define STEPPER_DRIVER_H
tbjazic 0:12be56dc6182 3
tbjazic 0:12be56dc6182 4 #include "mbed.h"
tbjazic 0:12be56dc6182 5
tbjazic 1:9888802e71b9 6 /** A class for driving a stepper motor in open loop.
tbjazic 1:9888802e71b9 7 * A driver must contain an indexer and sequencer.
tbjazic 1:9888802e71b9 8 * Each generated pulse moves the rotor by one step in a given direction.
tbjazic 1:9888802e71b9 9 */
tbjazic 0:12be56dc6182 10 class StepperDriver {
tbjazic 1:9888802e71b9 11
tbjazic 0:12be56dc6182 12 public:
tbjazic 1:9888802e71b9 13
tbjazic 1:9888802e71b9 14 /** Constructor receives output pin and direction pin.
tbjazic 1:9888802e71b9 15 * @param outputPin DigitalOut pin for sending pulses to the driver.
tbjazic 1:9888802e71b9 16 * @param directionPin DigitalOut pin for setting a direction of rotation.
tbjazic 1:9888802e71b9 17 */
tbjazic 1:9888802e71b9 18 StepperDriver(PinName outputPin, PinName directionPin);
tbjazic 1:9888802e71b9 19
tbjazic 1:9888802e71b9 20 /** Sets the motor resolution in steps per revolution (uint32_t) and minimum and maximum
tbjazic 1:9888802e71b9 21 * angle in degrees (angles range).
tbjazic 1:9888802e71b9 22 * @param resolution Resolution of the stepper motor in steps per revolution (uint32_t).
tbjazic 1:9888802e71b9 23 * Typical value is 200 steps per revolution, and that value is set by default in this class.
tbjazic 1:9888802e71b9 24 * @param minDegrees Minimum angle in degrees.
tbjazic 1:9888802e71b9 25 * @param maxDegrees Maximum angle in degrees.
tbjazic 1:9888802e71b9 26 */
tbjazic 1:9888802e71b9 27 void configure(uint32_t resolution, float minDegrees, float maxDegrees);
tbjazic 1:9888802e71b9 28
tbjazic 1:9888802e71b9 29 /** Sets the motor resolution in degrees per step (float) and minimum and maximum
tbjazic 1:9888802e71b9 30 * angle in degrees (angles range).
tbjazic 1:9888802e71b9 31 * @param resolution Resolution of the stepper motor in degrees per step (float).
tbjazic 1:9888802e71b9 32 * Typical value is 1.8 degrees per step, and that value is set by default in this class.
tbjazic 1:9888802e71b9 33 * @param minDegrees Minimum angle in degrees.
tbjazic 1:9888802e71b9 34 * @param maxDegrees Maximum angle in degrees.
tbjazic 1:9888802e71b9 35 */
tbjazic 1:9888802e71b9 36 void configure(float resolution, float minDegrees, float maxDegrees);
tbjazic 1:9888802e71b9 37
tbjazic 1:9888802e71b9 38 /** Sets the position in degrees.
tbjazic 1:9888802e71b9 39 * @param desiredPosition Desired position in degrees.
tbjazic 1:9888802e71b9 40 */
tbjazic 1:9888802e71b9 41 void setPosition(float desiredPosition);
tbjazic 0:12be56dc6182 42
tbjazic 1:9888802e71b9 43 /** Get the current position in degrees.
tbjazic 1:9888802e71b9 44 * @returns Current position in degrees.
tbjazic 1:9888802e71b9 45 */
tbjazic 1:9888802e71b9 46 float getPosition();
tbjazic 1:9888802e71b9 47
tbjazic 1:9888802e71b9 48 /** Set the speed in rpm (revolutions per minute).
tbjazic 1:9888802e71b9 49 * @param speedRPM Motor speed in rpm (revolutions per minute).
tbjazic 1:9888802e71b9 50 */
tbjazic 1:9888802e71b9 51 void setSpeedRPM(float speedRPM);
tbjazic 1:9888802e71b9 52
tbjazic 1:9888802e71b9 53 /** Set the speed in revolutions per second.
tbjazic 1:9888802e71b9 54 * @param speedRPS Motor speed in revolutions per second.
tbjazic 1:9888802e71b9 55 */
tbjazic 1:9888802e71b9 56 void setSpeedRPS(float speedRPS);
tbjazic 1:9888802e71b9 57
tbjazic 1:9888802e71b9 58 /** Set the speed in steps per second.
tbjazic 1:9888802e71b9 59 * @param speedSPS Motor speed in rpm (revolutions per minute).
tbjazic 1:9888802e71b9 60 */
tbjazic 1:9888802e71b9 61 void setSpeedSPS(float speedSPS);
tbjazic 1:9888802e71b9 62
tbjazic 1:9888802e71b9 63 /** Get the speed in rpm (revolutions per minute).
tbjazic 1:9888802e71b9 64 * @returns Motor speed in rpm (revolutions per minute).
tbjazic 1:9888802e71b9 65 */
tbjazic 1:9888802e71b9 66 float getSpeedRPM();
tbjazic 1:9888802e71b9 67
tbjazic 1:9888802e71b9 68 /** Get the speed in revolutions per second.
tbjazic 1:9888802e71b9 69 * @returns Motor speed in revolutions per second.
tbjazic 1:9888802e71b9 70 */
tbjazic 1:9888802e71b9 71 float getSpeedRPS();
tbjazic 1:9888802e71b9 72
tbjazic 1:9888802e71b9 73 /** Get the speed in steps per second.
tbjazic 1:9888802e71b9 74 * @returns Motor speed in steps per second.
tbjazic 1:9888802e71b9 75 */
tbjazic 1:9888802e71b9 76 float getSpeedSPS();
tbjazic 1:9888802e71b9 77
tbjazic 1:9888802e71b9 78 protected:
tbjazic 0:12be56dc6182 79 DigitalOut output, direction;
tbjazic 0:12be56dc6182 80 uint32_t currentPosition, previousPosition, homePosition, minPosition, maxPosition, desiredPosition;
tbjazic 1:9888802e71b9 81 float degreesPerStep, pulseWidth, speed;
tbjazic 0:12be56dc6182 82 Ticker ticker;
tbjazic 0:12be56dc6182 83 Timeout timeout;
tbjazic 0:12be56dc6182 84 void update();
tbjazic 0:12be56dc6182 85 void attachTicker();
tbjazic 0:12be56dc6182 86 void detachTicker();
tbjazic 0:12be56dc6182 87 void generateImpulse();
tbjazic 0:12be56dc6182 88 void turnOutputOff();
tbjazic 0:12be56dc6182 89 bool isTickerAttached;
tbjazic 1:9888802e71b9 90 void setResolution(uint32_t resolution);
tbjazic 1:9888802e71b9 91 void setResolution(float resolution);
tbjazic 1:9888802e71b9 92 void setDegreesRange(float minDegrees, float maxDegrees);
tbjazic 0:12be56dc6182 93 };
tbjazic 0:12be56dc6182 94
tbjazic 0:12be56dc6182 95 #endif