-
Notifications
You must be signed in to change notification settings - Fork 0
/
PIDAccumulator.cpp
49 lines (41 loc) · 1.17 KB
/
PIDAccumulator.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
//
// Created by Alex Chi on 2018/10/29.
//
#include "PIDAccumulator.h"
#include "utils.h"
bool PIDAccumulator::set_pid(double Kp, double Ki, double Kd) {
this->Kp = Kp;
this->Ki = Ki;
this->Kd = Kd;
return true;
}
bool PIDAccumulator::set_output(double output_min, double output_max) {
this->output_max = output_max;
this->output_min = output_min;
return true;
}
bool PIDAccumulator::reset() {
prev_error = total_error = 0;
return true;
}
PIDAccumulator::PIDAccumulator() : Kp(0), Ki(0), Kd(0), output_min(0), output_max(0), prev_error(0), total_error(0) {
}
double PIDRateAccumulator::calc(double error) {
if (Kp != 0) {
total_error = clamp(total_error + error,
output_min / Kp,
output_max / Kp);
}
double result = Kd * error + Kp * total_error;
return result;
}
double PIDDisplacementAccumulator::calc(double error) {
if (Ki != 0) {
total_error = clamp(total_error + error,
output_min / Ki,
output_max / Ki);
}
double result = Kp * error + Ki * total_error + Kd * (error - prev_error);
prev_error = error;
return result;
}