Skip to content

Commit

Permalink
This was the biggest merge of my life
Browse files Browse the repository at this point in the history
  • Loading branch information
keckothedragon committed May 18, 2024
1 parent fcc2a9d commit abbdc6d
Show file tree
Hide file tree
Showing 16 changed files with 353 additions and 8 deletions.
Binary file removed ctre_sim/Pigeon 2 - 020 - 0 - ext.dat
Binary file not shown.
24 changes: 24 additions & 0 deletions src/main/java/frc/robot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,30 @@ public static final class ShooterConstants {
new LoggedTunableNumber("Right test flywheel speed", 20.0);
}

public static final class IntakeConstants {
public static final double kPivotGearRatio = 36.0 / 16;

// TODO: untuned values, fix later
public static final Rotation2d kMaxAngle = Rotation2d.fromDegrees(90);
public static final Rotation2d kMinAngle = Rotation2d.fromDegrees(0);
public static final Rotation2d kHomeAngle = Rotation2d.fromDegrees(85);

public static final LoggedTunableNumber kPivotP =
new LoggedTunableNumber("Intake Pivot P", 1.0);
public static final LoggedTunableNumber kPivotI =
new LoggedTunableNumber("Intake Pivot I", 0.0);
public static final LoggedTunableNumber kPivotD =
new LoggedTunableNumber("Intake Pivot D", 0.0);
public static final double kPivotVelocity = 10.0;
public static final double kPivotAcceleration = 15.0;
public static final ProfiledPIDController kPivotController =
new ProfiledPIDController(
kPivotP.get(),
kPivotI.get(),
kPivotD.get(),
new Constraints(kPivotVelocity, kPivotAcceleration));
}

public static final class Ports {
public static final int kShooterPivotLeader = 39;
public static final int kShooterPivotFollower = 40;
Expand Down
34 changes: 33 additions & 1 deletion src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.wpilibj.RobotBase;
import edu.wpi.first.wpilibj2.command.Commands;
import frc.robot.Constants.IntakeConstants;
import frc.robot.Constants.Ports;
import frc.robot.Constants.ShooterConstants;
import frc.robot.commands.DriveCommands;
Expand All @@ -29,6 +30,11 @@
import frc.robot.subsystems.indexer.Indexer;
import frc.robot.subsystems.indexer.IndexerIOFalcon;
import frc.robot.subsystems.indexer.IndexerIOSim;
import frc.robot.subsystems.intake.Intake;
import frc.robot.subsystems.intake.pivot.IntakePivotIONeo;
import frc.robot.subsystems.intake.pivot.IntakePivotIOSim;
import frc.robot.subsystems.intake.roller.RollerIOKraken;
import frc.robot.subsystems.intake.roller.RollerIOSim;
import frc.robot.subsystems.shooter.Shooter;
import frc.robot.subsystems.shooter.flywheel.FlywheelIOKraken;
import frc.robot.subsystems.shooter.flywheel.FlywheelIOSim;
Expand All @@ -48,6 +54,7 @@ public class RobotContainer {
private Drive m_drive;
private Shooter m_shooter;
private Indexer m_indexer;
private Intake m_intake;

// Controllers
private DriverControls m_driverControls;
Expand Down Expand Up @@ -87,6 +94,12 @@ private void configureSubsystems() {
Ports.kFeeder,
Ports.kIndexerBeamBreakOne,
Ports.kIndexerBeamBreakTwo));

m_intake =
new Intake(
new RollerIOKraken(Ports.kIntakeRoller),
new IntakePivotIONeo(Ports.kIntakePivot),
IntakeConstants.kPivotController);
} else {
m_drive =
new Drive(
Expand All @@ -105,11 +118,15 @@ private void configureSubsystems() {
ShooterConstants.kLeftFlywheelFeedforward,
ShooterConstants.kRightFlywheelFeedforward);
m_indexer = new Indexer(new IndexerIOSim());

m_intake =
new Intake(new RollerIOSim(), new IntakePivotIOSim(), IntakeConstants.kPivotController);
}

m_shooter.setPivotAngle(ShooterConstants.kHomeAngle);
m_intake.setPivotAngle(IntakeConstants.kHomeAngle);

m_robotState = RobotState.startInstance(m_drive, m_shooter, m_indexer);
m_robotState = RobotState.startInstance(m_drive, m_shooter, m_indexer, m_intake);
}

