Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: HIDScope MODSERIAL QEI biquadFilter mbed Servo
help_functions/PID_controller.h
- Committer:
- MaikOvermars
- Date:
- 2018-10-29
- Revision:
- 26:23b0b6fce3a8
- Parent:
- 24:53c300d1320e
- Child:
- 45:829a3992d689
File content as of revision 26:23b0b6fce3a8:
#include "mbed.h" double Kp = 7.5; double Ki = 1.02; double Kd = 10; extern double samplingfreq = 1000; void PID_controller(double error1, double error2, double &u1, double &u2, const double &T) { // proportianal part double u1_k = Kp * error1; double u2_k = Kp * error2; static double error1_integral = 0; // static double error1_prev = error1; // initialization with this value only done once! static double error2_integral = 0; static double error2_prev = error2; // initialization with this value only done once! static BiQuad LowPassFilter1(0.0640, 0.1279, 0.0640, -1.1683, 0.4241); static BiQuad LowPassFilter2(0.0640, 0.1279, 0.0640, -1.1683, 0.4241); // integral part error1_integral = error1_integral + error1 * T; double u1_i = Ki * error1_integral; error2_integral = error2_integral + error2 * T; double u2_i = Ki * error2_integral; // derivative part double error1_derivative = (error1 - error1_prev) / T; double u1_d = Kd * LowPassFilter1.step(error1_derivative); // apply low pass filter to remove noise due to derivative amplification error1_prev = error1; double error2_derivative = (error2 - error2_prev) / T; double u2_d = Kd * LowPassFilter2.step(error2_derivative); // apply low pass filter to remove noise due to derivative amplification error2_prev = error2; u1 = u1_k+u1_i+u1_d; u2 = u2_k+u2_i+u2_d; }