-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPIDController.cpp
More file actions
56 lines (42 loc) · 1.11 KB
/
PIDController.cpp
File metadata and controls
56 lines (42 loc) · 1.11 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
#include "PIDController.h"
#include <Arduino.h>
PIDController::PIDController(float kp, float ki, float kd, float max_error, float min, float max) {
this->kp = kp; this->ki = ki; this->kd = kd;
this->min_error = min_error; this->min = min; this->max = max;
};
void PIDController::setConstants(float kp, float ki, float kd) {
this->kp = kp; this->ki = ki; this->kd = kd;
}
float PIDController::run(float currentVal, float targetVal) {
int err;
int delta_err;
int p_out;
int i_out;
int d_out;
int output;
err = targetVal - currentVal;
delta_err = prev_err - err;
if (abs(err) > this->min_error) {
integral_err +=err;
}
/*
if (integral_err > this->max_error) {
integral_err = max_error;
}
else if (integral_err < abs(max_error)) {
integral_err = max_error * -1;
}
*/
p_out = err * this->kp;
i_out = integral_err * this->ki;
d_out = delta_err * this->kd;
output = p_out + i_out + d_out;
if (output > this->max) {
output = this->max;
}
else if (output < this->min) {
output = this->min;
}
this->prev_err = err;
return output;
}