From: Wesley28w Date: Sat, 18 Apr 2026 15:29:55 +0000 (-0700) Subject: EMA breaker X-Git-Url: https://git.taranathan.com/?a=commitdiff_plain;h=1971ab968a1ea9fad5f502f85a0562b2d9d583f4;p=FRC2026.git EMA breaker --- diff --git a/src/main/java/frc/robot/subsystems/Breaker/Breaker.java b/src/main/java/frc/robot/subsystems/Breaker/Breaker.java deleted file mode 100644 index cf175ab..0000000 --- a/src/main/java/frc/robot/subsystems/Breaker/Breaker.java +++ /dev/null @@ -1,58 +0,0 @@ -package frc.robot.subsystems.Breaker; - -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import edu.wpi.first.wpilibj.PowerDistribution; -import edu.wpi.first.wpilibj2.command.SubsystemBase; -import frc.robot.constants.Constants; - -public class Breaker extends SubsystemBase { - - private static class Current { - double alpha; // how much of the error we correct per loop - double average = 0; - double threshold; - } - - PowerDistribution pDis = new PowerDistribution(); - - private List filters = new ArrayList<>(); // contains currents with their alphas and thresholds - - public Breaker() { - for (Map.Entry entry : BreakerConstants.THRESHOLDS.entrySet()) { - double tau = entry.getKey(); // sec - double threshold = entry.getValue(); // A - - Current w = new Current(); // create a filter for the threshold - w.threshold = threshold; - w.alpha = 1 - Math.exp(-Constants.LOOP_TIME / tau); // 1 - e^(-0.02/1) = 0.0198, 1 - e^(-0.02/2) = 0.00995 - - filters.add(w); - } - } - - @Override - public void periodic() { - double current = getCurrentFromPowerDistribution(); - for (Current f : filters) { - // new avg = old avg + fractionAlpha * difference - f.average += f.alpha * (current - f.average); - } - } - - public double getCurrentFromPowerDistribution() { - return pDis.getTotalCurrent(); // not using .getCurrent() and then an arguement for the port you can get just one port - } - - public boolean isOverCurrent() { - for (Current f : filters) { - if (f.average > f.threshold) { - return true; // uh oh - } - } - return false; - } -} diff --git a/src/main/java/frc/robot/subsystems/Breaker/EMABreaker.java b/src/main/java/frc/robot/subsystems/Breaker/EMABreaker.java new file mode 100644 index 0000000..244888c --- /dev/null +++ b/src/main/java/frc/robot/subsystems/Breaker/EMABreaker.java @@ -0,0 +1,58 @@ +package frc.robot.subsystems.Breaker; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import edu.wpi.first.wpilibj.PowerDistribution; +import edu.wpi.first.wpilibj2.command.SubsystemBase; +import frc.robot.constants.Constants; + +public class EMABreaker extends SubsystemBase { + + private static class Current { + double alpha; // how much of the error we correct per loop + double average = 0; + double threshold; + } + + PowerDistribution pDis = new PowerDistribution(); + + private List filters = new ArrayList<>(); // contains currents with their alphas and thresholds + + public EMABreaker() { + for (Map.Entry entry : BreakerConstants.THRESHOLDS.entrySet()) { + double tau = entry.getKey(); // sec + double threshold = entry.getValue(); // A + + Current w = new Current(); // create a filter for the threshold + w.threshold = threshold; + w.alpha = 1 - Math.exp(-Constants.LOOP_TIME / tau); // 1 - e^(-0.02/1) = 0.0198, 1 - e^(-0.02/2) = 0.00995 + + filters.add(w); + } + } + + @Override + public void periodic() { + double current = getCurrentFromPowerDistribution(); + for (Current f : filters) { + // new avg = old avg + fractionAlpha * difference + f.average += f.alpha * (current - f.average); + } + } + + public double getCurrentFromPowerDistribution() { + return pDis.getTotalCurrent(); // not using .getCurrent() and then an arguement for the port you can get just one port + } + + public boolean isOverCurrent() { + for (Current f : filters) { + if (f.average > f.threshold) { + return true; // uh oh + } + } + return false; + } +}