Skip to content

Commit

Permalink
add default constructor and multi example
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-8 committed Nov 17, 2022
1 parent 5aca290 commit 40e6756
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
46 changes: 46 additions & 0 deletions examples/Multi/Multi.ino
Original file line number Diff line number Diff line change
@@ -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 <Arduino.h>
#include <Derivs_Limiter.h> // 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();
}
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Derivs_Limiter
version=3.0.2
version=3.1.0
author=Joshua Phelps <[email protected]>
maintainer=Joshua Phelps <[email protected]>
sentence=This library can be used to limit the first and second derivative of a variable as it approaches a target value.
Expand Down
33 changes: 32 additions & 1 deletion src/Derivs_Limiter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 40e6756

Please sign in to comment.