Derivs Limiter
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Derivs_Limiter Class Reference

This library can be used to limit the first and second derivative of a variable as it approaches a target value. https://github.com/joshua-8/Derivs_Limiter. More...

#include <Derivs_Limiter.h>

Public Member Functions

 Derivs_Limiter (float _velLimit, float _accelLimit, float _decelLimit=NAN, float _target=0, float _startPos=0, float _startVel=0, bool _preventGoingWrongWay=false, bool _preventGoingTooFast=false, float _posLimitLow=-INFINITY, float _posLimitHigh=INFINITY, float _maxStoppingDecel=2, float *_posPointer=NULL, float *_velPointer=NULL)
 constructor for Derivs_Limiter class
 
 Derivs_Limiter ()
 default constructor for Derivs_Limiter
 
void setPositionVelocity (float pos=0, float vel=0)
 set position and velocity
 
void setTargetAndPosition (float targ=0, float pos=0)
 set target and position
 
bool setPosition (float pos=0)
 set position, ignored if NAN
 
bool setVelocity (float vel=0)
 set velocity
 
bool setVelLimit (float velLim)
 set velocity limit
 
bool setAccelLimit (float accelLim)
 set acceleration limit
 
void setDecelLimit (float _decelLimit=NAN)
 set deceleration limit
 
void setAccelAndDecelLimits (float _accelLimit, float _decelLimit=NAN)
 combines setAccelLimit() with setDecelLimit()
 
void setVelAccelLimits (float velLim, float accLim, float decLim=NAN)
 set velocity and acceleration limits
 
float getVelLimit ()
 get velocity limit setting
 
float getAccelLimit ()
 get acceleration limit setting
 
float getDecelLimit ()
 get deceleration limit setting
 
float getVelocity ()
 get the current velocity
 
float getAcceleration ()
 get the current acceleration
 
float getPosition ()
 get the current position value, but doesn't calculate anything
 
void setMaxStoppingDecel (float _maxStoppingDecel)
 set setting for how many times accelLimit can be used to stop in time for target position
 
float getMaxStoppingDecel ()
 get setting for how many times accelLimit can be used to stop in time for target position
 
float getLowPosLimit ()
 get the lower boundary for position
 
float getHighPosLimit ()
 get the higher boundary for position
 
bool setLowPosLimit (float lowLimit)
 set the lower boundary for position
 
bool setHighPosLimit (float highLimit)
 set the higher boundary for position
 
void setPosLimits (float lowLimit, float highLimit)
 set the boundaries for position
 
