Skip to content

Commit 55a0ac7

Browse files
committed
Sleep deprivation got to me and I wrote the bang bang controller wrong, fixed now (todo: add tests)
1 parent eb186d2 commit 55a0ac7

2 files changed

Lines changed: 23 additions & 62 deletions

File tree

nextcontrol/src/main/kotlin/dev/nextftc/nextcontrol/feedback/BangBangElement.kt

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,40 @@ package dev.nextftc.nextcontrol.feedback
2121
import dev.nextftc.nextcontrol.KineticState
2222
import 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
*/
3244
class 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
}

nextcontrol/src/test/java/dev/nextftc/nextcontrol/feedback/BangBangElementTest.kt

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)