001/*----------------------------------------------------------------------------*/
002/* Copyright (c) 2014-2018 FIRST. 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.smartdashboard.SendableBuilder;
012
013/**
014 * Class for getting voltage, current, temperature, power and energy from the Power Distribution
015 * Panel over CAN.
016 */
017public class PowerDistributionPanel extends SensorBase implements Sendable {
018  private final int m_module;
019
020  /**
021   * Constructor.
022   *
023   * @param module The CAN ID of the PDP
024   */
025  public PowerDistributionPanel(int module) {
026    m_module = module;
027    checkPDPModule(module);
028    PDPJNI.initializePDP(module);
029    setName("PowerDistributionPanel", module);
030  }
031
032  /**
033   * Constructor.  Uses the default CAN ID (0).
034   */
035  public PowerDistributionPanel() {
036    this(0);
037  }
038
039  /**
040   * Query the input voltage of the PDP.
041   *
042   * @return The voltage of the PDP in volts
043   */
044  public double getVoltage() {
045    return PDPJNI.getPDPVoltage(m_module);
046  }
047
048  /**
049   * Query the temperature of the PDP.
050   *
051   * @return The temperature of the PDP in degrees Celsius
052   */
053  public double getTemperature() {
054    return PDPJNI.getPDPTemperature(m_module);
055  }
056
057  /**
058   * Query the current of a single channel of the PDP.
059   *
060   * @return The current of one of the PDP channels (channels 0-15) in Amperes
061   */
062  public double getCurrent(int channel) {
063    double current = PDPJNI.getPDPChannelCurrent((byte) channel, m_module);
064
065    checkPDPChannel(channel);
066
067    return current;
068  }
069
070  /**
071   * Query the current of all monitored PDP channels (0-15).
072   *
073   * @return The current of all the channels in Amperes
074   */
075  public double getTotalCurrent() {
076    return PDPJNI.getPDPTotalCurrent(m_module);
077  }
078
079  /**
080   * Query the total power drawn from the monitored PDP channels.
081   *
082   * @return the total power in Watts
083   */
084  public double getTotalPower() {
085    return PDPJNI.getPDPTotalPower(m_module);
086  }
087
088  /**
089   * Query the total energy drawn from the monitored PDP channels.
090   *
091   * @return the total energy in Joules
092   */
093  public double getTotalEnergy() {
094    return PDPJNI.getPDPTotalEnergy(m_module);
095  }
096
097  /**
098   * Reset the total energy to 0.
099   */
100  public void resetTotalEnergy() {
101    PDPJNI.resetPDPTotalEnergy(m_module);
102  }
103
104  /**
105   * Clear all PDP sticky faults.
106   */
107  public void clearStickyFaults() {
108    PDPJNI.clearPDPStickyFaults(m_module);
109  }
110
111  @Override
112  public void initSendable(SendableBuilder builder) {
113    builder.setSmartDashboardType("PowerDistributionPanel");
114    for (int i = 0; i < kPDPChannels; ++i) {
115      final int chan = i;
116      builder.addDoubleProperty("Chan" + i, () -> getCurrent(chan), null);
117    }
118    builder.addDoubleProperty("Voltage", this::getVoltage, null);
119    builder.addDoubleProperty("TotalCurrent", this::getTotalCurrent, null);
120  }
121}