|
| 1 | +package frc.robot.subsystems; |
| 2 | + |
| 3 | +import com.ctre.phoenix6.configs.TalonFXConfiguration; |
| 4 | +import com.ctre.phoenix6.controls.DutyCycleOut; |
| 5 | +import com.ctre.phoenix6.controls.MotionMagicVoltage; |
| 6 | +import com.ctre.phoenix6.hardware.TalonFX; |
| 7 | +import com.ctre.phoenix6.signals.NeutralModeValue; |
| 8 | +import edu.wpi.first.wpilibj2.command.SubsystemBase; |
| 9 | +import frc.robot.config.CANMappings; |
| 10 | +import frc.robot.config.HopperConfig; |
| 11 | + |
| 12 | +public class Hopper extends SubsystemBase { |
| 13 | + private TalonFX leftHopper; |
| 14 | + private TalonFX rightHopper; |
| 15 | + |
| 16 | + public Hopper() { |
| 17 | + |
| 18 | + leftHopper = new TalonFX(CANMappings.LEFT_HOPPER_MOTOR_ID); |
| 19 | + rightHopper = new TalonFX(CANMappings.RIGHT_HOPPER_MOTOR_ID); |
| 20 | + |
| 21 | + TalonFXConfiguration leftHopperConfig = new TalonFXConfiguration(); |
| 22 | + TalonFXConfiguration rightHopperConfig = new TalonFXConfiguration(); |
| 23 | + |
| 24 | + leftHopperConfig.CurrentLimits.SupplyCurrentLimitEnable = true; |
| 25 | + leftHopperConfig.CurrentLimits.StatorCurrentLimitEnable = true; |
| 26 | + leftHopperConfig.CurrentLimits.StatorCurrentLimit = |
| 27 | + HopperConfig.LEFT_HOPPER_STATOR_CURRENT_LIMIT; |
| 28 | + leftHopperConfig.CurrentLimits.SupplyCurrentLimit = |
| 29 | + HopperConfig.LEFT_HOPPER_SUPPLY_CURRENT_LIMIT; |
| 30 | + ; |
| 31 | + |
| 32 | + rightHopperConfig.CurrentLimits.SupplyCurrentLimitEnable = true; |
| 33 | + rightHopperConfig.CurrentLimits.StatorCurrentLimitEnable = true; |
| 34 | + rightHopperConfig.CurrentLimits.StatorCurrentLimit = |
| 35 | + HopperConfig.RIGHT_HOPPER_STATOR_CURRENT_LIMIT; |
| 36 | + ; |
| 37 | + rightHopperConfig.CurrentLimits.SupplyCurrentLimit = |
| 38 | + HopperConfig.RIGHT_HOPPER_SUPPLY_CURRENT_LIMIT; |
| 39 | + ; |
| 40 | + |
| 41 | + leftHopperConfig.MotionMagic.MotionMagicCruiseVelocity = |
| 42 | + HopperConfig.LEFT_HOPPER_MAX_CRUISE_VELOCITY; |
| 43 | + rightHopperConfig.MotionMagic.MotionMagicCruiseVelocity = |
| 44 | + HopperConfig.RIGHT_HOPPER_MAX_CRUISE_VELOCITY; |
| 45 | + leftHopperConfig.MotionMagic.MotionMagicAcceleration = |
| 46 | + HopperConfig.LEFT_HOPPER_TARGET_ACCELERATION; |
| 47 | + rightHopperConfig.MotionMagic.MotionMagicAcceleration = |
| 48 | + HopperConfig.RIGHT_HOPPER_TARGET_ACCELERATION; |
| 49 | + |
| 50 | + leftHopperConfig.Slot0.kP = HopperConfig.LEFT_HOPPER_P; |
| 51 | + leftHopperConfig.Slot0.kI = HopperConfig.LEFT_HOPPER_I; |
| 52 | + leftHopperConfig.Slot0.kD = HopperConfig.LEFT_HOPPER_D; |
| 53 | + |
| 54 | + rightHopperConfig.Slot0.kP = HopperConfig.RIGHT_HOPPER_P; |
| 55 | + rightHopperConfig.Slot0.kI = HopperConfig.RIGHT_HOPPER_I; |
| 56 | + rightHopperConfig.Slot0.kD = HopperConfig.RIGHT_HOPPER_D; |
| 57 | + |
| 58 | + leftHopperConfig.Feedback.SensorToMechanismRatio = |
| 59 | + HopperConfig.LEFT_HOPPER_GEAR_RATIO; // gear ratio |
| 60 | + rightHopperConfig.Feedback.SensorToMechanismRatio = HopperConfig.RIGHT_HOPPER_GEAR_RATIO; |
| 61 | + |
| 62 | + leftHopperConfig.MotorOutput.NeutralMode = NeutralModeValue.Coast; |
| 63 | + rightHopperConfig.MotorOutput.NeutralMode = NeutralModeValue.Coast; |
| 64 | + |
| 65 | + leftHopper.getConfigurator().apply(leftHopperConfig); |
| 66 | + rightHopper.getConfigurator().apply(rightHopperConfig); |
| 67 | + |
| 68 | + // follower = new Follower(CANMappings.K_Hopper_LEFT_ID, false); |
| 69 | + } |
| 70 | + |
| 71 | + public void extend(double rotation) { |
| 72 | + leftHopper.setControl(new MotionMagicVoltage(rotation)); |
| 73 | + rightHopper.setControl(new MotionMagicVoltage(rotation)); |
| 74 | + } |
| 75 | + |
| 76 | + public void retract(double rotation) { |
| 77 | + leftHopper.setControl(new MotionMagicVoltage(rotation)); |
| 78 | + rightHopper.setControl(new MotionMagicVoltage(rotation)); |
| 79 | + } |
| 80 | + |
| 81 | + public void directionalExtend(double speed) { |
| 82 | + leftHopper.setControl(new DutyCycleOut(speed)); |
| 83 | + rightHopper.setControl(new DutyCycleOut(speed)); |
| 84 | + } |
| 85 | + |
| 86 | + public void directionalRetract(double speed) { |
| 87 | + leftHopper.setControl(new DutyCycleOut(speed)); |
| 88 | + rightHopper.setControl(new DutyCycleOut(speed)); |
| 89 | + } |
| 90 | + |
| 91 | + public void stop() { |
| 92 | + leftHopper.stopMotor(); |
| 93 | + rightHopper.stopMotor(); |
| 94 | + } |
| 95 | + |
| 96 | + public void zero() { |
| 97 | + leftHopper.setPosition(0.0); |
| 98 | + rightHopper.setPosition(0.0); |
| 99 | + } |
| 100 | + |
| 101 | + public boolean atPosition() { |
| 102 | + return (Math.abs(leftHopper.getClosedLoopError().getValueAsDouble()) |
| 103 | + <= HopperConfig.HOPPER_TOLERANCE) |
| 104 | + && (Math.abs(rightHopper.getClosedLoopError().getValueAsDouble()) |
| 105 | + <= HopperConfig.HOPPER_TOLERANCE); |
| 106 | + } |
| 107 | +} |
0 commit comments