001/* Copyright (C) Cross The Road Electronics 2024 */
002package com.ctre.phoenix.motorcontrol;
003
004
005/**
006 * Supply-side current limiting.  This is typically used to prevent breakers from tripping.
007 */
008public class SupplyCurrentLimitConfiguration{
009    /**
010     * True/False to enable/disable limit feature.
011     */
012    public boolean enable = false;
013    /**
014     * The "holding" current (amperes) to limit to when feature is activated.
015     */
016    public double currentLimit = 0;
017
018    /**
019     * Current must exceed this threshold (amperes) before limiting occurs.
020     * If this value is less than currentLimit, then currentLimit is used as the threshold.
021     */
022    public double triggerThresholdCurrent = 0;
023    /**
024     * How long current must exceed threshold (seconds) before limiting occurs.
025     */
026    public double triggerThresholdTime = 0;
027
028    public SupplyCurrentLimitConfiguration(){}
029
030    public SupplyCurrentLimitConfiguration(boolean enable, double currentLimit, double triggerThresholdCurrent, double triggerThresholdTime){
031        this.enable = enable;
032        this.currentLimit = currentLimit;
033        this.triggerThresholdCurrent = triggerThresholdCurrent;
034        this.triggerThresholdTime = triggerThresholdTime;
035    }
036
037    public SupplyCurrentLimitConfiguration(double[] doubleArray){
038        deserialize(doubleArray);
039    }
040
041    /**
042     * @return string representation of current faults tripped
043     */
044    public String toString(){
045        String retstr = "";
046
047        if(enable == false){
048            retstr = "Limiting is disabled.";
049        }
050        else{
051            /* If current limit is greater than triggerThresholdCurrent,
052            * the device will use current-limit as the threshold.
053            */
054            double effectiveThresholdCurr = Math.max(currentLimit, triggerThresholdCurrent);
055            retstr = "Current Limiting will activate if SUPPLY current exceeds " + effectiveThresholdCurr + " amps for " + triggerThresholdTime + " seconds." + "  Then current will hold at " + currentLimit + " amps";
056        }
057        return retstr;
058    }
059
060    public double[] toArray(){
061        double[] retval = new double[4];
062        retval[0] = ((double)(enable ? 1 : 0));
063        retval[1] = (currentLimit);
064        retval[2] = (triggerThresholdCurrent);
065        retval[3] = (triggerThresholdTime);
066        return retval;
067    }
068
069    public void deserialize(double[] doubles){
070        int doubleCnt = doubles.length;
071
072        if (doubleCnt <= 0) { return; }
073        int i = 0;
074
075        if (i < doubleCnt) {
076            enable = (doubles[i++] > 0);
077        }
078        if (i < doubleCnt) {
079            currentLimit = doubles[i++];
080        }
081        if (i < doubleCnt) {
082            triggerThresholdCurrent = doubles[i++];
083        }
084        if (i < doubleCnt) {
085            triggerThresholdTime = doubles[i++];
086        }
087    }
088    public boolean equals(SupplyCurrentLimitConfiguration rhs){
089        boolean retval = true;
090        retval &= (enable == rhs.enable);
091        retval &= (currentLimit == rhs.currentLimit);
092        retval &= (triggerThresholdCurrent == rhs.triggerThresholdCurrent);
093        retval &= (triggerThresholdTime == rhs.triggerThresholdTime);
094        return retval;
095    }
096}