1#ifndef _DERIVS_LIMITER_H_
2#define _DERIVS_LIMITER_H_
51 Derivs_Limiter(
float _velLimit,
float _accelLimit,
float _decelLimit = NAN,
float _target = 0,
52 float _startPos = 0,
float _startVel = 0,
bool _preventGoingWrongWay =
false,
bool _preventGoingTooFast =
false,
53 float _posLimitLow = -INFINITY,
float _posLimitHigh = INFINITY,
float _maxStoppingDecel = 2,
54 float* _posPointer = NULL,
float* _velPointer = NULL)
69 if (!isnan(_startPos))
74 if (!isnan(_startVel))
217 if (isnan(_decelLimit)) {
391 if (!isnan(_target)) {
659 if (isnan(_dist) || isnan(_time)) {
668 tempVelLimit = _dist / _time;
677 tempVelLimit = (-0.5 * acc * (-_time + sqrt(sq(_time) - 4 * _dist / acc)));
679 boolean possible = !isnan(tempVelLimit) && tempVelLimit <= abs(_maxVel);
745 if (!isnan(_target)) {
829 velocity = constrain(
velocity, -maxSpeedThatCanBeStopped, maxSpeedThatCanBeStopped);
838 boolean velPositive = (
velocity > 0);
This library can be used to limit the first and second derivative of a variable as it approaches a ta...
Definition Derivs_Limiter.h:8
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 po...
Definition Derivs_Limiter.h:657
float getVelTarget()
get the target velocity used by the velocity control mode
Definition Derivs_Limiter.h:634
void setMaxStoppingDecel(float _maxStoppingDecel)
set setting for how many times accelLimit can be used to stop in time for target position
Definition Derivs_Limiter.h:310
float position
Definition Derivs_Limiter.h:10
float originalVelLimit
Definition Derivs_Limiter.h:28
void setDecelLimit(float _decelLimit=NAN)
set deceleration limit
Definition Derivs_Limiter.h:215
void resetTime()
If calc hasn't been run for a while, use this before starting to use it again to protect from large j...
Definition Derivs_Limiter.h:433
virtual float _calc()
this is where the actual code is
Definition Derivs_Limiter.h:757
float posDelta
Definition Derivs_Limiter.h:27
float calc(float _target)
call this as frequently as possible to calculate all the values
Definition Derivs_Limiter.h:743
float distToTarget()
returns target - position
Definition Derivs_Limiter.h:587
float getHighPosLimit()
get the higher boundary for position
Definition Derivs_Limiter.h:337
float accel
Definition Derivs_Limiter.h:12
float velocity
Definition Derivs_Limiter.h:11
void setPositionVelocity(float pos=0, float vel=0)
set position and velocity
Definition Derivs_Limiter.h:124
bool setAccelLimit(float accelLim)
set acceleration limit
Definition Derivs_Limiter.h:201
float getTarget()
get target position
Definition Derivs_Limiter.h:402
void setPreventGoingWrongWay(bool _preventGoingWrongWay)
sets value of preventGoingWrongWay, true = immediately set velocity to zero if moving away from targe...
Definition Derivs_Limiter.h:531
float targetDelta
Definition Derivs_Limiter.h:25
float time
Definition Derivs_Limiter.h:18
void setPosLimits(float lowLimit, float highLimit)
set the boundaries for position
Definition Derivs_Limiter.h:378
bool preventGoingWrongWay
Definition Derivs_Limiter.h:19
void setVelConstant(float vel)
switch to velocity mode, and set velocity immediately to a constant value.
Definition Derivs_Limiter.h:597
float lastTarget
Definition Derivs_Limiter.h:24
void jogPosition(float increment)
set position and target to position + increment
Definition Derivs_Limiter.h:423
bool preventGoingTooFast
Definition Derivs_Limiter.h:20
float velLimit
Definition Derivs_Limiter.h:15
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
Definition Derivs_Limiter.h:51
float velocityTarget
Definition Derivs_Limiter.h:32
bool setLowPosLimit(float lowLimit)
set the lower boundary for position
Definition Derivs_Limiter.h:348
float getTargetDeltaPerTime()
how fast was target changing (distance/time)
Definition Derivs_Limiter.h:497
float lastPos
Definition Derivs_Limiter.h:26
bool getPreventGoingTooFast()
returns value of preventGoingTooFast setting
Definition Derivs_Limiter.h:559
bool isPosNotAtTarget()
is position not equal to target?
Definition Derivs_Limiter.h:577
float getLastPosition()
what was position in the most recent run of calc(), can be used to see if position was changed outsid...
Definition Derivs_Limiter.h:487
bool getPreventGoingWrongWay()
returns value of preventGoingWrongWay setting
Definition Derivs_Limiter.h:540
float getPositionDelta()
returns the change in position from the most recent run of calc()
Definition Derivs_Limiter.h:478
float getLastTarget()
what was target in the most recent run of calc()
Definition Derivs_Limiter.h:469
void setPositionPointer(float *_positionPointer)
set pointer to an external variable that will be read and modified during calc as position
Definition Derivs_Limiter.h:510
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 ti...
Definition Derivs_Limiter.h:712
bool isPosAtTarget()
does position equal target?
Definition Derivs_Limiter.h:568
float getAccelLimit()
get acceleration limit setting
Definition Derivs_Limiter.h:263
void resetVelLimitToOriginal()
resets the velocity limit to the value set in the constructor or setVelLimit()
Definition Derivs_Limiter.h:644
void setPositionAndTarget(float targPos)
set position and target to a value
Definition Derivs_Limiter.h:412
bool setPosition(float pos=0)
set position, ignored if NAN
Definition Derivs_Limiter.h:153
bool setVelLimit(float velLim)
set velocity limit
Definition Derivs_Limiter.h:186
void setVelAccelLimits(float velLim, float accLim, float decLim=NAN)
set velocity and acceleration limits
Definition Derivs_Limiter.h:243
float * velocityPointer
Definition Derivs_Limiter.h:30
bool posMode
Definition Derivs_Limiter.h:31
float maxStoppingDecel
Definition Derivs_Limiter.h:23
Derivs_Limiter()
default constructor for Derivs_Limiter
Definition Derivs_Limiter.h:91
float accelLimit
Definition Derivs_Limiter.h:16
float target
Definition Derivs_Limiter.h:14
bool setHighPosLimit(float highLimit)
set the higher boundary for position
Definition Derivs_Limiter.h:363
unsigned long getLastTime()
returns the value of micros() when calc() last ran
Definition Derivs_Limiter.h:442
void setVelocityPointer(float *_velocityPointer)
set pointer to an external variable that will be read and modified during calc as velocity
Definition Derivs_Limiter.h:521
float getTargetDelta()
returns the change in target from the most recent run of calc()
Definition Derivs_Limiter.h:460
float calc()
call this as frequently as possible to calculate all the values
Definition Derivs_Limiter.h:733
bool setTarget(float _target)
set target position (doesn't run calculation, make sure to run calc() yourself)
Definition Derivs_Limiter.h:389
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 ti...
Definition Derivs_Limiter.h:694
float getVelocity()
get the current velocity
Definition Derivs_Limiter.h:281
void setVelTarget(float vel)
switch to velocity mode, and set a target velocity that the target should go towards limited by accel...
Definition Derivs_Limiter.h:612
float getVelLimit()
get velocity limit setting
Definition Derivs_Limiter.h:254
float * positionPointer
Definition Derivs_Limiter.h:29
float getDecelLimit()
get deceleration limit setting
Definition Derivs_Limiter.h:272
float getMaxStoppingDecel()
get setting for how many times accelLimit can be used to stop in time for target position
Definition Derivs_Limiter.h:319
float posLimitHigh
Definition Derivs_Limiter.h:22
bool setVelocity(float vel=0)
set velocity
Definition Derivs_Limiter.h:170
float getTimeInterval()
returns the time (in seconds) between the two most recent calculation times
Definition Derivs_Limiter.h:451
void setPreventGoingTooFast(bool _preventGoingTooFast)
sets value of preventGoingTooFast, true = constrain velocity to velLimit, false decelerate at accelLi...
Definition Derivs_Limiter.h:550
void setTargetAndPosition(float targ=0, float pos=0)
set target and position
Definition Derivs_Limiter.h:138
bool isPosModeNotVelocity()
true if in position target mode, false if in velocity target mode
Definition Derivs_Limiter.h:625
float decelLimit
Definition Derivs_Limiter.h:17
float posLimitLow
Definition Derivs_Limiter.h:21
float getPosition()
get the current position value, but doesn't calculate anything
Definition Derivs_Limiter.h:300
unsigned long lastTime
Definition Derivs_Limiter.h:13
float getAcceleration()
get the current acceleration
Definition Derivs_Limiter.h:291
void setAccelAndDecelLimits(float _accelLimit, float _decelLimit=NAN)
combines setAccelLimit() with setDecelLimit()
Definition Derivs_Limiter.h:230
float getLowPosLimit()
get the lower boundary for position
Definition Derivs_Limiter.h:328