Skip to content

Commit

Permalink
Basic amp done
Browse files Browse the repository at this point in the history
  • Loading branch information
keckothedragon committed May 21, 2024
1 parent 926c21f commit 1a198c3
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 6 deletions.
16 changes: 16 additions & 0 deletions src/main/java/frc/robot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,22 @@ public static final class IntakeConstants {
new Constraints(kPivotVelocity, kPivotAcceleration));
}

public static final class AmpConstants {
public static final Rotation2d kMaxAngle = Rotation2d.fromDegrees(90);
public static final Rotation2d kMinAngle = Rotation2d.fromDegrees(0);
public static final Rotation2d kHomeAngle = Rotation2d.fromDegrees(0);
public static final Rotation2d kDeployedAngle = Rotation2d.fromDegrees(70);

public static final LoggedTunableNumber kP = new LoggedTunableNumber("Amp P", 1.0);
public static final LoggedTunableNumber kI = new LoggedTunableNumber("Amp I", 0.0);
public static final LoggedTunableNumber kD = new LoggedTunableNumber("Amp D", 0.0);
public static final double kVelocity = 10.0;
public static final double kAcceleration = 15.0;
public static final ProfiledPIDController kController =
new ProfiledPIDController(
kP.get(), kI.get(), kD.get(), new Constraints(kVelocity, kAcceleration));
}

