2014-05-30 14:04:05 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2014-2018 FIRST. All Rights Reserved. */
|
2014-05-30 14:04:05 -04:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2014-05-30 14:04:05 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "PowerDistributionPanel.h"
|
2016-09-05 13:55:31 -07:00
|
|
|
|
2017-08-27 00:11:52 -07:00
|
|
|
#include <HAL/HAL.h>
|
|
|
|
|
#include <HAL/PDP.h>
|
|
|
|
|
#include <HAL/Ports.h>
|
|
|
|
|
#include <llvm/SmallString.h>
|
|
|
|
|
#include <llvm/raw_ostream.h>
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
#include "SmartDashboard/SendableBuilder.h"
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "WPIErrors.h"
|
2014-05-30 14:04:05 -04:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2015-06-29 12:40:24 -04:00
|
|
|
PowerDistributionPanel::PowerDistributionPanel() : PowerDistributionPanel(0) {}
|
2015-06-16 16:47:35 -04:00
|
|
|
|
2014-05-30 14:04:05 -04:00
|
|
|
/**
|
|
|
|
|
* Initialize the PDP.
|
|
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
PowerDistributionPanel::PowerDistributionPanel(int module) : m_module(module) {
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_InitializePDP(m_module, &status);
|
|
|
|
|
if (status != 0) {
|
2016-07-13 20:29:28 -07:00
|
|
|
wpi_setErrorWithContextRange(status, 0, HAL_GetNumPDPModules(), module,
|
|
|
|
|
HAL_GetErrorMessage(status));
|
2016-07-12 10:45:14 -07:00
|
|
|
m_module = -1;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-12-04 23:28:33 -08:00
|
|
|
SetName("PowerDistributionPanel", module);
|
2014-05-30 14:04:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Query the input voltage of the PDP.
|
|
|
|
|
*
|
2015-01-02 16:08:19 -08:00
|
|
|
* @return The voltage of the PDP in volts
|
2014-05-30 14:04:05 -04:00
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double PowerDistributionPanel::GetVoltage() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double voltage = HAL_GetPDPVoltage(m_module, &status);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
if (status) {
|
|
|
|
|
wpi_setWPIErrorWithContext(Timeout, "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return voltage;
|
2014-05-30 14:04:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Query the temperature of the PDP.
|
|
|
|
|
*
|
2014-05-30 14:04:05 -04:00
|
|
|
* @return The temperature of the PDP in degrees Celsius
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double PowerDistributionPanel::GetTemperature() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double temperature = HAL_GetPDPTemperature(m_module, &status);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
if (status) {
|
|
|
|
|
wpi_setWPIErrorWithContext(Timeout, "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return temperature;
|
2014-05-30 14:04:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Query the current of a single channel of the PDP.
|
|
|
|
|
*
|
2014-07-02 15:15:56 -04:00
|
|
|
* @return The current of one of the PDP channels (channels 0-15) in Amperes
|
2014-05-30 14:04:05 -04:00
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double PowerDistributionPanel::GetCurrent(int channel) const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
|
|
|
|
|
|
|
|
|
if (!CheckPDPChannel(channel)) {
|
2017-05-15 23:10:40 -07:00
|
|
|
llvm::SmallString<32> str;
|
|
|
|
|
llvm::raw_svector_ostream buf(str);
|
2015-06-30 15:01:20 -04:00
|
|
|
buf << "PDP Channel " << channel;
|
|
|
|
|
wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, buf.str());
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double current = HAL_GetPDPChannelCurrent(m_module, channel, &status);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
if (status) {
|
|
|
|
|
wpi_setWPIErrorWithContext(Timeout, "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return current;
|
2014-05-30 14:04:05 -04:00
|
|
|
}
|
|
|
|
|
|
2014-11-25 00:52:41 -05:00
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Query the total current of all monitored PDP channels (0-15).
|
|
|
|
|
*
|
2014-11-25 00:52:41 -05:00
|
|
|
* @return The the total current drawn from the PDP channels in Amperes
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double PowerDistributionPanel::GetTotalCurrent() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double current = HAL_GetPDPTotalCurrent(m_module, &status);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
if (status) {
|
|
|
|
|
wpi_setWPIErrorWithContext(Timeout, "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return current;
|
2014-11-25 00:52:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Query the total power drawn from the monitored PDP channels.
|
|
|
|
|
*
|
2015-01-02 16:08:19 -08:00
|
|
|
* @return The the total power drawn from the PDP channels in Watts
|
2014-11-25 00:52:41 -05:00
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double PowerDistributionPanel::GetTotalPower() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double power = HAL_GetPDPTotalPower(m_module, &status);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
if (status) {
|
|
|
|
|
wpi_setWPIErrorWithContext(Timeout, "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return power;
|
2014-11-25 00:52:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Query the total energy drawn from the monitored PDP channels.
|
|
|
|
|
*
|
2015-01-02 16:08:19 -08:00
|
|
|
* @return The the total energy drawn from the PDP channels in Joules
|
2014-11-25 00:52:41 -05:00
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double PowerDistributionPanel::GetTotalEnergy() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double energy = HAL_GetPDPTotalEnergy(m_module, &status);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
if (status) {
|
|
|
|
|
wpi_setWPIErrorWithContext(Timeout, "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return energy;
|
2014-11-25 00:52:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Reset the total energy drawn from the PDP.
|
|
|
|
|
*
|
2014-11-25 00:52:41 -05:00
|
|
|
* @see PowerDistributionPanel#GetTotalEnergy
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void PowerDistributionPanel::ResetTotalEnergy() {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_ResetPDPTotalEnergy(m_module, &status);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
if (status) {
|
|
|
|
|
wpi_setWPIErrorWithContext(Timeout, "");
|
|
|
|
|
}
|
2014-11-25 00:52:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Remove all of the fault flags on the PDP.
|
2014-11-25 00:52:41 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void PowerDistributionPanel::ClearStickyFaults() {
|
|
|
|
|
int32_t status = 0;
|
2014-11-25 00:52:41 -05:00
|
|
|
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_ClearPDPStickyFaults(m_module, &status);
|
2014-12-30 12:02:13 -08:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
if (status) {
|
|
|
|
|
wpi_setWPIErrorWithContext(Timeout, "");
|
|
|
|
|
}
|
2014-12-30 12:02:13 -08:00
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void PowerDistributionPanel::InitSendable(SendableBuilder& builder) {
|
|
|
|
|
builder.SetSmartDashboardType("PowerDistributionPanel");
|
|
|
|
|
for (int i = 0; i < kPDPChannels; ++i) {
|
|
|
|
|
builder.AddDoubleProperty("Chan" + llvm::Twine(i),
|
|
|
|
|
[=]() { return GetCurrent(i); }, nullptr);
|
2017-09-02 00:17:43 -07:00
|
|
|
}
|
2017-12-04 23:28:33 -08:00
|
|
|
builder.AddDoubleProperty("Voltage", [=]() { return GetVoltage(); }, nullptr);
|
|
|
|
|
builder.AddDoubleProperty("TotalCurrent", [=]() { return GetTotalCurrent(); },
|
|
|
|
|
nullptr);
|
2014-12-30 12:02:13 -08:00
|
|
|
}
|