Stepper Motor 1.1
Stepper Motor Library
MD_Stepper Class Reference

#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)
 

Detailed Description

Core object for the MD_PWM library

Member Enumeration Documentation

◆ stepMode_t

Enumerated type for Stepper motor motion profile

Specifies the sequence for powering the motor windings during rotation.

Enumerator
FULL 

Full step pattern. 4 full steps: AB-BC-CD-DA.

WAVE 

Wave pattern. 4 wave steps: A-B-C-D.

HALF 

Half step pattern. 8 half steps: A-AB-B-BC-C-CD-D-DA.

Constructor & Destructor Documentation

◆ MD_Stepper()

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.

Parameters
inA1pin connected to hardware driver Coil A one side.
inA2pin connected to hardware driver Coil A other side.
inB1pin connected to hardware driver Coil B one side.
inB2pin connected to hardware driver Coil B other side.

◆ ~MD_Stepper()

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.

Member Function Documentation

◆ begin()

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.

See also
setBusyPin()
Parameters
busyPinthe optional busy pin.

◆ enableMotorLock()

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.

See also
setMotorLockTime(), getMotorLock().
Parameters
btrue if the motor is to remain locked when stopped.

◆ getMotorLock()

bool MD_Stepper::getMotorLock ( void  )

Get the current motor lock setting.

Returns the currently configured motor lock setting.

See also
enableMotorLock(), setMotorLockTime()
Returns
true if the motor remained lock when stopped

◆ getMotorLockTime()

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.

See also
setMotorLockTime(), enableMotorLock().
Returns
motor lock time as multiples of 0.1 sec (100 ms)

◆ getPosition()

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).

See also
resetPosition()
Returns
the current step position counter

◆ getRawStatus()

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.

See also
getDirection(), isBusy(), isMoveEnabled() isMotorLocked(), isForward()
isAutoRun()
Returns
the status byte

◆ getSpeed()

uint16_t MD_Stepper::getSpeed ( void  )

Gets the current speed setting.

See also
setSpeed(), setMaxSpeed()
Returns
the running speed in steps/sec

◆ getStepMode()

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.

See also
setStepMode()
Returns
the current motor stepping mode

◆ isAutoRun()

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().

See also
ENABLE_AUTORUN
Returns
true if the autorun is enabled

◆ isBusy()

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.

See also
begin(), setBusyPin()
Returns
true if the motor is running

◆ isForward()

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.

See also
setDirection()
Returns
true if the motor is moving in the 'forward' direction, false otherwise

◆ isMotorLocked()

bool MD_Stepper::isMotorLocked ( void  )

Check if motor is currently locked.

Returns true if current condition of the motors is stopped and motor locked.

See also
setMotorLock(), getMotorLock()
Returns
true if the motor is currently locked, false otherwise

◆ isMoveEnabled()

bool MD_Stepper::isMoveEnabled ( void  )

Check if next/current motion is a move.

Returns true if the motion control is set for or currently executing a move() and not free running. If the move is currently happening then isBusy() will also be true.

See also
isBusy(), move()
Returns
true if the motion is move and not free running.

◆ move()

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).

See also
setSpeed(), moveToGo()
Parameters
distthe number of steps (distance) required running speed in steps/sec

◆ moveHome()

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()).

See also
move(), moveToGo()

◆ moveToGo()

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).

See also
setSpeed(), moveToGo()
Returns
the number of steps required running speed in steps/sec

◆ resetPosition()

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.

See also
getPosition()

◆ run()

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.

See also
start(), stop()
Returns
true if a step was taken during the method invocation.

◆ setBusyPin()

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).

See also
isBusy()
Parameters
pinthe pin number to use (0xff to disable)

◆ setDirection()

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.

See also
getDirection()
Parameters
fwdsets the motor to 'forward' if true, 'reverse' if false

◆ setMotorLockTime()

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.

See also
getMotorLockTime(), enableMotorLock().
Parameters
tmotor lock time as multiples of 0.1 sec (100 ms)

◆ setSpeed()

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.

See also
getSpeed(), setMode()
Parameters
sthe required running speed in steps/sec

◆ setStepMode()

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.

See also
getStepMode(), stepMode_t
Parameters
modethe new motor stepping mode

◆ setVelocity()

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.

See also
setSpeed(), setDirection()
Parameters
vthe required velocity in steps/sec

◆ start()

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.

See also
setSpeed(), move(), run(), stop()

◆ stop()

void MD_Stepper::stop ( void  )

Stop movement.

Stop the motor moving immediately. This stops free running motion and will immediately terminate motion set up using move().

See also
move(), run(), start()

The documentation for this class was generated from the following files: