|
| 1 | +package frc.robot.subsystems.shooter; |
| 2 | + |
| 3 | +import static edu.wpi.first.units.Units.*; |
| 4 | + |
| 5 | +import com.ctre.phoenix6.controls.PositionTorqueCurrentFOC; |
| 6 | +import com.ctre.phoenix6.controls.VelocityVoltage; |
| 7 | +import com.ctre.phoenix6.hardware.TalonFX; |
| 8 | +import edu.wpi.first.epilogue.Logged; |
| 9 | +import edu.wpi.first.epilogue.Logged.Importance; |
| 10 | +import edu.wpi.first.units.measure.Angle; |
| 11 | +import edu.wpi.first.units.measure.AngularVelocity; |
| 12 | +import edu.wpi.first.units.measure.LinearVelocity; |
| 13 | +import edu.wpi.first.wpilibj2.command.Command; |
| 14 | +import edu.wpi.first.wpilibj2.command.Commands; |
| 15 | +import edu.wpi.first.wpilibj2.command.SubsystemBase; |
| 16 | +import frc.robot.subsystems.intake.IntakeConstants; |
| 17 | + |
| 18 | +@Logged |
| 19 | +public class ShooterSubsystem extends SubsystemBase { |
| 20 | + private final TalonFX flywheelMotor; |
| 21 | + private final TalonFX hoodMotor; |
| 22 | + |
| 23 | + @Logged(name = "Velocity Target", importance = Importance.CRITICAL) |
| 24 | + private AngularVelocity velocityTarget; // Rotations Per Second |
| 25 | + |
| 26 | + private VelocityVoltage velocityControl; |
| 27 | + |
| 28 | + @Logged(name = "Hood Target", importance = Importance.CRITICAL) |
| 29 | + private Angle hoodTarget; // Rotations |
| 30 | + |
| 31 | + private PositionTorqueCurrentFOC hoodControl; |
| 32 | + |
| 33 | + public class LaunchRequest { |
| 34 | + private Angle launchHoodTarget; |
| 35 | + private AngularVelocity launchVelocity; |
| 36 | + |
| 37 | + public LaunchRequest(Angle theta, LinearVelocity velocity) { |
| 38 | + // hood angle in degrees (0 degrees is all the way back) is converted to motor rotations |
| 39 | + launchHoodTarget = |
| 40 | + Rotations.of( |
| 41 | + theta.in(Degrees) * ShooterConstants.ROTATIONS_PER_LAUNCH_DEGREE.in(Rotations)); |
| 42 | + // meters per second is converted to rotations per second of the flywheel |
| 43 | + launchVelocity = |
| 44 | + RotationsPerSecond.of( |
| 45 | + velocity.in(MetersPerSecond) |
| 46 | + / (2 * Math.PI * ShooterConstants.FLYWHEEL_RADIUS.in(Meters))); |
| 47 | + } |
| 48 | + |
| 49 | + public Angle getHoodTarget() { |
| 50 | + return launchHoodTarget; |
| 51 | + } |
| 52 | + |
| 53 | + public AngularVelocity getVelocityTarget() { |
| 54 | + return launchVelocity; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public ShooterSubsystem() { |
| 59 | + flywheelMotor = new TalonFX(ShooterConstants.FLYWHEEL_MOTOR_ID); |
| 60 | + hoodMotor = new TalonFX(ShooterConstants.HOOD_MOTOR_ID); |
| 61 | + |
| 62 | + flywheelMotor.getConfigurator().apply(ShooterConstants.createFlywheelMotorSlot0Configs()); |
| 63 | + velocityTarget = RotationsPerSecond.of(0); |
| 64 | + velocityControl = new VelocityVoltage(0); |
| 65 | + |
| 66 | + hoodMotor.getConfigurator().apply(ShooterConstants.createHoodMotorSlot0Configs()); |
| 67 | + hoodMotor.getConfigurator().apply(ShooterConstants.createHoodSoftwareLimitSwitchConfigs()); |
| 68 | + hoodTarget = Rotations.of(0); |
| 69 | + hoodControl = new PositionTorqueCurrentFOC(0); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void periodic() { |
| 74 | + flywheelMotor.setControl(velocityControl.withVelocity(velocityTarget.in(RotationsPerSecond))); |
| 75 | + hoodMotor.setControl(hoodControl.withVelocity(hoodTarget.in(Rotations))); |
| 76 | + } |
| 77 | + |
| 78 | + public Command spinFlywheelCommand() { |
| 79 | + return runOnce( |
| 80 | + () -> |
| 81 | + velocityTarget = |
| 82 | + RotationsPerSecond.of(ShooterPreferences.flywheelLaunchSpeed.getValue())) |
| 83 | + .withName("Start Spinning Flywheel"); |
| 84 | + } |
| 85 | + |
| 86 | + public Command stopFlywheelCommand() { |
| 87 | + return runOnce(() -> velocityTarget = RotationsPerSecond.of(0)) |
| 88 | + .withName("Stop Spinning Flywheel"); |
| 89 | + } |
| 90 | + |
| 91 | + @Logged(name = "At Hood Setpoint", importance = Importance.CRITICAL) |
| 92 | + public boolean atHoodSetpoint() { |
| 93 | + return Math.abs(hoodMotor.getPosition().getValueAsDouble() - hoodTarget.in(Rotations)) |
| 94 | + < IntakeConstants.ALLOWABLE_EXTENSION_ERROR; |
| 95 | + } |
| 96 | + |
| 97 | + public Command setHoodCommand(Angle position) { |
| 98 | + return runOnce(() -> hoodTarget = position) |
| 99 | + .andThen(Commands.waitUntil(() -> atHoodSetpoint())) |
| 100 | + .withName("Set Hood Angle"); |
| 101 | + } |
| 102 | + |
| 103 | + public Command launchLemonsCommand() { |
| 104 | + return setHoodCommand(Rotations.of(ShooterPreferences.hoodLaunchAngle.getValue())) |
| 105 | + .andThen(spinFlywheelCommand()) |
| 106 | + .withName("Start Launching Lemons"); |
| 107 | + } |
| 108 | + |
| 109 | + public Command stowCommand() { |
| 110 | + return stopFlywheelCommand().andThen(setHoodCommand(Rotations.of(0))).withName("Stow Shooter"); |
| 111 | + } |
| 112 | + |
| 113 | + public Command homeShooterCommand() { |
| 114 | + return runEnd( |
| 115 | + () -> hoodMotor.set(ShooterConstants.SAFE_HOMING_EFFORT.Output), |
| 116 | + () -> hoodMotor.setPosition(0)) |
| 117 | + .until( |
| 118 | + () -> { |
| 119 | + return hoodMotor.getStatorCurrent().getValueAsDouble() |
| 120 | + > ShooterConstants.SAFE_STATOR_LIMIT.in(Amp); |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + // helper method to find, given a distance from the robot to the tag, |
| 125 | + // (1) necessary angle of the hood |
| 126 | + // (2) necessary velocity of flywheel |
| 127 | + // to land a lemon in the goal |
| 128 | + public LaunchRequest createLaunchRequest(double distanceToTag) { |
| 129 | + double y1 = ShooterConstants.SHOOTER_HEIGHT.in(Meters); |
| 130 | + double x2 = distanceToTag + ShooterConstants.OFFSET_DISTANCE.in(Meters); |
| 131 | + double y2 = ShooterConstants.GOAL_HEIGHT.in(Meters); |
| 132 | + |
| 133 | + double slope = ShooterConstants.OPTIMAL_ENTRY_SLOPE; |
| 134 | + double a, b, vertex; |
| 135 | + Angle theta, motorAngle; |
| 136 | + do { |
| 137 | + // system of equations |
| 138 | + // (y2) = a(x2*x2) + b(x2) + y1 |
| 139 | + // slope = 2a(x2) + b |
| 140 | + a = (slope * x2 + y1 - y2) / (x2 * x2); |
| 141 | + b = (slope - 2 * a * x2); |
| 142 | + theta = Radians.of(Math.atan(b)); // launch angle (Hood Angle Conversion: MATH.PI/2 - theta) |
| 143 | + motorAngle = Radians.of(Math.PI / 2 - theta.in(Radians)); |
| 144 | + vertex = -1 * b / (2 * a); |
| 145 | + slope -= 0.05; |
| 146 | + } while ((vertex > x2 - ShooterConstants.MIN_VERTEX_DISTANCE.in(Meters)) |
| 147 | + && !(motorAngle.in(Degrees) < ShooterConstants.MIN_HOOD_ANGLE.in(Degrees))); |
| 148 | + |
| 149 | + if (motorAngle.in(Degrees) < ShooterConstants.MIN_HOOD_ANGLE.in(Degrees)) return null; |
| 150 | + |
| 151 | + // system of equations |
| 152 | + // (-b/2a) = (velocity)*cos(theta)*t |
| 153 | + // 2g(t) = (velocity)*sin(theta) |
| 154 | + LinearVelocity velocity = |
| 155 | + MetersPerSecond.of( |
| 156 | + Math.sqrt( |
| 157 | + 2 * 9.8 * vertex / (Math.sin(theta.in(Radians)) * Math.cos(theta.in(Radians))))); |
| 158 | + |
| 159 | + return new LaunchRequest(theta, velocity); |
| 160 | + } |
| 161 | +} |
0 commit comments