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