Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from FRC3636/IndexerSim+IntakeSim
Browse files Browse the repository at this point in the history
  • Loading branch information
doinkythederp authored Nov 21, 2024
2 parents 850483a + 79c6a68 commit 1cd85e9
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 23 deletions.
20 changes: 19 additions & 1 deletion src/main/java/com/frcteam3636/frc2024/Robot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,30 @@ object Robot : LoggedRobot() {
}).ignoringDisable(true))

// //Intake
// controller.rightBumper()
// controller.a()
// .debounce(0.150)
// .whileTrue(
// Intake.outtake()
// )
//
// controller.x()
// .debounce(0.150)
// .whileTrue(
// Intake.intake()
// )
//
// controller.b()
// .debounce(0.150)
// .whileTrue(
// Indexer.outtakeBalloon()
// )
//
// controller.y()
// .debounce(0.150)
// .whileTrue(
// Indexer.indexBalloon()
// )

// //Outtake
// controller.leftBumper()
// .whileTrue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import edu.wpi.first.units.Angle
import edu.wpi.first.units.Measure
import edu.wpi.first.units.Units.Degrees
import edu.wpi.first.units.Units.Volts
import edu.wpi.first.units.Voltage
import edu.wpi.first.wpilibj2.command.Subsystem
import edu.wpi.first.wpilibj2.command.sysid.SysIdRoutine
import edu.wpi.first.wpilibj2.command.sysid.SysIdRoutine.Direction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ interface ArmIO{

private val rightMotor = TalonFX(CTREMotorControllerId.RightArmMotor)

private val absoluteEncoder = DutyCycleEncoder(DigitalInput(7))
private val absoluteEncoder = DutyCycleEncoder(DigitalInput(8))

override fun updateInputs(inputs: ArmIO.ArmInputs) {
inputs.position = Rotations.of(absoluteEncoder.absolutePosition)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.frcteam3636.frc2024.subsystems.indexer

import com.frcteam3636.frc2024.Robot
import com.frcteam3636.frc2024.subsystems.intake.Intake
import edu.wpi.first.units.Units.Degrees
import edu.wpi.first.wpilibj.DriverStation
import edu.wpi.first.wpilibj.util.Color
import edu.wpi.first.wpilibj.util.Color8Bit
import edu.wpi.first.wpilibj2.command.Command
import edu.wpi.first.wpilibj2.command.Subsystem
import org.littletonrobotics.junction.Logger
Expand All @@ -18,6 +22,13 @@ object Indexer: Subsystem {
override fun periodic() {
io.updateInputs(inputs)
Logger.processInputs("Indexer", inputs)

Intake.indexerAngleLigament.angle = inputs.position.`in`(Degrees)
Intake.indexerAngleLigament.color = when (inputs.balloonState) {
BalloonState.Blue -> Color8Bit(Color.kBlue)
BalloonState.Red -> Color8Bit(Color.kRed)
else -> Color8Bit(Color.kGreen)
}
}

/**
Expand Down Expand Up @@ -45,4 +56,14 @@ object Indexer: Subsystem {
io.setSpinSpeed(0.0)
}
)

fun indexBalloon(): Command = runEnd(
{io.setSpinSpeed(0.5)},
{io.setSpinSpeed(0.0)}
)

fun outtakeBalloon(): Command = runEnd(
{io.setSpinSpeed(-0.5)},
{io.setSpinSpeed(0.0)}
)
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.frcteam3636.frc2024.subsystems.indexer

import edu.wpi.first.math.geometry.Rotation2d
import org.littletonrobotics.junction.LogTable
import org.littletonrobotics.junction.inputs.LoggableInputs
import com.frcteam3636.frc2024.CANSparkFlex
import com.frcteam3636.frc2024.REVMotorControllerId
import com.frcteam3636.frc2024.Robot
import com.frcteam3636.frc2024.utils.LimelightHelpers
import com.frcteam3636.frc2024.utils.math.TAU
import com.revrobotics.CANSparkLowLevel
import edu.wpi.first.math.system.plant.DCMotor
import edu.wpi.first.units.Units.RadiansPerSecond
import edu.wpi.first.units.Units.RotationsPerSecond
import edu.wpi.first.units.Units.*
import edu.wpi.first.wpilibj.simulation.FlywheelSim

public enum class BalloonState {
enum class BalloonState {
Blue,
Red,
None
Expand All @@ -22,19 +21,22 @@ public enum class BalloonState {
interface IndexerIO {
class Inputs : LoggableInputs {
var indexerVelocity = RotationsPerSecond.zero()
var indexerCurrent: Double = 0.0
var indexerCurrent = Amps.zero()
var balloonState: BalloonState = BalloonState.None
var position = Radians.zero()

override fun toLog(table: LogTable?) {
table?.put("Indexer Wheel Velocity", indexerVelocity)
table?.put("Indexer Wheel Current", indexerCurrent)
table?.put("Balloon Color", balloonState)
table?.put("Indexer Wheel Angle", position)
}

override fun fromLog(table: LogTable) {
indexerVelocity = table.get("Indexer Velocity", indexerVelocity)!!
indexerVelocity = table.get("Indexer Velocity", indexerVelocity)
indexerCurrent = table.get("Indexer Wheel Current", indexerCurrent)
balloonState = table.get("Balloon Color", balloonState)
position = table.get("Indexer Wheel Angle", position)
}
}
fun updateInputs(inputs: Inputs)
Expand All @@ -45,9 +47,9 @@ interface IndexerIO {

class IndexerIOReal : IndexerIO{
companion object Constants {
const val RED_CLASS = "red";
const val BLUE_CLASS = "blue";
const val NONE_CLASS = "none";
const val RED_CLASS = "red"
const val BLUE_CLASS = "blue"
const val NONE_CLASS = "none"
}

private var indexerMotor =
Expand All @@ -57,13 +59,14 @@ class IndexerIOReal : IndexerIO{
)

override fun updateInputs(inputs: IndexerIO.Inputs) {
inputs.indexerVelocity = RadiansPerSecond.of(indexerMotor.encoder.velocity)
inputs.indexerCurrent = indexerMotor.outputCurrent
inputs.indexerVelocity = Rotations.per(Minute).of(indexerMotor.encoder.velocity)
inputs.indexerCurrent = Amps.of(indexerMotor.outputCurrent)
inputs.position = Rotations.of(indexerMotor.encoder.position)

when (val colorClass = LimelightHelpers.getClassifierClass("limelight-sensor")) {
RED_CLASS -> inputs.balloonState = BalloonState.Red;
BLUE_CLASS -> inputs.balloonState = BalloonState.Blue;
NONE_CLASS -> inputs.balloonState = BalloonState.None;
RED_CLASS -> inputs.balloonState = BalloonState.Red
BLUE_CLASS -> inputs.balloonState = BalloonState.Blue
NONE_CLASS -> inputs.balloonState = BalloonState.None
else -> throw AssertionError("Unknown balloon class: $colorClass")
}
}
Expand All @@ -84,6 +87,8 @@ class IndexerIOSim: IndexerIO {
override fun updateInputs(inputs: IndexerIO.Inputs) {
flywheelSim.update(Robot.period)
inputs.indexerVelocity = RadiansPerSecond.of(flywheelSim.angularVelocityRadPerSec)
inputs.position += Radians.of(flywheelSim.angularVelocityRadPerSec * Robot.period)
inputs.position = Radians.of(inputs.position.`in`(Radians).mod(TAU))
}

override fun setSpinSpeed(speed: Double) {
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/frcteam3636/frc2024/subsystems/intake/Intake.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.frcteam3636.frc2024.subsystems.intake

import com.frcteam3636.frc2024.Robot
import edu.wpi.first.units.Units.Degrees
import edu.wpi.first.wpilibj.smartdashboard.Mechanism2d
import edu.wpi.first.wpilibj.smartdashboard.MechanismLigament2d
import edu.wpi.first.wpilibj.util.Color
import edu.wpi.first.wpilibj.util.Color8Bit
import edu.wpi.first.wpilibj2.command.Command
import edu.wpi.first.wpilibj2.command.Subsystem
import org.littletonrobotics.junction.Logger
Expand All @@ -14,8 +19,25 @@ object Intake: Subsystem {

var inputs = IntakeIO.Inputs()

var mechanism = Mechanism2d(100.0, 200.0)
var intakeAngleLigament = MechanismLigament2d("Intake Ligament", 50.0, 90.0, 5.0, Color8Bit(Color.kGreen))
var indexerAngleLigament = MechanismLigament2d("Indexer Ligament", 50.0, 90.0, 5.0, Color8Bit(Color.kGreen))

init {
mechanism.getRoot("Indexer", 50.0, 150.0).apply{
append(indexerAngleLigament)
}
mechanism.getRoot("Intake", 50.0,50.0).apply{
append(intakeAngleLigament)
}
}

override fun periodic() {
io.updateInputs(inputs)

Logger.processInputs("Intake", inputs)
intakeAngleLigament.angle = inputs.position.`in`(Degrees)
Logger.recordOutput("Intake Angle", mechanism)
}

fun outtake(): Command =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
package com.frcteam3636.frc2024.subsystems.intake
import com.frcteam3636.frc2024.CANSparkFlex
import com.frcteam3636.frc2024.REVMotorControllerId
import com.frcteam3636.frc2024.Robot
import com.frcteam3636.frc2024.utils.math.TAU
import com.revrobotics.CANSparkLowLevel
import edu.wpi.first.math.geometry.Rotation2d
import edu.wpi.first.math.system.plant.DCMotor
import edu.wpi.first.units.Units.*
import edu.wpi.first.wpilibj.simulation.FlywheelSim
import org.littletonrobotics.junction.LogTable
import org.littletonrobotics.junction.inputs.LoggableInputs


interface IntakeIO {
class Inputs : LoggableInputs {
var rollerVelocity = Rotation2d()
var current: Double = 0.0
var rollerVelocity = RotationsPerSecond.zero()
var current = Amps.zero()
var position = Radians.zero()

override fun toLog(table: LogTable) {
table.put("Intake Velocity", rollerVelocity)
table.put("Intake Current", current)
table.put("Intake Roller Position", position)
}

override fun fromLog(table: LogTable) {
rollerVelocity = table.get("Intake Velocity", rollerVelocity)!![0]
rollerVelocity = table.get("Intake Velocity", rollerVelocity)
current = table.get("Intake Current", current)
position = table.get("Intake Roller Position", position)
}

}

fun setSpeed(percent: Double)

fun updateInputs(inputs: Inputs)

class IntakeIOReal: IntakeIO {
private var motor =
CANSparkFlex(
Expand All @@ -37,11 +46,33 @@ interface IntakeIO {
assert(percent in -1.0..1.0)
motor.set(percent)
}

override fun updateInputs(inputs: Inputs) {
inputs.rollerVelocity = RotationsPerSecond.of(motor.encoder.velocity)
inputs.current = Amps.of(motor.outputCurrent)
inputs.position = Rotations.of(motor.encoder.position.mod(1.0))
}
}

class IntakeIOSim: IntakeIO {

var motor = FlywheelSim(
DCMotor.getNeoVortex(1),
1.0,
1.0
)

override fun updateInputs(inputs: Inputs) {
motor.update(Robot.period)
inputs.rollerVelocity = RadiansPerSecond.of(motor.angularVelocityRadPerSec)
inputs.current = Amps.of(motor.currentDrawAmps)
inputs.position += Radians.of(motor.angularVelocityRadPerSec * Robot.period)
inputs.position = Radians.of(inputs.position.`in`(Radians).mod(TAU))
}

override fun setSpeed(percent: Double) {
TODO("Not yet implemented")
assert(percent in -1.0..1.0)
motor.setInputVoltage(percent * 12)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.frcteam3636.frc2024.subsystems.wrist

import com.frcteam3636.frc2024.subsystems.arm.Arm
import com.frcteam3636.frc2024.subsystems.intake.IntakeIO
import edu.wpi.first.wpilibj2.command.Subsystem
import org.littletonrobotics.junction.Logger

Expand Down

0 comments on commit 1cd85e9

Please sign in to comment.