Skip to content

Instantly share code, notes, and snippets.

@moppius
Created April 2, 2022 20:20
Show Gist options
  • Save moppius/da35386dd212797116bcb47153fdc568 to your computer and use it in GitHub Desktop.
Save moppius/da35386dd212797116bcb47153fdc568 to your computer and use it in GitHub Desktop.
PID Controller for Unreal Angelscript
struct FPIDController
{
UPROPERTY(Category = "PID Controller")
const float ProportionalMultiplier = 1.f;
UPROPERTY(Category = "PID Controller")
const float IntegralMultiplier = 0.f;
UPROPERTY(Category = "PID Controller")
const float DerivativeMultiplier = 0.f;
UPROPERTY(Category = "PID Controller")
const float Bias = 0.f;
private float LastError = 0.f;
private float LastIntegral = 0.f;
float Update(float DesiredValue, float CurrentValue, float DeltaSeconds)
{
const float Error = DesiredValue - CurrentValue;
const float Integral = LastIntegral + Error * DeltaSeconds;
const float Derivative = (Error - LastError) / DeltaSeconds;
const float Output = (
ProportionalMultiplier * Error
+ IntegralMultiplier * Integral
+ DerivativeMultiplier * Derivative
+ Bias
);
LastError = Error;
LastIntegral = Integral;
return Output;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment