-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSimplePID.cpp
61 lines (57 loc) · 1.67 KB
/
SimplePID.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
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "SimpleDriver.h"
PID::PID(double kp, double ki, double kd, double sp, double dt)
: kp(kp), ki(ki), kd(kd), setPoint(sp), dt(dt)
{
}
void PID::setSetPoint(double setPoint){
this->setPoint = setPoint;
}
void PID::setInput(double input){
this->input = input;
}
void PID::setKp(double kp){
this->kp = kp;
}
void PID::setKi(double ki){
this->ki = ki;
}
void PID::setKd(double kd){
this->kd = kd;
}
void PID::setSampleTime(double sampleTime){
this->sampleTime = sampleTime;
}
double PID::compute(double input){
error = setPoint - input;
integral = integral + error * dt;
if (integral > maxI){
integral = maxI;
}
else if (integral < -maxI){
integral = -maxI;
}
derivative = (error - last_e) / dt;
if (derivative > maxD){
derivative = maxD;
}
else if (derivative < -maxD){
derivative = -maxD;
}
output = kp * error + ki * integral + kd * derivative;
last_e = error;
return output;
}
double PID::getError(){
return error;
}
double PID::getOutput(){
return output;
}