Hi
I have been struggling for days, trying to implement a PID for a simple dc motor with gearbox. It reads a angle sensor through spi that is meant to set the "Setpoint".
One of the hangups are that the PID library by Brett Beauregard calls for a variable
"double error = *mySetpoint - input;"
My "error" relates to 360 degrees and the wrap around at the 360 >0 degrees point. Rafi Sidqi solved the issue but the code did not work, needed to change final 360 to -360 like this:
int Sp = 1; // set point
int pV = 182; // present value
void setup() {
// put your setup code here, to run once:
Serial.begin (9600);
}
void loop() {
// put your main code here, to run repeatedly:
double error;
if (Sp>pV) {
if (abs(Sp-pV) < abs(-360 + Sp - pV)) error = Sp - pV;
else error = abs(-360 + Sp - pV);
}
else{
if(abs(Sp-pV)< abs(360 - pV + Sp)) error = Sp - pV;
else error = abs( -360 - Sp + pV);
}
Serial.println (error);
}
SP and PV seems to be "special" terms and cant be used (sketch would not compile) hense small "p's"
So I need to replace the "double error = *mySetpoint - input;" in the library code with double error = ...if statements...
My hangup is that the library file is in C++. Might as well have been in Marchen
So in order to change the file I downloaded notepad ++. I can now copy past the file and edit it. If I new C++ Abstract from the library code that need to change:
bool PID::Compute()
{
if(!inAuto) return false;
unsigned long now = millis();
unsigned long timeChange = (now - lastTime);
if(timeChange>=SampleTime)
{
/*Compute all the working error variables*/
double input = *myInput;
double error = *mySetpoint - input;
/**************************************************************************/
/* double error ();{double Sp = *mySetpoint; double pV = input;
if (Sp>pV) {
if (abs(Sp-pV) < abs(-360 + Sp - pV)) error = Sp - pV;
else error = -360 + Sp - pV;
}
else{
if(abs(Sp-pV)< abs(360 - pV + Sp)) error = Sp - pV;
else error = -360 - Sp + pV;
}*/
/****************************************************************************/
double dInput = (input - lastInput);
outputSum+= (ki * error);
/*Add Proportional on Measurement, if P_ON_M is specified*/
if(!pOnE) outputSum-= kp * dInput;
AND
How to I replace the now modified .cpp file with the current file in the: arduino,library, pid, folder ?
Everytime I replace it my sketch stops compiling saying "multiple library's found" even if i delete the original.
The .cpp file now:
bool PID::Compute()
{
if(!inAuto) return false;
unsigned long now = millis();
unsigned long timeChange = (now - lastTime);
if(timeChange>=SampleTime)
{
/*Compute all the working error variables*/
double input = *myInput;
/*double error = *mySetpoint - input;*/
/*******************************************************************/
double error;
double Sp = *mySetpoint;
double pV = input;
if (Sp>pV)
{
if (abs(Sp-pV) < abs(-360 + Sp - pV)) error = Sp - pV;
else error = -360 + Sp - pV;
}
else{
if(abs(Sp-pV)< abs(360 - pV + Sp)) error = Sp - pV;
else error = abs(-360 - Sp + pV);
}
/*************************************************************************/
double dInput = (input - lastInput);
outputSum+= (ki * error);
/*Add Proportional on Measurement, if P_ON_M is specified*/
if(!pOnE) outputSum-= kp * dInput;
if(outputSum > outMax) outputSum= outMax;