I've been using successfully the old PID Library of Brett, but it does not work anymore with the current IDE. If someone wants to fix it it would be great,
So Im trying the new library mentioned above.
I am having a bi-directional situation where the robot has to balance leaning fwd or back.
Feeding negative values to the PID was a mess, so I went absolute numbers.
The results are not right. The higher the input the lower the output , Opposite from what it should be....
Here is the code, please help,
#include "ArduPID.h"
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
double X1_Latest, X2_Latest;
boolean newData = false;
ArduPID myController;
unsigned long PrintTimer = 0;
double input;
double output;
// Arbitrary setpoint and gains - adjust these as fit for your project:
double setpoint = 0;
double p = 1;
double i = 0.3;
double d = 0.5;
// Motor Controls
#define LeanFwd 5 // Yellow Wire, PWM will lean WallE forward
#define LeanBck 3 // Orange Wire, PWM will lean WallE Back
void setup()
{
Serial.begin(115200);
Serial3.begin(115200);
myController.begin(&input, &output, &setpoint, p, i, d);
// myController.reverse() ; // Uncomment if controller output is "reversed"
// myController.setSampleTime(10); // OPTIONAL - will ensure at least 10ms have past between successful compute() calls
myController.setOutputLimits(0, 255);
myController.setBias(255.0 / 2.0);
// myController.setWindUpLimits(-10, 10); // Groth bounds for the integral term to prevent integral wind-up
myController.start();
// myController.reset(); // Used for resetting the I and D terms - only use this if you know what you're doing
// myController.stop(); // Turn off the PID controller (compute() will not do anything until start() is called)
Serial.println("BALANCE_PID_RXTX");
}
void loop()
{ recvWithEndMarker();
showNewData();
input = abs(X1_Latest - 1500) /4.00; // Replace with sensor feedback
myController.compute();
if (input > 0)
{ //analogWrite(LeanFwd, abs(output)); // Replace with plant control signal
}
else
{ //analogWrite(LeanBck, abs(output)); // Replace with plant control signal
}
if (millis() - PrintTimer >= 200)
{ PrintTimer = millis();
//Serial.print(" X1 Latest: ");
//Serial.print(X1_Latest);
Serial.println(" input: "+ String(input) + " Out:" + output);
}
} //end of loop

