Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

Commit

Permalink
refactor: Add stator current limit config.
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenheroux committed May 5, 2024
1 parent 8e8fbff commit f190f64
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 24 deletions.
45 changes: 29 additions & 16 deletions src/main/java/frc/lib/config/MotorConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -28,29 +30,34 @@ 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) {
return new MotorConfigBuilder(
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) {
Expand All @@ -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);
}
}

Expand All @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/frc/robot/arm/Arm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/frc/robot/intake/Intake.java
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/frc/robot/shooter/Shooter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/frc/robot/swerve/Swerve.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand All @@ -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();
Expand Down

0 comments on commit f190f64

Please sign in to comment.