Skip to content

Commit

Permalink
intake code is finished
Browse files Browse the repository at this point in the history
  • Loading branch information
Shom770 committed Jan 13, 2024
1 parent 4571cd4 commit b419cda
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 360 deletions.
14 changes: 7 additions & 7 deletions src/main/kotlin/com/team4099/robot2023/BuildConstants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ package com.team4099.robot2023
const val MAVEN_GROUP = ""
const val MAVEN_NAME = "Crescendo-2024"
const val VERSION = "unspecified"
const val GIT_REVISION = -1
const val GIT_SHA = "UNKNOWN"
const val GIT_DATE = "UNKNOWN"
const val GIT_BRANCH = "UNKNOWN"
const val BUILD_DATE = "2023-12-16T19:11:21Z"
const val BUILD_UNIX_TIME = 1702771881467L
const val DIRTY = 129
const val GIT_REVISION = 20
const val GIT_SHA = "4571cd4f3189ad1982f91f7dde0d0c7d7ec30341"
const val GIT_DATE = "2024-01-12T19:01:07Z"
const val GIT_BRANCH = "intake"
const val BUILD_DATE = "2024-01-12T19:14:59Z"
const val BUILD_UNIX_TIME = 1705104899720L
const val DIRTY = 1
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.team4099.robot2023.config.constants

import com.fasterxml.jackson.annotation.JsonAutoDetect.Value
import edu.wpi.first.wpilibj.DoubleSolenoid
import org.team4099.lib.units.Fraction
import org.team4099.lib.units.Velocity
import org.team4099.lib.units.base.amps
import org.team4099.lib.units.derived.*
import org.team4099.lib.units.perSecond
import org.team4099.lib.units.derived.degrees
import org.team4099.lib.units.derived.volts

