Skip to content

Commit 90d01ca

Browse files
committed
improve examples, add setTargetTimedMovePreferred
1 parent f8c7125 commit 90d01ca

File tree

4 files changed

+64
-4
lines changed

4 files changed

+64
-4
lines changed

examples/SmoothServoTimeBased/SmoothServoTimeBased.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ void loop()
2424
if (state == true)
2525
limiter.setTargetAndVelLimitForTimedMove(0, 10);
2626
state = false;
27-
myServo.write(limiter.calc());
2827
limiter.calc();
28+
myServo.write(limiter.getPosition());
2929
} else {
3030
if (state == false)
3131
limiter.setTargetAndVelLimitForTimedMove(180, 10);
3232
state = true;
33-
myServo.write(limiter.calc());
3433
limiter.calc();
34+
myServo.write(limiter.getPosition());
3535
}
3636
Serial.print(limiter.getPosition());
3737
Serial.print(",");

examples/TimeBased/TimeBased.ino

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* TimeBased, an example for the Derivs_Limiter library
3+
* https://github.com/joshua-8/Derivs_Limiter
4+
*
5+
* Open the serial plotter to see the smoothed position values.
6+
* Every 15 seconds the values should move for 10 seconds.
7+
*/
8+
#include <Arduino.h>
9+
#include <Derivs_Limiter.h>
10+
11+
Derivs_Limiter limiter = Derivs_Limiter(14, 20); // velocityLimit, accelerationLimit
12+
13+
boolean state = true;
14+
void setup()
15+
{
16+
Serial.begin(9600);
17+
}
18+
void loop()
19+
{
20+
if (millis() % 30000 < 15000) { //toggles every 15 seconds
21+
if (state == true) //important to only start timed move once
22+
limiter.setTargetAndVelLimitForTimedMove(0, 10);
23+
state = false;
24+
limiter.calc();
25+
} else {
26+
if (state == false) //important to only start timed move once
27+
limiter.setTargetAndVelLimitForTimedMove(15, 10);
28+
state = true;
29+
limiter.calc();
30+
}
31+
Serial.print(limiter.getPosition(), 4);
32+
Serial.print(",");
33+
Serial.print(limiter.getVelocity());
34+
Serial.print(",");
35+
Serial.print(limiter.getAcceleration());
36+
Serial.println();
37+
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Derivs_Limiter
2-
version=2.6.0
2+
version=2.6.1
33
author=Joshua Phelps <[email protected]>
44
maintainer=Joshua Phelps <[email protected]>
55
sentence=This library can be used to limit the first and second derivative of a variable as it approaches a target value.

src/Derivs_Limiter.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ class Derivs_Limiter {
517517
}
518518

519519
/**
520-
* @brief This function changes velLimit so that a move to the specified target position takes the specified time (if possible given acceleration limit) use like setTarget()
520+
* @brief This function changes velLimit so that a move to the specified target position takes the specified time (if possible given acceleration limit)
521521
* @note using this function changes the value of velLimit from whatever you set it to when you created the Derivs_Limiter object
522522
* @param _target: (float) position you'd like to move to
523523
* @param _time: (float) how long you would like the movement to take
@@ -532,6 +532,29 @@ class Derivs_Limiter {
532532
return ret;
533533
}
534534

535+
/**
536+
* @brief 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
537+
* @note using this function changes the value of velLimit from whatever you set it to when you created the Derivs_Limiter object
538+
* @param _target: (float) position you'd like to move to
539+
* @param _time: (float) how long you would like the movement to take
540+
* @param _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
541+
* @retval (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)
542+
*/
543+
boolean setTargetTimedMovePreferred(float _target, float _time, float _maxVel = NAN)
544+
{
545+
boolean ret = setVelLimitForTimedMove(_target - position, _time, _maxVel);
546+
if (ret)
547+
target = _target;
548+
else { //not possible in time given acceleration
549+
if (isnan(_maxVel))
550+
resetVelLimitToOriginal();
551+
else
552+
velLimit = _maxVel;
553+
target = _target;
554+
}
555+
return ret;
556+
}
557+
535558
protected:
536559
/**
537560
* @brief this is where the actual code is

0 commit comments

Comments
 (0)