-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPID.cpp
88 lines (71 loc) · 2.02 KB
/
PID.cpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**********************************************************************************************
Arduino PID Library - Version 1.1.1
by Brett Beauregard <[email protected]> brettbeauregard.com
This Library is licensed under a GPLv3 License
**********************************************************************************************/
#include "Arduino.h"
#include <math.h>
#include "PID.h"
#define D_TERM_FILTER_CONSTANT (0.25)
PID::PID(const double& kp, const double& ki, const double& kd )
: kp(kp)
, ki(ki)
, kd(kd)
, i_term(0.0)
, lastInput(NAN)
{
PID::SetOutputLimits(0, 255);
}
void PID::loop(const double& input, const double& setpoint, double& output)
{
p_term = setpoint - input;
i_term += (ki * p_term);
i_term = clamp(i_term);
if(isnan(lastInput)) lastInput = input;
d_term =
(d_term * D_TERM_FILTER_CONSTANT ) +
(1 - D_TERM_FILTER_CONSTANT) * (input - lastInput);
lastInput = input;
output = clamp(kp * p_term + i_term - kd * d_term);
}
double PID::get_p_term()
{
return p_term;
}
double PID::get_i_term()
{
return i_term;
}
double PID::get_d_term()
{
return d_term;
}
/* SetOutputLimits(...)****************************************************
This function will be used far more often than SetInputLimits. while
the input to the controller will generally be in the 0-1023 range (which is
the default already,) the output will be a little different. maybe they'll
be doing a time window and will need 0-8000 or something. or maybe they'll
want to clamp it from 0-125. who knows. at any rate, that can all be done
here.
**************************************************************************/
void PID::SetOutputLimits(double Min, double Max)
{
if (Min > Max) {
outMin = Max;
outMax = Min;
}
else {
outMin = Min;
outMax = Max;
}
}
void PID::Reset()
{
i_term = 0.0;
lastInput = NAN;
}
double PID::clamp(const double& input) {
if (input > outMax) return outMax;
else if (input < outMin) return outMin;
else return input;
}