alterae

🏳️‍⚧️

  • it/its, she/her

former government employee, CS major, ski lift operator, and summer camp counselor. currently shooting for EMT certification


homepage (old)
alterae.github.io/
other homepage (largely deprecated)
alterae.srht.site/
reposted writings, something blog-ish
alterae.prose.sh/
fediverse (again!)
tech.lgbt/@alterae

i'm messing around with Godot again, and i made the mistake of using a rigid body for the player character. turns out rotating a body using forces is actually kinda tricky.

had to learn to write an entire PID controller, which was very confusing but thankfully turned out pretty simple

source code below the cut


/// <summary>
///     A proportional-integral-derivative controller.
/// </summary>
/// <param name="Kp">Proportional gain.</param>
/// <param name="Ki">Integral gain.</param>
/// <param name="Kd">Derivative gain.</param>
public record Pid(float Kp, float Ki, float Kd)
{
    private float PreviousError { get; set; }
    private float Integral { get; set; }
    private float Derivative { get; set; }

    /// <summary>
    ///     Iterates the controller and returns the next output value.
    ///     TODO: Implement support for clamping the output.
    /// </summary>
    /// <param name="setPoint">The target state for the controller to reach.</param>
    /// <param name="measuredValue">The current state of the controller.</param>
    /// <param name="delta">The iteration time in seconds.</param>
    /// <returns>The control variable for the controller.</returns>
    public float Next(float setPoint, float measuredValue, float delta)
    {
        var error = setPoint - measuredValue;
        Integral += error * delta;
        Derivative = (error - PreviousError) / delta;
        PreviousError = error;
        return Kp * error + Ki * Integral + Kd * Derivative;
    }
}
syntax highlighting by codehost

You must log in to comment.