Successful acro and level mode now! Relying on MPU9250 as base sensor. I'm working continuously on tuning and features :) NEWEST VERSION ON: https://github.com/MaEtUgR/FlyBed (CODE 100% compatible/copyable)
PID/PID.cpp
- Committer:
- maetugr
- Date:
- 2015-09-09
- Revision:
- 1:60882db03b0f
- Parent:
- 0:37f0c1e8fa66
- Child:
- 5:8ea99e98de73
File content as of revision 1:60882db03b0f:
#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(); } void 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; }