bool setTarget (float _target)
 set target position (doesn't run calculation, make sure to run calc() yourself)
 
float getTarget ()
 get target position
 
void setPositionAndTarget (float targPos)
 set position and target to a value
 
void jogPosition (float increment)
 set position and target to position + increment
 
void resetTime ()
 If calc hasn't been run for a while, use this before starting to use it again to protect from large jumps.
 
unsigned long getLastTime ()
 returns the value of micros() when calc() last ran
 
float getTimeInterval ()
 returns the time (in seconds) between the two most recent calculation times
 
float getTargetDelta ()
 returns the change in target from the most recent run of calc()
 
float getLastTarget ()
 what was target in the most recent run of calc()
 
float getPositionDelta ()
 returns the change in position from the most recent run of calc()
 
float getLastPosition ()
 what was position in the most recent run of calc(), can be used to see if position was changed outside of calc()
 
float getTargetDeltaPerTime ()
 how fast was target changing (distance/time)
 
void setPositionPointer (float *_positionPointer)
 set pointer to an external variable that will be read and modified during calc as position
 
void setVelocityPointer (float *_velocityPointer)
 set pointer to an external variable that will be read and modified during calc as velocity
 
void setPreventGoingWrongWay (bool _preventGoingWrongWay)
 sets value of preventGoingWrongWay, true = immediately set velocity to zero if moving away from target, false = stay under accel limit
 
bool getPreventGoingWrongWay ()
 returns value of preventGoingWrongWay setting
 
void setPreventGoingTooFast (bool _preventGoingTooFast)
 sets value of preventGoingTooFast, true = constrain velocity to velLimit, false decelerate at accelLimit to velLimit
 
bool getPreventGoingTooFast ()
 returns value of preventGoingTooFast setting
 
bool isPosAtTarget ()
 does position equal target?
 
bool isPosNotAtTarget ()
 is position not equal to target?
 
float distToTarget ()
 returns target - position
 
void setVelConstant (float vel)
 switch to velocity mode, and set velocity immediately to a constant value.
 
void setVelTarget (float vel)
 switch to velocity mode, and set a target velocity that the target should go towards limited by accelLimit
 
bool isPosModeNotVelocity ()
 true if in position target mode, false if in velocity target mode
 
float getVelTarget ()
 get the target velocity used by the velocity control mode
 
void resetVelLimitToOriginal ()
 resets the velocity limit to the value set in the constructor or setVelLimit()
 
boolean setVelLimitForTimedMove (float _dist, float _time, float _maxVel=NAN)
 This function changes velLimit so that a move of a specified distance takes the specified time (if possible given acceleration limit)
 
boolean setTargetAndVelLimitForTimedMove (float _target, float _time, float _maxVel=NAN)
 This function changes velLimit so that a move to the specified target position takes the specified time (if possible given acceleration limit)
 
boolean setTargetTimedMovePreferred (float _target, float _time, float _maxVel=NAN)
 This function changes velLimit so that a move to the specified target position takes the specified time if possible given acceleration limit, and if not possible resets the velocity limit to the original value (or _maxVel if not NAN) and goes to the target at that speed instead.
 
float calc ()
 call this as frequently as possible to calculate all the values
 
float calc (float _target)
 call this as frequently as possible to calculate all the values
 

Protected Member Functions

virtual float _calc ()
 this is where the actual code is
 

Protected Attributes

float position
 
float velocity
 
float accel
 
unsigned long lastTime
 
float target
 
float velLimit
 
float accelLimit
 
float decelLimit
 
float time
 
bool preventGoingWrongWay
 
bool preventGoingTooFast
 
float posLimitLow
 
float posLimitHigh
 
float maxStoppingDecel
 
float lastTarget
 
float targetDelta
 
float lastPos
 
float posDelta
 
float originalVelLimit
 
float * positionPointer
 
float * velocityPointer
 
bool posMode
 
float velocityTarget
 

Detailed Description

This library can be used to limit the first and second derivative of a variable as it approaches a target value. https://github.com/joshua-8/Derivs_Limiter.

Constructor & Destructor Documentation

◆ Derivs_Limiter() [1/2]

Derivs_Limiter::Derivs_Limiter ( float  _velLimit,
float  _accelLimit,
float  _decelLimit = NAN,
float  _target = 0,
float  _startPos = 0,
float  _startVel = 0,
bool  _preventGoingWrongWay = false,
bool  _preventGoingTooFast = false,
float  _posLimitLow = -INFINITY,
float  _posLimitHigh = INFINITY,
float  _maxStoppingDecel = 2,
float *  _posPointer = NULL,
float *  _velPointer = NULL 
)
inline

constructor for Derivs_Limiter class

Parameters
_velLimit(float) velocity limit (units per second)
_accelLimit(float) acceleration limit (units per second per second)
_decelLimit(float) default=NAN, deceleration limit (units per second per second), set to NAN to use accelLimit
_target(float) default=0, target value to make position approach
_startPos(float) default=0, starting position
_startVel(float) default=0, starting velocity
_preventGoingWrongWay(bool) default=false, stop immediately if velocity is going away from target
_preventGoingTooFast(bool) default=false, constrain velocity to within velLimit
_posLimitLow(float) default=-INFINITY, lower bound for position
_posLimitHigh(float) default=INFINITY, upper bound for position, will be set to _posLimitLow if below _posLimitLow
_maxStoppingDecel(float) default=2, how many times accelLimit can be used to stop in time for target position (can be 1 through INFINITY)
_posPointerset pointer to an external variable that will be read and modified during calc as position. use &var
_velPointerset pointer to an external variable that will be read and modified during calc as velocity. use &var

◆ Derivs_Limiter() [2/2]

Derivs_Limiter::Derivs_Limiter ( )
inline

default constructor for Derivs_Limiter

Note
make sure to use the normal constructor after this, this constructor is only to allow arrays of Derivs_Limiters

Member Function Documentation

◆ _calc()

virtual float Derivs_Limiter::_calc ( )
inlineprotectedvirtual

this is where the actual code is

Return values
(float)position

◆ calc() [1/2]

float Derivs_Limiter::calc ( )
inline

call this as frequently as possible to calculate all the values

Return values
(float)position

◆ calc() [2/2]

float Derivs_Limiter::calc ( float  _target)
inline

call this as frequently as possible to calculate all the values

Parameters
_targetset the target position, ignored if NAN
Return values
(float)position

◆ distToTarget()

float Derivs_Limiter::distToTarget ( )
inline

returns target - position

Note
use abs(distToTarget()) if you don't care about direction
Return values
(float)

◆ getAcceleration()

float Derivs_Limiter::getAcceleration ( )
inline

get the current acceleration

Note
for debugging only, value noisy
Return values
(float)(units per second per second)

◆ getAccelLimit()

float Derivs_Limiter::getAccelLimit ( )
inline

get acceleration limit setting

Return values
(float)

◆ getDecelLimit()

float Derivs_Limiter::getDecelLimit ( )
inline

get deceleration limit setting

Return values
(float)

◆ getHighPosLimit()

float Derivs_Limiter::getHighPosLimit ( )
inline

get the higher boundary for position

Return values
(float)

◆ getLastPosition()

float Derivs_Limiter::getLastPosition ( )
inline

what was position in the most recent run of calc(), can be used to see if position was changed outside of calc()

Return values
(float)

◆ getLastTarget()

float Derivs_Limiter::getLastTarget ( )
inline

what was target in the most recent run of calc()

Return values
(float)

◆ getLastTime()

unsigned long Derivs_Limiter::getLastTime ( )
inline

returns the value of micros() when calc() last ran

Return values
unsignedlong

◆ getLowPosLimit()

float Derivs_Limiter::getLowPosLimit ( )
inline

get the lower boundary for position

Return values
(float)

◆ getMaxStoppingDecel()

float Derivs_Limiter::getMaxStoppingDecel ( )
inline

get setting for how many times accelLimit can be used to stop in time for target position

Return values
(float)

◆ getPosition()

float Derivs_Limiter::getPosition ( )
inline

get the current position value, but doesn't calculate anything

Return values
(float)

◆ getPositionDelta()

float Derivs_Limiter::getPositionDelta ( )
inline

returns the change in position from the most recent run of calc()

Return values
(float)

◆ getPreventGoingTooFast()

bool Derivs_Limiter::getPreventGoingTooFast ( )
inline

returns value of preventGoingTooFast setting

Return values
(bool)

◆ getPreventGoingWrongWay()

bool Derivs_Limiter::getPreventGoingWrongWay ( )
inline

returns value of preventGoingWrongWay setting

Return values
(bool)

◆ getTarget()

float Derivs_Limiter::getTarget ( )
inline

get target position

Return values
(float)

◆ getTargetDelta()

float Derivs_Limiter::getTargetDelta ( )
inline

returns the change in target from the most recent run of calc()

Return values
(float)

◆ getTargetDeltaPerTime()

float Derivs_Limiter::getTargetDeltaPerTime ( )
inline

how fast was target changing (distance/time)

Note
returns 0 if time is 0
Return values
(float)

◆ getTimeInterval()

float Derivs_Limiter::getTimeInterval ( )
inline

returns the time (in seconds) between the two most recent calculation times

Return values
(float)

◆ getVelLimit()

float Derivs_Limiter::getVelLimit ( )
inline

get velocity limit setting

Return values
(float)

◆ getVelocity()

float Derivs_Limiter::getVelocity ( )
inline

get the current velocity

Return values
(float)(units per second)

◆ getVelTarget()

float Derivs_Limiter::getVelTarget ( )
inline

get the target velocity used by the velocity control mode

Return values
(float)

◆ isPosAtTarget()

bool Derivs_Limiter::isPosAtTarget ( )
inline

does position equal target?

Return values
(bool)

◆ isPosModeNotVelocity()

bool Derivs_Limiter::isPosModeNotVelocity ( )
inline

true if in position target mode, false if in velocity target mode

Return values
(bool)

◆ isPosNotAtTarget()

bool Derivs_Limiter::isPosNotAtTarget ( )
inline

is position not equal to target?

Return values
(bool)

◆ jogPosition()

void Derivs_Limiter::jogPosition ( float  increment)
inline

set position and target to position + increment

Parameters
increment(float)
Return values
None

◆ resetTime()

void Derivs_Limiter::resetTime ( )
inline

If calc hasn't been run for a while, use this before starting to use it again to protect from large jumps.

Return values
None

◆ resetVelLimitToOriginal()

void Derivs_Limiter::resetVelLimitToOriginal ( )
inline

resets the velocity limit to the value set in the constructor or setVelLimit()

Note
may be useful, since setTargetAndVelLimitForTimedMove and setVelLimitForTimedMove change the velocity limit
Return values
None

◆ setAccelAndDecelLimits()

void Derivs_Limiter::setAccelAndDecelLimits ( float  _accelLimit,
float  _decelLimit = NAN 
)
inline

combines setAccelLimit() with setDecelLimit()

Parameters
_accelLimit
_decelLimit
Return values
None

◆ setAccelLimit()

bool Derivs_Limiter::setAccelLimit ( float  accelLim)
inline

set acceleration limit

Parameters
accelLim(float) acceleration limit (units per second per second)
Return values
(bool)true if limit changed

◆ setDecelLimit()

void Derivs_Limiter::setDecelLimit ( float  _decelLimit = NAN)
inline

set deceleration limit

Parameters
_decelLimit(float) deceleration limit, if NAN decelLimit gets set to accelLimit
Return values
None

◆ setHighPosLimit()

bool Derivs_Limiter::setHighPosLimit ( float  highLimit)
inline

set the higher boundary for position

Note
must be higher than lowPosLimit
Parameters
highLimit(float), INFINITY means no limit
Return values
(bool)did boundary change (was it valid)

◆ setLowPosLimit()

bool Derivs_Limiter::setLowPosLimit ( float  lowLimit)
inline

set the lower boundary for position

Note
must be lower than highPosLimit
Parameters
lowLimit(float), -INFINITY means no limit
Return values
(bool)did boundary change (was it valid)

◆ setMaxStoppingDecel()

void Derivs_Limiter::setMaxStoppingDecel ( float  _maxStoppingDecel)
inline

set setting for how many times accelLimit can be used to stop in time for target position

Parameters
_maxStoppingDecel(float) must be >=1.0, can be INFINITY
Return values
None

◆ setPosition()

bool Derivs_Limiter::setPosition ( float  pos = 0)
inline

set position, ignored if NAN

Parameters
pos(float) default: 0, ignored if NAN
Return values
(bool)true if position changed

◆ setPositionAndTarget()

void Derivs_Limiter::setPositionAndTarget ( float  targPos)
inline

set position and target to a value

Parameters
targPos(float)
Return values
None

◆ setPositionPointer()

void Derivs_Limiter::setPositionPointer ( float *  _positionPointer)
inline

set pointer to an external variable that will be read and modified during calc as position

Note
set to NULL to not use, set to variable with setPositionPointer(&variable)
Parameters
_positionPointer(float*)
Return values
None

◆ setPositionVelocity()

void Derivs_Limiter::setPositionVelocity ( float  pos = 0,
float  vel = 0 
)
inline

set position and velocity

Parameters
pos(float) default: 0, ignored if NAN
vel(float) default: 0, ignored if NAN
Return values
None

◆ setPosLimits()

void Derivs_Limiter::setPosLimits ( float  lowLimit,
float  highLimit 
)
inline

set the boundaries for position

Parameters
lowLimit(float)
highLimit(float)
Return values
None

◆ setPreventGoingTooFast()

void Derivs_Limiter::setPreventGoingTooFast ( bool  _preventGoingTooFast)
inline

sets value of preventGoingTooFast, true = constrain velocity to velLimit, false decelerate at accelLimit to velLimit

Parameters
_preventGoingTooFast(bool)
Return values
None

◆ setPreventGoingWrongWay()

void Derivs_Limiter::setPreventGoingWrongWay ( bool  _preventGoingWrongWay)
inline

sets value of preventGoingWrongWay, true = immediately set velocity to zero if moving away from target, false = stay under accel limit

Parameters
_preventGoingWrongWay(bool)
Return values
None

◆ setTarget()

bool Derivs_Limiter::setTarget ( float  _target)
inline

set target position (doesn't run calculation, make sure to run calc() yourself)

Parameters
_target(float) position, ignored if NAN
Return values
(bool)position==target

◆ setTargetAndPosition()

void Derivs_Limiter::setTargetAndPosition ( float  targ = 0,
float  pos = 0 
)
inline

set target and position

Parameters
targ(float) default: 0, ignored if NAN
pos(float) default: 0, ignored if NAN
Return values
None

◆ setTargetAndVelLimitForTimedMove()

boolean Derivs_Limiter::setTargetAndVelLimitForTimedMove ( float  _target,
float  _time,
float  _maxVel = NAN 
)
inline

This function changes velLimit so that a move to the specified target position takes the specified time (if possible given acceleration limit)

Note
using this function changes the value of velLimit from whatever you set it to when you created the Derivs_Limiter object
Parameters
_target(float) position you'd like to move to
_time(float) how long you would like the movement to take
_maxVel(float, optional, default=NAN) maximum allowable velocity, if the required velocity exceeds this the function returns false, if NAN the velocity limit set in the constructor or setVelLimit() is used
Return values
(bool)true if move possible within time given acceleration limit, false if not possible (and nothing is changed)

◆ setTargetTimedMovePreferred()

boolean Derivs_Limiter::setTargetTimedMovePreferred ( float  _target,
float  _time,
float  _maxVel = NAN 
)
inline

This function changes velLimit so that a move to the specified target position takes the specified time if possible given acceleration limit, and if not possible resets the velocity limit to the original value (or _maxVel if not NAN) and goes to the target at that speed instead.

Note
using this function changes the value of velLimit from whatever you set it to when you created the Derivs_Limiter object
Parameters
_target(float) position you'd like to move to
_time(float) how long you would like the movement to take
_maxVel(float, optional, default=NAN) maximum allowable velocity, if the required velocity exceeds this the function returns false, if NAN the velocity limit set in the constructor or setVelLimit() is used
Return values
(bool)true if move possible within time given acceleration limit, false if not possible (and move happens with maxVel instead but will not complete in time)

◆ setVelAccelLimits()

void Derivs_Limiter::setVelAccelLimits ( float  velLim,
float  accLim,
float  decLim = NAN 
)
inline

set velocity and acceleration limits

Parameters
velLim(float) velocity limit
accLim(float) acceleration limit
decLim(float) deceleration limit, set NAN to set equal to acceleration limit
Return values
None

◆ setVelConstant()

void Derivs_Limiter::setVelConstant ( float  vel)
inline

switch to velocity mode, and set velocity immediately to a constant value.

Parameters
vel(float)
Return values
None

◆ setVelLimit()

bool Derivs_Limiter::setVelLimit ( float  velLim)
inline

set velocity limit

Parameters
velLim(float) velocity limit (units per second)
Return values
(bool)true if limit changed

◆ setVelLimitForTimedMove()

boolean Derivs_Limiter::setVelLimitForTimedMove ( float  _dist,
float  _time,
float  _maxVel = NAN 
)
inline

This function changes velLimit so that a move of a specified distance takes the specified time (if possible given acceleration limit)

Note
using this function changes the value of velLimit from whatever you set it to when you created the Derivs_Limiter object
Parameters
_dist(float) how far you want to move
_time(float) time in seconds that you would like it to take to move the given distance
_maxVel(float, optional, default=NAN) maximum allowable velocity, if the required velocity exceeds this the function returns false, if NAN the velocity limit set in the constructor or setVelLimit() is used
Return values
(bool)true if move possible within time given acceleration limit, false if not possible (and nothing is changed)

◆ setVelocity()

bool Derivs_Limiter::setVelocity ( float  vel = 0)
inline

set velocity

Note
If you want to switch to velocity control mode look at setVelConstant() and setVelTarget()
Parameters
vel(float) default: 0, ignored if NAN
Return values
(bool)true if velocity changed

◆ setVelocityPointer()

void Derivs_Limiter::setVelocityPointer ( float *  _velocityPointer)
inline

set pointer to an external variable that will be read and modified during calc as velocity

Note
set to NULL to not use, set to variable with setVelocityPointer(&variable)
Parameters
_velocityPointer(float*)
Return values
None

◆ setVelTarget()

void Derivs_Limiter::setVelTarget ( float  vel)
inline

switch to velocity mode, and set a target velocity that the target should go towards limited by accelLimit

Parameters
vel(float)
Return values
None

Member Data Documentation

◆ accel

float Derivs_Limiter::accel
protected

◆ accelLimit

float Derivs_Limiter::accelLimit
protected

◆ decelLimit

float Derivs_Limiter::decelLimit
protected

◆ lastPos

float Derivs_Limiter::lastPos
protected

◆ lastTarget

float Derivs_Limiter::lastTarget
protected

◆ lastTime

unsigned long Derivs_Limiter::lastTime
protected

◆ maxStoppingDecel

float Derivs_Limiter::maxStoppingDecel
protected

◆ originalVelLimit

float Derivs_Limiter::originalVelLimit
protected

◆ posDelta

float Derivs_Limiter::posDelta
protected

◆ position

float Derivs_Limiter::position
protected

◆ positionPointer

float* Derivs_Limiter::positionPointer
protected

◆ posLimitHigh

float Derivs_Limiter::posLimitHigh
protected

◆ posLimitLow

float Derivs_Limiter::posLimitLow
protected

◆ posMode

bool Derivs_Limiter::posMode
protected

◆ preventGoingTooFast

bool Derivs_Limiter::preventGoingTooFast
protected

◆ preventGoingWrongWay

bool Derivs_Limiter::preventGoingWrongWay
protected

◆ target

float Derivs_Limiter::target
protected

◆ targetDelta

float Derivs_Limiter::targetDelta
protected

◆ time

float Derivs_Limiter::time
protected

◆ velLimit

float Derivs_Limiter::velLimit
protected

◆ velocity

float Derivs_Limiter::velocity
protected

◆ velocityPointer

float* Derivs_Limiter::velocityPointer
protected

◆ velocityTarget

float Derivs_Limiter::velocityTarget
protected

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