Skip to content

Commit

Permalink
v0.5.1-beta1: Controllables!
Browse files Browse the repository at this point in the history
  • Loading branch information
rowan-mcalpin committed Dec 29, 2024
1 parent c68bf47 commit 55a5130
Show file tree
Hide file tree
Showing 22 changed files with 208 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ android {
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "17"
jvmTarget = "1.8"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.rowanmcalpin.nextftc.core.command.Command
* @param subsystemCollection a set of subsystems this command implements
* @param interruptible whether this command can be stopped due to an overlap of subsystems
*/
class LambdaCommand(
class LambdaCommand @JvmOverloads constructor(
private val isDoneLambda: () -> Boolean = { true },
private val startLambda: () -> Unit = { },
private val updateLambda: () -> Unit = { },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.rowanmcalpin.nextftc.core.control.controllers

import kotlin.math.abs

/**
* Interface all controllers must inherit from.
*/
Expand All @@ -9,6 +11,11 @@ interface Controller {
*/
var target: Double

/**
* The tolerance for being "at the target"
*/
var setPointTolerance: Double

/**
* Given a reference, calculates how to best match the target.
*
Expand All @@ -20,4 +27,12 @@ interface Controller {
* Resets the control loop
*/
fun reset()

/**
* Whether the controller is within a tolerable distance of the target
*/
fun atTarget(reference: Double): Boolean {
if (abs(target - reference) <= setPointTolerance) return true
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ open class PIDFController(val coefficients: PIDFCoefficients): Controller {

override var target: Double = 0.0

var setPointTolerance: Double = 0.0
override var setPointTolerance: Double = 0.0

private var lastError = 0.0
private var integralSum = 0.0
Expand All @@ -45,6 +45,7 @@ open class PIDFController(val coefficients: PIDFCoefficients): Controller {
return (error * coefficients.kP) + (integralSum * coefficients.kI) + (derivative * coefficients.kD) + coefficients.kF
}

@Deprecated("Removed to shift to Controllable interface")
/**
* Whether this controller is within a certain distance of the [target].
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import kotlin.math.sqrt
open class SqrtController(val kS: Double): Controller {
override var target: Double = 0.0

var setPointTolerance: Double = 0.0
override var setPointTolerance: Double = 0.0

override fun calculate(reference: Double): Double {
val error = target - reference

return (sqrt(error) * kS)
}

@Deprecated("Removed to shift to Controllable interface")
/**
* Whether this controller is within a certain distance of the [target].
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.qualcomm.robotcore.hardware.DcMotorEx
import com.rowanmcalpin.nextftc.core.Subsystem
import com.rowanmcalpin.nextftc.core.command.Command

@Deprecated("Deprecated in favor of controllables", replaceWith = ReplaceWith("SetPower"))
/**
* Sets a motor to a specific power without any internal feedback
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.rowanmcalpin.nextftc.core.command.Command
import com.rowanmcalpin.nextftc.core.control.controllers.PIDFController
import kotlin.math.abs

@Deprecated("Removed in favor of controllables", replaceWith = ReplaceWith("RunToPosition"))
/**
* This command holds a motor's position until another command is scheduled that uses the same
* subsystem.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import com.qualcomm.robotcore.hardware.DcMotorEx
import com.rowanmcalpin.nextftc.core.Subsystem
import com.rowanmcalpin.nextftc.core.command.Command
import com.rowanmcalpin.nextftc.core.control.controllers.PIDFController
import com.rowanmcalpin.nextftc.ftc.hardware.controllables.RunToPosition
import kotlin.math.abs

@Deprecated("Removed in favor of controllables", replaceWith = ReplaceWith("RunToPosition"))
/**
* This implements a PID controller to drive a motor to a specified target position.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.rowanmcalpin.nextftc.core.command.Command
import com.rowanmcalpin.nextftc.core.control.controllers.PIDFController
import kotlin.math.abs

@Deprecated("Deprecated in favor of controllables", ReplaceWith("RunToVelocity"))
/**
* This implements a PID controller to drive a motor to a specified target velocity.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.rowanmcalpin.nextftc.core.command.Command
import com.rowanmcalpin.nextftc.core.control.controllers.PIDFController
import kotlin.math.abs

@Deprecated("Removed in favor of controllables", replaceWith = ReplaceWith("RunToPosition"))
/**
* This command holds a multiple motor's positions until another command is scheduled that uses the
* same subsystem.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.rowanmcalpin.nextftc.core.command.Command
import com.rowanmcalpin.nextftc.core.control.controllers.PIDFController
import kotlin.math.abs

@Deprecated("Removed in favor of controllables", replaceWith = ReplaceWith("RunToPosition"))
/**
* This implements a PID controller to drive multiple motors to a specified target position.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.rowanmcalpin.nextftc.ftc.hardware.controllables

/**
* An item that has a position and a power.
*/
interface Controllable {
var currentPosition: Double
var velocity: Double
var power: Double
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.rowanmcalpin.nextftc.ftc.hardware.controllables

import com.qualcomm.robotcore.hardware.DcMotor
import com.qualcomm.robotcore.hardware.DcMotorEx
import com.rowanmcalpin.nextftc.ftc.OpModeData

/**
* Wrapper class for motors that implements controllable (and can therefore be used with RunToPosition
* commands).
*/
class MotorEx(private val motor: DcMotorEx): Controllable {

constructor(name: String): this(OpModeData.hardwareMap.get(DcMotorEx::class.java, name))

override var currentPosition: Double
get() = motor.currentPosition.toDouble()
set(value) { }

override var velocity: Double
get() = motor.velocity
set(value) { }

override var power: Double
get() = motor.power
set(value) {
motor.mode = DcMotor.RunMode.RUN_WITHOUT_ENCODER
motor.power = value
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.rowanmcalpin.nextftc.ftc.hardware.controllables

/**
* A MotorGroup is a collection of [MotorEx]s that are all controlled by a single encoder (connected
* to the leader motor)
*
* @param leader the [MotorEx] with the encoder that will be used
* @param followers any other motors to control
*/
class MotorGroup(val leader: MotorEx, vararg val followers: MotorEx): Controllable {

constructor(vararg names: String): this(
MotorEx(names[0]),
*createMotors(names)
)

override var currentPosition: Double
get() = leader.currentPosition
set(value) { }

override var velocity: Double
get() = leader.velocity
set(value) { }

override var power: Double
get() = leader.power
set(value) {
leader.power = value
followers.forEach {
it.power = value
}
}

companion object {
fun createMotors(names: Array<out String>): Array<out MotorEx> {
val list = mutableListOf<MotorEx>()
if (names.isEmpty()) {
throw NotEnoughMotorsException()
}

for (i in 1..names.size) {
list += MotorEx(names[i])
}

return list.toTypedArray()
}
}
}

class NotEnoughMotorsException(): Exception("You must supply at least one motor to a MotorGroup.")
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.rowanmcalpin.nextftc.ftc.hardware.controllables

import com.rowanmcalpin.nextftc.core.Subsystem
import com.rowanmcalpin.nextftc.core.command.Command
import com.rowanmcalpin.nextftc.core.control.controllers.Controller
import kotlin.math.abs

/**
* This implements a [Controller] to drive a [Controllable] to a specified target position
*/
class RunToPosition @JvmOverloads constructor(val controllable: Controllable, val target: Double, val controller: Controller,
override val subsystems: Set<Subsystem> = setOf()): Command() {
override val isDone: Boolean
get() = controller.atTarget(controllable.currentPosition)

override fun start() {
controller.target = target
controller.reset()
}

override fun update() {
val calculatedPower = controller.calculate(controllable.currentPosition)
if (abs(controllable.power - calculatedPower) > 0.01) {
controllable.power = calculatedPower
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.rowanmcalpin.nextftc.ftc.hardware.controllables

import com.rowanmcalpin.nextftc.core.Subsystem
import com.rowanmcalpin.nextftc.core.command.Command
import com.rowanmcalpin.nextftc.core.control.controllers.Controller
import kotlin.math.abs

/**
* This implements a PID controller to drive a motor to a specified target velocity.
*
* @param controllable the [Controllable] to control
* @param targetVelocity the target velocity
* @param controller the [Controller] to implement
* @param subsystems the list of [Subsystem]s this command interacts with (should be whatever
* subsystem holds this command)
* @param outCondition will be evaluated every update, and the command will stop once it returns true
*/
class RunToVelocity @JvmOverloads constructor(val controllable: Controllable, val targetVelocity: Double,
val controller: Controller, override val subsystems: Set<Subsystem> = setOf(),
val outCondition: () -> Boolean = { abs(controllable.velocity)-targetVelocity < 10 }): Command() {
override val isDone: Boolean
get() = outCondition.invoke()

override fun start() {
controller.target = targetVelocity
controller.reset()
}

override fun update() {
val calculatedPower = controller.calculate(controllable.velocity)
if (abs(controllable.power - calculatedPower) > 0.01) {
controllable.power = calculatedPower
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.rowanmcalpin.nextftc.ftc.hardware.controllables

import com.rowanmcalpin.nextftc.core.Subsystem
import com.rowanmcalpin.nextftc.core.command.Command

/**
* Sets a controllable to a specific power without any internal feedback
*
* @param controllable the [Controllable] to control
* @param power the power to set the [Controllable] to
* @param subsystems the [Subsystem]s this command interacts with (should be whatever
* subsystem holds this command)
*/
class SetPower @JvmOverloads constructor(val controllable: Controllable, val power: Double,
override val subsystems: Set<Subsystem> = setOf()): Command() {
override val isDone = true

override fun start() {
controllable.power = power
}
}

This file was deleted.

This file was deleted.

8 changes: 4 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[versions]
agp = "8.7.3"
kotlin = "1.9.24"
kotlin = "1.9.0"

commons-math3 = "3.6.1"
pedro = "0.0.1-beta14"
ftc = "10.1.1"
dokka = "1.9.24"

module-pedro = "0.5.0-beta4"
module-ftc = "0.5.0-beta4"
module-core = "0.5.0-beta4"
module-pedro = "0.5.1-beta1"
module-ftc = "0.5.1-beta1"
module-core = "0.5.1-beta1"

[libraries]
commons-math3 = { module = "org.apache.commons:commons-math3", version.ref = "commons-math3" }
Expand Down
2 changes: 1 addition & 1 deletion maven.rowanmcalpin.com
Submodule maven.rowanmcalpin.com updated 75 files
+ com/rowanmcalpin/nextftc/core/0.5.1-beta1/core-0.5.1-beta1-sources.jar
+1 −0 com/rowanmcalpin/nextftc/core/0.5.1-beta1/core-0.5.1-beta1-sources.jar.md5
+1 −0 com/rowanmcalpin/nextftc/core/0.5.1-beta1/core-0.5.1-beta1-sources.jar.sha1
+1 −0 com/rowanmcalpin/nextftc/core/0.5.1-beta1/core-0.5.1-beta1-sources.jar.sha256
+1 −0 com/rowanmcalpin/nextftc/core/0.5.1-beta1/core-0.5.1-beta1-sources.jar.sha512
+ com/rowanmcalpin/nextftc/core/0.5.1-beta1/core-0.5.1-beta1.aar
+1 −0 com/rowanmcalpin/nextftc/core/0.5.1-beta1/core-0.5.1-beta1.aar.md5
+1 −0 com/rowanmcalpin/nextftc/core/0.5.1-beta1/core-0.5.1-beta1.aar.sha1
+1 −0 com/rowanmcalpin/nextftc/core/0.5.1-beta1/core-0.5.1-beta1.aar.sha256
+1 −0 com/rowanmcalpin/nextftc/core/0.5.1-beta1/core-0.5.1-beta1.aar.sha512
+110 −0 com/rowanmcalpin/nextftc/core/0.5.1-beta1/core-0.5.1-beta1.module
+1 −0 com/rowanmcalpin/nextftc/core/0.5.1-beta1/core-0.5.1-beta1.module.md5
+1 −0 com/rowanmcalpin/nextftc/core/0.5.1-beta1/core-0.5.1-beta1.module.sha1
+1 −0 com/rowanmcalpin/nextftc/core/0.5.1-beta1/core-0.5.1-beta1.module.sha256
+1 −0 com/rowanmcalpin/nextftc/core/0.5.1-beta1/core-0.5.1-beta1.module.sha512
+28 −0 com/rowanmcalpin/nextftc/core/0.5.1-beta1/core-0.5.1-beta1.pom
+1 −0 com/rowanmcalpin/nextftc/core/0.5.1-beta1/core-0.5.1-beta1.pom.md5
+1 −0 com/rowanmcalpin/nextftc/core/0.5.1-beta1/core-0.5.1-beta1.pom.sha1
+1 −0 com/rowanmcalpin/nextftc/core/0.5.1-beta1/core-0.5.1-beta1.pom.sha256
+1 −0 com/rowanmcalpin/nextftc/core/0.5.1-beta1/core-0.5.1-beta1.pom.sha512
+4 −3 com/rowanmcalpin/nextftc/core/maven-metadata.xml
+1 −1 com/rowanmcalpin/nextftc/core/maven-metadata.xml.md5
+1 −1 com/rowanmcalpin/nextftc/core/maven-metadata.xml.sha1
+1 −1 com/rowanmcalpin/nextftc/core/maven-metadata.xml.sha256
+1 −1 com/rowanmcalpin/nextftc/core/maven-metadata.xml.sha512
+ com/rowanmcalpin/nextftc/ftc/0.5.1-beta1/ftc-0.5.1-beta1-sources.jar
+1 −0 com/rowanmcalpin/nextftc/ftc/0.5.1-beta1/ftc-0.5.1-beta1-sources.jar.md5
+1 −0 com/rowanmcalpin/nextftc/ftc/0.5.1-beta1/ftc-0.5.1-beta1-sources.jar.sha1
+1 −0 com/rowanmcalpin/nextftc/ftc/0.5.1-beta1/ftc-0.5.1-beta1-sources.jar.sha256
+1 −0 com/rowanmcalpin/nextftc/ftc/0.5.1-beta1/ftc-0.5.1-beta1-sources.jar.sha512
+ com/rowanmcalpin/nextftc/ftc/0.5.1-beta1/ftc-0.5.1-beta1.aar
+1 −0 com/rowanmcalpin/nextftc/ftc/0.5.1-beta1/ftc-0.5.1-beta1.aar.md5
+1 −0 com/rowanmcalpin/nextftc/ftc/0.5.1-beta1/ftc-0.5.1-beta1.aar.sha1
+1 −0 com/rowanmcalpin/nextftc/ftc/0.5.1-beta1/ftc-0.5.1-beta1.aar.sha256
+1 −0 com/rowanmcalpin/nextftc/ftc/0.5.1-beta1/ftc-0.5.1-beta1.aar.sha512
+103 −0 com/rowanmcalpin/nextftc/ftc/0.5.1-beta1/ftc-0.5.1-beta1.module
+1 −0 com/rowanmcalpin/nextftc/ftc/0.5.1-beta1/ftc-0.5.1-beta1.module.md5
+1 −0 com/rowanmcalpin/nextftc/ftc/0.5.1-beta1/ftc-0.5.1-beta1.module.sha1
+1 −0 com/rowanmcalpin/nextftc/ftc/0.5.1-beta1/ftc-0.5.1-beta1.module.sha256
+1 −0 com/rowanmcalpin/nextftc/ftc/0.5.1-beta1/ftc-0.5.1-beta1.module.sha512
+28 −0 com/rowanmcalpin/nextftc/ftc/0.5.1-beta1/ftc-0.5.1-beta1.pom
+1 −0 com/rowanmcalpin/nextftc/ftc/0.5.1-beta1/ftc-0.5.1-beta1.pom.md5
+1 −0 com/rowanmcalpin/nextftc/ftc/0.5.1-beta1/ftc-0.5.1-beta1.pom.sha1
+1 −0 com/rowanmcalpin/nextftc/ftc/0.5.1-beta1/ftc-0.5.1-beta1.pom.sha256
+1 −0 com/rowanmcalpin/nextftc/ftc/0.5.1-beta1/ftc-0.5.1-beta1.pom.sha512
+4 −3 com/rowanmcalpin/nextftc/ftc/maven-metadata.xml
+1 −1 com/rowanmcalpin/nextftc/ftc/maven-metadata.xml.md5
+1 −1 com/rowanmcalpin/nextftc/ftc/maven-metadata.xml.sha1
+1 −1 com/rowanmcalpin/nextftc/ftc/maven-metadata.xml.sha256
+1 −1 com/rowanmcalpin/nextftc/ftc/maven-metadata.xml.sha512
+ com/rowanmcalpin/nextftc/pedro/0.5.1-beta1/pedro-0.5.1-beta1-sources.jar
+1 −0 com/rowanmcalpin/nextftc/pedro/0.5.1-beta1/pedro-0.5.1-beta1-sources.jar.md5
+1 −0 com/rowanmcalpin/nextftc/pedro/0.5.1-beta1/pedro-0.5.1-beta1-sources.jar.sha1
+1 −0 com/rowanmcalpin/nextftc/pedro/0.5.1-beta1/pedro-0.5.1-beta1-sources.jar.sha256
+1 −0 com/rowanmcalpin/nextftc/pedro/0.5.1-beta1/pedro-0.5.1-beta1-sources.jar.sha512
+ com/rowanmcalpin/nextftc/pedro/0.5.1-beta1/pedro-0.5.1-beta1.aar
+1 −0 com/rowanmcalpin/nextftc/pedro/0.5.1-beta1/pedro-0.5.1-beta1.aar.md5
+1 −0 com/rowanmcalpin/nextftc/pedro/0.5.1-beta1/pedro-0.5.1-beta1.aar.sha1
+1 −0 com/rowanmcalpin/nextftc/pedro/0.5.1-beta1/pedro-0.5.1-beta1.aar.sha256
+1 −0 com/rowanmcalpin/nextftc/pedro/0.5.1-beta1/pedro-0.5.1-beta1.aar.sha512
+131 −0 com/rowanmcalpin/nextftc/pedro/0.5.1-beta1/pedro-0.5.1-beta1.module
+1 −0 com/rowanmcalpin/nextftc/pedro/0.5.1-beta1/pedro-0.5.1-beta1.module.md5
+1 −0 com/rowanmcalpin/nextftc/pedro/0.5.1-beta1/pedro-0.5.1-beta1.module.sha1
+1 −0 com/rowanmcalpin/nextftc/pedro/0.5.1-beta1/pedro-0.5.1-beta1.module.sha256
+1 −0 com/rowanmcalpin/nextftc/pedro/0.5.1-beta1/pedro-0.5.1-beta1.module.sha512
+52 −0 com/rowanmcalpin/nextftc/pedro/0.5.1-beta1/pedro-0.5.1-beta1.pom
+1 −0 com/rowanmcalpin/nextftc/pedro/0.5.1-beta1/pedro-0.5.1-beta1.pom.md5
+1 −0 com/rowanmcalpin/nextftc/pedro/0.5.1-beta1/pedro-0.5.1-beta1.pom.sha1
+1 −0 com/rowanmcalpin/nextftc/pedro/0.5.1-beta1/pedro-0.5.1-beta1.pom.sha256
+1 −0 com/rowanmcalpin/nextftc/pedro/0.5.1-beta1/pedro-0.5.1-beta1.pom.sha512
+4 −3 com/rowanmcalpin/nextftc/pedro/maven-metadata.xml
+1 −1 com/rowanmcalpin/nextftc/pedro/maven-metadata.xml.md5
+1 −1 com/rowanmcalpin/nextftc/pedro/maven-metadata.xml.sha1
+1 −1 com/rowanmcalpin/nextftc/pedro/maven-metadata.xml.sha256
+1 −1 com/rowanmcalpin/nextftc/pedro/maven-metadata.xml.sha512

0 comments on commit 55a5130

Please sign in to comment.