annotate core/Pid.cpp @ 152:8f98b32d0e23 ClockSync

Last commit on this branch for a while. Overall not very succesful
author Giulio Moro <giuliomoro@yahoo.it>
date Mon, 05 Oct 2015 13:06:14 +0100
parents 134bff10e561
children
rev   line source
giuliomoro@149 1 /*
giuliomoro@149 2 * Pid.cpp
giuliomoro@149 3 *
giuliomoro@149 4 * Created on: 14 Sep 2015
giuliomoro@149 5 * Author: giulio
giuliomoro@149 6 */
giuliomoro@149 7
giuliomoro@149 8 #include "Pid.h"
giuliomoro@149 9
giuliomoro@149 10 Pid::Pid(){
giuliomoro@149 11 integralError = 0;
giuliomoro@149 12 pastError = 0;
giuliomoro@149 13 error = 0;
giuliomoro@149 14 differentialError = 0;
giuliomoro@149 15 kp = 5;
giuliomoro@152 16 ki = 2;// 2;
giuliomoro@152 17 kd = 0.8; //0.8;
giuliomoro@152 18 gain = 1;
giuliomoro@149 19 idleTimeout = 0;
giuliomoro@149 20 timeoutCount = 0;
giuliomoro@149 21 output = 0;
giuliomoro@149 22 // printf("----------%f %f %f %f %f %f\n", kp, error, ki, integralError, kd, differentialError);
giuliomoro@149 23 }
giuliomoro@149 24 void Pid::updateIntegralError(){
giuliomoro@149 25 integralError += (error + pastError) * 0.5 * ts;
giuliomoro@149 26 }
giuliomoro@149 27 void Pid::updateDifferentialError(){
giuliomoro@149 28 differentialError = (error - pastError) / ts;
giuliomoro@149 29 }
giuliomoro@149 30 float Pid::setError(float anError){
giuliomoro@149 31 pastError = error;
giuliomoro@149 32 error = anError;
giuliomoro@149 33 ts = 1; //TODO: this should be passed as a parameter, unless setError() gets always called at constant intervals
giuliomoro@149 34 updateIntegralError();
giuliomoro@149 35 updateDifferentialError();
giuliomoro@152 36 output = gain * (kp * error + ki * integralError + kd * differentialError);
giuliomoro@149 37 // printf("%f %f %f %f %f %f %f\n", output, kp, error, ki, integralError, kd, differentialError);
giuliomoro@149 38 return output;
giuliomoro@149 39 }
giuliomoro@149 40 float Pid::getOutput(){
giuliomoro@149 41 return output;
giuliomoro@149 42 }
giuliomoro@152 43 void Pid::setProportionalGain(float proportionalGain){
giuliomoro@152 44 kp = proportionalGain;
giuliomoro@152 45 }
giuliomoro@152 46 void Pid::setDerivativeGain(float derivativeGain){
giuliomoro@152 47 kd = derivativeGain;
giuliomoro@152 48 }
giuliomoro@152 49 void Pid::setIntegralGain(float integralGain){
giuliomoro@152 50 ki = integralGain;
giuliomoro@152 51 }
giuliomoro@152 52 void Pid::setGlobalGain(float globalGain){
giuliomoro@152 53 gain = globalGain;
giuliomoro@152 54 }
giuliomoro@152 55 float Pid::getProportionalGain(){
giuliomoro@152 56 return kp;
giuliomoro@152 57 }
giuliomoro@152 58 float Pid::getDerivativeGain(){
giuliomoro@152 59 return kd;
giuliomoro@152 60 }
giuliomoro@152 61 float Pid::getIntegralGain(){
giuliomoro@152 62 return ki;
giuliomoro@152 63 }
giuliomoro@152 64 float Pid::getGlobalGain(){
giuliomoro@152 65 return gain;
giuliomoro@152 66 }
giuliomoro@152 67 float Pid::getIntegralError(){
giuliomoro@152 68 return integralError;
giuliomoro@152 69 }