![]() |
Stepper Motor 1.0
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 inA, uint8_t inB, uint8_t inC, uint8_t inD, uint16_t speedMax=SPEEDMAX_DEFAULT) | |
~MD_Stepper (void) | |
Methods for core object control. | |
void | begin (uint8_t busyPin=0xff) |
void | start (void) |
void | stop (void) |
bool | run (void) |
Methods for motion parameter management. | |
bool | getDirection (void) |
void | setDirection (bool fwd) |
void | resetPosition (void) |
int32_t | getPosition (void) |
uint16_t | getMaxSpeed (void) |
void | setMaxSpeed (uint16_t s) |
uint16_t | getSpeed (void) |
void | setSpeed (uint16_t s) |
void | move (int32_t dist) |
uint32_t | moveToGo (void) |
void | moveHome (void) |
Methods for hardware and options management. | |
bool | isBusy (void) |
bool | isAutoRun (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) |
AUTORUN and ISR management related. NOT for Applications! | |
static const uint8_t | MAX_INSTANCE = 4 |
Maximum concurrent instances. | |
static bool | _bInitialised |
ISR - Global vector initialization flag. | |
static volatile uint8_t | _instCount |
ISR - Number of instances currently configured. | |
static MD_Stepper * | _cbInstance [] |
ISR - Callback instance handle per pin slot. | |
void | setFrequency (uint32_t freq) |
Core object for the MD_PWM library
MD_Stepper::MD_Stepper | ( | uint8_t | inA, |
uint8_t | inB, | ||
uint8_t | inC, | ||
uint8_t | inD, | ||
uint16_t | speedMax = SPEEDMAX_DEFAULT |
||
) |
Class Constructor
Instantiate a new instance of the class.
The main function for the core object is to set the internal shared variables and timers to default values.
inA | pin connected to hardware driver INA or IN1 input (Coil A). |
inB | pin connected to hardware driver INB or IN2 input (Coil A). |
inC | pin connected to hardware driver INC or IN3 input (Coil B). |
inD | pin connected to hardware driver IND or IN4 input (Coil B). |
speedMax | the maximum speed the motor can work at in steps/second. |
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::getDirection | ( | void | ) |
Get the current motor direction.
Returns the current direction of 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.
uint16_t MD_Stepper::getMaxSpeed | ( | void | ) |
Gets the configured maximum speed.
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 ststus 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.
uint16_t MD_Stepper::getSpeed | ( | void | ) |
Gets the configured running speed.
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.
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.
bool MD_Stepper::run | ( | void | ) |
Run motion.
if autorun is enabled, then this is called automatically every time the ISR is triggered.
If autorun is not enabled, this needs to be called every iteration through loop() to run all the required Stepper management functions.
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::setFrequency | ( | uint32_t | freq | ) |
ISR - Set the timer frequecy for the
NOT FOR END USER APPLICATIONS!
Manipulates the CPU registers to set the appropriate clock counters to achieve the desired output frequency.
freq | the desired frequency. |
void MD_Stepper::setMaxSpeed | ( | uint16_t | s | ) |
Gets the configured maximum speed.
Motors can be practically run up to a maximum speed (steps/sec). The library can be configured with this speed to allow upper-bound checks. This method sets the maximum speed for the instance of the library. The setting can also be set as an optional parameter in the class constructor.
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.
s | the maximum speed in steps/sec |
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 between 0 and the configured maximum speed. Speeds higher than the maximum are capped at the maximum. 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 fore the same numeric setting.
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 FULL.
mode | the new motor stepping mode |
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 | ) |