blob: f9aee7929b1acde396e2257ac60967df8143f4e2 (
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
#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;
}
float AdvancedPIDAlgorithmCalculation(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->ProportionalErrorMultiplier + params->Ki**_integral/params->IntegralErrorMultiplier + 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;
}
|