![]() |
Stepper Motor 1.1
Stepper Motor Library
|
#include <MD_Stepper.h>
Public Types | |
Structures, Enumerated Types and Constants. | |
enum | stepMode_t { FULL = 0 , WAVE = 1 , HALF = 2 } |
Public Member Functions | |
Class constructor and destructor. | |
MD_Stepper (uint8_t inA1, uint8_t inA2, uint8_t inB1, uint8_t inB2) | |
~MD_Stepper (void) | |
Methods for core object control. | |
void | begin (uint8_t busyPin=0xff) |
void | start (void) |
void | stop (void) |
void | run (void) |
Methods for motion parameter management. | |
void | setDirection (bool fwd) |
void | resetPosition (void) |
int32_t | getPosition (void) |
uint16_t | getSpeed (void) |
void | setSpeed (uint16_t s) |
void | setVelocity (int16_t v) |
void | move (int32_t dist) |
uint32_t | moveToGo (void) |
void | moveHome (void) |
Methods for status and hardware options management. | |
bool | isBusy (void) |
bool | isAutoRun (void) |
bool | isForward (void) |
bool | isMotorLocked (void) |
bool | isMoveEnabled (void) |
void | setBusyPin (uint8_t pin) |
stepMode_t | getStepMode (void) |
void | setStepMode (stepMode_t mode) |
bool | getMotorLock (void) |
void | enableMotorLock (bool b) |
uint8_t | getMotorLockTime (void) |
void | setMotorLockTime (uint8_t t) |
uint8_t | getRawStatus (void) |
Core object for the MD_PWM library
MD_Stepper::MD_Stepper | ( | uint8_t | inA1, |
uint8_t | inA2, | ||
uint8_t | inB1, | ||
uint8_t | inB2 | ||
) |
Class Constructor
Instantiate a new instance of the class.
The main function for the core object is to set the internal setup Parameters and other default values.
The pins are passed as one pair for coil A and one pair for coil B.
inA1 | pin connected to hardware driver Coil A one side. |
inA2 | pin connected to hardware driver Coil A other side. |
inB1 | pin connected to hardware driver Coil B one side. |
inB2 | pin connected to hardware driver Coil B other side. |
MD_Stepper::~MD_Stepper | ( | void | ) |
Class Destructor.
Release any allocated memory and clean up anything else.
If all the instances of the class are closed, then the ISR is disconnected and the timer is stopped.
void MD_Stepper::begin | ( | uint8_t | busyPin = 0xff | ) |
Initialize the object.
Initialize the object data. This needs to be called during setup() to set items that cannot be done during object creation.
If this is the first instance of this class, then the ISR TIMER code is initialized and the frequency of the hardware timer is set. Subsequent instances do not affect the timer frequency.
The busy pin can be specified as an option parameter.
busyPin | the optional busy pin. |
void MD_Stepper::enableMotorLock | ( | bool | b | ) |
Enable the motor lock setting.
When the motor is not being moved, the coils can be left energized for maximum holding torque or turned off to reduce power consumption and motor heating. This method changes the setting between locked (true) and unlocked (false).
If set to unlock, when the motor stops it remains locked for the time specified by setMotorLockTime() before the motor coils are released.
If the motor is set to unlock while it is not running, it will be unlocked after the lock time expires. Locking while stopped will lock the motor the next time it runs and stops.
The library default is for the motors to unlock.
b | true if the motor is to remain locked when stopped. |
bool MD_Stepper::getMotorLock | ( | void | ) |
Get the current motor lock setting.
Returns the currently configured motor lock setting.
uint8_t MD_Stepper::getMotorLockTime | ( | void | ) |
Get the current motor lock hold time.
Returns the currently configured motor lock time in units of 0.1 seconds.
int32_t MD_Stepper::getPosition | ( | void | ) |
Gets the current step counter.
The library counts the number of steps it moves (+1 for forward motion, -1 for reverse motion) into a cumulative counter. This method returns the current step count.
The number of steps proportional to the current motor running mode (ie, HALF stepping creates double the number of steps as FULL or WAVE stepping to for the same physical distance).
uint8_t MD_Stepper::getRawStatus | ( | void | ) |
Get the internal status byte.
The library maintains internal status bit field of status information. This method returns the bit field as a byte, mainly for debugging purposes.
The meaning of each status bit is defined in the class' private section and can be obtained separately using one of the is*() methods.
uint16_t MD_Stepper::getSpeed | ( | void | ) |
Gets the current speed setting.
stepMode_t MD_Stepper::getStepMode | ( | void | ) |
Get the current motor stepping mode.
Returns the current motor stepping mode as one of the stepMode_t enumerated values.
bool MD_Stepper::isAutoRun | ( | void | ) |
Check if autorun is enabled.
Check if autorun is enabled. This required the compile time switch to be set and the autorun initialization in begin() to have succeeded. If autorun the run() method will be automatically invoked on an interrupt timer and does not need to be called from loop().
bool MD_Stepper::isBusy | ( | void | ) |
Check if motor is running.
Check if motors are currently running. This method is useful to check when move() has completed their motions. The output on the 'busy pin', if enabled, is the same as this status.
bool MD_Stepper::isForward | ( | void | ) |
Check if motor direction is forward.
Returns true if the current motor direction is forward. By convention the forward direction is 'true' and the reverse is 'false'.
Note that forward and reverse have no inherent meaning other than they are in opposite rotational directions.
bool MD_Stepper::isMotorLocked | ( | void | ) |
Check if motor is currently locked.
Returns true if current condition of the motors is stopped and motor locked.
bool MD_Stepper::isMoveEnabled | ( | void | ) |
void MD_Stepper::move | ( | int32_t | dist | ) |
Set the number of steps to move.
The motor can be set to move a specific number of steps and then stop. This method sets up the number of steps for the next start().
If the number of steps is positive the motion is in the forward direction; negative for reverse motion. Any change in direction cause by move() remains in effect at the end of the motor movement.
To cancel a move() set the number of steps to 0.
The move is specified as the number of steps in the current motor mode (ie, HALF stepping requires double the number of steps as FULL or WAVE stepping to move the same physical distance).
dist | the number of steps (distance) required running speed in steps/sec |
void MD_Stepper::moveHome | ( | void | ) |
Move back the home position.
The motor is set up to move back to its home position, defined as position with step count 0. It is equivalent to move(-getPosition()).
uint32_t MD_Stepper::moveToGo | ( | void | ) |
Set the number of steps to move.
The motor can be set to move a specific number of steps and then stop. This method sets up the number of steps for the next start().
If the number of steps is positive the motion is in the forward direction; negative for reverse motion. Any change in direction cause by move() remains in effect at the end of the motor movement.
To cancel a move() set the number of steps to 0.
The move is specified as the number of steps in the current motor mode (ie, HALF stepping requires double the number of steps as FULL or WAVE stepping to move the same physical distance).
void MD_Stepper::resetPosition | ( | void | ) |
Reset the current step counter.
The library counts the number of steps it moves (+1 for forward motion, -1 for reverse motion) into a cumulative counter. This method sets the current step count to zero, setting a new 'home' position.
void MD_Stepper::run | ( | void | ) |
Run motion.
This needs to be called every iteration through loop() to run all the required Stepper management functions.
Note that if AUTORUN is not enabled, this will also step the motors. If AUTORUN is enabled, the motors will be stepped through a timer interrupt and managed via this call.
void MD_Stepper::setBusyPin | ( | uint8_t | pin | ) |
Set pin for hardware busy status.
If this pin is specified, it will mirror the status returned by isBusy() at the output pin. The output is set HIGH when the motor is running.
As an alternative the pin can also be specified in the begin() method.
Set 0xff (255) to disable this pin (the default).
pin | the pin number to use (0xff to disable) |
void MD_Stepper::setDirection | ( | bool | fwd | ) |
Set the current motor direction.
Set the current rotation direction for the motor. By convention the forward direction is 'true' and the reverse is 'false'.
Note that forward and reverse have no inherent meaning other than they are in opposite rotational directions.
fwd | sets the motor to 'forward' if true, 'reverse' if false |
void MD_Stepper::setMotorLockTime | ( | uint8_t | t | ) |
Set the motor lock hold time.
When the motor is not being moved, the coils can be left energized for maximum holding torque or turned off to reduce power consumption and motor heating. If set to unlock, it will remain locked for the period of time specified by this method before the motor coils are released.
This setting is specified in units of 0.1 seconds (0 to 25.5 seconds). The library default is 2 seconds.
t | motor lock time as multiples of 0.1 sec (100 ms) |
void MD_Stepper::setSpeed | ( | uint16_t | s | ) |
Set the running speed.
The motor speed can be set to any value but speeds higher than the motor maximum attainable speed will result in it stalling. Setting the motor speed to 0 has the same effect as a stop().
The new speed takes effect at the next motor step (immediately if running or at the next start()).
As the speed is specified in steps/sec, the current motor stepping mode has an effect. HALF stepping results in half the physical speed of a FULL or WAVE stepping for the same numeric setting, so when the mode is changed all items mesaured in steps or steps per unit time are adjusted appropriately.
s | the required running speed in steps/sec |
void MD_Stepper::setStepMode | ( | stepMode_t | mode | ) |
Get the current motor stepping mode.
Sets the new motor stepping mode to one of the stepMode_t enumerated values.
The change from a full step mode (FULL, WAVE) to HALF step mode, and vice versa, creates a doubling/halving adjustment to current counts and speeds that are defined so that they remain consistent with the new steps generated.
The library default stepping mode is HALF.
mode | the new motor stepping mode |
void MD_Stepper::setVelocity | ( | int16_t | v | ) |
Set the velocity (speed and direction).
Sets the new velocity. positive velocity is forward movement, negative velocity reverse movement. This is a wrapper for setDirection() and setSpeed().
As velocity is specified in steps/sec.
v | the required velocity in steps/sec |
void MD_Stepper::start | ( | void | ) |
Start movement.
Start the motor moving using the currently configured motion parameters.
If a move() was specified, this will take run priority until it concludes. If no move() was specified or it has completed (ie, the move distance is 0) then free running is started until stop() is invoked.
void MD_Stepper::stop | ( | void | ) |