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