change the code in a library (SOLVED)

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 :frowning:
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;

Tjaart:
My hangup is that the library file is in C++. Might as well have been in Marchen :frowning:

The Arduino language is essentially C++. The significant difference is that the Arduino IDE, etc. automatically generate function prototypes for any functions that don't already have one.

Tjaart:
I can now copy past the file and edit it.
...
How to I replace the now modified .cpp file with the current file in the: arduino,library, pid, folder ?

Open the file directly in Notepad++. Then you can just modify the code and save the file.

Tjaart:
Everytime I replace it my sketch stops compiling saying "multiple library's found" even if i delete the original.

The "Multiple libraries found" is just a helpful informational message. It will not cause compilation to fail. It may be that the Arduino IDE is selecting the library that you didn't intend it to, which could cause compilation to fail, but simply having multiple libraries that match an #include directive won't.

If you scroll the black console pane up, you'll see the true compilation error message, which will tell you what the real problem is. If you want help with that, you'll need to post the full and exact text of the error output here.