/Simple-PID-correction

It's a simple PID simulator.

Primary LanguageC#

Simple-PID

It's a simple PID simulator in C# and Python.

You can set kp, ki, kd, setpoint, dt, and the function will return a value.

The pid formula should be:

image



What it looks like in C#:

double Kp = 0.5, Ki = 0.5, Kd = 0.1, SetPoint = 10, dt = 0.1, integral, pre_err;

public double Start(double measured_value)
{
    double err, output, derivative;

    err = SetPoint - measured_value;
    integral += err * dt;
    derivative = (err - pre_err) / dt;
    output = (Kp * err + Ki * integral + Kd * derivative);


    pre_err = err;

    return output;
}

And what it looks like in Python:

def compute(self, feedback_value = None):

    err, output, derivative = 0, 0, 0
    
    if(feedback_value is not None):
        self.feedback_value = feedback_value
        
    err = self.SetPoint - self.feedback_value
    self.integral += err * self.dt
    derivative = (err - self.pre_err) / self.dt
    output = (self.Kp * err + self.Ki * self.integral + self.Kd * derivative)


    self.pre_err = err
    self.feedback_value = output

    return output

How to use:

In C#:

1.You can copy the method above.
2.Use .dll in library folder.
3.Download the project and add in to your project.

In Python:

0.Download the file Simple_PID_Python/PID_Demo.py, and see how to use.
1.Download the file Simple_PID_Python/PID.py
2.import PID
3.Set kp,ki,kd

pid = PID(1, 10, 0.001, SetPoint=1, dt = 0.1)

4.Use it.

output = pid.compute(feedback_signal)

5.If you want to start at 0

pid.clear()

Example

In C#:

You can open project in example folder.
It's a simple pid demo. I use a lowpass filter which I write before to simulate a system.(https://github.com/jeremy7710/LowpassFilter)
Just set kp, ki, kd, dt, setpoint, and click set button. And image will be plotted.

In Python:

1.You just run the code at Simple_PID_Python/PID_Demo.py, and you will see a beautiful output.

cd YourPath
python PID_Demo.py

image

Reference:

https://en.wikipedia.org/wiki/PID_controller