From 40e6756c3c620e3408aaab07648dd5a91b7aac6a Mon Sep 17 00:00:00 2001 From: joshua-8 Date: Wed, 16 Nov 2022 21:09:40 -0500 Subject: [PATCH] add default constructor and multi example --- examples/Multi/Multi.ino | 46 ++++++++++++++++++++++++++++++++++++++++ library.properties | 2 +- src/Derivs_Limiter.h | 33 +++++++++++++++++++++++++++- 3 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 examples/Multi/Multi.ino diff --git a/examples/Multi/Multi.ino b/examples/Multi/Multi.ino new file mode 100644 index 0000000..8e43e6e --- /dev/null +++ b/examples/Multi/Multi.ino @@ -0,0 +1,46 @@ +/* + This example demonstrates the use of multiple instances of DerivsLimiter. + Each one calulates an independent motion profile. + This example uses arrays to avoid duplicating code + Tested on an Arduino Uno but should work on anything. + Requires Derivs_Limiter version 3.1.0 or greater + + v1.0 by joshua-8 2022-11-16 +*/ +#include +#include // https://github.com/joshua-8/Derivs_Limiter +// detailed documentation of Derivs_Limiter can be found here: https://joshua-8.github.io/Derivs_Limiter/class_derivs___limiter.html + +const int NUM_DLs = 4; // limited only by the Arduino's memory and the time it takes to compute each Derivs_Limiter + +Derivs_Limiter DL[NUM_DLs]; + +// random settings just as an example of each Derivs_Limiter getting unique settings +float velLimit[NUM_DLs] = { 150, 222.2, 122, 160 }; +float accLimit[NUM_DLs] = { 270, 245.5, 250, 340 }; + +void setup() +{ + Serial.begin(115200); + for (int i = 0; i < NUM_DLs; i++) { + DL[i] = Derivs_Limiter(velLimit[i], accLimit[i]); // https://joshua-8.github.io/Derivs_Limiter/class_derivs___limiter.html#a674339d1db4ca3a2358e1d25b9eb6cf4 + // now is a good place to set other settings for each DL + } +} + +void loop() +{ + for (int i = 0; i < NUM_DLs; i++) { + + float targ = ((millis() % (500 + 100 * i)) < (500 + 100 * i) / 2) ? -(i + 1) : (i + 1); // targets are set to be -(i+1) or (i+1) on an interval that is unique for each DL + DL[i].setTarget(targ); // you could give each DL any target you want it to go to + + DL[i].calc(); + + Serial.print(DL[i].getPosition()); // DL[i].getPosition() is the value that could be sent to the i numbered servo + if (i < NUM_DLs - 1) // add commas between numbers, a newline at the end + Serial.print(", "); + else + Serial.println(); + } +} \ No newline at end of file diff --git a/library.properties b/library.properties index f59448e..21b7e50 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Derivs_Limiter -version=3.0.2 +version=3.1.0 author=Joshua Phelps maintainer=Joshua Phelps sentence=This library can be used to limit the first and second derivative of a variable as it approaches a target value. diff --git a/src/Derivs_Limiter.h b/src/Derivs_Limiter.h index 6b6106d..bc32756 100644 --- a/src/Derivs_Limiter.h +++ b/src/Derivs_Limiter.h @@ -43,7 +43,7 @@ class Derivs_Limiter { * @param _preventGoingWrongWay: (bool) default=false, stop immediately if velocity is going away from target * @param _preventGoingTooFast: (bool) default=false, constrain velocity to within velLimit * @param _posLimitLow: (float) default=-INFINITY, lower bound for position - * @param _posLimitHigh: (float) default=INFINITY, upper bound for position + * @param _posLimitHigh: (float) default=INFINITY, upper bound for position, will be set to _posLimitLow if below _posLimitLow * @param _maxStoppingDecel: (float) default=2, how many times accelLimit can be used to stop in time for target position (can be 1 through INFINITY) * @param _posPointer: set pointer to an external variable that will be read and modified during calc as position. use &var * @param _velPointer: set pointer to an external variable that will be read and modified during calc as velocity. use &var @@ -84,6 +84,37 @@ class Derivs_Limiter { velocityTarget = 0; } + /** + * @brief 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 + */ + Derivs_Limiter() + { + accel = 0; + lastTime = 0; + velLimit = 0; + originalVelLimit = 0; + accelLimit = 0; + setDecelLimit(NAN); + target = 0; + posMode = true; + lastTarget = 0; + targetDelta = 0; + position = 0; + lastPos = 0; + posDelta = 0; + velocity = 0; + time = 0; + preventGoingWrongWay = false; + preventGoingTooFast = false; + posLimitLow = 0; + posLimitHigh = 0; + maxStoppingDecel = 1; + positionPointer = NULL; + velocityPointer = NULL; + velocityTarget = 0; + } + /** * @brief set position and velocity * @param pos: (float) default: 0, ignored if NAN