Matthias Grob
/
FlyBed2
My fully self designed first stable working Quadrocopter Software.
PID/PID.cpp
- Committer:
- maetugr
- Date:
- 2015-08-31
- Revision:
- 10:14390c90c3f5
- Parent:
- 7:ac2895479e34
File content as of revision 10:14390c90c3f5:
#include "PID.h" PID::PID(float P, float I, float D, float Integral_Max) { Value = 0; Integral = 0; LastTime = 0; Integrate = true; PID::P = P; PID::I = I; PID::D = D; PID::Integral_Max = Integral_Max; dtTimer.start(); } float PID::compute(float SetPoint, float ProcessValue) { // meassure dt float dt = dtTimer.read() - LastTime; // time in us since last loop LastTime = dtTimer.read(); // set new time for next measurement // Proportional float Error = ProcessValue - SetPoint; // Integral if (dt > 2 || !Integrate) // Todo: 2 secs is the maximal time between two computations Integral = 0; else if (abs(Integral + Error) <= Integral_Max) Integral += Error * dt; // Derivative float Derivative = (Error - PreviousError) / dt; PreviousError = Error; // Final Formula Value = P * Error + I * Integral + D * Derivative; } void PID::setPID(float P, float I, float D) { PID::P = P; PID::I = I; PID::D = D; } void PID::setIntegrate(bool Integrate) { PID::Integrate = Integrate; }