Skip to content

Commit

Permalink
Reductions in Utils (#35)
Browse files Browse the repository at this point in the history
* added reduciton parameter to conversion methods, added docs, made function explicitly public

* more docs

* Update build.gradle

* Fix issue with reductions math
  • Loading branch information
BBScholar authored Feb 11, 2019
1 parent 1703e60 commit dfb382e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 18 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dependencies {
}

group = 'org.team5499'
version = '2.6.0'
version = '2.6.1'

task sourcesJar(type: Jar) {
from sourceSets.main.allJava
Expand Down
91 changes: 74 additions & 17 deletions src/main/kotlin/org/team5499/monkeyLib/util/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,105 @@ package org.team5499.monkeyLib.util
@SuppressWarnings("MagicNumber")
object Utils {

fun limit(value: Double, limit: Double): Double {
/**
* method that limits input to a certain range.
* @param value value to be limited
* @param limit upper and lower bound of the value
* @return bounded value
*/
public fun limit(value: Double, limit: Double): Double {
return limit(value, -limit, limit)
}

fun limit(value: Double, min: Double, max: Double): Double {
/**
* method that limits input to a certain range.
* @param value value to be limited
* @param min lower bound of the value
* @param max upper bound of the value
* @return bounded value
*/
public fun limit(value: Double, min: Double, max: Double): Double {
return Math.min(max, Math.max(min, value))
}

fun interpolate(a: Double, b: Double, x: Double): Double {
/**
* method to interpolate between 2 doubles
* @param a first value
* @param b second value
* @param x value between the 2 values
* @return interpolated value
*/
public fun interpolate(a: Double, b: Double, x: Double): Double {
val newX = limit(x, 0.0, 1.0)
return a + (b - a) * newX
}

fun inchesPerSecondToEncoderTicksPer100Ms(
/**
* @param encoderTicksPerRotation encoder ticks per 360 degrees of rotation of the encoder shaft
* @param circumference circumference of the object being rotated
* @param inchesPerSecond velocity to be converted
* @param reduction reduction from the encoder shaft to the output shaft
* @return velocity in ticks per 100ms
*/
public fun inchesPerSecondToEncoderTicksPer100Ms(
encoderTicksPerRotation: Int,
wheelCircumference: Double,
ips: Double
circumference: Double,
inchesPerSecond: Double,
reduction: Double = 1.0
): Double {
return inchesToEncoderTicks(encoderTicksPerRotation, wheelCircumference, ips) / 10.0
return inchesToEncoderTicks(encoderTicksPerRotation, circumference, inchesPerSecond, reduction) / 10.0
}

fun encoderTicksPer100MsToInchesPerSecond(
/**
* @param encoderTicksPerRotation encoder ticks per 360 degrees of rotation of the encoder shaft
* @param circumference circumference of the object being rotated
* @param encoderTicksPer100ms velocity to be converted
* @param reduction reduction from the encoder shaft to the output shaft
*/
public fun encoderTicksPer100MsToInchesPerSecond(
encoderTicksPerRotation: Int,
wheelCircumference: Double,
eps: Int
circumference: Double,
encoderTicksPer100ms: Int,
reduction: Double = 1.0
): Double {
return encoderTicksToInches(encoderTicksPerRotation, wheelCircumference, eps) * 10.0
return encoderTicksToInches(encoderTicksPerRotation, circumference, encoderTicksPer100ms, reduction) * 10.0
}

fun inchesToEncoderTicks(encoderTicksPerRotation: Int, wheelCircumference: Double, inches: Double): Int {
return ((encoderTicksPerRotation / wheelCircumference) * inches).toInt()
/**
* @param encoderTicksPerRotation encoder ticks per 360 degrees of rotation of the encoder shaft
* @param circumference circumference of the object being rotated
* @param inches inches of travel to be converted to ticks of travel
* @param reduction reduction from the encoder shaft to the output shaft
*/
public fun inchesToEncoderTicks(
encoderTicksPerRotation: Int,
circumference: Double,
inches: Double,
reduction: Double = 1.0
): Int {
return (encoderTicksPerRotation * inches * reduction / circumference).toInt()
}

fun encoderTicksToInches(encoderTicksPerRotation: Int, wheelCircumference: Double, ticks: Int): Double {
return (wheelCircumference / encoderTicksPerRotation) * ticks
/**
* @param encoderTicksPerRotation encoder ticks per 360 degrees of rotation of the encoder shaft
* @param circumference circumference of the object being rotated
* @param ticks ticks of travel to be converted to inches of travel
* @param reduction reduction from the encoder shaft to the output shaft
*/
public fun encoderTicksToInches(
encoderTicksPerRotation: Int,
circumference: Double,
ticks: Int,
reduction: Double = 1.0
): Double {
return (circumference * ticks) / (encoderTicksPerRotation * reduction)
}

fun talonAngleToDegrees(gyroTicksPerRotation: Int, ticks: Int): Double {
public fun talonAngleToDegrees(gyroTicksPerRotation: Int, ticks: Int): Double {
return (360.0 / gyroTicksPerRotation) * ticks
}

fun degreesToTalonAngle(gyroTicksPerRotation: Int, degrees: Double): Int {
public fun degreesToTalonAngle(gyroTicksPerRotation: Int, degrees: Double): Int {
return ((gyroTicksPerRotation / 360.0) * degrees).toInt()
}
}

0 comments on commit dfb382e

Please sign in to comment.