001package com.ctre.phoenix.sensors;
002
003/**
004 * Faults available to Pigeon (Currently has none)
005 */
006public class Pigeon2_StickyFaults {
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     * The Motion Driver Software took too long to complete.
041     * This is typical when calibrating.
042     */
043    public boolean MotionDriverTookTooLong;
044    /**
045     * The Pigeon missed an opportunity to acquire data.
046     * This is typical when calibrating.
047     */
048    public boolean DataAcquiredLate;
049    /**
050     * The Pigeon saw motion as soon as it booted, and didn't
051     *  attempt to self-test its features.
052     * This isn't an issue, but to prevent this don't turn the
053     *  robot on while moving it.
054     */
055    public boolean BootIntoMotion;
056    /**
057     * The magnetometer failed its self-test.
058     * This is likely due to hardware damage, oftentimes from
059     *  exposing the Pigeon to a very large magnetic field.
060     */
061    public boolean MagnetometerFault;
062    /**
063     * The gyro failed its self-test.
064     * This is likely due to hardware damage.
065     */
066    public boolean GyroFault;
067    /**
068     * The Accelerometer failed its self-test.
069     * This is likely due to hardware damage, oftentimes from
070     *  exposing the Pigeon to a very large impact.
071     */
072    public boolean AccelFault;
073
074    /**
075     * @return true if any faults are tripped
076     */
077    public boolean hasAnyFault() {
078        return  HardwareFault |
079                APIError |
080                UnderVoltage |
081                ResetDuringEn |
082                SaturatedRotVelocity |
083                SaturatedAccel |
084                SaturatedMag |
085                MotionDriverTookTooLong |
086                DataAcquiredLate |
087                BootIntoMotion |
088                MagnetometerFault |
089                GyroFault |
090                AccelFault;
091    }
092    /**
093     * @return Current fault list as a bit field
094     */
095    public int toBitfield() {
096        int commonFaults = 0;
097        commonFaults |= HardwareFault ? 1 : 0; commonFaults <<= 1;
098        commonFaults |= APIError ? 1 : 0; commonFaults <<= 1;
099        commonFaults |= UnderVoltage ? 1 : 0; commonFaults <<= 1;
100        commonFaults |= ResetDuringEn ? 1 : 0; commonFaults <<= 1;
101        
102        int deviceFaults = 0;
103        deviceFaults |= SaturatedRotVelocity ? 1 : 0; deviceFaults <<= 1;
104        deviceFaults |= SaturatedAccel ? 1 : 0; deviceFaults <<= 1;
105        deviceFaults |= SaturatedMag ? 1 : 0; deviceFaults <<= 1;
106        deviceFaults <<= 1; /* Unused bitfield */
107        deviceFaults |= MotionDriverTookTooLong ? 1 : 0; deviceFaults <<= 1;
108        deviceFaults |= DataAcquiredLate ? 1 : 0; deviceFaults <<= 1;
109        deviceFaults |= BootIntoMotion ? 1 : 0; deviceFaults <<= 1;
110        deviceFaults |= MagnetometerFault ? 1 : 0; deviceFaults <<= 1;
111        deviceFaults |= GyroFault ? 1 : 0; deviceFaults <<= 1;
112        deviceFaults |= AccelFault ? 1 : 0; deviceFaults <<= 1;
113        
114        return commonFaults | (deviceFaults << 30);
115    }
116    /**
117     * Updates current fault list with specified bit field of faults
118     * 
119     * @param bits bit field of faults to update with
120     */
121    public void update(int bits) {
122        int mask = 1;
123        HardwareFault = ((bits & mask) != 0) ? true : false; mask <<= 1;
124        APIError =              ((bits & mask) != 0) ? true : false; mask <<= 1;
125        UnderVoltage =  ((bits & mask) != 0) ? true : false; mask <<= 1;
126        ResetDuringEn = ((bits & mask) != 0) ? true : false; mask <<= 1;
127        mask <<= 30; /* 30 faults currently unused */
128        AccelFault = ((bits & mask) != 0) ? true : false; mask <<= 1;
129        GyroFault = ((bits & mask) != 0) ? true : false; mask <<= 1;
130        MagnetometerFault = ((bits & mask) != 0) ? true : false; mask <<= 1;
131        BootIntoMotion = ((bits & mask) != 0) ? true : false; mask <<= 1;
132        DataAcquiredLate = ((bits & mask) != 0) ? true : false; mask <<= 1;
133        MotionDriverTookTooLong = ((bits & mask) != 0) ? true : false; mask <<= 1;
134        mask <<= 1; /* unused bit field */
135        SaturatedMag = ((bits & mask) != 0) ? true : false; mask <<= 1;
136        SaturatedAccel = ((bits & mask) != 0) ? true : false; mask <<= 1;
137        SaturatedRotVelocity = ((bits & mask) != 0) ? true : false; mask <<= 1;
138    }
139    public Pigeon2_StickyFaults(int bits) {
140        update(bits);
141    }
142    public Pigeon2_StickyFaults() {
143        update(0);
144    }
145};