This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from yuliu2016/coroutine-actions
Refactor to prepare for merging with 2019 Robot Code
- Loading branch information
Showing
54 changed files
with
814 additions
and
633 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
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 |
---|---|---|
@@ -1,39 +1,129 @@ | ||
/** | ||
* Commons.kt --- common constants and functions | ||
*/ | ||
|
||
@file:JvmName("Commons") | ||
|
||
package ca.warp7.frc | ||
|
||
import kotlin.math.abs | ||
import kotlin.math.sign | ||
|
||
fun Double.epsilonEquals(other: Double, epsilon: Double) = this - epsilon <= other && this + epsilon >= other | ||
const val kFeetToMetres: Double = 0.3048 | ||
|
||
const val kInchesToMetres: Double = 0.0254 | ||
|
||
const val kMetresToFeet: Double = 1 / kFeetToMetres | ||
|
||
const val kMetresToInches: Double = 1 / kInchesToMetres | ||
|
||
/** | ||
* Check if a number is close enough to another by [epsilon] | ||
*/ | ||
fun Double.epsilonEquals(other: Double, epsilon: Double) = | ||
(this - epsilon <= other) && (this + epsilon >= other) | ||
|
||
/** | ||
* Check if a number is close enough to another by 1E-12 | ||
*/ | ||
fun Double.epsilonEquals(other: Double) = epsilonEquals(other, 1E-12) | ||
|
||
fun linearInterpolate(a: Double, b: Double, x: Double) = a + (b - a) * x.coerceIn(0.0, 1.0) | ||
/** | ||
* Interpolate between two numbers | ||
* | ||
* This function is undefined if any of its parameters are | ||
* Infinity or NaN | ||
*/ | ||
fun linearInterpolate(a: Double, b: Double, x: Double): Double { | ||
if (x < 0.0) { | ||
return a | ||
} | ||
if (x > 1.0) { | ||
return b | ||
} | ||
return a + (b - a) * x | ||
} | ||
|
||
val Double.f get() = "%.3f".format(this) | ||
val Double.f1 get() = "%.1f".format(this) | ||
|
||
/** | ||
* Limit a value within a magnitude range | ||
* | ||
* @param max the maximum magnitude of the value. Must be positive | ||
*/ | ||
fun Double.limit(max: Double): Double { | ||
if (this > max) { | ||
return max | ||
} | ||
if (this < -max) { | ||
return -max | ||
} | ||
return this | ||
} | ||
|
||
|
||
/** | ||
* Apply a deadband to a value | ||
*/ | ||
fun applyDeadband(value: Double, max: Double, deadband: Double): Double { | ||
val v = value.coerceIn(-max, max) | ||
val v = value.limit(max) | ||
return if (abs(v) > deadband) { | ||
if (v > 0.0) (v - deadband) / (max - deadband) | ||
else (v + deadband) / (max - deadband) | ||
} else 0.0 | ||
if (v > 0.0) { | ||
(v - deadband) / (max - deadband) | ||
} else { | ||
(v + deadband) / (max - deadband) | ||
} | ||
} else { | ||
0.0 | ||
} | ||
} | ||
|
||
const val kFeetToMeters: Double = 0.3048 | ||
|
||
const val kInchesToMeters: Double = 0.0254 | ||
|
||
const val kMetersToFeet: Double = 1 / kFeetToMeters | ||
/** | ||
* Format a number to 3 decimal places | ||
*/ | ||
val Double.f get() = "%.3f".format(this) | ||
|
||
const val kMetersToInches: Double = 1 / kInchesToMeters | ||
/** | ||
* Format a number to 1 decimal places | ||
*/ | ||
val Double.f1 get() = "%.1f".format(this) | ||
|
||
val Number.feet: Double get() = this.toDouble() * kFeetToMeters | ||
/** | ||
* Convert a number in feet into metres | ||
*/ | ||
val Number.feet: Double get() = this.toDouble() * kFeetToMetres | ||
|
||
val Number.inches: Double get() = this.toDouble() * kInchesToMeters | ||
/** | ||
* Convert a number in inches into metres | ||
*/ | ||
val Number.inches: Double get() = this.toDouble() * kInchesToMetres | ||
|
||
/** | ||
* Square a number | ||
*/ | ||
val Double.squared: Double get() = this * this | ||
|
||
/** | ||
* Cube a number | ||
*/ | ||
val Double.cubed: Double get() = this * this * this | ||
|
||
/** | ||
* Square a number and keep the sign | ||
*/ | ||
val Double.squaredWithSign: Double get() = this * this * sign | ||
|
||
fun Boolean.toInt() = if (this) 1 else -1 | ||
/** | ||
* Create an integer sign representation of a boolean | ||
*/ | ||
fun Boolean.toSign() = if (this) 1 else -1 | ||
|
||
/** | ||
* Create a double representation of a boolean | ||
*/ | ||
fun Boolean.toDouble() = if (this) 1.0 else 0.0 | ||
|
||
/** | ||
* Create an integer representation of a boolean | ||
*/ | ||
fun Boolean.toInt() = if (this) 1 else 0 |
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,16 @@ | ||
package ca.warp7.frc | ||
|
||
@Deprecated("", ReplaceWith("ca.warp7.frc.control.CycleCounter")) | ||
typealias CycleCounter = ca.warp7.frc.control.CycleCounter | ||
|
||
@Deprecated("", ReplaceWith("ca.warp7.frc.control.Delta")) | ||
typealias Delta = ca.warp7.frc.control.Delta | ||
|
||
@Deprecated("", ReplaceWith("ca.warp7.frc.control.LatchedBoolean")) | ||
typealias LatchedBoolean = ca.warp7.frc.control.LatchedBoolean | ||
|
||
@Deprecated("", ReplaceWith("ca.warp7.frc.control.MinCycleBoolean")) | ||
typealias MinCycleBoolean = ca.warp7.frc.control.MinCycleBoolean | ||
|
||
@Deprecated("", ReplaceWith(" ca.warp7.frc.control.PID")) | ||
typealias PID = ca.warp7.frc.control.PID |
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 was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
src/main/kotlin/ca/warp7/frc/CycleCounter.kt → ...tlin/ca/warp7/frc/control/CycleCounter.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
2 changes: 1 addition & 1 deletion
2
src/main/kotlin/ca/warp7/frc/Delta.kt → ...main/kotlin/ca/warp7/frc/control/Delta.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package ca.warp7.frc | ||
package ca.warp7.frc.control | ||
|
||
class Delta { | ||
var value = 0.0 | ||
|
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,7 @@ | ||
package ca.warp7.frc.control | ||
|
||
@Deprecated("", ReplaceWith("ca.warp7.frc.inputs.ButtonState")) | ||
typealias ControllerState = ca.warp7.frc.inputs.ButtonState | ||
|
||
@Deprecated("", ReplaceWith("ca.warp7.frc.inputs.RobotController")) | ||
typealias RobotController = ca.warp7.frc.inputs.RobotController |
2 changes: 1 addition & 1 deletion
2
...ain/kotlin/ca/warp7/frc/LatchedBoolean.kt → ...in/ca/warp7/frc/control/LatchedBoolean.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package ca.warp7.frc | ||
package ca.warp7.frc.control | ||
|
||
class LatchedBoolean { | ||
|
||
|
4 changes: 2 additions & 2 deletions
4
...in/kotlin/ca/warp7/frc/MinCycleBoolean.kt → ...n/ca/warp7/frc/control/MinCycleBoolean.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
Oops, something went wrong.