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
18 changes: 17 additions & 1 deletion MiniPID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,22 @@ double MiniPID::getOutput(double actual, double setpoint){
Ioutput=I*errorSum;
if(maxIOutput!=0){
Ioutput=clamp(Ioutput,-maxIOutput,maxIOutput);
}
}

//And, finally, we can just add the terms up
output=Foutput + Poutput + Ioutput + Doutput;

// To synchronize two motors we are using this->positionDiff
// if this->positionDiff is positive this means we need to reduce output and vice versa
// if output is negative we need to add
//output = output + (sgn(output) * -1.0 * this->positionDiff * maxOutput*0.05);

float posOutputFilter = 0.35;
double POSOutput = clamp(this->positionDiff, -5.0, 5.0) * maxOutput*0.01;
double POSOutputFiltered=POSOutput*posOutputFilter+POSOutput*(1-posOutputFilter);
output = output - POSOutputFiltered;


//Figure out what we're doing with the error.
if(minOutput!=maxOutput && !bounded(output, minOutput,maxOutput) ){
errorSum=error;
Expand Down Expand Up @@ -370,3 +381,8 @@ void MiniPID::checkSigns(){
if(F<0) F*=-1;
}
}

void MiniPID::setPositionDiff(double _positionDiff)
{
this->positionDiff = _positionDiff;
}
8 changes: 8 additions & 0 deletions MiniPID.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef MINIPID_H
#define MINIPID_H

template <typename T> int sgn(T val) {
return (T(0) < val) - (val < T(0));
}

class MiniPID{
public:
MiniPID(double, double, double);
Expand All @@ -20,10 +24,12 @@ class MiniPID{
void setOutputRampRate(double);
void setSetpointRange(double);
void setOutputFilter(double);
void setPositionDiff(double);
double getOutput();
double getOutput(double);
double getOutput(double, double);


private:
double clamp(double, double, double);
bool bounded(double, double, double);
Expand All @@ -38,6 +44,8 @@ class MiniPID{
double maxError;
double errorSum;

double positionDiff;

double maxOutput;
double minOutput;

Expand Down