Skip to content

Instantly share code, notes, and snippets.

@alphaville
Last active March 11, 2022 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alphaville/9568bd559ec99de3e94b63f2b6b52459 to your computer and use it in GitHub Desktop.
Save alphaville/9568bd559ec99de3e94b63f2b6b52459 to your computer and use it in GitHub Desktop.
Simple C implementation of PID controller. MIT Licence.
#include "pid.h"
pid_controller_t new_pid(real_t Kp, real_t Ki, real_t Kd)
{
pid_controller_t ctrl;
ctrl.Kp = Kp;
ctrl.Ki = Ki;
ctrl.Kd = Kd;
ctrl.sum_errors = 0.0;
ctrl.previous_error = 0.0;
ctrl.previous_error_recorded = 0;
return ctrl;
}
real_t compute_control_action(pid_controller_t *controller, real_t error)
{
controller->sum_errors += error;
real_t ud = 0.;
if (controller->previous_error_recorded == 1) {
real_t diff = error - controller->previous_error;
ud = controller->Kd * diff;
}
real_t up = controller->Kp * error;
real_t ui = controller->Ki * controller->sum_errors;
controller->previous_error = error;
controller->previous_error_recorded = 1;
return up + ui + ud;
}
/*
* Discrete-time PID controller
*
* Author: Pantelis Sopasakis
* Queen's University Belfast
* Date: 18 December, 2019
*
* License: MIT License
*
* MIT License
*
* Copyright (c) 2019 Pantelis Sopasakis <p.sopasakis@qub.ac.uk>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Library-free implementation; compile with `gcc -c pid.c` or
* `clang -c pid.c`
*
* Example of use:
*
* ```
* real_t control_action;
* pid_controller_t pid = new_pid(5.0, 0.5, 2.0);
* control_action = compute_control_action(&pid, 1.0);
* ```
*/
/**
* Number type (double, float or other)
*/
typedef double real_t;
/**
* PID controller structure
*/
typedef struct
{
real_t Kp; /* proportional gain */
real_t Ki; /* integral gain */
real_t Kd; /* derviative gain */
real_t sum_errors; /* sum of errors */
real_t previous_error; /* previous error */
char previous_error_recorded;
} pid_controller_t;
/**
* Construct a new PID controller with given parameters
*
* Arguments:
* - Kp (real_t): proportional gain
* - Ki (real_t): integral gain
* - Kd (real_t): derivative gain
*
* Returns:
* - Instance of `pid_controller_t`
*/
pid_controller_t new_pid(real_t Kp, real_t Ki, real_t Kd);
/**
* Compute PID cojntrol action
*
* Arguments:
* - controller (pid_controller_t): instance of PID controller
* - error (real_t): current error
*
* Returns:
*/
real_t compute_control_action(pid_controller_t *controller, real_t error);
@nadukandi
Copy link

Isn't the time interval term missing in the integral and derivative gain terms?

@alphaville
Copy link
Author

@nadukandi no, because this is a discrete-time version of the PID controller; the user is expected to provide appropriate gains for a given sampling time. In other words, Kp, Ki and Kd are not the continuous-time PID gains.

@alphaville
Copy link
Author

@nadukandi if you want to provide the continuous-time PID gains and the sampling time, you can do the following:

id_controller_t new_pid(real_t Kp, real_t Ki, real_t Kd, real_t sampling_time)
{
    pid_controller_t ctrl;
    ctrl.Kp = Kp;
    ctrl.Ki = Ki * sampling_time;
    ctrl.Kd = Kd / sampling_time;
    ctrl.sum_errors = 0.0;
    ctrl.previous_error = 0.0;
    ctrl.previous_error_recorded = 0;
    return ctrl;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment