PID Demo

Interactive PID Demo

This is a little Processing sketch I wrote for playing with the parameters of a PID controller. Getting an interactive, visual feedback of the response can go a long way when trying to figure out the dependency between the values and the effect on the system.

Changes to the parameters on the right will update the simulation instantly. Use the horizontal slider to change the set position (the position the controller wants to reach). The vertical slider changes the slope for the car to simulate an asymmetrical system. Right-clicking a slider resets it.

The graph at the bottom displays some system states over time.
Dark grey is the set point, blue is the current position and green is the motor force.

The little force vectors draw over the car are the separate components that went into the controller reaction. 
Red for proportional, green for integral and blue for derivative. 

The motor in this case is a “super motor”, it reaches its desired torque instantaneously. I might add a better model for this later on to get a more realistic response.

For a general explanation see the PID controller wikipedia page. It’s pretty good.

Find the source code below.

no HTML5 support
PID parameters
Car parameters
motor force limit(N):

The PID update loop looks like this and is called for every time step.

float Update(float error, float dt)
    integError += error * dt;
    compP = error * Kp;
    compI = integError * Ki;
    compD = ((error - lastError) / dt) * Kd;
    lastError = error;
    return compP + compI + compD;

The car is then updated like with the resulting force.

float Update(float force, float dt)
    // consider the slope
    force += 9.81 * mass * sin(slope);
    // forward euler
    float accel = force / mass;
    velocity += accel * dt;
    velocity *= damping;
    position += velocity * dt;
    return position;

Here’s the source code of the page if you want to play with it:

Like my work?
Why not buy me a coffee