This repository was archived by the owner on Sep 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPID loop
More file actions
66 lines (49 loc) · 1.3 KB
/
PID loop
File metadata and controls
66 lines (49 loc) · 1.3 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
#include <iostream>
using namespace std;
class PID{
//scale factors
double pScale = 0.0;
double iScale = 0.0;
double dScale = 0.0;
//holding values
double iTemp = 0;
double dTemp = 0;
//integrator max and min
double iMax = 300;
double iMin = -300;
//Variable terms
double pTerm = 0;
double iTerm = 0;
double dTerm = 0;
double pid = 0; //these terms and above need to be inisalized outside the loop
// Constructor
//PID(); // Constructor, does nothing now
// Class methods
public:
double update(double currVal, double idealVal);
void print() {cout << pid << endl;}
};
double PID::update(double currVal, double idealVal){
//finding error
double errorValue = currVal - idealVal; //currentValue is from sensors, idealValue is the wanted position
pTerm = pScale * errorValue;
dTerm = errorValue - dTemp;
dTemp = errorValue;
iTemp = iTemp + errorValue;
if (iTemp > iMax)
iTemp = iMax;
if (iTemp < iMin)
iTemp = iMin;
iTerm = iScale * iTemp;
pid = pTerm + iTerm + dTerm;
return pid;
}
int main(){
PID myPid;
//need to insert error value and desired valur into these arguments
double arg1 = 0.5;
double arg2 = 0.0;
myPid.update(arg1, arg2);
myPid.print();
return 0;
}