001/*----------------------------------------------------------------------------*/
002/* Copyright (c) FIRST 2014-2017. 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 edu.wpi.first.wpilibj.hal.PDPJNI;
011import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
012import edu.wpi.first.wpilibj.tables.ITable;
013
014/**
015 * Class for getting voltage, current, temperature, power and energy from the Power Distribution
016 * Panel over CAN.
017 */
018public class PowerDistributionPanel extends SensorBase implements LiveWindowSendable {
019
020  private final int m_module;
021
022  /**
023   * Constructor.
024   *
025   * @param module The CAN ID of the PDP
026   */
027  public PowerDistributionPanel(int module) {
028    m_module = module;
029    checkPDPModule(module);
030    PDPJNI.initializePDP(module);
031  }
032
033  /**
034   * Constructor.  Uses the default CAN ID (0).
035   */
036  public PowerDistributionPanel() {
037    this(0);
038  }
039
040  /**
041   * Query the input voltage of the PDP.
042   *
043   * @return The voltage of the PDP in volts
044   */
045  public double getVoltage() {
046    return PDPJNI.getPDPVoltage(m_module);
047  }
048
049  /**
050   * Query the temperature of the PDP.
051   *
052   * @return The temperature of the PDP in degrees Celsius
053   */
054  public double getTemperature() {
055    return PDPJNI.getPDPTemperature(m_module);
056  }
057
058  /**
059   * Query the current of a single channel of the PDP.
060   *
061   * @return The current of one of the PDP channels (channels 0-15) in Amperes
062   */
063  public double getCurrent(int channel) {
064    double current = PDPJNI.getPDPChannelCurrent((byte) channel, m_module);
065
066    checkPDPChannel(channel);
067
068    return current;
069  }
070
071  /**
072   * Query the current of all monitored PDP channels (0-15).
073   *
074   * @return The current of all the channels in Amperes
075   */
076  public double getTotalCurrent() {
077    return PDPJNI.getPDPTotalCurrent(m_module);
078  }
079
080  /**
081   * Query the total power drawn from the monitored PDP channels.
082   *
083   * @return the total power in Watts
084   */
085  public double getTotalPower() {
086    return PDPJNI.getPDPTotalPower(m_module);
087  }
088
089  /**
090   * Query the total energy drawn from the monitored PDP channels.
091   *
092   * @return the total energy in Joules
093   */
094  public double getTotalEnergy() {
095    return PDPJNI.getPDPTotalEnergy(m_module);
096  }
097
098  /**
099   * Reset the total energy to 0.
100   */
101  public void resetTotalEnergy() {
102    PDPJNI.resetPDPTotalEnergy(m_module);
103  }
104
105  /**
106   * Clear all PDP sticky faults.
107   */
108  public void clearStickyFaults() {
109    PDPJNI.clearPDPStickyFaults(m_module);
110  }
111
112  @Override
113  public String getSmartDashboardType() {
114    return "PowerDistributionPanel";
115  }
116
117  /*
118   * Live Window code, only does anything if live window is activated.
119   */
120  private ITable m_table;
121
122  @Override
123  public void initTable(ITable subtable) {
124    m_table = subtable;
125    updateTable();
126  }
127
128  @Override
129  public ITable getTable() {
130    return m_table;
131  }
132
133  @Override
134  public void updateTable() {
135    if (m_table != null) {
136      m_table.putNumber("Chan0", getCurrent(0));
137      m_table.putNumber("Chan1", getCurrent(1));
138      m_table.putNumber("Chan2", getCurrent(2));
139      m_table.putNumber("Chan3", getCurrent(3));
140      m_table.putNumber("Chan4", getCurrent(4));
141      m_table.putNumber("Chan5", getCurrent(5));
142      m_table.putNumber("Chan6", getCurrent(6));
143      m_table.putNumber("Chan7", getCurrent(7));
144      m_table.putNumber("Chan8", getCurrent(8));
145      m_table.putNumber("Chan9", getCurrent(9));
146      m_table.putNumber("Chan10", getCurrent(10));
147      m_table.putNumber("Chan11", getCurrent(11));
148      m_table.putNumber("Chan12", getCurrent(12));
149      m_table.putNumber("Chan13", getCurrent(13));
150      m_table.putNumber("Chan14", getCurrent(14));
151      m_table.putNumber("Chan15", getCurrent(15));
152      m_table.putNumber("Voltage", getVoltage());
153      m_table.putNumber("TotalCurrent", getTotalCurrent());
154    }
155  }
156
157  /**
158   * PDP doesn't have to do anything special when entering the LiveWindow. {@inheritDoc}
159   */
160  @Override
161  public void startLiveWindowMode() {
162  }
163
164  /**
165   * PDP doesn't have to do anything special when exiting the LiveWindow. {@inheritDoc}
166   */
167  @Override
168  public void stopLiveWindowMode() {
169  }
170
171}