-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPIDPositionMotor.cpp
110 lines (99 loc) · 2.52 KB
/
PIDPositionMotor.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
//PIDPositionMotor.cpp
//Includes
#include <cmath>
#include "PIDPositionMotor.h"
//Constructor
PIDPositionMotor::PIDPositionMotor(char * _name,
Victor &_vic,
Encoder &_encoder,
const double _velPID[],
const double _posPID[]) : name(_name),
motor(_name, _vic, _encoder, _velPID),
encoder(_encoder),
position(0.0),
target(0.0),
error(0.0),
deltaTime(0.0),
kp(_posPID[0]),
ki(_posPID[1]),
kd(_posPID[2]),
kf(_posPID[3]),
scalar(_velPID[4]),
proportional(0.0),
integral(0.0),
derivative(0.0),
feedForward(0.0),
pout(0.0),
iout(0.0),
dout(0.0),
fout(0.0),
out(0.0),
lastError(0.0),
lastTime(0)
{
printf("PIDPositionMotor %s created!\n", name);
}
//Destructor
PIDPositionMotor::~PIDPositionMotor() {}
//Functions
//PIDPositionMotor control
void PIDPositionMotor::run() {
if(GetTime() - 0.01 > lastTime) {
position = encoder.GetDistance();
while(position > 360 || position < -360) {
position /= 360;
}
error = target - encoder.GetDistance();
if(target == encoder.GetDistance()) {
error = 0.0;
integral = 0.0;
lastError = 0.0;
}
deltaTime = GetTime() - lastTime;
proportional = error;
integral += error * deltaTime;
derivative = (error - lastError) / deltaTime;
feedForward = target;
pout = kp * proportional;
iout = ki * integral;
dout = kd * derivative;
fout = kf * feedForward;
if(pout > 1.0) {
pout = 1.0;
} else if(pout < -1.0) {
pout = -1.0;
}
if(iout > 1.0) {
iout = 1.0;
integral = iout / ki;
} else if(iout < -1.0) {
iout = -1.0;
integral = iout / ki;
}
if(dout > 1.0) {
dout = 1.0;
} else if(dout < -1.0) {
dout = -1.0;
}
out = pout + iout + dout + fout;
if(out > 1.0) {
out = 1.0;
} else if(out < -1.0) {
out = -1.0;
}
motor.run(out);
printf("Name: %s KP: %f Target: %f CurrentPos: %f Error: %f Get(): %d Out: %f\n", name, kp, target, encoder.GetDistance(), error, encoder.Get(), out);
lastError = error;
lastTime = GetTime();
} else {
//printf("PIDPositionMotor %s waiting... Time: %f\n", name, GetTime());
}
}
//PIDPositionMotor control
void PIDPositionMotor::run(double pos) {
target = pos;
run();
}
char * PIDPositionMotor::getName() {
return name;
}