I'm not quite sure if this is the right place to ask about this. I think this is a programming problem.
I am trying to create the simplest PID controller simulator in Java.
In short, there is a target value and a current value. The current value is changed by a number. You give the PID controller the current value, and it will try to return the number in the hope that such a number will cause the current value to approximate the target value. Overtime, the more you use the PID controller, it will "learn" (using integrals and derivatives) and ultimately will receive more and more accurate values. This is useful, for example, to maintain the balance of the boat by controlling the movement of the wheel.
The formula used by the PID controller is quite ordinary and quite simple - or so I thought. In the example below, the value returned by the PID controller is simply added to the current value. I assume that it will work with more complex applications (including multiplication or division, etc.). This is my program:
public class PID { private static double Kp = 0.1; private static double Kd = 0.01; private static double Ki = 0.005; private static double targetValue = 100.0; private static double currentValue = 1.0; private static double integral = 0.0; private static double previousError = 0.0; private static double dt = 0.5; private static double max = 5; private static double min = -5; public static void main(String[] args) throws Exception { while (true) { Thread.sleep((long) (1000.0 * dt)); double error = targetValue - currentValue; double derivative = 0.0; double output = 0.0; integral = integral + error * dt; derivative = (error - previousError) / dt; output = Kp * error + Ki * integral + Kd * derivative; previousError = error; if (output > max) output = max; if (output < min) output = min;
If you run this, you will see that the PID controller can ultimately cause the current value to be very close to the target value.
This is pretty cool. Now I wanted to see my results a little faster (because I plan to make some kind of interactive chart), so I decided to change the dt delta to 0.1 .
Alas, the resulting value is no longer close to 100! Now it reaches 105, and then, very slowly , it decreases to 100. This is not good!
Now imagine that dt at 0.01 ! Now it’s very slow to reach 102, and now it doesn’t even return to 100, now it’s just increasing!
So my question is: why is the cause of this lower delta?
My code is based on this PDF document and they make excellent use of 0.01 .