-
Notifications
You must be signed in to change notification settings - Fork 0
/
PIDVelocityMotor.cpp
79 lines (66 loc) · 2.22 KB
/
PIDVelocityMotor.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
//PIDVelocityMotor.cpp
//Includes
#include <cmath>
#include <ctime>
#include "PIDVelocityMotor.h"
//Constructor
PIDVelocityMotor::PIDVelocityMotor(char *_name,
Victor &_motor,
Encoder &_encoder,
const double _pid[]) : name(_name),
motor(_motor),
encoder(_encoder),
target(0.0),
error(0.0),
deltaTime(0.0),
kp(_pid[0]),
ki(_pid[1]),
kd(_pid[2]),
maxVel(_pid[3]),
integral(0.0),
derivative(0.0),
out(0.0),
lastError(0.0),
lastTime(0)
{
}
//Destructor
PIDVelocityMotor::~PIDVelocityMotor() {}
//Functions
//PIDMotor control
void PIDVelocityMotor::run() {
if(time(NULL) - 10 > lastTime) {
error = target - (encoder.GetRate() / 60);
if(target == 0.0) {
integral = 0.0;
}
deltaTime = time(NULL) - lastTime;
integral += error * deltaTime;
derivative = (error - lastError) / deltaTime;
out = (kp * error) + (ki * integral) + (kd * derivative);
//TODO: Fix!
//motor.SetSpeed(out / maxVel);
printf("Name: %s KP: %f Target: %f CurrentRPM: %f Error: %f Get(): %d Out: %f\n", name, kp, target, (encoder.GetRate() / 60), error, encoder.Get(), out);
lastError = error;
lastTime = time(NULL);
}
}
//PIDMotor control
void PIDVelocityMotor::run(double rpm) {
target = rpm;
run();
}
bool PIDVelocityMotor::atTarget() {
if(abs(error - target) <= 5) {
return true;
} else {
return false;
}
}
void PIDVelocityMotor::Set(double speed, UINT8 syncGroup) {
run(speed);
}
double PIDVelocityMotor::Get() {
return target;
}
void PIDVelocityMotor::Disable() {}