001package com.ctre.phoenix.sensors;
002
003/**
004 * Faults available to Pigeon (Currently has none)
005 */
006public class Pigeon2_Faults {
007    /**
008     * Device detects hardware failure
009     */
010    public boolean HardwareFault;
011    /**
012     * API error detected.  Make sure API and firmware versions are compatible.
013     */
014    public boolean APIError;
015    /**
016     * Device is under 6.5V
017     */
018    public boolean UnderVoltage;
019    /**
020     * Device was powered-on or reset while robot is enabled.
021     * Check your breakers and wiring.
022     */
023    public boolean ResetDuringEn;
024    /**
025     * The device rotated at a rate that exceeded its maximum.
026     * Increase the range or slow the rate of rotation.
027     */
028    public boolean SaturatedRotVelocity;
029    /**
030     * The device saw an acceleration that exceeded its maximum.
031     * Increase the range or avoid high-g events.
032     */
033    public boolean SaturatedAccel;
034    /**
035     * The device saw a magnetic field that exceeded its maximum.
036     * Keep the device far from strong magnetic fields.
037     */
038    public boolean SaturatedMag;
039    /**
040    /**
041     * The Pigeon saw motion as soon as it booted, and didn't
042     *  attempt to self-test its features.
043     * This isn't an issue, but to prevent this don't turn the
044     *  robot on while moving it.
045     */
046    public boolean BootIntoMotion;
047    /**
048     * The magnetometer failed its self-test.
049     * This is likely due to hardware damage, oftentimes from
050     *  exposing the Pigeon to a very large magnetic field.
051     */
052    public boolean MagnetometerFault;
053    /**
054     * The gyro failed its self-test.
055     * This is likely due to hardware damage.
056     */
057    public boolean GyroFault;
058    /**
059     * The Accelerometer failed its self-test.
060     * This is likely due to hardware damage, oftentimes from
061     *  exposing the Pigeon to a very large impact.
062     */
063    public boolean AccelFault;
064
065    /**
066     * @return true if any faults are tripped
067     */
068    public boolean hasAnyFault() {
069        return  HardwareFault |
070                APIError |
071                UnderVoltage |
072                ResetDuringEn |
073                SaturatedRotVelocity |
074                SaturatedAccel |
075                SaturatedMag |
076                BootIntoMotion |
077                MagnetometerFault |
078                GyroFault |
079                AccelFault;
080    }
081    /**
082     * @return Current fault list as a bit field
083     */
084    public int toBitfield() {
085        int commonFaults = 0;
086        commonFaults |= HardwareFault ? 1 : 0; commonFaults <<= 1;
087        commonFaults |= APIError ? 1 : 0; commonFaults <<= 1;
088        commonFaults |= UnderVoltage ? 1 : 0; commonFaults <<= 1;
089        commonFaults |= ResetDuringEn ? 1 : 0; commonFaults <<= 1;
090        
091        int deviceFaults = 0;
092        deviceFaults |= SaturatedRotVelocity ? 1 : 0; deviceFaults <<= 1;
093        deviceFaults |= SaturatedAccel ? 1 : 0; deviceFaults <<= 1;
094        deviceFaults |= SaturatedMag ? 1 : 0; deviceFaults <<= 1;
095        deviceFaults <<= 1; /* Unused bitfield */
096        deviceFaults <<= 1;
097        deviceFaults <<= 1;
098        deviceFaults |= BootIntoMotion ? 1 : 0; deviceFaults <<= 1;
099        deviceFaults |= MagnetometerFault ? 1 : 0; deviceFaults <<= 1;
100        deviceFaults |= GyroFault ? 1 : 0; deviceFaults <<= 1;
101        deviceFaults |= AccelFault ? 1 : 0; deviceFaults <<= 1;
102        
103        return commonFaults | (deviceFaults << 30);
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        HardwareFault = ((bits & mask) != 0) ? true : false; mask <<= 1;
113        APIError =              ((bits & mask) != 0) ? true : false; mask <<= 1;
114        UnderVoltage =  ((bits & mask) != 0) ? true : false; mask <<= 1;
115        ResetDuringEn = ((bits & mask) != 0) ? true : false; mask <<= 1;
116        mask <<= 30; /* 30 faults currently unused */
117        AccelFault = ((bits & mask) != 0) ? true : false; mask <<= 1;
118        GyroFault = ((bits & mask) != 0) ? true : false; mask <<= 1;
119        MagnetometerFault = ((bits & mask) != 0) ? true : false; mask <<= 1;
120        BootIntoMotion = ((bits & mask) != 0) ? true : false; mask <<= 1;
121        mask <<= 1;
122        mask <<= 1;
123        mask <<= 1; /* unused bit field */
124        SaturatedMag = ((bits & mask) != 0) ? true : false; mask <<= 1;
125        SaturatedAccel = ((bits & mask) != 0) ? true : false; mask <<= 1;
126        SaturatedRotVelocity = ((bits & mask) != 0) ? true : false; mask <<= 1;
127    }
128    public Pigeon2_Faults(int bits) {
129        update(bits);
130    }
131    public Pigeon2_Faults() {
132        update(0);
133    }
134};