uStepper
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | Friends | List of all members
uStepper Class Reference

Prototype of class for accessing all features of the uStepper in a single object. More...

#include <uStepper.h>

Public Member Functions

 uStepper (float accel, float vel)
 Constructor of uStepper class. More...
 
 uStepper (void)
 Constructor of uStepper class. More...
 
void setMaxAcceleration (float accel)
 Set the maximum acceleration of the stepper motor. More...
 
float getMaxAcceleration (void)
 Get the value of the maximum motor acceleration. More...
 
void setMaxVelocity (float vel)
 Sets the maximum rotational velocity of the motor. More...
 
float getMaxVelocity (void)
 Returns the maximum rotational velocity of the motor. More...
 
void runContinous (bool dir)
 Make the motor rotate continuously. More...
 
void moveSteps (int32_t steps, bool dir, bool holdMode)
 Make the motor perform a predefined number of steps. More...
 
void hardStop (bool holdMode)
 Stop the motor without deceleration. More...
 
void softStop (bool holdMode)
 Stop the motor with deceleration. More...
 
void setup (uint8_t mode=NORMAL, uint8_t microStepping=SIXTEEN, float faultTolerance=10.0, float faultHysteresis=5.0, float pTerm=1.0, float iTerm=0.02, float dterm=0.006, bool setHome=true)
 Initializes the different parts of the uStepper object. More...
 
bool getCurrentDirection (void)
 Returns the direction the motor is currently configured to rotate. More...
 
bool getMotorState (void)
 Get the current state of the motor. More...
 
int32_t getStepsSinceReset (void)
 Get the number of steps applied since reset. More...
 
void pwmD8 (double duty)
 Generate PWM signal on digital output 8. More...
 
void setCurrent (double current)
 Set motor output current. More...
 
void pwmD8 (int mode)
 Sets the mode of digital pin D8. More...
 
void pwmD3 (double duty)
 Generate PWM signal on digital output 3. More...
 
void pwmD3 (int mode)
 Sets the mode of digital pin D3. More...
 
void updateSetPoint (float setPoint)
 Updates setpoint for the motor. More...
 
float moveToEnd (bool dir)
 Moves the motor to its physical limit, without limit switch. More...
 
void moveToAngle (float angle, bool holdMode)
 Moves the motor to an absolute angle. More...
 
void moveAngle (float angle, bool holdMode)
 Moves the motor to a relative angle. More...
 
bool isStalled (void)
 This method returns a bool variable indicating wether the motor is stalled or not. More...
 
bool detectStall (float diff, bool running)
 

Public Attributes

uStepperTemp temp
 
uStepperEncoder encoder
 

Private Member Functions

void startTimer (void)
 Starts timer for stepper algorithm. More...
 
void stopTimer (void)
 Stops the timer for the stepper algorithm. More...
 
void enableMotor (void)
 Enables the stepper driver output stage. More...
 
void disableMotor (void)
 Disables the stepper driver output stage. More...
 
void pidDropIn (void)
 This method handles the actual PID controller calculations for drop-in feature, if enabled.
 
void pid (void)
 This method handles the actual PID controller calculations, if enabled.
 

Private Attributes

uint16_t cruiseDelay
 
float2 multiplier
 
uint8_t state
 
uint32_t accelSteps
 
uint32_t decelSteps
 
uint32_t initialDecelSteps
 
uint32_t cruiseSteps
 
uint32_t currentStep
 
uint32_t totalSteps
 
bool continous
 
bool hold
 
bool direction
 
volatile int32_t stepsSinceReset
 
int32_t dummy
 
float2 exactDelay
 
uint16_t delay
 
bool dropIn
 
float velocity
 
float acceleration
 
volatile float tolerance
 
volatile float hysteresis
 
volatile float stepConversion
 
volatile uint16_t counter
 
volatile int32_t stepCnt
 
volatile int32_t control
 
volatile uint32_t speedValue [2]
 
float pTerm
 
float iTerm
 
float dTerm
 
uint8_t mode
 
float angleToStep
 
volatile bool stall
 
volatile bool invertDir
 

Friends

void naked
 
void used
 
