001/* Copyright (C) Cross The Road Electronics 2024 */
002package com.ctre.phoenix.motorcontrol;
003
004/**
005 * All the sticky faults available to motor controllers
006 */
007public class StickyFaults {
008        /**
009         * Motor Controller is under 6.5V
010         */
011        public boolean UnderVoltage;
012        /**
013         * Forward limit switch is tripped and device is trying to go forward
014         * Only trips when the device is limited
015         */
016        public boolean ForwardLimitSwitch;
017        /**
018         * Reverse limit switch is tripped and device is trying to go reverse
019         * Only trips when the device is limited
020         */
021        public boolean ReverseLimitSwitch;
022        /**
023         * Sensor is beyond forward soft limit and device is trying to go forward
024         * Only trips when the device is limited
025         */
026        public boolean ForwardSoftLimit;
027        /**
028         * Sensor is beyond reverse soft limit and device is trying to go reverse
029         * Only trips when the device is limited
030         */
031        public boolean ReverseSoftLimit;
032        /**
033         * Device was powered-on or reset while robot is enabled.
034         * Check your breakers and wiring.
035         */
036        public boolean ResetDuringEn;
037        /**
038         * Device's sensor overflowed
039         */
040        public boolean SensorOverflow;
041        /**
042         * Device detects its sensor is out of phase
043         */
044        public boolean SensorOutOfPhase;
045        /**
046         * Not used, @see ResetDuringEn
047         */
048        public boolean HardwareESDReset;
049        /**
050         * Remote Sensor is no longer detected on bus
051         */
052        public boolean RemoteLossOfSignal;
053        /**
054         * API error detected.  Make sure API and firmware versions are compatible.
055         */
056        public boolean APIError;
057        /**
058         * Supply is well above the rated voltage of the hardware. This fault is specific to Brushless.
059         */
060        public boolean SupplyOverV;
061        /**
062         * Supply is rapidly fluctuating and unstable. This fault is specific to Brushless.
063         */
064        public boolean SupplyUnstable;
065
066        /**
067         * @return true if any faults are tripped
068         */
069        public boolean hasAnyFault() {
070                return  UnderVoltage |
071                                ForwardLimitSwitch |
072                                ReverseLimitSwitch |
073                                ForwardSoftLimit |
074                                ReverseSoftLimit |
075                                ResetDuringEn |
076                                SensorOverflow |
077                                SensorOutOfPhase |
078                                HardwareESDReset |
079                                RemoteLossOfSignal |
080                                APIError |
081                                SupplyOverV |
082                                SupplyUnstable;
083        }
084        /**
085         * @return Current fault list as a bit field
086         */
087        public int toBitfield() {
088                int retval = 0;
089                int mask = 1;
090                retval |= UnderVoltage ? mask : 0; mask <<= 1;
091                retval |= ForwardLimitSwitch ? mask : 0; mask <<= 1;
092                retval |= ReverseLimitSwitch ? mask : 0; mask <<= 1;
093                retval |= ForwardSoftLimit ? mask : 0; mask <<= 1;
094                retval |= ReverseSoftLimit ? mask : 0; mask <<= 1;
095                retval |= ResetDuringEn ? mask : 0; mask <<= 1;
096                retval |= SensorOverflow ? mask : 0; mask <<= 1;
097                retval |= SensorOutOfPhase ? mask : 0; mask <<= 1;
098                retval |= HardwareESDReset ? mask : 0; mask <<= 1;
099                retval |= RemoteLossOfSignal ? mask : 0; mask <<= 1;
100                retval |= APIError ? mask : 0; mask <<= 1;
101                retval |= SupplyOverV ? mask : 0; mask <<= 1;
102                retval |= SupplyUnstable ? mask : 0; mask <<= 1;
103                return retval;
104        }
105        /**
106         * Updates current fault list with specified bit field of faults
107         * 
108         * @param bits bit field of faults to update with
109         */
110        public void update(int bits) {
111                int mask = 1;
112                UnderVoltage = ((bits & mask)!=0) ? true : false; mask <<= 1;
113                ForwardLimitSwitch = ((bits & mask)!=0) ? true : false; mask <<= 1;
114                ReverseLimitSwitch = ((bits & mask)!=0) ? true : false; mask <<= 1;
115                ForwardSoftLimit = ((bits & mask)!=0) ? true : false; mask <<= 1;
116                ReverseSoftLimit = ((bits & mask)!=0) ? true : false; mask <<= 1;
117                ResetDuringEn = ((bits & mask)!=0) ? true : false; mask <<= 1;
118                SensorOverflow = ((bits & mask)!=0) ? true : false; mask <<= 1;
119                SensorOutOfPhase = ((bits & mask)!=0) ? true : false; mask <<= 1;
120                HardwareESDReset = ((bits & mask)!=0) ? true : false; mask <<= 1;
121                RemoteLossOfSignal = ((bits & mask)!=0) ? true : false; mask <<= 1;
122                APIError = ((bits & mask)!=0) ? true : false; mask <<= 1;
123                SupplyOverV = ((bits & mask)!=0) ? true : false; mask <<= 1;
124                SupplyUnstable = ((bits & mask)!=0) ? true : false; mask <<= 1;
125        }
126        public StickyFaults() {
127                UnderVoltage = false;
128                ForwardLimitSwitch = false;
129                ReverseLimitSwitch = false;
130                ForwardSoftLimit = false;
131                ReverseSoftLimit = false;
132                ResetDuringEn = false;
133                SensorOverflow = false;
134                SensorOutOfPhase = false;
135                HardwareESDReset = false;
136                RemoteLossOfSignal = false;
137                APIError = false;
138                SupplyOverV = false;
139                SupplyUnstable = false;
140        }
141        /**
142         * @return string representation of current faults tripped
143         */
144        public String toString() {
145                StringBuilder work = new StringBuilder();
146                work.append(" UnderVoltage:"); work.append(UnderVoltage ? "1" : "0");
147                work.append( " ForwardLimitSwitch:"); work.append(ForwardLimitSwitch ? "1" : "0");
148                work.append( " ReverseLimitSwitch:"); work.append(ReverseLimitSwitch ? "1" : "0");
149                work.append( " ForwardSoftLimit:"); work.append(ForwardSoftLimit ? "1" : "0");
150                work.append( " ReverseSoftLimit:"); work.append(ReverseSoftLimit ? "1" : "0");
151                work.append( " ResetDuringEn:"); work.append(ResetDuringEn ? "1" : "0");
152                work.append( " SensorOverflow:"); work.append(SensorOverflow ? "1" : "0");
153                work.append( " SensorOutOfPhase:"); work.append(SensorOutOfPhase ? "1" : "0");
154                work.append( " HardwareESDReset:"); work.append(HardwareESDReset ? "1" : "0");
155                work.append( " RemoteLossOfSignal:"); work.append(RemoteLossOfSignal ? "1" : "0");
156                work.append( " APIError:"); work.append(APIError ? "1" : "0");
157                work.append( " SupplyOverV:"); work.append(SupplyOverV ? "1" : "0");
158                work.append( " SupplyUnstable:"); work.append(SupplyUnstable ? "1" : "0");
159                return work.toString();
160        }
161};
162