Skip to content

Commit

Permalink
Speedrun
Browse files Browse the repository at this point in the history
Signed-off-by: Jade Turner <[email protected]>
  • Loading branch information
spacey-sooty committed Dec 6, 2024
1 parent 4af4a0c commit 6db16c8
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/main/java/frc/robot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package frc.robot;

public final class Constants {
public static final Mode currentMode = Mode.REAL;
public static final Mode currentMode = Mode.SIM;

public static enum Mode {
/** Running on a real robotokent. */
Expand Down
58 changes: 45 additions & 13 deletions src/main/java/frc/robot/Robot.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
import frc.robot.subsystems.drive.Drive;
import frc.robot.subsystems.drive.DriveIO;
import frc.robot.subsystems.drive.DriveIOCTRE;
import frc.robot.subsystems.index.Index;
import frc.robot.subsystems.index.IndexIO;
import frc.robot.subsystems.index.IndexIOSparkMax;
import frc.robot.subsystems.intake.Intake;
import frc.robot.subsystems.intake.IntakeIO;
import frc.robot.subsystems.intake.IntakeIOSparkMax;
import frc.robot.subsystems.shooter.Shooter;
import frc.robot.subsystems.shooter.ShooterIO;
import frc.robot.subsystems.shooter.ShooterIOSim;
import frc.robot.subsystems.shooter.ShooterIOSparkMax;
import frc.robot.subsystems.vision.VisionIO;
import frc.robot.subsystems.vision.VisionIOLimelight;
import frc.robot.subsystems.vision.VisionIOSim;
Expand All @@ -57,6 +67,10 @@ public class Robot extends LoggedRobot {
private final CommandXboxController driver = new CommandXboxController(0);

private final Drive drive;
private final Shooter m_shooter;
private final Intake m_intake;
private final Index m_index;
private final Superstructure superstructure;
private final VisionSource limelight_back;
private final VisionSource limelight_front;

Expand Down Expand Up @@ -111,6 +125,10 @@ public Robot() {
TunerConstants.BackLeft,
TunerConstants.BackRight));

m_shooter = new Shooter(new ShooterIOSparkMax());
m_index = new Index(new IndexIOSparkMax());
m_intake = new Intake(new IntakeIOSparkMax());

limelight_back = new VisionSource(new VisionIOLimelight("limelight-back"));
limelight_front = new VisionSource(new VisionIOLimelight("limelight-front"));
break;
Expand All @@ -129,6 +147,10 @@ public Robot() {
TunerConstants.BackLeft,
TunerConstants.BackRight));

m_shooter = new Shooter(new ShooterIOSim());
m_index = new Index(new IndexIO() {});
m_intake = new Intake(new IntakeIO() {});

limelight_back = new VisionSource(new VisionIOSim("limelight-back"));
limelight_front = new VisionSource(new VisionIOSim("limelight-front"));
break;
Expand All @@ -143,6 +165,9 @@ public Robot() {

// inputs come from log file
drive = new Drive(new DriveIO() {});
m_shooter = new Shooter(new ShooterIO() {});
m_index = new Index(new IndexIO() {});
m_intake = new Intake(new IntakeIO() {});
limelight_back = new VisionSource(new VisionIO() {});
limelight_front = new VisionSource(new VisionIO() {});
break;
Expand All @@ -152,6 +177,8 @@ public Robot() {
Logger.registerURCL(URCL.startExternal());
Logger.start();

superstructure = new Superstructure(m_shooter, m_intake, m_index);

// TODO remove me and use LL LED abstraction
NetworkTableInstance.getDefault().getTable("limelight-front").getEntry("ledMode").setNumber(1);

Expand All @@ -169,6 +196,10 @@ public Robot() {
Millimeters.of(0),
Rotation3d.kZero)); // we only care about 2d translation data

m_intake.setDefaultCommand(superstructure.intake());
m_shooter.setDefaultCommand(m_shooter.stop());
m_index.setDefaultCommand(m_index.stop());

drive.setDefaultCommand(
// Drivetrain will execute this command periodically
drive.applyRequest(
Expand All @@ -180,21 +211,22 @@ public Robot() {
driver.getRightX()
* MaxAngularRate) // Drive counterclockwise with negative X (left)
));
driver
.leftBumper()
.whileTrue(
drive.driverAssistance(
new Pose2d(2, 5.6, new Rotation2d()),
() -> -driver.getLeftY() * MaxSpeed,
() -> -driver.getLeftX() * MaxSpeed));
// driver
// .leftBumper()
// .whileTrue(
// drive.driverAssistance(
// new Pose2d(2, 5.6, new Rotation2d()),
// () -> -driver.getLeftY() * MaxSpeed,
// () -> -driver.getLeftX() * MaxSpeed));
driver.rightBumper().whileTrue(superstructure.shoot());
driver.a().whileTrue(drive.brake());
driver.y().onTrue(drive.seedFieldCentric());
driver
.x()
.whileTrue(
drive
.moveToPosition(new Pose2d(2, 5.6, new Rotation2d()))
.until(drive::isTeleopAtSetpoint));
// driver
// .x()
// .whileTrue(
// drive
// .moveToPosition(new Pose2d(2, 5.6, new Rotation2d()))
// .until(drive::isTeleopAtSetpoint));

// Run SysId routines when holding back/start and X/Y.
// Note that each routine should be run exactly once in a single log.
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/frc/robot/Superstructure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) 2024 CurtinFRC
// Open Source Software, you can modify it according to the terms
// of the MIT License at the root of this project

package frc.robot;

import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.Commands;
import edu.wpi.first.wpilibj2.command.WaitCommand;
import edu.wpi.first.wpilibj2.command.button.Trigger;
import frc.robot.subsystems.index.Index;
import frc.robot.subsystems.intake.Intake;
import frc.robot.subsystems.shooter.Shooter;

public class Superstructure {
private static final int FRONT_BEAMBREAK_PORT = 5;
private static final int BACK_BEAMBREAK_PORT = 6;

// TODO should be logged akit style
private final DigitalInput frontBeambreak = new DigitalInput(FRONT_BEAMBREAK_PORT);
private final DigitalInput backBeamBreak = new DigitalInput(BACK_BEAMBREAK_PORT);

private final Shooter m_shooter;
private final Intake m_intake;
private final Index m_index;

public final Trigger m_hasNote = new Trigger(backBeamBreak::get);
public final Trigger m_intaking = new Trigger(frontBeambreak::get).negate();

public Superstructure(Shooter shooter, Intake intake, Index index) {
m_shooter = shooter;
m_intake = intake;
m_index = index;

m_intaking.onTrue(
Commands.parallel(
m_intake.setVoltage(5).until(m_hasNote).andThen(m_intake.stop()),
m_index
.setVoltage(-3.5)
.until(m_hasNote)
.andThen(new WaitCommand(0.5))
.andThen(m_index.setVoltage(2))
.until(m_hasNote)
.andThen(m_index.stop())));
}

public Command intake() {
return m_intake.setVoltage(10).until(m_intaking);
}

public Command shoot() {
return m_shooter
.spinup(250)
.withTimeout(1.5)
.andThen(Commands.parallel(m_shooter.maintain(), m_index.setVoltage(8)));
}

public Command stop() {
return Commands.parallel(m_shooter.stop(), m_intake.stop(), m_index.stop());
}
}
10 changes: 5 additions & 5 deletions src/main/java/frc/robot/generated/BuildConstants.java

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

29 changes: 29 additions & 0 deletions src/main/java/frc/robot/subsystems/index/Index.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package frc.robot.subsystems.index;

import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import org.littletonrobotics.junction.Logger;

public class Index extends SubsystemBase {
private final IndexIO io;
private final IndexIOInputsAutoLogged inputs;

public Index(IndexIO io) {
this.io = io;
inputs = new IndexIOInputsAutoLogged();
}

@Override
public void periodic() {
io.updateInputs(inputs);
Logger.processInputs("Index", inputs);
}

public Command stop() {
return runOnce(() -> io.stop());
}

public Command setVoltage(double volts) {
return run(() -> io.setVoltage(volts));
}
}
22 changes: 22 additions & 0 deletions src/main/java/frc/robot/subsystems/index/IndexIO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package frc.robot.subsystems.index;

import org.littletonrobotics.junction.AutoLog;

public interface IndexIO {
@AutoLog
public static class IndexIOInputs {
public double position;
public double velocity;
public double appliedVolts;
public double currentAmps;
}

/** Updates the set of loggable inputs. */
public default void updateInputs(IndexIOInputs inputs) {}

/** Run open loop at the specified voltage. */
public default void setVoltage(double volts) {}

/** Stop in open loop. */
public default void stop() {}
}
29 changes: 29 additions & 0 deletions src/main/java/frc/robot/subsystems/index/IndexIOSparkMax.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package frc.robot.subsystems.index;

import com.revrobotics.spark.SparkLowLevel.MotorType;
import com.revrobotics.spark.SparkMax;
import edu.wpi.first.wpilibj.RobotController;

public class IndexIOSparkMax implements IndexIO {
private static final int PORT = 21;

SparkMax spark = new SparkMax(PORT, MotorType.kBrushless);

@Override
public void updateInputs(IndexIOInputs inputs) {
inputs.appliedVolts = spark.getAppliedOutput() * RobotController.getBatteryVoltage();
inputs.currentAmps = spark.getOutputCurrent();
inputs.position = spark.getEncoder().getPosition();
inputs.velocity = spark.getEncoder().getVelocity();
}

@Override
public void setVoltage(double volts) {
spark.setVoltage(volts);
}

@Override
public void stop() {
spark.stopMotor();
}
}
29 changes: 29 additions & 0 deletions src/main/java/frc/robot/subsystems/intake/Intake.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package frc.robot.subsystems.intake;

import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import org.littletonrobotics.junction.Logger;

public class Intake extends SubsystemBase {
private final IntakeIO io;
private final IntakeIOInputsAutoLogged inputs;

public Intake(IntakeIO io) {
this.io = io;
inputs = new IntakeIOInputsAutoLogged();
}

@Override
public void periodic() {
io.updateInputs(inputs);
Logger.processInputs("Intake", inputs);
}

public Command stop() {
return runOnce(() -> io.stop());
}

public Command setVoltage(double volts) {
return run(() -> io.setVoltage(volts));
}
}
22 changes: 22 additions & 0 deletions src/main/java/frc/robot/subsystems/intake/IntakeIO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package frc.robot.subsystems.intake;

import org.littletonrobotics.junction.AutoLog;

public interface IntakeIO {
@AutoLog
public static class IntakeIOInputs {
public double position;
public double velocity;
public double appliedVolts;
public double currentAmps;
}

/** Updates the set of loggable inputs. */
public default void updateInputs(IntakeIOInputs inputs) {}

/** Run open loop at the specified voltage. */
public default void setVoltage(double volts) {}

/** Stop in open loop. */
public default void stop() {}
}
29 changes: 29 additions & 0 deletions src/main/java/frc/robot/subsystems/intake/IntakeIOSparkMax.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package frc.robot.subsystems.intake;

import com.revrobotics.spark.SparkLowLevel.MotorType;
import com.revrobotics.spark.SparkMax;
import edu.wpi.first.wpilibj.RobotController;

public class IntakeIOSparkMax implements IntakeIO {
private static final int PORT = 32;

SparkMax spark = new SparkMax(PORT, MotorType.kBrushless);

@Override
public void updateInputs(IntakeIOInputs inputs) {
inputs.appliedVolts = spark.getAppliedOutput() * RobotController.getBatteryVoltage();
inputs.currentAmps = spark.getOutputCurrent();
inputs.position = spark.getEncoder().getPosition();
inputs.velocity = spark.getEncoder().getVelocity();
}

@Override
public void setVoltage(double volts) {
spark.setVoltage(volts);
}

@Override
public void stop() {
spark.stopMotor();
}
}
8 changes: 4 additions & 4 deletions src/main/java/frc/robot/subsystems/shooter/Shooter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import org.littletonrobotics.junction.Logger;

public class Shooter extends SubsystemBase {
private static final double P = 0.1;
private static final double P = 0.00035273;
private static final double D = 0;
private static final double S = 0;
private static final double V = 0;
private static final double A = 0;
private static final double S = 0.090624;
private static final double V = 0.0021896;
private static final double A = 0.00070176;

private final ShooterIO io;
private final ShooterIOInputsAutoLogged inputs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import edu.wpi.first.wpilibj.RobotController;

public class ShooterIOSparkMax implements ShooterIO {
private static final int PORT = 99;
private static final int PORT = 31;

SparkMax spark = new SparkMax(PORT, MotorType.kBrushless);

Expand Down

0 comments on commit 6db16c8

Please sign in to comment.