blob: f37759943b1b314a6e9f99aaa90a7f8adbc42556 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#include "PIDAlgo.h"
float PIDAlgorithmCalculation(float _setPoint,float _mesuredParam , PID_Config_Params *params, float *_pre_error, float *_integral)
{
float error;
float derivative;
float output;
//Calculate P,I,D
error = _setPoint - _mesuredParam;
//In case of error too small then stop integration
if(abs(error) > params->epsilon)
{
*_integral = *_integral + error*params->dt;
}
derivative = (error - *_pre_error)/params->dt;
output = params->Kp*error + params->Ki**_integral + params->Kd*derivative;
//Saturation Filter
if(output > params->MAX)
{
output = params->MAX;
}
else if(output < params->MIN)
{
output = params->MIN;
}
//Update error
*_pre_error = error;
return output;
}
|