001/* Copyright (C) Cross The Road Electronics 2024 */
002package com.ctre.phoenix.motorcontrol;
003
004/**
005 * Stator-side current limiting.  This is typically used to limit acceleration/torque and heat generation.
006 *
007 * @deprecated This device's Phoenix 5 API is deprecated for removal in the
008 * 2025 season. Users should update to Phoenix 6 firmware and migrate to the
009 * Phoenix 6 API. A migration guide is available at
010 * https://v6.docs.ctr-electronics.com/en/stable/docs/migration/migration-guide/index.html.
011 * <p>
012 * If the Phoenix 5 API must be used for this device, the device must have 22.X
013 * firmware. This firmware is available in Tuner X after selecting Phoenix 5 in
014 * the firmware year dropdown.
015 */
016@Deprecated(since = "2024", forRemoval = true)
017public class StatorCurrentLimitConfiguration{
018    /**
019     * True/False to enable/disable limit feature.
020     */
021    public boolean enable = false;
022    /**
023     * The "holding" current (amperes) to limit to when feature is activated.
024     */
025    public double currentLimit = 0;
026
027    /**
028     * Current must exceed this threshold (amperes) before limiting occurs.
029     * If this value is less than currentLimit, then currentLimit is used as the threshold.
030     */
031    public double triggerThresholdCurrent = 0;
032    /**
033     * How long current must exceed threshold (seconds) before limiting occurs.
034     */
035    public double triggerThresholdTime = 0;
036
037    public StatorCurrentLimitConfiguration(){}
038
039    public StatorCurrentLimitConfiguration(boolean enable, double currentLimit, double triggerThresholdCurrent, double triggerThresholdTime){
040        this.enable = enable;
041        this.currentLimit = currentLimit;
042        this.triggerThresholdCurrent = triggerThresholdCurrent;
043        this.triggerThresholdTime = triggerThresholdTime;
044    }
045
046    public StatorCurrentLimitConfiguration(double[] doubleArray){
047        deserialize(doubleArray);
048    }
049
050    /**
051     * @return string representation of current faults tripped
052     */
053    public String toString(){
054        String retstr = "";
055
056        if(enable == false){
057            retstr = "Limiting is disabled.";
058        }
059        else{
060            /* If current limit is greater than triggerThresholdCurrent,
061            * the device will use current-limit as the threshold.
062            */
063            double effectiveThresholdCurr = Math.max(currentLimit, triggerThresholdCurrent);
064            retstr = "Current Limiting will activate if STATOR current exceeds " + effectiveThresholdCurr + " amps for " + triggerThresholdTime + " seconds." + "  Then current will hold at " + currentLimit + " amps";
065        }
066        return retstr;
067    }
068
069    public double[] toArray(){
070        double[] retval = new double[4];
071        retval[0] = ((double)(enable ? 1 : 0));
072        retval[1] = (currentLimit);
073        retval[2] = (triggerThresholdCurrent);
074        retval[3] = (triggerThresholdTime);
075        return retval;
076    }
077
078    public void deserialize(double[] doubles){
079        int doubleCnt = doubles.length;
080
081        if (doubleCnt <= 0) { return; }
082        int i = 0;
083
084        if (i < doubleCnt) {
085            enable = (doubles[i++] > 0);
086        }
087        if (i < doubleCnt) {
088            currentLimit = doubles[i++];
089        }
090        if (i < doubleCnt) {
091            triggerThresholdCurrent = doubles[i++];
092        }
093        if (i < doubleCnt) {
094            triggerThresholdTime = doubles[i++];
095        }
096    }
097    public boolean equals(StatorCurrentLimitConfiguration rhs){
098        boolean retval = true;
099        retval &= (enable == rhs.enable);
100        retval &= (currentLimit == rhs.currentLimit);
101        retval &= (triggerThresholdCurrent == rhs.triggerThresholdCurrent);
102        retval &= (triggerThresholdTime == rhs.triggerThresholdTime);
103        return retval;
104    }
105}