001/*----------------------------------------------------------------------------*/
002/* Copyright (c) FIRST 2014. All Rights Reserved.                             */
003/* Open Source Software - may be modified and shared by FRC teams. The code   */
004/* must be accompanied by the FIRST BSD license file in the root directory of */
005/* the project.                                                               */
006/*----------------------------------------------------------------------------*/
007
008package edu.wpi.first.wpilibj;
009
010import java.nio.ByteBuffer;
011import java.nio.ByteOrder;
012
013import edu.wpi.first.wpilibj.hal.PDPJNI;
014import edu.wpi.first.wpilibj.hal.HALUtil;
015import edu.wpi.first.wpilibj.livewindow.LiveWindow;
016import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
017import edu.wpi.first.wpilibj.tables.ITable;
018
019/**
020 * Class for getting voltage, current, temperature, power and energy from the CAN PDP.
021 * The PDP must be at CAN Address 0.
022 * @author Thomas Clark
023 */
024public class PowerDistributionPanel extends SensorBase implements LiveWindowSendable {
025        public PowerDistributionPanel() {
026        }
027
028        /**
029         * Query the input voltage of the PDP
030         * @return The voltage of the PDP in volts
031         */
032        public double getVoltage() {
033                ByteBuffer status = ByteBuffer.allocateDirect(4);
034                status.order(ByteOrder.LITTLE_ENDIAN);
035
036                double voltage = PDPJNI.getPDPVoltage(status.asIntBuffer());
037
038                return voltage;
039        }
040
041        /**
042         * Query the temperature of the PDP
043         * @return The temperature of the PDP in degrees Celsius
044         */
045        public double getTemperature() {
046                ByteBuffer status = ByteBuffer.allocateDirect(4);
047                status.order(ByteOrder.LITTLE_ENDIAN);
048
049                double temperature = PDPJNI.getPDPTemperature(status.asIntBuffer());
050
051                return temperature;
052        }
053
054        /**
055         * Query the current of a single channel of the PDP
056         * @return The current of one of the PDP channels (channels 0-15) in Amperes
057         */
058        public double getCurrent(int channel) {
059                ByteBuffer status = ByteBuffer.allocateDirect(4);
060                status.order(ByteOrder.LITTLE_ENDIAN);
061
062                double current = PDPJNI.getPDPChannelCurrent((byte)channel, status.asIntBuffer());
063
064                checkPDPChannel(channel);
065
066                return current;
067        }
068
069        /**
070        * Query the current of all monitored PDP channels (0-15)
071        * @return The current of all the channels in Amperes
072        */
073        public double getTotalCurrent(){
074                ByteBuffer status = ByteBuffer.allocateDirect(4);
075                status.order(ByteOrder.LITTLE_ENDIAN);
076
077                double current = PDPJNI.getPDPTotalCurrent(status.asIntBuffer());
078
079                return current;
080        }
081
082        /**
083        * Query the total power drawn from the monitored PDP channels
084        * @return the total power in Watts
085        */
086        public double getTotalPower(){
087                ByteBuffer status = ByteBuffer.allocateDirect(4);
088                status.order(ByteOrder.LITTLE_ENDIAN);
089
090                double power = PDPJNI.getPDPTotalPower(status.asIntBuffer());
091
092                return power;
093
094        }
095
096        /**
097        * Query the total energy drawn from the monitored PDP channels
098        * @return the total energy in Joules
099        */
100        public double getTotalEnergy(){
101                ByteBuffer status = ByteBuffer.allocateDirect(4);
102                status.order(ByteOrder.LITTLE_ENDIAN);
103
104                double energy = PDPJNI.getPDPTotalEnergy(status.asIntBuffer());
105
106                return energy;
107        }
108
109        /**
110        * Reset the total energy to 0
111        */
112        public void resetTotalEnergy(){
113                ByteBuffer status = ByteBuffer.allocateDirect(4);
114                status.order(ByteOrder.LITTLE_ENDIAN);
115
116                PDPJNI.resetPDPTotalEnergy(status.asIntBuffer());
117        }
118
119        /**
120        * Clear all PDP sticky faults
121        */
122        public void clearStickyFaults(){
123                ByteBuffer status = ByteBuffer.allocateDirect(4);
124                status.order(ByteOrder.LITTLE_ENDIAN);
125                
126                PDPJNI.clearPDPStickyFaults(status.asIntBuffer());
127        }
128        
129    public String getSmartDashboardType() {
130        return "PowerDistributionPanel";
131    }
132    /*
133     * Live Window code, only does anything if live window is activated.
134     */
135    private ITable m_table;
136
137    /**
138     * {@inheritDoc}
139     */
140    public void initTable(ITable subtable) {
141        m_table = subtable;
142        updateTable();
143    }
144
145    /**
146     * {@inheritDoc}
147     */
148    public ITable getTable() {
149        return m_table;
150    }
151
152    /**
153     * {@inheritDoc}
154     */
155    public void updateTable() {
156        if (m_table != null) {
157            m_table.putNumber("Chan0", getCurrent(0));
158            m_table.putNumber("Chan1", getCurrent(1));
159            m_table.putNumber("Chan2", getCurrent(2));
160            m_table.putNumber("Chan3", getCurrent(3));
161            m_table.putNumber("Chan4", getCurrent(4));
162            m_table.putNumber("Chan5", getCurrent(5));
163            m_table.putNumber("Chan6", getCurrent(6));
164            m_table.putNumber("Chan7", getCurrent(7));
165            m_table.putNumber("Chan8", getCurrent(8));
166            m_table.putNumber("Chan9", getCurrent(9));
167            m_table.putNumber("Chan10", getCurrent(10));
168            m_table.putNumber("Chan11", getCurrent(11));
169            m_table.putNumber("Chan12", getCurrent(12));
170            m_table.putNumber("Chan13", getCurrent(13));
171            m_table.putNumber("Chan14", getCurrent(14));
172            m_table.putNumber("Chan15", getCurrent(15));
173            m_table.putNumber("Voltage", getVoltage());
174            m_table.putNumber("TotalCurrent", getTotalCurrent());
175        }
176    }
177
178    /**
179     * PDP doesn't have to do anything special when entering the LiveWindow.
180     * {@inheritDoc}
181     */
182    public void startLiveWindowMode() {}
183
184    /**
185     * PDP doesn't have to do anything special when exiting the LiveWindow.
186     * {@inheritDoc}
187     */
188    public void stopLiveWindowMode() {}
189
190}