-
Notifications
You must be signed in to change notification settings - Fork 0
/
pid.c
executable file
·92 lines (77 loc) · 1.42 KB
/
pid.c
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
task RPM() {
float LastValue;
while(true) {
Value = SensorValue[flywheelEncoder];
FlyRPM = Value - LastValue;
LastValue = Value;
wait1Msec(50);
}
}
task error() {
while (true) {
CurrentValue = FlyRPM;
Error = Target - CurrentValue;
wait1Msec(50);
LastError = Error;
}
}
task Flywheel(){
while(true) {
Integral += 50 * ((Error + LastError)/2);
if(Error == 0) {
Integral = 0;
}
if(fabs(Integral) > integralCap) {
Integral = integralCap;
}
Derivative = kd * (Error - LastError)/50;
speed = kp * Error + ki * Integral + kd * Derivative;
if(speed < 0)
speed = 45;
if (speed > 127)
speed = 127;
if (Error < 0)
speed = 20;
wait1Msec(50);
}
}
task bangBang() {
while (true) {
if (Error >= 0)
speed = maxSpeed;
else
speed = minSpeed;
}
}
task motorrun() {
while(true) {
motor[fly1] = speed;
motor[fly2] = speed;
}
}
task toneBack() {
while (true) {
tone = FlyRPM * tm;
playTone (tone, 1);
}
}
//task bangbang() {
// while(true) {
// while(flyTarget != 0) {
// if(estimate < flyTarget) {
// output = 127;
// }
// else if(estimate >= flyTarget) {
// if(flyTarget < 440) {
// output = 10;
// }
// if(flyTarget < 575 && flyTarget >= 440) {
// output = 30;
// }
// if(flyTarget >= 575) {
// output = 60;
// }
// }
// }
// }
//}