-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPID_Test.cpp
120 lines (93 loc) · 3.06 KB
/
PID_Test.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
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
#include <iostream>
#include <stdio.h>
#include <cmath>
//#include "liftHeight.cpp"
#define PRESET_POWER .75 //power per inch
#define MANUAL_POWER .45
using namespace std;
/*
SetPoint: How high I want the lift to go
PID Controller: Calculates how much the lift should move (power)
Controller Input: The value that the PID Controller gives
Disturbances: Weight on lift; Snags in chain; Momentum
Process: Speed that lift will go to change its height
Measured Process Variable: Speed of lift
Process Variable: Current height of lift
Sensor: Encoders
Disturbances
↓
Set_Point → PID_Controller → Controller_Input → Process → Process_Variable
↑ ↓
Measured_Process_Variable ← ← ← ← ← ← ← Sensor
→ → Error (SP - PV) → → Proportional → →
↑ ↑
Set_Point → → Error (SP - PV) → → Integral → → → → → Add all Three (Or all Used) → → Controller_Input
↓ ↑
→ → Error (SP - PV) → → Derivative → → →
*/
struct PID_Values {
const double p = 0.1, //Proportional
i = 0.0, //Integral
d = 0.0; //Derivative
};
struct PID_Controller {
double p = 0.0, //Proportional
i = 0.0, //Integral
d = 0.0; //Derivative
};
double Proportional(double error, PID_Values pid) {
return pid.p * error;
}
double Integral(double error, PID_Values pid, double rate, double integral) {
integral += rate * error;
return integral * pid.i;
}
double Derivative(double error, PID_Values pid) {
return 0.0;
}
PID_Controller PID(double error, PID_Values pid, double rate, double integral) {
PID_Controller test;
test.p = Proportional(error, pid);
test.i = Integral(error, pid, rate, integral);
test.d = Derivative(error, pid);
return test;
}
#ifdef PID_TEST
int main() {
double currentHeight = 0,
desiredHeight = 0,
set_point = 0.0,
controller_input = 0.0,
disturbances = 0.0,
measured_process_variable = 0.0,
process = 0.0,
process_variable = 0.0,
error = 0.0,
power = 0.0,
integral = 0.0;
PID_Values original;
PID_Controller value;
cout << endl << "Enter current height (0 - 100): ";
cin >> currentHeight;
cout << "Enter desired height(0 - 100): ";
cin >> desiredHeight;
set_point = desiredHeight;
process_variable = currentHeight;
//3.5 seconds to go 56 inches
for(int i = 0; i < 3500; i++) { //milliseconds
error = set_point - process_variable;
value = PID(error, original, 1.0, integral);
integral += value.i;
power = value.p + value.i + value.d;
if (process_variable > set_point) {
power = -original.p;
}
else {
power = original.p;
}
process_variable+= (power * 1.00);
cout << endl << "Time Step: " << i +1 << endl << "Error: " << error << endl << "Power: " << power << endl << "Current Height: " << process_variable << endl;
}
return 0;
}
#endif