void TIMER2_COMPA_vect (void) __attribute__((signal
 Used to apply step pulses to the motor. More...
 
void TIMER1_COMPA_vect (void) __attribute__((signal
 Measures angle and speed of motor. More...
 
void interrupt1 (void)
 Used by dropin feature to take in enable signal. More...
 
void uStepperEncoder::setHome (void)
 

Detailed Description

Prototype of class for accessing all features of the uStepper in a single object.

This class enables the user of the library to access all features of the uStepper board, by use of a single object.

Definition at line 589 of file uStepper.h.

Constructor & Destructor Documentation

◆ uStepper() [1/2]

uStepper::uStepper ( float  accel,
float  vel 
)

Constructor of uStepper class.

This is the constructor of the uStepper class. This version of the constructor takes in two arguments, "accel" and "vel". These two arguments lets the programmer set the maximum acceleration and velocity, respectively, during instantiation of the uStepper object.

Parameters
accel- Floating point representation of the maximum acceleration allowed in steps/s^2.
vel- Floating point representation of the maximum velocity allowed in steps/s.

Definition at line 797 of file uStepper.cpp.

◆ uStepper() [2/2]

uStepper::uStepper ( void  )

Constructor of uStepper class.

This is the constructor of the uStepper class. This version of the constructor doesn't take any arguments, and instantiates an object with a maximum acceleration and velocity of 1000 steps/s^2 and 1000 steps/s, respectively.

Definition at line 781 of file uStepper.cpp.

Member Function Documentation

◆ disableMotor()

void uStepper::disableMotor ( void  )
private

Disables the stepper driver output stage.

This function disables the stepper driver output stage. If this function is used, the motor will not brake, and it will be possible to turn the motor shaft by hand

Definition at line 1313 of file uStepper.cpp.

◆ enableMotor()

void uStepper::enableMotor ( void  )
private

Enables the stepper driver output stage.

This function enables the output stage of the stepper driver. If no step pulses is applied to the stepper driver, either manually or by means of the stepper algorithm of this library, this will make the force the motor to brake in the current position.

Definition at line 1308 of file uStepper.cpp.

◆ getCurrentDirection()

bool uStepper::getCurrentDirection ( void  )

Returns the direction the motor is currently configured to rotate.

This function checks the last configured direction of rotation and returns this.

Returns
0 - Clockwise
1 - Counter Clockwise

Definition at line 1318 of file uStepper.cpp.

◆ getMaxAcceleration()

float uStepper::getMaxAcceleration ( void  )

Get the value of the maximum motor acceleration.

This function returns the maximum acceleration used by the stepper algorithm.

Returns
Maximum acceleration in steps/s^2

Definition at line 832 of file uStepper.cpp.

◆ getMaxVelocity()

float uStepper::getMaxVelocity ( void  )

Returns the maximum rotational velocity of the motor.

This function returns the maximum rotational velocity the motor is allowed to run. In order to change this velocity, The function setMaximumVelocity() should be used.

Returns
maximum rotational velocity of the motor in steps/s.

Definition at line 870 of file uStepper.cpp.

◆ getMotorState()

bool uStepper::getMotorState ( void  )

Get the current state of the motor.

This function returns the current state of the motor, i.e. if the motor is rotating or not.

Returns
0 - Motor not rotating
1 - Motor rotating

Definition at line 1323 of file uStepper.cpp.

◆ getStepsSinceReset()

int32_t uStepper::getStepsSinceReset ( void  )

Get the number of steps applied since reset.

This function returns the number of steps applied to the motor driver since reset. This function doesn't care if the step has actually been performed or not. Steps applied in the clockwise direction is added and steps applied in the counter clockwise direction is subtracted. This means that a negative return value indicates that the motor should have rotated the returned amount of steps in the counter clockwise direction, with respect to the initial position. Likewise a positive value indicate the number of steps the motor should have rotated in the clockwise direction, with respect to the initial position.

Returns
negative value - number of steps the motor should have rotated in the counter clockwise direction, with respect to the initial position.
positive value - number of steps the motor should have rotated in the clockwise direction, with respect to the initial position.

Definition at line 1348 of file uStepper.cpp.

◆ hardStop()

void uStepper::hardStop ( bool  holdMode)

Stop the motor without deceleration.

This function will stop any ongoing motor movement, without any deceleration phase. If the motor is rotation at a significant speed, the motor might not stop instantaneously, due to build up inertia. The argument "holdMode" can be used to define whether the motor should brake or freewheel after the function has been called.

Parameters
holdMode- can be set to "HARD" for brake mode or "SOFT" for freewheel mode (without the quotes).

Definition at line 1136 of file uStepper.cpp.

◆ isStalled()

bool uStepper::isStalled ( void  )

This method returns a bool variable indicating wether the motor is stalled or not.

Returns
0 = not stalled, 1 = stalled

Definition at line 1917 of file uStepper.cpp.

◆ moveAngle()

void uStepper::moveAngle ( float  angle,
bool  holdMode 
)

Moves the motor to a relative angle.

Parameters
[in]angleRelative angle from current position. A positive angle makes the motor turn clockwise, and a negative angle, counterclockwise.
[in]holdModecan be set to "HARD" for brake mode or "SOFT" for freewheel mode (without the quotes).

Definition at line 1518 of file uStepper.cpp.

◆ moveSteps()

void uStepper::moveSteps ( int32_t  steps,
bool  dir,
bool  holdMode 
)

Make the motor perform a predefined number of steps.

This function makes the motor perform a predefined number of steps, using the acceleration profile implemented in this library. The motor will accelerate at the rate set by setMaximumAcceleration(), and eventually reach the speed set by setMaximumVelocity() function. The direction of rotation is set by the argument "dir". The argument "holdMode", defines whether the motor should brake or let the motor freewheel after the steps has been performed.

Parameters
steps- Number of steps to be performed.
dir- Can be set to "CCW" or "CW" (without the quotes).
holdMode- can be set to "HARD" for brake mode or "SOFT" for freewheel mode (without the quotes).

Definition at line 960 of file uStepper.cpp.

◆ moveToAngle()

void uStepper::moveToAngle ( float  angle,
bool  holdMode 
)

Moves the motor to an absolute angle.

Parameters
[in]angleAbsolute angle. A positive angle makes the motor turn clockwise, and a negative angle, counterclockwise.
[in]holdModecan be set to "HARD" for brake mode or "SOFT" for freewheel mode (without the quotes).

Definition at line 1499 of file uStepper.cpp.

◆ moveToEnd()

float uStepper::moveToEnd ( bool  dir)

Moves the motor to its physical limit, without limit switch.

This function, makes the motor run continously, untill the encoder detects a stall, at which point the motor is assumed to be at it's limit.

Parameters
[in]dirDirection to search for limit
Returns
Degrees turned from calling the function, till end was reached

Definition at line 1453 of file uStepper.cpp.

◆ pwmD3() [1/2]

void uStepper::pwmD3 ( double  duty)

Generate PWM signal on digital output 3.

This function allows the user to generate PWM signal on digital output 3. The PWM signal has a fixed frequency of 28.2kHz, from 0% - 100% duty cycle, in steps of 1.43% (resolution of 6.13 bits).

Parameters
duty- Desired duty cycle of PWM signal. range: 0.0 to 100.0.

Definition at line 1404 of file uStepper.cpp.

◆ pwmD3() [2/2]

void uStepper::pwmD3 ( int  mode)

Sets the mode of digital pin D3.

This function changes digital pin D3 between PWM mode and normal I/O mode.

Parameters
[in]modeBy supplying 'PWM' as argument, the digital pin acts as a PWM pin. By supplying 'NORMAL' as argument, the digital pin acts as a normal I/O pin.

Definition at line 1426 of file uStepper.cpp.

◆ pwmD8() [1/2]

void uStepper::pwmD8 ( double  duty)

Generate PWM signal on digital output 8.

This function allows the user to generate PWM signal on digital output 8. The PWM signal has a fixed frequency of 1kHz, from 0% - 100% duty cycle, in steps of 0.00625% (resolution of 13.97 bits).

Parameters
duty- Desired duty cycle of PWM signal. range: 0.0 to 100.0.

Definition at line 1365 of file uStepper.cpp.

◆ pwmD8() [2/2]

void uStepper::pwmD8 ( int  mode)

Sets the mode of digital pin D8.

This function changes digital pin D8 between PWM mode and normal I/O mode.

Parameters
[in]modeBy supplying 'PWM' as argument, the digital pin acts as a PWM pin. By supplying 'NORMAL' as argument, the digital pin acts as a normal I/O pin.

Definition at line 1387 of file uStepper.cpp.

◆ runContinous()

void uStepper::runContinous ( bool  dir)

Make the motor rotate continuously.

This function makes the motor rotate continuously, using the acceleration profile implemented in this library. The motor will accelerate at the rate set by setMaximumAcceleration(), and eventually reach the speed set by setMaximumVelocity() function. The direction of rotation is set by the argument "dir".

Parameters
dir- Can be set to "CCW" or "CW" (without the quotes)

Definition at line 875 of file uStepper.cpp.

◆ setCurrent()

void uStepper::setCurrent ( double  current)

Set motor output current.

This function allows the user to change the current setting of the motor driver. In order to utilize this feature, the current jumper should be placed in the "I-PWM" position on the uStepper board.

Parameters
[in]currentDesired current setting in percent (0% - 100%)

Definition at line 1360 of file uStepper.cpp.

◆ setMaxAcceleration()

void uStepper::setMaxAcceleration ( float  accel)

Set the maximum acceleration of the stepper motor.

This function lets the user of this library set the acceleration used by the stepper algorithm. The algorithm is a second order acceleration profile, meaning that the acceleration only assumes three values; -a, 0 and a, with a being the acceleration set by this function.

Parameters
accel- Maximum acceleration in steps/s^2

Definition at line 812 of file uStepper.cpp.

◆ setMaxVelocity()

void uStepper::setMaxVelocity ( float  vel)

Sets the maximum rotational velocity of the motor.

This function sets the maximum velocity at which the motor is allowed to run. The rotational velocity of the motor will gradually be ramped up to the value set by this function, by the stepper acceleration profile implemented in this library.

Parameters
vel- Maximum rotational velocity of the motor in steps/s

Definition at line 837 of file uStepper.cpp.

◆ setup()

void uStepper::setup ( uint8_t  mode = NORMAL,
uint8_t  microStepping = SIXTEEN,
float  faultTolerance = 10.0,
float  faultHysteresis = 5.0,
float  pTerm = 1.0,
float  iTerm = 0.02,
float  dterm = 0.006,
bool  setHome = true 
)

Initializes the different parts of the uStepper object.

This function initializes the different parts of the uStepper object, and should be called in the setup() function of the arduino sketch. This function is needed as some things, like the timer can not be setup in the constructor, since arduino for some strange reason, resets a lot of the AVR registers just before entering the setup() function.

Parameters
[in]modeDefault is normal mode. Pass the constant "DROPIN" to configure the uStepper to act as dropin compatible to the stepstick. Pass the constant "PID", to enable PID feature for regular movement functions, such as moveSteps()
[in]microSteppingWhen mode is set to anythings else than "NORMAL", this parameter should be set to the current microstep setting. available arguments are: FULL HALF QUARTER EIGHT SIXTEEN
[in]faultToleranceThis parameter defines the allowed number of missed steps before the correction should kick in.
[in]faultHysteresisThe number of missed steps allowed for the PID to turn off
[in]pTermThe proportional coefficent of the PID controller
[in]iTermThe integral coefficent of the PID controller
[in]dtermThe differential coefficent of the PID controller
[in]setHomeWhen set to true, the encoder position is Reset. When set to false, the encoder position is not reset.

Definition at line 1222 of file uStepper.cpp.

◆ softStop()

void uStepper::softStop ( bool  holdMode)

Stop the motor with deceleration.

This function stops any ongoing motor movement, with a deceleration phase. This will take longer for the motor to stop, however the mechanical vibrations related to the stopping of the motor can be significantly reduced compared to the hardStop() function. The argument "holdMode" can be used to define whether the motor should brake or freewheel after the function has been called.

Parameters
holdMode- can be set to "HARD" for brake mode or "SOFT" for freewheel mode (without the quotes).

Definition at line 1169 of file uStepper.cpp.

◆ startTimer()

void uStepper::startTimer ( void  )
private

Starts timer for stepper algorithm.

This function actually doesn't start the timer for the stepper algorithm, as the timer is always running. Instead it clears the counter value, clears any pending interrupts and enables the timer compare match interrupt.

Definition at line 1294 of file uStepper.cpp.

◆ stopTimer()

void uStepper::stopTimer ( void  )
private

Stops the timer for the stepper algorithm.

As the startTimer() function, this function doesn't stop the timer, instead it disables the timer compare match interrupt bit in the timer registers, ensuring that the stepper algorithm will not run when the motor is not supposed to run.

Definition at line 1303 of file uStepper.cpp.

◆ updateSetPoint()

void uStepper::updateSetPoint ( float  setPoint)

Updates setpoint for the motor.

This method updates the setpoint for the motor. This function is used when it is desired to provide an absolute position for the motor, and should be used in the DROPIN mode

Parameters
[in]setPointThe setpoint in degrees

Definition at line 1443 of file uStepper.cpp.

Friends And Related Function Documentation

◆ interrupt1

void interrupt1 ( void  )
friend

Used by dropin feature to take in enable signal.

This interrupt routine is used by the dropin feature to keep track of enable signal from main controller

Definition at line 82 of file uStepper.cpp.

◆ TIMER1_COMPA_vect

void TIMER1_COMPA_vect ( void  )
friend

Measures angle and speed of motor.

This interrupt routine is in charge of sampling the encoder and measure the current speed of the motor. In case of Dropin or PID feature this routine runs at a frequency of 500Hz while during normal operation it runs at a frequency of 1kHz.

Definition at line 271 of file uStepper.cpp.

◆ TIMER2_COMPA_vect

void TIMER2_COMPA_vect ( void  )
friend

Used to apply step pulses to the motor.

This interrupt routine is in charge of applying step pulses to the motor. The routine runs at a frequency of 28.2kHz, and handles acceleration algorithm calculations, as well as applying step pulses during compensation for missed steps, while either dropin or PID feature are enabled.

Definition at line 128 of file uStepper.cpp.

Member Data Documentation

◆ acceleration

float uStepper::acceleration
private

This variable contains the maximum acceleration to be used. The can be set and read by the user of the library using the functions setMaxAcceleration() and getMaxAcceleration() respectively. Since this library uses a second order acceleration curve, the acceleration applied will always be either +/- this value (acceleration/deceleration)or zero (cruise).

Definition at line 702 of file uStepper.h.

◆ accelSteps

uint32_t uStepper::accelSteps
private

This variable keeps track of how many steps to perform in the acceleration phase of the profile.

Definition at line 611 of file uStepper.h.

◆ angleToStep

float uStepper::angleToStep
private

This variable converts an angle in degrees into a corresponding number of steps

Definition at line 760 of file uStepper.h.

◆ continous

bool uStepper::continous
private

This variable tells the algorithm whether the motor should rotated continuous or only a limited number of steps. If set to 1, the motor will rotate continous.

Definition at line 642 of file uStepper.h.

◆ control

volatile int32_t uStepper::control
private

This variable contains the number of steps we are off the setpoint, and is updated once every PID sample.

Definition at line 732 of file uStepper.h.

◆ counter

volatile uint16_t uStepper::counter
private

This variable is used by Timer2 to check wether it is time to generate steps or not. only used if PID is activated

Definition at line 722 of file uStepper.h.

◆ cruiseDelay

uint16_t uStepper::cruiseDelay
private

This variable is used by the stepper acceleration algorithm to set the delay between step pulses when running at the set cruise speed

Definition at line 594 of file uStepper.h.

◆ cruiseSteps

uint32_t uStepper::cruiseSteps
private

This variable keeps track of how many steps to perform in the cruise phase of the profile.

Definition at line 626 of file uStepper.h.

◆ currentStep

uint32_t uStepper::currentStep
private

This variable keeps track of the current step number in the current move of a predefined number of steps.

Definition at line 631 of file uStepper.h.

◆ decelSteps

uint32_t uStepper::decelSteps
private

This variable keeps track of how many steps to perform in the deceleration phase of the profile.

Definition at line 616 of file uStepper.h.

◆ delay

uint16_t uStepper::delay
private

This variable is used by the stepper algorithm to keep track of when to apply the next step pulse. When the algorithm have applied a step pulse, it will calculate the next delay (in number of interrupts) needed before the next pulse should be applied. A truncated version of this delay will be put in this variable and is decremented by one for each interrupt untill it reaches zero and a step is applied.

Definition at line 682 of file uStepper.h.

◆ direction

bool uStepper::direction
private

This variable tells the algorithm the direction of rotation for the commanded move.

Definition at line 654 of file uStepper.h.

◆ dropIn

bool uStepper::dropIn
private

Not used anymore !

Definition at line 686 of file uStepper.h.

◆ dTerm

float uStepper::dTerm
private

This variable contains the differential coefficient used by the PID

Definition at line 751 of file uStepper.h.

◆ dummy

int32_t uStepper::dummy
private

Dummy variable, to make variable addresses fit with algorithm After the stepsSinceReset has been changed to int32_t

Definition at line 666 of file uStepper.h.

◆ encoder

uStepperEncoder uStepper::encoder

Instantiate object for the encoder

Definition at line 832 of file uStepper.h.

◆ exactDelay

float2 uStepper::exactDelay
private

This variable contains the exact delay (in number of interrupts) before the next step is applied. This variable is used in the calculations of the next step delay.

Definition at line 672 of file uStepper.h.

◆ hold

bool uStepper::hold
private

This variable tells the algorithm if it should block the motor by keeping the motor coils excited after the commanded number of steps have been carried out, or if it should release the motor coil, allowing the shaft to be rotated freely.

Definition at line 649 of file uStepper.h.

◆ hysteresis

volatile float uStepper::hysteresis
private

This variable contains the error which the PID controller should have obtained in order to switch off

Definition at line 712 of file uStepper.h.

◆ initialDecelSteps

uint32_t uStepper::initialDecelSteps
private

This variable keeps track of how many steps to perform in the initial deceleration phase of the profile.

Definition at line 621 of file uStepper.h.

◆ iTerm

float uStepper::iTerm
private

This variable contains the integral coefficient used by the PID

Definition at line 746 of file uStepper.h.

◆ mode

uint8_t uStepper::mode
private

This variable is used to indicate which mode the uStepper is running in (Normal, dropin or pid)

Definition at line 756 of file uStepper.h.

◆ multiplier

float2 uStepper::multiplier
private

This is the constant multiplier used by the stepper algorithm. See description of timer2 overflow interrupt routine for more details.

Definition at line 600 of file uStepper.h.

◆ pTerm

float uStepper::pTerm
private

This variable contains the proportional coefficient used by the PID

Definition at line 742 of file uStepper.h.

◆ speedValue

volatile uint32_t uStepper::speedValue[2]
private

This variable contains the number of microseconds between last step pulse from external controller

Definition at line 737 of file uStepper.h.

◆ stall

volatile bool uStepper::stall
private

This variable holds information on wether the motor is stalled or not. 0 = OK, 1 = stalled

Definition at line 764 of file uStepper.h.

◆ state

uint8_t uStepper::state
private

This variable is used by the stepper algorithm to keep track of which part of the acceleration profile the motor is currently operating at.

Definition at line 606 of file uStepper.h.

◆ stepCnt

volatile int32_t uStepper::stepCnt
private

This variable contains the number of steps commanded by external controller, in case of dropin feature

Definition at line 727 of file uStepper.h.

◆ stepConversion

volatile float uStepper::stepConversion
private

This variable contains the conversion coefficient from raw encoder data to number of steps

Definition at line 717 of file uStepper.h.

◆ stepsSinceReset

volatile int32_t uStepper::stepsSinceReset
private

This variable contains an open-loop number of steps moved from the position the motor had when powered on (or reset). a negative value represents a rotation in the counter clock wise direction and a positive value corresponds to a rotation in the clock wise direction.

Definition at line 662 of file uStepper.h.

◆ temp

uStepperTemp uStepper::temp

Instantiate object for the temperature sensor

Definition at line 830 of file uStepper.h.

◆ tolerance

volatile float uStepper::tolerance
private

This variable contains the number of missed steps allowed before the PID controller kicks in, if activated

Definition at line 707 of file uStepper.h.

◆ totalSteps

uint32_t uStepper::totalSteps
private

This variable keeps track of the total number of steps to be performed in the current move of a predefined number of steps.

Definition at line 636 of file uStepper.h.

◆ velocity

float uStepper::velocity
private

This variable contains the maximum velocity, the motor is allowed to reach at any given point. The user of the library can set this by use of the setMaxVelocity() function, and get the current value with the getMaxVelocity() function.

Definition at line 693 of file uStepper.h.


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