Created
August 12, 2024 01:37
-
-
Save kurtdekker/87ae3d8f79dae48505f5e327445765b8 to your computer and use it in GitHub Desktop.
Filter utilities: a deadband expander
This file contains 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
public static partial class FilterUtilities | |
{ | |
// @kurtdekker | |
// | |
// Part of Jetpack Kurt Space Flight available for iOS/Android | |
// | |
// Evalutes a normalized analog input signal (such as an analog | |
// axis) and filters it: | |
// | |
// If signal is below the deadband in absolute magnitude: | |
// - set signal to zero | |
// - return false, no input present in signal | |
// | |
// If signal is at or above the deadband in absolute magnitude: | |
// - re-zero-scale the signal from 0 to 1 (preserving sign!) | |
// - return true, yes we have input | |
// | |
// The purpose is to allow deadbands without a discontinuity | |
// (eg, no sudden snap from 0.0 to deadband minimum) | |
// | |
// Expected signal input domain is [-1 .. +1]. You are responsible for this. | |
// Expected deadband input is zero or positive, but always less than +1 | |
// | |
public static bool DeadbandAndExpand( ref float axis, float deadband) | |
{ | |
bool haveInput = false; | |
if (axis < 0) | |
{ | |
if (axis > -deadband) | |
{ | |
axis = 0; | |
} | |
else | |
{ | |
haveInput = true; | |
axis = (axis + deadband) / ( 1.0f - deadband); | |
} | |
} | |
else | |
{ | |
if (axis < deadband) | |
{ | |
axis = 0; | |
} | |
else | |
{ | |
haveInput = true; | |
axis = (axis - deadband) / ( 1.0f - deadband); | |
} | |
} | |
return haveInput; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment