PID Controller in MATLAB

I’ve had several people ask me for a MATLAB implementation of a PID controller. I took my previous PID controller post and ported it to MATLAB. Because MATLAB is not designed for software like this, I made a single PID instance code set. In other words, the following code only represents one controller.

Here is the code for the update function (you must place it in a file named pid_update.m)

function pid_update(curr_error, dt)
    global windupGuard;
    global proportional_gain;
    global integral_gain;
    global derivative_gain;
    global prev_error;
    global int_error;
    global control;

    % integration
    int_error = int_error + (curr_error * dt);

    % integration windup guarding
    if (int_error < -(windupGuard))
         int_error = -(windupGuard);
    elseif (int_error > windupGuard)
         int_error = windupGuard;

    % differentiation
    diff = ((curr_error - prev_error) / dt);

    % scaling
    p_term = (proportional_gain * curr_error);
    i_term = (integral_gain     * int_error);
    d_term = (derivative_gain   * diff);

    % summation of terms
    control = p_term + i_term + d_term;

    % save current error as previous error for next iteration
    prev_error = curr_error;

Here is some VERY basic code just used to call the update function once.

global windupGuard;
global proportional_gain;
global integral_gain;
global derivative_gain;
global prev_error;
global int_error;
global control;

% set these as needed
windupGuard = 10.0;
proportional_gain = 4.0;
integral_gain = 5.0;
derivative_gain = 3.0;

% this is the zeroize function
prev_error = 0.0;
int_error = 0.0;

% call update function
pid_update(19, 1.0)

Here’s a question for those of you here reading this: Why on earth are you using MATLAB for a PID controller?

About these ads
    • Ryan
    • August 5th, 2013

    Hi, I have a problem implementing this code. In the last line of the code: ‘pid_update(19, 1.0)’, where does the current error 19 come from? I believe e(t) = r(t)-y(t), and y(t) is a function of control u(t), BUT u(t) is based on e(t). Then, how can you get e(t) without u(t)? Could you clarify my doubt? thank you!

    • 19 is just some random number I chose to insert into the pid_update() function. This MATLAB code was ported from C code, thus for each time unit, the values are passed to the controller. I’ll admit, this makes for ugly MATLAB code.

      You should loop over your values, and call this MATLAB function for each one. Alternatively, you could modify this code to handle vectors.

    • Álvaro
    • January 22nd, 2014

    Nice Code Man! I’m working with MATLAB for controll a basic experiment also I’m working with PID ’cause is basic and simple for learning process controll. I will test your code. Anyway, thank you! Good Work

    • Leandro
    • June 15th, 2014

    The controller differential part in line 21 should be:
    diff = ((curr_error – prev_error) /dt) instead of diff = ((curr_error – prev_error) * dt). Should not it be?

    • Yes, good catch. My original PID controller post is correct but somehow this MATLAB version picked up a typo. It has been edited. Thanks!

    • Skoodge
    • January 22nd, 2015

    I don´t know if you read old posts but. My idea is using Matlab on a laptop as a PID controller for a Quadcopter and releasing the quad from the procesing job.
    The quad would only transmit the IMU values and the laptop should answer the values of the Motors/ESCs,

    Do you think this is posible? I have almost zero knowledge in Matlab,

    Sorry if a made some mistakes, i´m not english native speaker,

    Thanks you for the example

    • This makes sense and should work, but why use Matlab if you don’t already know it? The sensors on a quadcopter run fairly slow so I’d just perform the computation there.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

%d bloggers like this: