Skip to content

Commit

Permalink
units fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sswadkar committed Mar 5, 2023
1 parent c2b7d3e commit 28c8678
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 30 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ publishing {
release(MavenPublication) {
groupId = 'org.team4099'
artifactId = 'falconutils'
version = '1.1.15'
version = '1.1.16'

from(components["kotlin"])
}
Expand Down
56 changes: 35 additions & 21 deletions src/main/kotlin/org/team4099/lib/units/MechanismUnits.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@ import org.team4099.lib.units.derived.radians
import org.team4099.lib.units.derived.rotations
import kotlin.math.PI

enum class Timescale(val velocity: Time, val acceleration: Time, val pidTimeConversion: (Time) -> Double) {
REV_NEO(1.minutes, 1.seconds, {it.inMilliseconds}),
CTRE(100.milli.seconds, 1.seconds, {it.inSeconds}),
enum class Timescale(
val velocity: Time,
val acceleration: Time,
val pidTimeConversion: (Time) -> Double
) {
REV_NEO(1.minutes, 1.seconds, { it.inMilliseconds }),
CTRE(100.milli.seconds, 1.seconds, { it.inSeconds }),
}

interface MechanismSensor<U : UnitKey> {
Expand Down Expand Up @@ -125,9 +129,10 @@ class LinearMechanismSensor(
derivativeGain: DerivativeGain<Meter, Volt>,
): Double {
return (
derivativeGain.inVoltsPerMeterPerSecond * timescale.pidTimeConversion(timescale.velocity) /
positionToRawUnits(1.meters)
) / compensationVoltage.inVolts * fullPowerThrottle
derivativeGain.inVoltsPerMeterPerSecond *
timescale.pidTimeConversion(timescale.velocity) / positionToRawUnits(1.meters)
) /
compensationVoltage.inVolts * fullPowerThrottle
}

override inline fun proportionalVelocityGainToRawUnits(
Expand All @@ -142,18 +147,22 @@ class LinearMechanismSensor(
): Double {
return (
integralGain.inVoltsPerMeters /
(velocityToRawUnits(1.meters.perSecond) * timescale.pidTimeConversion(timescale.velocity))
) /
compensationVoltage.inVolts * fullPowerThrottle
(
velocityToRawUnits(1.meters.perSecond) *
timescale.pidTimeConversion(timescale.velocity)
)
) / compensationVoltage.inVolts *
fullPowerThrottle
}

override inline fun derivativeVelocityGainToRawUnits(
derivativeGain: DerivativeGain<Velocity<Meter>, Volt>,
): Double {
return (
derivativeGain.inVoltsPerMetersPerSecondPerSecond * timescale.pidTimeConversion(timescale.velocity) /
velocityToRawUnits(1.meters.perSecond)
) / compensationVoltage.inVolts * fullPowerThrottle
derivativeGain.inVoltsPerMetersPerSecondPerSecond *
timescale.pidTimeConversion(timescale.velocity) / velocityToRawUnits(1.meters.perSecond)
) /
compensationVoltage.inVolts * fullPowerThrottle
}

override inline fun velocityFeedforwardToRawUnits(
Expand Down Expand Up @@ -212,9 +221,10 @@ class AngularMechanismSensor(
derivativeGain: DerivativeGain<Radian, Volt>
): Double {
return (
derivativeGain.inVoltsPerRadianPerSecond * timescale.pidTimeConversion(timescale.velocity) /
positionToRawUnits(1.radians)
) / compensationVoltage.inVolts * fullPowerThrottle
derivativeGain.inVoltsPerRadianPerSecond *
timescale.pidTimeConversion(timescale.velocity) / positionToRawUnits(1.radians)
) /
compensationVoltage.inVolts * fullPowerThrottle
}

override inline fun proportionalVelocityGainToRawUnits(
Expand All @@ -231,18 +241,22 @@ class AngularMechanismSensor(
): Double {
return (
integralGain.inVoltsPerRadians /
(velocityToRawUnits(1.radians.perSecond) * timescale.pidTimeConversion(timescale.velocity))
) /
compensationVoltage.inVolts * fullPowerThrottle
(
velocityToRawUnits(1.radians.perSecond) *
timescale.pidTimeConversion(timescale.velocity)
)
) / compensationVoltage.inVolts *
fullPowerThrottle
}

override inline fun derivativeVelocityGainToRawUnits(
derivativeGain: DerivativeGain<Velocity<Radian>, Volt>
): Double {
return (
derivativeGain.inVoltsPerRadiansPerSecondPerSecond * timescale.pidTimeConversion(timescale.velocity) /
velocityToRawUnits(1.radians.perSecond)
) / compensationVoltage.inVolts * fullPowerThrottle
derivativeGain.inVoltsPerRadiansPerSecondPerSecond *
timescale.pidTimeConversion(timescale.velocity) / velocityToRawUnits(1.radians.perSecond)
) /
compensationVoltage.inVolts * fullPowerThrottle
}

override inline fun velocityFeedforwardToRawUnits(
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/org/team4099/lib/units/derived/Controller.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ inline val <K : UnitKey> Value<K>.perMeter
get() = Value<Fraction<K, Meter>>(value)

inline val <K : UnitKey> Value<K>.perInch
get() = perMeter / METERS_PER_INCH
get() = perMeter * METERS_PER_INCH

inline val <K : UnitKey> Value<K>.perFoot
get() = perMeter / METERS_PER_FOOT
get() = perMeter * METERS_PER_FOOT

inline val <K : UnitKey> Value<K>.perRadian
get() = Value<Fraction<K, Radian>>(value)

inline val <K : UnitKey> Value<K>.perDegree
get() = perRadian / RADIANS_PER_DEGREES
get() = perRadian * RADIANS_PER_DEGREES

inline val <K : UnitKey> Value<K>.perRotation
get() = perRadian / RADIANS_PER_ROTATION
get() = perRadian * RADIANS_PER_ROTATION

inline val <K : UnitKey> Value<K>.perMeterSeconds
get() = Value<Fraction<K, Product<Meter, Second>>>(value)
Expand Down
8 changes: 4 additions & 4 deletions src/test/kotlin/team4099/controller/PIDControllerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import org.team4099.lib.units.base.meters
import org.team4099.lib.units.base.seconds
import org.team4099.lib.units.derived.ProportionalGain
import org.team4099.lib.units.derived.degrees
import org.team4099.lib.units.derived.inDegreesPerSecondPerDegreesPerSecond
import org.team4099.lib.units.derived.inVoltsPerDegree
import org.team4099.lib.units.derived.inVoltsPerRotation
import org.team4099.lib.units.derived.inVoltsPerRotationPerMinute
import org.team4099.lib.units.derived.inVoltsPerRotationsPerMinutePerSecond
import org.team4099.lib.units.derived.metersPerSecondPerMetersPerSecond
import org.team4099.lib.units.derived.radiansPerSecondPerRadiansPerSecond
import org.team4099.lib.units.derived.perDegree
import org.team4099.lib.units.derived.rotations
import org.team4099.lib.units.derived.volts
import org.team4099.lib.units.perMinute
Expand Down Expand Up @@ -75,9 +75,9 @@ class PIDControllerTest {

@Test
fun testConversion() {
val kp = (0.1.degrees.perSecond / (1.degrees / 1.seconds)).radiansPerSecondPerRadiansPerSecond
val kp = 0.4.volts.perDegree

println(kp.inDegreesPerSecondPerDegreesPerSecond)
println(kp.inVoltsPerDegree)
}

@Test
Expand Down

0 comments on commit 28c8678

Please sign in to comment.