Skip to content

Instantly share code, notes, and snippets.

@lashtear
Created August 3, 2017 08:35
Show Gist options
  • Save lashtear/a6fab258af11c8a46582fedb46da5690 to your computer and use it in GitHub Desktop.
Save lashtear/a6fab258af11c8a46582fedb46da5690 to your computer and use it in GitHub Desktop.
pid controller changes for output and integral clamping
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