public static final class Ports {
public static final int kShooterPivotLeader = 39;
public static final int kShooterPivotFollower = 40;
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@
import edu.wpi.first.math.geometry.Rotation2d;
import edu.wpi.first.wpilibj.RobotBase;
import edu.wpi.first.wpilibj2.command.Commands;
import frc.robot.Constants.AmpConstants;
import frc.robot.Constants.IntakeConstants;
import frc.robot.Constants.Ports;
import frc.robot.Constants.ShooterConstants;
import frc.robot.commands.DriveCommands;
import frc.robot.oi.DriverControls;
import frc.robot.oi.DriverControlsPS5;
import frc.robot.oi.DriverControlsXbox;
import frc.robot.subsystems.amp.Amp;
import frc.robot.subsystems.amp.AmpIOFalcon;
import frc.robot.subsystems.amp.AmpIOSim;
import frc.robot.subsystems.drive.Drive;
import frc.robot.subsystems.drive.GyroIOPigeon2;
import frc.robot.subsystems.drive.ModuleIOSim;
Expand Down Expand Up @@ -55,6 +59,7 @@ public class RobotContainer {
private Shooter m_shooter;
private Indexer m_indexer;
private Intake m_intake;
private Amp m_amp;

// Controllers
private DriverControls m_driverControls;
Expand All @@ -75,6 +80,7 @@ private void configureSubsystems() {
new ModuleIOTalonFX(1),
new ModuleIOTalonFX(2),
new ModuleIOTalonFX(3));

m_shooter =
new Shooter(
new ShooterPivotIOFalcon(
Expand All @@ -87,6 +93,7 @@ private void configureSubsystems() {
ShooterConstants.kRightFlywheelController,
ShooterConstants.kLeftFlywheelFeedforward,
ShooterConstants.kRightFlywheelFeedforward);

m_indexer =
new Indexer(
new IndexerIOFalcon(
Expand All @@ -100,6 +107,8 @@ private void configureSubsystems() {
new RollerIOKraken(Ports.kIntakeRoller),
new IntakePivotIONeo(Ports.kIntakePivot),
IntakeConstants.kPivotController);

m_amp = new Amp(new AmpIOFalcon(Ports.kAmp), AmpConstants.kController);
} else {
m_drive =
new Drive(
Expand All @@ -108,6 +117,7 @@ private void configureSubsystems() {
new ModuleIOSim(),
new ModuleIOSim(),
new ModuleIOSim());

m_shooter =
new Shooter(
new ShooterPivotIOSim(),
Expand All @@ -117,16 +127,19 @@ private void configureSubsystems() {
ShooterConstants.kRightFlywheelController,
ShooterConstants.kLeftFlywheelFeedforward,
ShooterConstants.kRightFlywheelFeedforward);

m_indexer = new Indexer(new IndexerIOSim());

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

m_amp = new Amp(new AmpIOSim(), AmpConstants.kController);
}

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

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

private void configureControllers() {
Expand Down Expand Up @@ -187,6 +200,11 @@ private void configureButtonBindings() {
m_intake.setRollerVoltage(0);
m_intake.setPivotAngle(IntakeConstants.kHomeAngle);
}));

m_driverControls
.deployAmp()
.onTrue(Commands.runOnce(() -> m_amp.setAmpAngle(AmpConstants.kDeployedAngle)))
.onFalse(Commands.runOnce(() -> m_amp.setAmpAngle(AmpConstants.kHomeAngle)));
}

public void updateRobotState() {
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/frc/robot/RobotState.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.util.Color;
import edu.wpi.first.wpilibj.util.Color8Bit;
import frc.robot.subsystems.amp.Amp;
import frc.robot.subsystems.drive.Drive;
import frc.robot.subsystems.indexer.Indexer;
import frc.robot.subsystems.intake.Intake;
Expand All @@ -25,6 +26,7 @@ public class RobotState {
private Shooter m_shooter;
private Indexer m_indexer;
private Intake m_intake;
private Amp m_amp;

// mechanism
private final boolean kMechanismEnabled = true;
Expand All @@ -43,12 +45,13 @@ public class RobotState {
new Translation3d(-0.269, -0.01, 0.2428),
new Rotation3d(Units.degreesToRadians(180), Units.degreesToRadians(180), 0.0));

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

// mechanism set up
m_mechanism = new Mechanism2d(1.5, 1);
Expand Down Expand Up @@ -88,9 +91,9 @@ private RobotState(Drive drive, Shooter shooter, Indexer indexer, Intake intake)
}

public static RobotState startInstance(
Drive drive, Shooter shooter, Indexer indexer, Intake intake) {
Drive drive, Shooter shooter, Indexer indexer, Intake intake, Amp amp) {
if (instance == null) {
instance = new RobotState(drive, shooter, indexer, intake);
instance = new RobotState(drive, shooter, indexer, intake, amp);
} 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 @@ -16,4 +16,6 @@ public interface DriverControls {
public Trigger testFeeder();

public Trigger testIntake();

public Trigger deployAmp();
}
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 @@ -44,4 +44,9 @@ public Trigger testFeeder() {
public Trigger testIntake() {
return m_controller.R2();
}

@Override
public Trigger deployAmp() {
return m_controller.triangle();
}
}
6 changes: 6 additions & 0 deletions src/main/java/frc/robot/oi/DriverControlsXbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,13 @@ public Trigger testFeeder() {
return m_controller.button(3);
}

@Override
public Trigger testIntake() {
return m_controller.button(4);
}

@Override
public Trigger deployAmp() {
return m_controller.button(5);
}
}
51 changes: 51 additions & 0 deletions src/main/java/frc/robot/subsystems/amp/Amp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package frc.robot.subsystems.amp;

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.SubsystemBase;
import frc.lib.utils.LoggedTunableNumber;
import frc.robot.Constants.AmpConstants;
import org.littletonrobotics.junction.Logger;

public class Amp extends SubsystemBase {
public AmpIO m_io;
public AmpInputsAutoLogged m_inputs;
private ProfiledPIDController m_controller;

public Amp(AmpIO io, ProfiledPIDController controller) {
m_io = io;
m_inputs = new AmpInputsAutoLogged();
m_controller = controller;
}

@Override
public void periodic() {
LoggedTunableNumber.ifChanged(
hashCode(),
() -> {
m_controller.setP(AmpConstants.kP.get());
m_controller.setI(AmpConstants.kI.get());
m_controller.setD(AmpConstants.kD.get());
},
AmpConstants.kP,
AmpConstants.kI,
AmpConstants.kD);

m_io.updateInputs(m_inputs);

double pidVoltage = m_controller.calculate(m_inputs.curAngle);
m_io.setVoltage(pidVoltage);

Logger.processInputs("Amp", m_inputs);

Logger.recordOutput("Amp/PIDVoltage", pidVoltage);
Logger.recordOutput("Amp/CurAngle", Units.rotationsToDegrees(m_inputs.curAngle));
Logger.recordOutput(
"Amp/DesiredAngle", Units.rotationsToDegrees(m_controller.getGoal().position));
}

public void setAmpAngle(Rotation2d angle) {
m_controller.setGoal(angle.getRotations());
}
}
16 changes: 16 additions & 0 deletions src/main/java/frc/robot/subsystems/amp/AmpIO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package frc.robot.subsystems.amp;

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

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

public void setVoltage(double voltage);
}
37 changes: 37 additions & 0 deletions src/main/java/frc/robot/subsystems/amp/AmpIOFalcon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package frc.robot.subsystems.amp;

import com.ctre.phoenix6.BaseStatusSignal;
import com.ctre.phoenix6.StatusSignal;
import com.ctre.phoenix6.hardware.TalonFX;

public class AmpIOFalcon implements AmpIO {
private TalonFX m_motor;
private StatusSignal<Double> m_velocitySignal;
private StatusSignal<Double> m_currentSignal;
private StatusSignal<Double> m_voltageSignal;
private StatusSignal<Double> m_angleSignal;

public AmpIOFalcon(int port) {
m_motor = new TalonFX(port);

m_velocitySignal = m_motor.getVelocity();
m_currentSignal = m_motor.getSupplyCurrent();
m_voltageSignal = m_motor.getMotorVoltage();
m_angleSignal = m_motor.getPosition();
}

@Override
public void updateInputs(AmpInputs inputs) {
BaseStatusSignal.refreshAll(m_velocitySignal, m_currentSignal, m_voltageSignal);

inputs.curVelocity = m_velocitySignal.getValueAsDouble();
inputs.curAmps = m_currentSignal.getValueAsDouble();
inputs.curVoltage = m_voltageSignal.getValueAsDouble();
inputs.curAngle = m_angleSignal.getValueAsDouble();
}

@Override
public void setVoltage(double voltage) {
m_motor.setVoltage(voltage);
}
}
51 changes: 51 additions & 0 deletions src/main/java/frc/robot/subsystems/amp/AmpIOSim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package frc.robot.subsystems.amp;

import edu.wpi.first.math.system.plant.DCMotor;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.wpilibj.simulation.SingleJointedArmSim;
import frc.robot.Constants.AmpConstants;

public class AmpIOSim implements AmpIO {
private SingleJointedArmSim m_sim;
private double m_voltage;

public AmpIOSim() {
DCMotor gearbox = DCMotor.getFalcon500(1);
double gearing = 20;
double armLength = 0.4572;
double jKgMetersSquared = SingleJointedArmSim.estimateMOI(armLength, 0.91);

double minAngle = AmpConstants.kMinAngle.getRadians();
double maxAngle = AmpConstants.kMaxAngle.getRadians();
double startingAngle = AmpConstants.kHomeAngle.getRadians();

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

m_voltage = 0.0;
}

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

inputs.curAngle = Units.radiansToRotations(m_sim.getAngleRads());
inputs.curVoltage = m_voltage;
inputs.curVelocity = Units.radiansToRotations(m_sim.getVelocityRadPerSec());
inputs.curAmps = m_sim.getCurrentDrawAmps();
}

@Override
public void setVoltage(double voltage) {
m_sim.setInputVoltage(voltage);
m_voltage = voltage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class IntakePivotIOSim implements IntakePivotIO {
public IntakePivotIOSim() {
DCMotor gearbox = DCMotor.getNeo550(1);
double gearing = 24.2391;
double armLength = 0.21;
double armLength = 0.381;
double jKgMetersSquared = SingleJointedArmSim.estimateMOI(armLength, 4);
double minAngle = IntakeConstants.kMinAngle.getDegrees();
double maxAngle = IntakeConstants.kMaxAngle.getDegrees();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ShooterPivotIOSim() {
DCMotor.getFalcon500(2),
46.722,
10,
.24,
0.5334,
ShooterConstants.kMinAngle.getRadians(),
ShooterConstants.kMaxAngle.getRadians(),
false,
Expand Down

0 comments on commit 1a198c3

Please sign in to comment.