object IntakeConstants {
val VOLTAGE_COMPENSATION = 12.0.volts
Expand All @@ -16,12 +12,8 @@ object IntakeConstants {

// TODO: Change gear ratio according to robot
val ROLLER_CURRENT_LIMIT = 50.0.amps
val ARM_CURRENT_LIMIT = 50.0.amps
const val ROLLER_MOTOR_INVERTED = true
const val ARM_MOTOR_INVERTED = false
const val ROLLER_GEAR_RATIO = 36.0 / 18.0
const val ARM_GEAR_RATIO = ((60.0 / 12.0) * (80.0 / 18.0) * (32.0 / 16.0))
const val ARM_ENCODER_GEAR_RATIO = 36.0 / 18.0

// TODO: Update value
val IDLE_ROLLER_VOLTAGE = 2.0.volts
Expand Down
184 changes: 78 additions & 106 deletions src/main/kotlin/com/team4099/robot2023/subsystems/intake/Intake.kt
Original file line number Diff line number Diff line change
@@ -1,138 +1,110 @@
package com.team4099.robot2023.subsystems.intake

import com.team4099.lib.hal.Clock
import com.team4099.lib.logging.LoggedTunableNumber
import com.team4099.lib.logging.LoggedTunableValue
import com.team4099.robot2023.config.constants.Constants
import com.team4099.robot2023.config.constants.IntakeConstants
import com.team4099.robot2023.subsystems.superstructure.Request
import edu.wpi.first.wpilibj.RobotBase
import org.littletonrobotics.junction.Logger
import org.team4099.lib.controller.ArmFeedforward
import org.team4099.lib.controller.TrapezoidProfile
import org.team4099.lib.units.AngularVelocity
import org.team4099.lib.units.base.inSeconds
import org.team4099.lib.units.base.seconds
import org.team4099.lib.units.derived.Angle
import org.team4099.lib.units.derived.ElectricalPotential
import org.team4099.lib.units.derived.Radian
import org.team4099.lib.units.derived.degrees
import org.team4099.lib.units.derived.inDegrees
import org.team4099.lib.units.derived.inVolts
import org.team4099.lib.units.derived.inVoltsPerDegree
import org.team4099.lib.units.derived.inVoltsPerDegreePerSecond
import org.team4099.lib.units.derived.inVoltsPerDegreeSeconds
import org.team4099.lib.units.derived.perDegree
import org.team4099.lib.units.derived.perDegreePerSecond
import org.team4099.lib.units.derived.perDegreeSeconds
import org.team4099.lib.units.derived.volts
import org.team4099.lib.units.inDegreesPerSecond
import org.team4099.lib.units.perSecond

class Intake(private val io: IntakeIO) {
val inputs = IntakeIO.IntakeIOInputs()
val inputs = IntakeIO.IntakeIOInputs()

var rollerVoltageTarget: ElectricalPotential = 0.0.volts
var rollerVoltageTarget: ElectricalPotential = 0.0.volts

var isZeroed = false
var isZeroed = false

var lastIntakeRuntime = Clock.fpgaTime
var currentState: IntakeStake = IntakeStake.UNINITIALIZED
var currentState: IntakeStake = IntakeStake.UNINITIALIZED

var currentRequest: Request.IntakeRequest = Request.IntakeRequest.Idle()
set(value) {
rollerVoltageTarget = when (value) {
is Request.IntakeRequest.OpenLoop -> {
value.rollerVoltage
}

is Request.IntakeRequest.Idle -> {
IntakeConstants.IDLE_ROLLER_VOLTAGE
}
}

field = value
var currentRequest: Request.IntakeRequest = Request.IntakeRequest.Idle()
set(value) {
rollerVoltageTarget =
when (value) {
is Request.IntakeRequest.OpenLoop -> {
value.rollerVoltage
}
is Request.IntakeRequest.Idle -> {
IntakeConstants.IDLE_ROLLER_VOLTAGE
}
}

private var timeProfileGeneratedAt = Clock.fpgaTime

fun periodic() {
io.updateInputs(inputs)

Logger.processInputs("GroundIntake", inputs)
field = value
}

Logger.recordOutput("GroundIntake/currentState", currentState.name)
fun periodic() {
io.updateInputs(inputs)

Logger.recordOutput("GroundIntake/requestedState", currentRequest.javaClass.simpleName)
Logger.processInputs("Intake", inputs)

Logger.recordOutput("GroundIntake/isZeroed", isZeroed)
Logger.recordOutput("Intake/currentState", currentState.name)

if (Constants.Tuning.DEBUGING_MODE) {
Logger.recordOutput("GroundIntake/isAtCommandedState", currentState.equivalentToRequest(currentRequest))
Logger.recordOutput("Intake/requestedState", currentRequest.javaClass.simpleName)

Logger.recordOutput("GroundIntake/timeProfileGeneratedAt", timeProfileGeneratedAt.inSeconds)
Logger.recordOutput("Intake/isZeroed", isZeroed)

Logger.recordOutput("GroundIntake/rollerVoltageTarget", rollerVoltageTarget.inVolts)
}
if (Constants.Tuning.DEBUGING_MODE) {
Logger.recordOutput(
"Intake/isAtCommandedState", currentState.equivalentToRequest(currentRequest)
)

var nextState = currentState
when (currentState) {
IntakeStake.UNINITIALIZED -> {
// Outputs
// No designated output functionality because targeting position will take care of it next
// loop cycle

// Transitions
nextState = IntakeStake.IDLE
}
IntakeStake.IDLE -> {
setRollerVoltage(IntakeConstants.IDLE_ROLLER_VOLTAGE)

// Transitions
nextState = fromRequestToState(currentRequest)
}
IntakeStake.OPEN_LOOP_REQUEST -> {
setRollerVoltage(rollerVoltageTarget)

// Transitions
nextState = fromRequestToState(currentRequest)
}
}

// The next loop cycle, we want to run ground intake at the state that was requested. setting
// current state to the next state ensures that we run the logic for the state we want in the
// next loop cycle.
currentState = nextState
Logger.recordOutput("Intake/rollerVoltageTarget", rollerVoltageTarget.inVolts)
}

/** @param appliedVoltage Represents the applied voltage of the roller motor */
fun setRollerVoltage(appliedVoltage: ElectricalPotential) {
io.setRollerVoltage(appliedVoltage)
var nextState = currentState
when (currentState) {
IntakeStake.UNINITIALIZED -> {
// Outputs
// No designated output functionality because targeting position will take care of it next
// loop cycle

// Transitions
nextState = IntakeStake.IDLE
}
IntakeStake.IDLE -> {
setRollerVoltage(IntakeConstants.IDLE_ROLLER_VOLTAGE)

// Transitions
nextState = fromRequestToState(currentRequest)
}
IntakeStake.OPEN_LOOP -> {
setRollerVoltage(rollerVoltageTarget)

// Transitions
nextState = fromRequestToState(currentRequest)
}
}

fun zeroArm() {
io.zeroEncoder()
// The next loop cycle, we want to run ground intake at the state that was requested. setting
// current state to the next state ensures that we run the logic for the state we want in the
// next loop cycle.
currentState = nextState
}

/** @param appliedVoltage Represents the applied voltage of the roller motor */
fun setRollerVoltage(appliedVoltage: ElectricalPotential) {
io.setRollerVoltage(appliedVoltage)
}

companion object {
enum class IntakeStake {
UNINITIALIZED,
IDLE,
OPEN_LOOP;

inline fun equivalentToRequest(request: Request.IntakeRequest): Boolean {
return (
(request is Request.IntakeRequest.OpenLoop && this == OPEN_LOOP) ||
(request is Request.IntakeRequest.Idle && this == IDLE)
)
}
}

companion object {
enum class IntakeStake {
UNINITIALIZED,
IDLE,
OPEN_LOOP_REQUEST;

inline fun equivalentToRequest(request: Request.IntakeRequest): Boolean {
return (
(request is Request.IntakeRequest.OpenLoop && this == OPEN_LOOP_REQUEST) ||
(request is Request.IntakeRequest.Idle && this == IDLE)
)
}
}

inline fun fromRequestToState(request: Request.IntakeRequest): IntakeStake {
return when (request) {
is Request.IntakeRequest.OpenLoop -> IntakeStake.OPEN_LOOP_REQUEST
is Request.IntakeRequest.Idle -> IntakeStake.IDLE
}
}
inline fun fromRequestToState(request: Request.IntakeRequest): IntakeStake {
return when (request) {
is Request.IntakeRequest.OpenLoop -> IntakeStake.OPEN_LOOP
is Request.IntakeRequest.Idle -> IntakeStake.IDLE
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,10 @@ import org.team4099.lib.units.base.amps
import org.team4099.lib.units.base.celsius
import org.team4099.lib.units.base.inAmperes
import org.team4099.lib.units.base.inCelsius
import org.team4099.lib.units.derived.Angle
import org.team4099.lib.units.derived.DerivativeGain
import org.team4099.lib.units.derived.ElectricalPotential
import org.team4099.lib.units.derived.IntegralGain
import org.team4099.lib.units.derived.ProportionalGain
import org.team4099.lib.units.derived.Radian
import org.team4099.lib.units.derived.Volt
import org.team4099.lib.units.derived.degrees
import org.team4099.lib.units.derived.inDegrees
import org.team4099.lib.units.derived.inVolts
import org.team4099.lib.units.derived.rotations
import org.team4099.lib.units.derived.volts
import org.team4099.lib.units.inDegreesPerSecond
import org.team4099.lib.units.inRotationsPerMinute
import org.team4099.lib.units.perMinute
import org.team4099.lib.units.perSecond
Expand All @@ -32,25 +23,10 @@ interface IntakeIO {
var rollerStatorCurrent = 0.0.amps
var rollerTemp = 0.0.celsius

var armPosition = 0.0.degrees
var armVelocity = 0.0.degrees.perSecond
var armAbsoluteEncoderPosition = 0.0.degrees

var armAppliedVoltage = 0.0.volts
var armSupplyCurrent = 0.0.amps
var armStatorCurrent = 0.0.amps
var armTemp = 0.0.celsius

var isSimulated = false

override fun toLog(table: LogTable?) {
table?.put("armPositionDegrees", armPosition.inDegrees)
table?.put("armAbsoluteEncoderPositionDegrees", armAbsoluteEncoderPosition.inDegrees)
table?.put("armVelocityDegreesPerSec", armVelocity.inDegreesPerSecond)
table?.put("armAppliedVoltage", armAppliedVoltage.inVolts)
table?.put("armSupplyCurrentAmps", armSupplyCurrent.inAmperes)
table?.put("armStatorCurrentAmps", armStatorCurrent.inAmperes)
table?.put("armTempCelsius", armTemp.inCelsius)
table?.put("isSimulated", isSimulated)

table?.put("rollerVelocityRPM", rollerVelocity.inRotationsPerMinute)
table?.put("rollerAppliedVoltage", rollerAppliedVoltage.inVolts)
Expand All @@ -60,30 +36,6 @@ interface IntakeIO {
}

override fun fromLog(table: LogTable?) {
table?.get("armPositionDegrees", armPosition.inDegrees)?.let { armPosition = it.degrees }

table?.get("armAbsoluteEncoderPositionDegrees", armAbsoluteEncoderPosition.inDegrees)?.let {
armAbsoluteEncoderPosition = it.degrees
}

table?.get("armVelocityDegreesPerSec", armVelocity.inDegreesPerSecond)?.let {
armVelocity = it.degrees.perSecond
}

table?.get("armAppliedVoltage", armAppliedVoltage.inVolts)?.let {
armAppliedVoltage = it.volts
}

table?.get("armSupplyCurrentAmps", armSupplyCurrent.inAmperes)?.let {
armSupplyCurrent = it.amps
}

table?.get("armStatorCurrentAmps", armStatorCurrent.inAmperes)?.let {
armStatorCurrent = it.amps
}

table?.get("armTempCelsius", armTemp.inCelsius)?.let { armTemp = it.celsius }

table?.get("rollerVelocityRPM", rollerVelocity.inRotationsPerMinute)?.let {
rollerVelocity = it.rotations.perSecond
}
Expand All @@ -104,31 +56,9 @@ interface IntakeIO {
}
}

fun updateInputs(io: IntakeIOInputs) {}

fun setRollerVoltage(voltage: ElectricalPotential) {}

fun setArmVoltage(voltage: ElectricalPotential) {}

fun setArmPosition(armPosition: Angle, feedforward: ElectricalPotential) {}

/**
* Updates the PID constants using the implementation controller
*
* @param kP accounts for linear error
* @param kI accounts for integral error
* @param kD accounts for derivative error
*/
fun configPID(
kP: ProportionalGain<Radian, Volt>,
kI: IntegralGain<Radian, Volt>,
kD: DerivativeGain<Radian, Volt>
) {}

/** Sets the current encoder position to be the zero value */
fun zeroEncoder() {}
fun updateInputs(io: IntakeIOInputs)

fun setRollerBrakeMode(brake: Boolean) {}
fun setRollerVoltage(voltage: ElectricalPotential)

fun setArmBrakeMode(brake: Boolean)
fun setRollerBrakeMode(brake: Boolean)
}
Loading

0 comments on commit b419cda

Please sign in to comment.