private void configureControllers() {
Expand Down Expand Up @@ -155,6 +172,21 @@ private void configureButtonBindings() {
.testFeeder()
.onTrue(m_indexer.runFeeder(6.0))
.onFalse(m_indexer.runFeeder(0.0));

m_driverControls
.testIntake()
.onTrue(
Commands.runOnce(
() -> {
m_intake.setRollerVoltage(12);
m_intake.setPivotAngle(Rotation2d.fromDegrees(15));
}))
.onFalse(
Commands.runOnce(
() -> {
m_intake.setRollerVoltage(0);
m_intake.setPivotAngle(IntakeConstants.kHomeAngle);
}));
}

public void updateRobotState() {
Expand Down
22 changes: 19 additions & 3 deletions src/main/java/frc/robot/RobotState.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import edu.wpi.first.wpilibj.util.Color8Bit;
import frc.robot.subsystems.drive.Drive;
import frc.robot.subsystems.indexer.Indexer;
import frc.robot.subsystems.intake.Intake;
import frc.robot.subsystems.shooter.Shooter;
import org.littletonrobotics.junction.Logger;

Expand All @@ -22,6 +23,7 @@ public class RobotState {
private Drive m_drive;
private Shooter m_shooter;
private Indexer m_indexer;
private Intake m_intake;

// mechanism
private final boolean kMechanismEnabled = true;
Expand All @@ -33,11 +35,14 @@ public class RobotState {
// advantagescope components
private final boolean kComponentsEnabled = true;
private final Translation3d kShooterZeroTranslation = new Translation3d(0.017, 0.0, 0.415);
MechanismLigament2d m_intakePivotMech;

private RobotState(Drive drive, Shooter shooter, Indexer indexer) {
private RobotState(Drive drive, Shooter shooter, Indexer indexer, Intake intake) {
// subsystems
m_drive = drive;
m_shooter = shooter;
m_indexer = indexer;
m_intake = intake;

// mechanism set up
m_mechanism = new Mechanism2d(1.5, 1);
Expand All @@ -62,13 +67,24 @@ private RobotState(Drive drive, Shooter shooter, Indexer indexer) {
6,
new Color8Bit(Color.kPurple)));

MechanismRoot2d intakeRoot = m_mechanism.getRoot("intake", 0.5, 1);
m_intakePivotMech =
intakeRoot.append(
new MechanismLigament2d(
"pivot",
1,
180 - m_intake.getPivotAngle().getDegrees(),
6,
new Color8Bit(Color.kBlue)));

// post to dashboard
SmartDashboard.putData("Mech2d", m_mechanism);
}

public static RobotState startInstance(Drive drive, Shooter shooter, Indexer indexer) {
public static RobotState startInstance(
Drive drive, Shooter shooter, Indexer indexer, Intake intake) {
if (instance == null) {
instance = new RobotState(drive, shooter, indexer);
instance = new RobotState(drive, shooter, indexer, intake);
} else {
throw new IllegalStateException("RobotState instance already started");
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/frc/robot/oi/DriverControls.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public interface DriverControls {
public Trigger testKicker();

public Trigger testFeeder();

public Trigger testIntake();
}
5 changes: 5 additions & 0 deletions src/main/java/frc/robot/oi/DriverControlsPS5.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public Trigger testKicker() {
public Trigger testFeeder() {
return m_controller.L1();
}

@Override
public Trigger testIntake() {
return m_controller.R2();
}
}
4 changes: 4 additions & 0 deletions src/main/java/frc/robot/oi/DriverControlsXbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ public Trigger testKicker() {
public Trigger testFeeder() {
return m_controller.button(3);
}

public Trigger testIntake() {
return m_controller.button(4);
}
}
68 changes: 68 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,68 @@
package frc.robot.subsystems.intake;

import edu.wpi.first.math.controller.ProfiledPIDController;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.Commands;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import frc.robot.subsystems.intake.pivot.IntakePivotIO;
import frc.robot.subsystems.intake.pivot.IntakePivotInputsAutoLogged;
import frc.robot.subsystems.intake.roller.RollerIO;
import frc.robot.subsystems.intake.roller.RollerInputsAutoLogged;
import org.littletonrobotics.junction.Logger;

public class Intake extends SubsystemBase {
private RollerIO m_rollerIO;
private IntakePivotIO m_pivotIO;

public final RollerInputsAutoLogged m_rollerInputs;
public final IntakePivotInputsAutoLogged m_pivotInputs;

private ProfiledPIDController m_pivotController;

public Intake(RollerIO io, IntakePivotIO pivotIO, ProfiledPIDController pivotController) {
m_rollerIO = io;
m_pivotIO = pivotIO;
m_pivotController = pivotController;

m_rollerInputs = new RollerInputsAutoLogged();
m_pivotInputs = new IntakePivotInputsAutoLogged();
}

@Override
public void periodic() {
m_pivotIO.updateInputs(m_pivotInputs);
m_rollerIO.updateInputs(m_rollerInputs);

double pivotPidVoltage = m_pivotController.calculate(m_pivotInputs.curAngle);
m_pivotIO.setVoltage(pivotPidVoltage);

Logger.processInputs("Intake/Pivot", m_pivotInputs);
Logger.processInputs("Intake/Roller", m_rollerInputs);

Logger.recordOutput("Intake/Pivot/PIDVoltage", pivotPidVoltage);
Logger.recordOutput("Intake/Pivot/DesiredAngle", m_pivotController.getSetpoint().position);
Logger.recordOutput("Intake/CurrentAngle", Units.radiansToDegrees(pivotPidVoltage));
}

public void setRollerVoltage(double voltage) {
m_rollerIO.setVoltage(voltage);
}

public void setPivotAngle(Rotation2d angle) {
m_pivotController.setGoal(angle.getRadians());
}

public Command runRollerCommand(double voltage) {
return Commands.runOnce(() -> m_rollerIO.setVoltage(voltage));
}

public Command setPivotAngleCommand(Rotation2d angle) {
return Commands.runOnce(() -> m_pivotController.setGoal(angle.getRadians()));
}

public Rotation2d getPivotAngle() {
return Rotation2d.fromRadians(m_pivotInputs.curAngle);
}
}
19 changes: 19 additions & 0 deletions src/main/java/frc/robot/subsystems/intake/pivot/IntakePivotIO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package frc.robot.subsystems.intake.pivot;

import edu.wpi.first.math.geometry.Rotation2d;
import frc.lib.advantagekit.LoggedIO;
import org.littletonrobotics.junction.AutoLog;

public interface IntakePivotIO extends LoggedIO<IntakePivotIO.IntakePivotInputs> {
@AutoLog
public static class IntakePivotInputs {
public double curAngle;
public double curVelocity;
public double curVoltage;
public double curAmps;
}

public void setVoltage(double voltage);

public Rotation2d getCurrentAngle();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package frc.robot.subsystems.intake.pivot;

import com.revrobotics.AbsoluteEncoder;
import com.revrobotics.CANSparkMax;
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.util.Units;
import frc.robot.Constants.IntakeConstants;

public class IntakePivotIONeo implements IntakePivotIO {
private CANSparkMax m_motor;
private AbsoluteEncoder m_encoder;

public IntakePivotIONeo(int port) {
m_motor = new CANSparkMax(port, CANSparkMax.MotorType.kBrushless);
m_encoder = m_motor.getAbsoluteEncoder();
m_encoder.setPositionConversionFactor(IntakeConstants.kPivotGearRatio);
}

@Override
public void updateInputs(IntakePivotInputs inputs) {
inputs.curAngle = Units.rotationsToRadians(m_encoder.getPosition());
inputs.curVelocity =
Units.rotationsPerMinuteToRadiansPerSecond(m_motor.getEncoder().getVelocity());
inputs.curVoltage = m_motor.getAppliedOutput();
inputs.curAmps = m_motor.getOutputCurrent();
}

@Override
public void setVoltage(double voltage) {
m_motor.setVoltage(voltage);
}

@Override
public Rotation2d getCurrentAngle() {
return Rotation2d.fromRotations(m_encoder.getPosition());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package frc.robot.subsystems.intake.pivot;

import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.math.system.plant.DCMotor;
import edu.wpi.first.wpilibj.simulation.SingleJointedArmSim;
import frc.robot.Constants.IntakeConstants;

public class IntakePivotIOSim implements IntakePivotIO {
private SingleJointedArmSim m_sim;
private double m_voltage;

public IntakePivotIOSim() {
DCMotor gearbox = DCMotor.getNeo550(1);
double gearing = 24.2391;
double armLength = 0.21;
double jKgMetersSquared = SingleJointedArmSim.estimateMOI(armLength, 4);
double minAngle = IntakeConstants.kMinAngle.getDegrees();
double maxAngle = IntakeConstants.kMaxAngle.getDegrees();
double startingAngle = IntakeConstants.kHomeAngle.getDegrees();

m_sim =
new SingleJointedArmSim(
gearbox,
gearing,
jKgMetersSquared,
armLength,
minAngle,
maxAngle,
false,
startingAngle);

m_voltage = 0.0;
}

@Override
public void updateInputs(IntakePivotInputs inputs) {
m_sim.update(0.02);

inputs.curAngle = getCurrentAngle().getRadians();
inputs.curVoltage = m_voltage;
inputs.curVelocity = m_sim.getVelocityRadPerSec();
inputs.curAmps = m_sim.getCurrentDrawAmps();
}

@Override
public void setVoltage(double voltage) {
m_sim.setInputVoltage(voltage);
m_voltage = voltage;
}

@Override
public Rotation2d getCurrentAngle() {
return Rotation2d.fromRadians(m_sim.getAngleRads());
}
}
15 changes: 15 additions & 0 deletions src/main/java/frc/robot/subsystems/intake/roller/RollerIO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package frc.robot.subsystems.intake.roller;

import frc.lib.advantagekit.LoggedIO;
import org.littletonrobotics.junction.AutoLog;

public interface RollerIO extends LoggedIO<RollerIO.RollerInputs> {
@AutoLog
public static class RollerInputs {
public double curVelocity;
public double curVoltage;
public double curAmps;
}

public void setVoltage(double voltage);
}
Loading

0 comments on commit abbdc6d

Please sign in to comment.