Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #4 from yuliu2016/coroutine-actions
Browse files Browse the repository at this point in the history
Refactor to prepare for merging with 2019 Robot Code
  • Loading branch information
yuliu2016 authored Oct 8, 2019
2 parents 29cd409 + fffde11 commit ffa7a70
Show file tree
Hide file tree
Showing 54 changed files with 814 additions and 633 deletions.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,31 @@
A Kotlin library for common FRC math and control flow

[![Build](https://travis-ci.org/Team865/FRC-Commons-Kotlin.svg?branch=master)](https://travis-ci.org/Team865/FRC-Commons-Kotlin)
[![Release](https://jitpack.io/v/Team865/FRC-Commons-Kotlin.svg)](https://jitpack.io/#Team865/FRC-Commons-Kotlin)
[![Release](https://jitpack.io/v/Team865/FRC-Commons-Kotlin.svg)](https://jitpack.io/#Team865/FRC-Commons-Kotlin)


### Vendor File

Copy this code into `Commons.json` in the `vendordeps` folder of a robot project:

```json
{
"fileName": "Commons.json",
"name": "FRC-Commons-Kotlin",
"version": "v2019.2.0",
"uuid": "ab676553-b602-441f-a38d-f1296eff6538",
"mavenUrls": [
"https://jitpack.io"
],
"jsonUrl": "",
"javaDependencies": [
{
"groupId": "com.github.Team865",
"artifactId": "FRC-Commons-Kotlin",
"version": "v2019.2"
}
],
"jniDependencies": [],
"cppDependencies": []
}
```
5 changes: 3 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repositories {
}

group = "ca.warp7.frc"
version = "2019.2.0"
version = "2019.3.0"

tasks.withType<KotlinCompile> {
kotlinOptions {
Expand All @@ -26,7 +26,8 @@ tasks.withType<KotlinCompile> {
"-Xuse-experimental=kotlin.Experimental",
"-Xallow-kotlin-package",
"-Xno-call-assertions",
"-Xno-param-assertions"
"-Xno-param-assertions",
"-Xinline-classes"
)
kotlinOptions.jvmTarget = "11"
}
Expand Down
14 changes: 7 additions & 7 deletions path-planner/src/main/kotlin/ca/warp7/pathplanner/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package ca.warp7.pathplanner

// Unit conversion constants

const val kFeetToMeters: Double = 0.3048
const val kInchesToMeters: Double = 0.0254
const val kFeetToMetres: Double = 0.3048
const val kInchesToMetres: Double = 0.0254
const val kFeetToInches: Double = 12.0

// Dimension Constants

const val kWheelRadius = 2.95 * kInchesToMeters // m
const val kTurningDiameter = 24.75 * kInchesToMeters // m
const val kWheelRadius = 2.95 * kInchesToMetres // m
const val kTurningDiameter = 24.75 * kInchesToMetres // m

// Kinematic constants

const val kMaxVelocity = 12.0 * kFeetToMeters // m/s
const val kMaxAcceleration = 9.0 * kFeetToMeters // m/s^2
const val kMaxFreeSpeed = 16.5 * kFeetToMeters// m/s
const val kMaxVelocity = 12.0 * kFeetToMetres // m/s
const val kMaxAcceleration = 9.0 * kFeetToMetres // m/s^2
const val kMaxFreeSpeed = 16.5 * kFeetToMetres// m/s

const val kScrubFactor = 1.45
const val kEffectiveWheelBaseRadius = kTurningDiameter / 2 * kScrubFactor // m
Expand Down
20 changes: 11 additions & 9 deletions path-planner/src/main/kotlin/ca/warp7/pathplanner/PathPlanner.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("DEPRECATION")

package ca.warp7.pathplanner

import ca.warp7.frc.*
Expand All @@ -9,8 +11,8 @@ import ca.warp7.frc.path.QuinticSegment2D
import ca.warp7.frc.path.parameterized
import ca.warp7.frc.path.quinticSplinesOf
import ca.warp7.frc.path.sumDCurvature2
import ca.warp7.frc.drive.trajectory.TrajectoryState
import ca.warp7.frc.drive.trajectory.generateTrajectory
import ca.warp7.frc.trajectory.TrajectoryState
import ca.warp7.frc.trajectory.generateTrajectory
import processing.core.PApplet
import processing.core.PConstants
import processing.core.PImage
Expand Down Expand Up @@ -40,7 +42,7 @@ class PathPlanner : PApplet() {
val nAng = Rotation2D.fromDegrees(-1.0)
val reversedRotation = Rotation2D(-1.0, 0.0)

val wheelBaseRadius = kInchesToMeters * 12.4
val wheelBaseRadius = kInchesToMetres * 12.4
val robotLength = wheelBaseRadius * 1.3
val robotDrawCenter = Translation2D(768.0, 100.0)

Expand Down Expand Up @@ -328,7 +330,7 @@ class PathPlanner : PApplet() {
lineTo(left, af)
lineTo(right, bf)
val msg = "ΣΔk²=${curvatureSum.f1} " +
"ΣΔd=${(kMetersToFeet * arcLength).f1}ft " +
"ΣΔd=${(kMetresToFeet * arcLength).f1}ft " +
"ΣΔt=${trajectory.last().t.f1}s " +
"O=$optimizing " +
"V=${maxVRatio.f1} " +
Expand Down Expand Up @@ -371,7 +373,7 @@ class PathPlanner : PApplet() {
}
'r' -> {
val newWaypoints = waypoints.reversedArray()
for (i in 0 until newWaypoints.size) {
for (i in newWaypoints.indices) {
newWaypoints[i] = newWaypoints[i].run {
Pose2D(translation, rotation.rotate(reversedRotation))
}
Expand All @@ -383,7 +385,7 @@ class PathPlanner : PApplet() {
regenerate()
}
'f' -> {
for (i in 0 until waypoints.size) {
for (i in waypoints.indices) {
waypoints[i] = waypoints[i].run {
Pose2D(Translation2D(translation.x, -translation.y), Rotation2D(rotation.cos, -rotation.sin))
}
Expand Down Expand Up @@ -479,7 +481,7 @@ class PathPlanner : PApplet() {
}

fun translateAll(by: Translation2D) {
for (i in 0 until waypoints.size) waypoints[i] = waypoints[i]
for (i in waypoints.indices) waypoints[i] = waypoints[i]
.run { Pose2D(translation + by, rotation) }
regenerate()
}
Expand Down Expand Up @@ -564,8 +566,8 @@ class PathPlanner : PApplet() {
redrawScreen()
}
's' -> showForCopy(waypoints.joinToString(",\n") {
"Pose2D(${(kMetersToFeet * it.translation.x).f}.feet, " +
"${(kMetersToFeet * it.translation.y).f}.feet, " +
"Pose2D(${(kMetresToFeet * it.translation.x).f}.feet, " +
"${(kMetresToFeet * it.translation.y).f}.feet, " +
"${it.rotation.degrees.f}.degrees)"
})
'0' -> {
Expand Down
122 changes: 106 additions & 16 deletions src/main/kotlin/ca/warp7/frc/Commons.kt
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
16 changes: 16 additions & 0 deletions src/main/kotlin/ca/warp7/frc/Deprecations.kt
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
5 changes: 1 addition & 4 deletions src/main/kotlin/ca/warp7/frc/action/ActionControl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ca.warp7.frc.action
/**
* An executor wrapper that can swap out actions
*/
@Deprecated("")
class ActionControl : Action {
private var currentAction: Action? = null
private var stopping = false
Expand Down Expand Up @@ -33,8 +34,4 @@ class ActionControl : Action {
override fun shouldFinish(): Boolean {
return stopping || currentAction?.shouldFinish() ?: true
}

fun flagAsDone() {
stopping = true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.util.concurrent.ConcurrentHashMap
/**
* Single-Instance scheduler for actions
*/
object Scheduler {
object Looper {

private val actionLoops: MutableSet<Action> = ConcurrentHashMap.newKeySet()
private val toRemove = mutableListOf<Action>()
Expand Down
5 changes: 0 additions & 5 deletions src/main/kotlin/ca/warp7/frc/control/ControllerState.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ca.warp7.frc
package ca.warp7.frc.control

class CycleCounter(private val cycles: Int = 1) {
private var count: Int = cycles
Expand Down
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
Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/ca/warp7/frc/control/Deprecations.kt
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
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 {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ca.warp7.frc
package ca.warp7.frc.control

class MinCycleBoolean(val minCycles: Int = 1) {
class MinCycleBoolean(private val minCycles: Int = 1) {
private var lastValue = false
private var cycleCount = 0

Expand Down
Loading

0 comments on commit ffa7a70

Please sign in to comment.