@@ -21,20 +21,40 @@ package dev.nextftc.nextcontrol.feedback
2121import dev.nextftc.nextcontrol.KineticState
2222import kotlin.math.sign
2323
24+ /* *
25+ * Parameters for a [BangBangElement]
26+ *
27+ * @param gain the constant that is outputted when the error is positive (negated when error is negative)
28+ * @param hysteresis how close to 0 is considered 0
29+ */
30+ data class BangBangParameters @JvmOverloads constructor(
31+ @JvmField var gain : Double ,
32+ @JvmField var hysteresis : Double = 5.0
33+ )
34+
2435/* *
2536 * A [FeedbackElement] that is a Bang Bang controller (when error is positive, output 1, when error is negative, output
2637 * -1, when error is 0, output 0.
2738 *
2839 * @param feedbackType The type of component this controller operates on
40+ * @param gain the constant that the output is multiplied by
2941 *
3042 * @author rowan-mcalpin
3143 */
3244class BangBangElement (
3345 private val feedbackType : FeedbackType ,
46+ private val parameters : BangBangParameters
3447) : FeedbackElement {
3548
36- override fun calculate (error : KineticState ): Double = when (feedbackType) {
37- FeedbackType .POSITION -> sign(error.position)
38- FeedbackType .VELOCITY -> sign(error.velocity)
49+ override fun calculate (error : KineticState ): Double {
50+ val feedbackError = when (feedbackType) {
51+ FeedbackType .POSITION -> error.position
52+ FeedbackType .VELOCITY -> error.velocity
53+ }
54+ return when {
55+ feedbackError >= parameters.hysteresis -> parameters.gain
56+ feedbackError <= - parameters.hysteresis -> - parameters.gain
57+ else -> 0.0
58+ }
3959 }
4060}
0 commit comments