Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions MiniPID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ void MiniPID::init(){
minOutput=0;
setpoint=0;
lastActual=0;
lastSetpoint=0;
firstRun=true;
reversed=false;
useDt=false;
outputRampRate=0;
lastOutput=0;
outputFilter=0;
setpointRange=0;
dt=0;
}

//**********************************
Expand Down Expand Up @@ -163,6 +166,12 @@ void MiniPID::setOutputLimits(double minimum,double maximum){
void MiniPID::setDirection(bool reversed){
this->reversed=reversed;
}
/** Set the desired differential part calculation
* @param useDt Set true to calculate time derivative
*/
void MiniPID::useDeltaTime(bool useDt){
this->useDt=useDt;
}

//**********************************
//Primary operating functions
Expand Down Expand Up @@ -209,6 +218,7 @@ double MiniPID::getOutput(double actual, double setpoint){
//For last output, we can assume it's the current time-independent outputs.
if(firstRun){
lastActual=actual;
lastSetpoint=setpoint;
lastOutput=Poutput+Foutput;
firstRun=false;
}
Expand All @@ -218,8 +228,14 @@ double MiniPID::getOutput(double actual, double setpoint){
//Note, this->is negative. this->actually "slows" the system if it's doing
//the correct thing, and small values helps prevent output spikes and overshoot

Doutput= -D*(actual-lastActual);
lastActual=actual;
if(useDt && (dt > 0)){
Doutput= D*((setpoint-lastSetpoint) - (actual-lastActual)) / dt;
lastActual=actual;
lastSetpoint=setpoint;
} else{
Doutput= -D*(actual-lastActual);
lastActual=actual;
}



Expand Down Expand Up @@ -323,6 +339,12 @@ void MiniPID::setOutputFilter(double strength){
outputFilter=strength;
}
}
/**
* Sets the delta time between each iteration.
*/
void MiniPID::setDt(double dt){
this->dt=dt;
}

//**************************************
// Helper functions
Expand Down
5 changes: 5 additions & 0 deletions MiniPID.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class MiniPID{
void setOutputRampRate(double);
void setSetpointRange(double);
void setOutputFilter(double);
void setDt(double);
void useDeltaTime(bool);
double getOutput();
double getOutput(double);
double getOutput(double, double);
Expand All @@ -33,6 +35,7 @@ class MiniPID{
double I;
double D;
double F;
double dt;

double maxIOutput;
double maxError;
Expand All @@ -42,11 +45,13 @@ class MiniPID{
double minOutput;

double setpoint;
double lastSetpoint;

double lastActual;

bool firstRun;
bool reversed;
bool useDt;

double outputRampRate;
double lastOutput;
Expand Down