-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcurrentPID.cpp
More file actions
162 lines (127 loc) · 3.84 KB
/
Copy pathconcurrentPID.cpp
File metadata and controls
162 lines (127 loc) · 3.84 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
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "concurrentPID.h"
//***************************PID class***********************//
void PID::setPIDvalues(float kp, float ki, float kd) {
this->kp = kp;
this->ki = ki;
this->kd = kd;
}
void PID::setTolerance(float tolerance){
this->tolerance = tolerance;
}
void PID::setTimeRequired(long time){
this->timeRequired = time;
}
void PID::setGoal(float goal) {
this->goal = goal;
}
bool PID::goalReached() {
// Check if the input (current state) is within tolerance of the goal
return fabs(goal - inputFunction()) <= tolerance;
}
bool PID::goalReachedForT() {
// Ensure the error is below tolerance for the required time
return goalReached() && (timeBelowTolerance >= timeRequired);
}
void PID::calculate() {
unsigned long currentTime = millis();
float dt = (currentTime - prevT) / 1000.0f;
float input = inputFunction();
float proportional = goal - input;
float derivative = (proportional - previousProportional) / dt;
integral += proportional * dt;
float output = kp * proportional + kd * derivative + ki * integral;
outputFunction(output);
prevT = currentTime;
previousProportional = proportional;
// Time tracking logic
if (goalReached()) {
if (timeBelowTolerance == 0) {
startTime = currentTime;
}
timeBelowTolerance = currentTime - startTime;
} else {
timeBelowTolerance = 0;
}
}
void PID::start() {
while (!goalReachedForT()) {
calculate();
delay(10); // Small delay to avoid maxing out CPU
}
}
//******************************ConcurrentPID class***************************//
ConcurrentPID::ConcurrentPID(PID* vec[], int numberOfPIDs) : numPIDs(numberOfPIDs) {
PIDmatrix = new PID*[numPIDs];
for (int i = 0; i < numPIDs; i++) {
PIDmatrix[i] = vec[i];
}
}
ConcurrentPID::~ConcurrentPID() {
delete[] PIDmatrix;
}
void ConcurrentPID::setPIDvalues(float kp, float ki, float kd) {
for (int PIDnum = 0; PIDnum < numPIDs; PIDnum++) {
PIDmatrix[PIDnum]->setPIDvalues(kp, ki, kd);
}
}
void ConcurrentPID::setPIDvalues(int PIDnum, float kp, float ki, float kd) {
if (PIDnum >= 0 && PIDnum < numPIDs) {
PIDmatrix[PIDnum]->setPIDvalues(kp, ki, kd);
}
}
void ConcurrentPID::setTolerance(float tolerance) {
for (int PIDnum = 0; PIDnum < numPIDs; PIDnum++) {
PIDmatrix[PIDnum]->setTolerance(tolerance);
}
}
void ConcurrentPID::setTolerance(int PIDnum, float tolerance) {
if (PIDnum >= 0 && PIDnum < numPIDs) {
PIDmatrix[PIDnum]->setTolerance(tolerance);
}
}
void ConcurrentPID::setTimeRequired(long time) {
for (int PIDnum = 0; PIDnum < numPIDs; PIDnum++) {
PIDmatrix[PIDnum]->setTimeRequired(time);
}
}
void ConcurrentPID::setTimeRequired(int PIDnum, long time) {
if (PIDnum >= 0 && PIDnum < numPIDs) {
PIDmatrix[PIDnum]->setTimeRequired(time);
}
}
void ConcurrentPID::setGoal(float goal) {
for (int PIDnum = 0; PIDnum < numPIDs; PIDnum++) {
PIDmatrix[PIDnum]->setGoal(goal);
}
}
void ConcurrentPID::setGoal(int PIDnum, float goal) {
if (PIDnum >= 0 && PIDnum < numPIDs) {
PIDmatrix[PIDnum]->setGoal(goal);
}
}
void ConcurrentPID:: setPIDdelay(float time){
delayConst = time;
}
void ConcurrentPID::start() {
while (true) {
bool allDone = true;
calculate(allDone);
if (allDone) {
break; // Exit if all PIDs are within tolerance
}
delay(delayConst); // Small delay to avoid maxing out CPU
}
}
void ConcurrentPID::calculate(bool &allDone) {
for (int i = 0; i < numPIDs; i++) {
PIDmatrix[i]->calculate();
if (!PIDmatrix[i]->goalReachedForT()) {
allDone = false;
}
}
}
void ConcurrentPID::calculate() {
for (int i = 0; i < numPIDs; i++) {
PIDmatrix[i]->calculate();
}
}