forked from FIRST-Tech-Challenge/FtcRobotController
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3272606
commit d5a3b14
Showing
7 changed files
with
157 additions
and
13 deletions.
There are no files selected for viewing
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
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
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
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
18 changes: 18 additions & 0 deletions
18
TeamCode/src/main/java/org/firstinspires/ftc/teamcode/mmooover/Speed2Power.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.firstinspires.ftc.teamcode.mmooover | ||
|
||
import kotlin.math.abs | ||
|
||
class Speed2Power(val thresh: Double) { | ||
companion object { | ||
const val EPSILON = 0.001 | ||
} | ||
|
||
constructor(thresh: Number) : this(thresh.toDouble()) | ||
|
||
fun speed2power(speed: Double) = when { | ||
abs(speed) < EPSILON -> 0.0 | ||
speed > 0 -> thresh + ((1 - thresh) * speed) | ||
speed < 0 -> -thresh + ((1 - thresh) * speed) | ||
else -> throw IllegalArgumentException() | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
TeamCode/src/test/java/org/firstinspires/ftc/teamcode/test/FloatUtil.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.firstinspires.ftc.teamcode.test | ||
|
||
import kotlin.math.abs | ||
|
||
object FloatUtil { | ||
const val EPS = 1e-6 | ||
|
||
@JvmStatic | ||
@JvmOverloads | ||
fun assertClose(a: Number, b: Number, lazyMessage: (() -> Any)? = null) { | ||
val message = lazyMessage ?: { | ||
String.format("Expected %.4f to be approximately %.4f", a.toDouble(), b.toDouble()) | ||
} | ||
val diff = abs(a.toDouble() - b.toDouble()) | ||
assert(diff < EPS, message) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
TeamCode/src/test/java/org/firstinspires/ftc/teamcode/test/TestSpeedToPower.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.firstinspires.ftc.teamcode.test | ||
|
||
import org.firstinspires.ftc.teamcode.mmooover.Speed2Power | ||
import org.junit.jupiter.api.Test | ||
|
||
class TestSpeedToPower { | ||
companion object { | ||
val zeroThresh = Speed2Power(0.0) | ||
val oneThresh = Speed2Power(1.0) | ||
} | ||
@Test | ||
fun zeroThreshZeroSpeed() { | ||
FloatUtil.assertClose(zeroThresh.speed2power(0.0), 0.0) | ||
} | ||
@Test | ||
fun zeroThreshFullSpeed() { | ||
FloatUtil.assertClose(zeroThresh.speed2power(1.0), 1.0) | ||
FloatUtil.assertClose(zeroThresh.speed2power(-1.0), -1.0) | ||
} | ||
@Test | ||
fun oneThreshAlways1() { | ||
FloatUtil.assertClose(oneThresh.speed2power(1.0), 1.0) | ||
FloatUtil.assertClose(oneThresh.speed2power(.1), 1.0) | ||
FloatUtil.assertClose(oneThresh.speed2power(-1.0), -1.0) | ||
FloatUtil.assertClose(oneThresh.speed2power(-.1), -1.0) | ||
} | ||
} |