From f190f64ed8907e09de2fe88527bcef94e190496f Mon Sep 17 00:00:00 2001 From: Hayden Heroux Date: Sun, 5 May 2024 01:18:33 -0400 Subject: [PATCH] refactor: Add stator current limit config. --- src/main/java/frc/lib/config/MotorConfig.java | 45 ++++++++++++------- src/main/java/frc/robot/arm/Arm.java | 6 ++- src/main/java/frc/robot/intake/Intake.java | 10 ++++- src/main/java/frc/robot/shooter/Shooter.java | 9 ++-- src/main/java/frc/robot/swerve/Swerve.java | 11 ++++- 5 files changed, 57 insertions(+), 24 deletions(-) diff --git a/src/main/java/frc/lib/config/MotorConfig.java b/src/main/java/frc/lib/config/MotorConfig.java index cac5b30..9ce52b2 100644 --- a/src/main/java/frc/lib/config/MotorConfig.java +++ b/src/main/java/frc/lib/config/MotorConfig.java @@ -12,13 +12,15 @@ public record MotorConfig( boolean neutralBrake, boolean ccwPositive, double motorToMechanismRatio, - double currentLimitAmps) { + double statorCurrentLimit, + double supplyCurrentLimit) { public MotorConfig { Objects.requireNonNull(neutralBrake); Objects.requireNonNull(ccwPositive); Objects.requireNonNull(motorToMechanismRatio); - Objects.requireNonNull(currentLimitAmps); + Objects.requireNonNull(statorCurrentLimit); + Objects.requireNonNull(supplyCurrentLimit); } public static final class MotorConfigBuilder { @@ -28,10 +30,12 @@ public static final class MotorConfigBuilder { private double motorToMechanismRatio; - private double currentLimitAmps; + private double statorCurrentLimit; + + private double supplyCurrentLimit; public static MotorConfigBuilder defaults() { - return new MotorConfigBuilder(false, true, 1.0, 40.0); + return new MotorConfigBuilder(false, true, 1.0, 80.0, 40.0); } public static MotorConfigBuilder from(MotorConfig motorConfig) { @@ -39,18 +43,21 @@ public static MotorConfigBuilder from(MotorConfig motorConfig) { motorConfig.neutralBrake, motorConfig.ccwPositive, motorConfig.motorToMechanismRatio, - motorConfig.currentLimitAmps); + motorConfig.statorCurrentLimit, + motorConfig.supplyCurrentLimit); } private MotorConfigBuilder( boolean neutralBrake, boolean ccwPositive, double motorToMechanismRatio, - double currentLimitAmps) { + double statorCurrentLimit, + double supplyCurrentLimit) { this.neutralBrake = neutralBrake; this.ccwPositive = ccwPositive; this.motorToMechanismRatio = motorToMechanismRatio; - this.currentLimitAmps = currentLimitAmps; + this.statorCurrentLimit = statorCurrentLimit; + this.supplyCurrentLimit = supplyCurrentLimit; } public MotorConfigBuilder neutralBrake(boolean neutralBrake) { @@ -68,13 +75,19 @@ public MotorConfigBuilder motorToMechanismRatio(double motorToMechanismRatio) { return this; } - public MotorConfigBuilder currentLimitAmps(double currentLimitAmps) { - this.currentLimitAmps = currentLimitAmps; + public MotorConfigBuilder statorCurrentLimit(double statorCurrentLimit) { + this.statorCurrentLimit = statorCurrentLimit; + return this; + } + + public MotorConfigBuilder supplyCurrentLimit(double supplyCurrentLimit) { + this.supplyCurrentLimit = supplyCurrentLimit; return this; } public MotorConfig build() { - return new MotorConfig(neutralBrake, ccwPositive, motorToMechanismRatio, currentLimitAmps); + return new MotorConfig( + neutralBrake, ccwPositive, motorToMechanismRatio, statorCurrentLimit, supplyCurrentLimit); } } @@ -88,14 +101,14 @@ private CurrentLimitsConfigs createCurrentLimitsConfigs() { // Stator current is a measure of the current inside of the motor and is typically higher than // supply (breaker) current - currentLimitsConfigs.StatorCurrentLimit = currentLimitAmps() * 2.0; + currentLimitsConfigs.StatorCurrentLimit = statorCurrentLimit; currentLimitsConfigs.StatorCurrentLimitEnable = true; - currentLimitsConfigs.SupplyCurrentLimit = currentLimitAmps(); - // Allow higher current spikes (150%) for a brief duration (one second) - // REV 40A auto-resetting breakers typically trip when current exceeds 300% for one second - currentLimitsConfigs.SupplyCurrentThreshold = currentLimitAmps() * 1.5; - currentLimitsConfigs.SupplyTimeThreshold = 1; + currentLimitsConfigs.SupplyCurrentLimit = supplyCurrentLimit; + // TODO Determine if spikes should be eliminated to preserve battery + // Allow higher current spikes (150%) for a very brief duration (tenth second) + currentLimitsConfigs.SupplyCurrentThreshold = supplyCurrentLimit * 1.5; + currentLimitsConfigs.SupplyTimeThreshold = 0.1; currentLimitsConfigs.SupplyCurrentLimitEnable = true; return currentLimitsConfigs; diff --git a/src/main/java/frc/robot/arm/Arm.java b/src/main/java/frc/robot/arm/Arm.java index 0b60dc8..125d65f 100644 --- a/src/main/java/frc/robot/arm/Arm.java +++ b/src/main/java/frc/robot/arm/Arm.java @@ -26,7 +26,11 @@ public class Arm extends Subsystem { absoluteEncoder.ccwPositive(false).offset(Rotation2d.fromDegrees(-173.135))) .motorConfig( motor -> - motor.ccwPositive(true).neutralBrake(true).motorToMechanismRatio(39.771428571)) + motor + .ccwPositive(true) + .neutralBrake(true) + .motorToMechanismRatio(39.771428571) + .statorCurrentLimit(40.0)) .motionProfileConfig( motionProfile -> motionProfile diff --git a/src/main/java/frc/robot/intake/Intake.java b/src/main/java/frc/robot/intake/Intake.java index afea39a..e3c3718 100644 --- a/src/main/java/frc/robot/intake/Intake.java +++ b/src/main/java/frc/robot/intake/Intake.java @@ -19,7 +19,10 @@ public class Intake extends Subsystem { MechanismConfigBuilder.defaults() .motorConfig( motor -> - motor.ccwPositive(false).neutralBrake(false).motorToMechanismRatio(24.0 / 16.0)) + motor + .ccwPositive(false) + .motorToMechanismRatio(24.0 / 16.0) + .statorCurrentLimit(80.0)) .motionProfileConfig(motionProfile -> motionProfile.maximumVelocity(66)) .feedforwardControllerConfig(feedforward -> feedforward.kS(0.13).kV(0.1683)) .feedbackControllerConfig(feedback -> feedback.kP(0.1)) @@ -30,7 +33,10 @@ public class Intake extends Subsystem { MechanismConfigBuilder.defaults() .motorConfig( motor -> - motor.ccwPositive(false).neutralBrake(false).motorToMechanismRatio(24.0 / 16.0)) + motor + .ccwPositive(false) + .motorToMechanismRatio(24.0 / 16.0) + .statorCurrentLimit(80.0)) .motionProfileConfig(motionProfile -> motionProfile.maximumVelocity(66)) .feedforwardControllerConfig(feedforward -> feedforward.kS(0.13).kV(0.1759)) .feedbackControllerConfig(feedback -> feedback.kP(0.1)) diff --git a/src/main/java/frc/robot/shooter/Shooter.java b/src/main/java/frc/robot/shooter/Shooter.java index 082d6f1..56dab6d 100644 --- a/src/main/java/frc/robot/shooter/Shooter.java +++ b/src/main/java/frc/robot/shooter/Shooter.java @@ -20,7 +20,10 @@ public class Shooter extends Subsystem { MechanismConfigBuilder.defaults() .motorConfig( motor -> - motor.ccwPositive(false).neutralBrake(true).motorToMechanismRatio(28.0 / 16.0)) + motor + .ccwPositive(false) + .motorToMechanismRatio(28.0 / 16.0) + .statorCurrentLimit(40.0)) // TODO Test, 40A -> 80A? .motionProfileConfig( motionProfile -> motionProfile.maximumVelocity(60).maximumAcceleration(200)) .feedforwardControllerConfig(feedforward -> feedforward.kS(0.14).kV(0.2)) @@ -43,8 +46,8 @@ public class Shooter extends Subsystem { motorConfig -> motorConfig .ccwPositive(true) - .neutralBrake(false) - .motorToMechanismRatio(36.0 / 16.0)) + .motorToMechanismRatio(36.0 / 16.0) + .statorCurrentLimit(40.0)) .motionProfileConfig( motionProfileConfig -> motionProfileConfig.maximumVelocity(45).maximumAcceleration(450)) diff --git a/src/main/java/frc/robot/swerve/Swerve.java b/src/main/java/frc/robot/swerve/Swerve.java index 2e89d29..654ce70 100644 --- a/src/main/java/frc/robot/swerve/Swerve.java +++ b/src/main/java/frc/robot/swerve/Swerve.java @@ -40,7 +40,10 @@ public class Swerve extends Subsystem { MechanismConfigBuilder.defaults() .motorConfig( motor -> - motor.ccwPositive(false).currentLimitAmps(20).motorToMechanismRatio(150.0 / 7.0)) + motor + .ccwPositive(false) + .motorToMechanismRatio(150.0 / 7.0) + .statorCurrentLimit(20.0)) .feedforwardControllerConfig(feedforward -> feedforward.kS(0.205)) .feedbackControllerConfig( feedback -> @@ -55,7 +58,11 @@ public class Swerve extends Subsystem { private final MechanismConfig driveConfig = MechanismConfigBuilder.defaults() .motorConfig( - motor -> motor.ccwPositive(false).currentLimitAmps(40.0).motorToMechanismRatio(6.75)) + motor -> + motor + .ccwPositive(false) + .motorToMechanismRatio(6.75) + .statorCurrentLimit(60.0)) // TODO Test, 80A -> 60A -> 50A? .feedforwardControllerConfig(feedforward -> feedforward.kS(0.14).kV(0.725)) .feedbackControllerConfig(feedback -> feedback.kP(0.75)) .build();