Created
August 3, 2017 08:35
-
-
Save lashtear/a6fab258af11c8a46582fedb46da5690 to your computer and use it in GitHub Desktop.
pid controller changes for output and integral clamping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun process(inputs: Array<Double?>, deltaTime: Double): Double { | |
val error = (inputs[0] ?: 0.0) - (inputs[1] ?: 0.0) | |
val deltaError = error - lastError | |
lastError = error | |
errorIntegral += error * deltaTime | |
val result = Kp * error + Ki * errorIntegral + Kd * deltaError / deltaTime | |
if (Ki > 0 && (result > 50.0 || result < 0.0)) { | |
val clamp = Math.max(Math.min(result,50.0),0.0) | |
errorIntegral = (clamp-Kp*error-Kd*deltaError/deltaTime)/Ki | |
return clamp | |
} else { | |
return result | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment