forked from r-downing/AutoPID
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoPID.cpp
More file actions
148 lines (124 loc) · 3.68 KB
/
Copy pathAutoPID.cpp
File metadata and controls
148 lines (124 loc) · 3.68 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "AutoPID.h"
AutoPID::AutoPID(double *input, double *setpoint, double *output, double outputMin, double outputMax,
double Kp, double Ki, double Kd) {
_input = input;
_setpoint = setpoint;
_output = output;
_outputMin = outputMin;
_outputMax = outputMax;
setGains(Kp, Ki, Kd);
_timeStep = 1000;
}//AutoPID::AutoPID
void AutoPID::setGains(double Kp, double Ki, double Kd) {
_Kp = Kp;
_Ki = Ki;
_Kd = Kd;
}//AutoPID::setControllerParams
void AutoPID::setBangBang(double bangOn, double bangOff) {
_bangOn = bangOn;
_bangOff = bangOff;
}//void AutoPID::setBangBang
void AutoPID::setBangBang(double bangRange) {
setBangBang(bangRange, bangRange);
}//void AutoPID::setBangBang
void AutoPID::setOutputRange(double outputMin, double outputMax) {
_outputMin = outputMin;
_outputMax = outputMax;
}//void AutoPID::setOutputRange
void AutoPID::setTimeStep(unsigned long timeStep){
_timeStep = timeStep;
}
bool AutoPID::atSetPoint(double threshold) {
return abs(*_setpoint - *_input) <= threshold;
}//bool AutoPID::atSetPoint
void AutoPID::run() {
if (_stopped) {
_stopped = false;
reset();
}
if(!_paused) {
//if bang thresholds are defined and we're outside of them, use bang-bang control
if (_bangOn && ((*_setpoint - *_input) > _bangOn)) {
*_output = _outputMax;
_lastStep = millis();
} else if (_bangOff && ((*_input - *_setpoint) > _bangOff)) {
*_output = _outputMin;
_lastStep = millis();
} else { //otherwise use PID control
unsigned long _dT = millis() - _lastStep; //calculate time since last update
if (_dT >= _timeStep) { //if long enough, do PID calculations
_lastStep = millis();
_ran = true;
double _error = *_setpoint - *_input;
//Serial.print("Error: ");
//Serial.println(_error);
_integral += (_error + _previousError) / 2 * _dT / 1000.0; //Riemann sum integral
//_integral = constrain(_integral, _outputMin/_Ki, _outputMax/_Ki);
//Serial.print("Integral: ");
//Serial.println(_integral);
double _dError = (_error - _previousError) / _dT / 1000.0; //derivative
//Serial.print("dError: ");
//Serial.println(_dError);
_previousError = _error;
double PID = (_Kp * _error) + (_Ki * _integral) + (_Kd * _dError);
Serial.print(*_input);
Serial.print(",");
Serial.print(_Kp);
Serial.print(",");
Serial.print(_Ki);
Serial.print(",");
Serial.print(_Kd);
//Serial.print(",");
//Serial.print(_error);Serial.print(",");Serial.print(_integral);Serial.print(",");Serial.print(_dError);
Serial.print(",");
Serial.println(PID);
//*_output = _outputMin + (constrain(PID, 0, 1) * (_outputMax - _outputMin));
*_output = constrain(PID, _outputMin, _outputMax);
//*_output = 50;
}
}
}
}//void AutoPID::run
void AutoPID::pause() {
_paused = true;
}
void AutoPID::unpause() {
_lastStep = millis();
_paused = false;
}
void AutoPID::stop() {
_stopped = true;
reset();
}
void AutoPID::reset() {
_lastStep = millis();
_integral = 0;
_previousError = 0;
}
bool AutoPID::isPaused(){
return _paused;
}
bool AutoPID::isStopped(){
return _stopped;
}
bool AutoPID::hasRun(){
if(_ran == true) {
_ran = false;
return true;
}
return false;
}
double AutoPID::getIntegral(){
return _integral;
}
void AutoPID::setIntegral(double integral){
_integral = integral;
}
void AutoPIDRelay::run() {
AutoPID::run();
while ((millis() - _lastPulseTime) > _pulseWidth) _lastPulseTime += _pulseWidth;
*_relayState = ((millis() - _lastPulseTime) < (_pulseValue * _pulseWidth));
}
double AutoPIDRelay::getPulseValue(){
return (isStopped()?0:_pulseValue);
}