001/* Copyright (C) Cross The Road Electronics 2024 */
002package com.ctre.phoenix.sensors;
003
004/**
005 * Faults available to Pigeon (Currently has none)
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 Pigeon2_Faults {
018    /**
019     * Device detects hardware failure
020     */
021    public boolean HardwareFault;
022    /**
023     * API error detected.  Make sure API and firmware versions are compatible.
024     */
025    public boolean APIError;
026    /**
027     * Device is under 6.5V
028     */
029    public boolean UnderVoltage;
030    /**
031     * Device was powered-on or reset while robot is enabled.
032     * Check your breakers and wiring.
033     */
034    public boolean ResetDuringEn;
035    /**
036     * The device rotated at a rate that exceeded its maximum.
037     * Increase the range or slow the rate of rotation.
038     */
039    public boolean SaturatedRotVelocity;
040    /**
041     * The device saw an acceleration that exceeded its maximum.
042     * Increase the range or avoid high-g events.
043     */
044    public boolean SaturatedAccel;
045    /**
046     * The device saw a magnetic field that exceeded its maximum.
047     * Keep the device far from strong magnetic fields.
048     */
049    public boolean SaturatedMag;
050    /**
051    /**
052     * The Pigeon saw motion as soon as it booted, and didn't
053     *  attempt to self-test its features.
054     * This isn't an issue, but to prevent this don't turn the
055     *  robot on while moving it.
056     */
057    public boolean BootIntoMotion;
058    /**
059     * The magnetometer failed its self-test.
060     * This is likely due to hardware damage, oftentimes from
061     *  exposing the Pigeon to a very large magnetic field.
062     */
063    public boolean MagnetometerFault;
064    /**
065     * The gyro failed its self-test.
066     * This is likely due to hardware damage.
067     */
068    public boolean GyroFault;
069    /**
070     * The Accelerometer failed its self-test.
071     * This is likely due to hardware damage, oftentimes from
072     *  exposing the Pigeon to a very large impact.
073     */
074    public boolean AccelFault;
075
076    /**
077     * @return true if any faults are tripped
078     */
079    public boolean hasAnyFault() {
080        return  HardwareFault |
081                APIError |
082                UnderVoltage |
083                ResetDuringEn |
084                SaturatedRotVelocity |
085                SaturatedAccel |
086                SaturatedMag |
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 <<= 1;
108        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        mask <<= 1;
133        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_Faults(int bits) {
140        update(bits);
141    }
142    public Pigeon2_Faults() {
143        update(0);
144    }
145};