-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPID.kt
More file actions
29 lines (22 loc) · 851 Bytes
/
PID.kt
File metadata and controls
29 lines (22 loc) · 851 Bytes
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
package org.firstinspires.ftc.teamcode
class PID(private val maxI: Double) {
private var currentTime: Long = 0
private var currentError: Double = 0.0
private var previousError: Double = 0.0
private var previousTime: Long = 0
var p: Double = 0.0
var i: Double = 0.0
var d: Double = 0.0
fun step(current: Double, desired: Double): Double {
currentTime = System.currentTimeMillis()
currentError = desired - current
p = PIDConstants.Kp * currentError
i += PIDConstants.Ki * (currentError * (currentTime - previousTime))
if (i > maxI) i = maxI
else if(i < -maxI) i = -maxI
d = PIDConstants.Kd * (currentError * previousError) / (currentTime - previousTime)
previousError = currentError
previousTime = currentTime
return p + i + d
}
}