From 3729aa7c8be61e480cae12165d950ac5158e7525 Mon Sep 17 00:00:00 2001 From: zoe Date: Fri, 10 Apr 2026 10:37:42 -0700 Subject: [PATCH] add auto-only current limit --- src/main/java/frc/robot/Constants.java | 3 ++- src/main/java/frc/robot/Robot.java | 9 +++++++ .../drivetrain/CommandSwerveDrivetrain.java | 27 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/main/java/frc/robot/Constants.java b/src/main/java/frc/robot/Constants.java index 9e418037..ca27d27d 100644 --- a/src/main/java/frc/robot/Constants.java +++ b/src/main/java/frc/robot/Constants.java @@ -57,7 +57,8 @@ public static class DrivetrainConstants { // Drive motor current limits (in Amps) public static final double DRIVE_STATOR_CURRENT_LIMIT = 80; - public static final double DRIVE_SUPPLY_CURRENT_LIMIT = 45; + public static final double DRIVE_SUPPLY_CURRENT_LIMIT = 35; + public static final double DRIVE_SUPPLY_CURRENT_LIMIT_AUTO = 45; // Steer motor current limits (in Amps) public static final double STEER_STATOR_CURRENT_LIMIT = 60; diff --git a/src/main/java/frc/robot/Robot.java b/src/main/java/frc/robot/Robot.java index cb67b08b..d0a36d45 100644 --- a/src/main/java/frc/robot/Robot.java +++ b/src/main/java/frc/robot/Robot.java @@ -121,6 +121,9 @@ public void disabledPeriodic() {} /** This autonomous runs the autonomous command selected by your {@link RobotContainer} class. */ @Override public void autonomousInit() { + // Set drive motor current limit for autonomous + m_robotContainer.m_drivetrain.setAutoDriveMotorSupplyCurrentLimit(); + m_autonomousCommand = m_robotContainer.getAutonomousCommand(); m_robotContainer.resetPosition(); @@ -134,6 +137,12 @@ public void autonomousInit() { @Override public void autonomousPeriodic() {} + @Override + public void autonomousExit() { + // Reset drive motor current limit to default when exiting autonomous + m_robotContainer.m_drivetrain.setDefaultDriveMotorSupplyCurrentLimit(); + } + @Override public void teleopInit() { // This makes sure that the autonomous stops running when diff --git a/src/main/java/frc/robot/subsystems/drivetrain/CommandSwerveDrivetrain.java b/src/main/java/frc/robot/subsystems/drivetrain/CommandSwerveDrivetrain.java index 92db278f..fc033d33 100644 --- a/src/main/java/frc/robot/subsystems/drivetrain/CommandSwerveDrivetrain.java +++ b/src/main/java/frc/robot/subsystems/drivetrain/CommandSwerveDrivetrain.java @@ -6,6 +6,7 @@ import com.ctre.phoenix6.SignalLogger; import com.ctre.phoenix6.Utils; +import com.ctre.phoenix6.configs.CurrentLimitsConfigs; import com.ctre.phoenix6.swerve.SwerveDrivetrainConstants; import com.ctre.phoenix6.swerve.SwerveModuleConstants; import com.ctre.phoenix6.swerve.SwerveRequest; @@ -231,6 +232,32 @@ public CommandSwerveDrivetrain( configureAutoBuilder(); } + /** + * Sets the supply current limit for all drive motors. + * + * @param currentLimit The supply current limit in amps + */ + private void setDriveMotorSupplyCurrentLimit(double currentLimit) { + var currentLimitsConfig = new CurrentLimitsConfigs() + .withStatorCurrentLimit(Amps.of(DrivetrainConstants.DRIVE_STATOR_CURRENT_LIMIT)) + .withStatorCurrentLimitEnable(true) + .withSupplyCurrentLimit(currentLimit) + .withSupplyCurrentLimitEnable(true); + + // Apply to all modules' drive motors + for (var module : getModules()) { + module.getDriveMotor().getConfigurator().apply(currentLimitsConfig); + } + } + + public void setAutoDriveMotorSupplyCurrentLimit() { + setDriveMotorSupplyCurrentLimit(DrivetrainConstants.DRIVE_SUPPLY_CURRENT_LIMIT_AUTO); + } + + public void setDefaultDriveMotorSupplyCurrentLimit() { + setDriveMotorSupplyCurrentLimit(DrivetrainConstants.DRIVE_SUPPLY_CURRENT_LIMIT); + } + /** * Returns a command that applies the specified control request to this swerve drivetrain. *