-
Notifications
You must be signed in to change notification settings - Fork 1
/
RobotSupport.cpp
69 lines (63 loc) · 1.13 KB
/
RobotSupport.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
struct VSPMessage
{
float rotationSpeed;
float angle;
float distance;
float offset;
VSPMessage()
{
rotationSpeed = 0.0;
angle = 0.0;
distance = 0.0;
offset = 0.0;
}
};
class PIDScale
{
public:
float p;
float i;
float d;
float CurrentMotorValue;
float totalError;
float previousError;
float maxOut;
float minOut;
PIDScale(float P, float I, float D, float Scale)
{
p = P;
i = I;
d = D;
totalError = 0.0;
previousError = 0.0;
scale = Scale;
CurrentMotorValue = 0.0;
maxOut = 1.0;
minOut = 0.0;
}
float CalculateChange(float CurrentValue, float TargetValue)
{
float error = (TargetValue - CurrentValue) / scale;
/*
if((totalError + error) * i < 1 &&
((totalError + error) * i) > 0)
{
*/
totalError += error;
/* }*/
float retError = p * error + i * totalError + d * (error - previousError);
previousError = error;
float result = retError / scale;
/*if (result > maxOut){ result = maxOut;}
else if (result < minOut){ result = minOut;}
*/
return result;
}
void zeroOut() {
totalError = 0.0;
previousError = 0.0;
CurrentMotorValue = 0.0;
}
private:
float scale;
};