2014-05-30 14:04:05 -04:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-08-23 22:06:13 -07:00
|
|
|
/* Copyright (c) 2014-2017 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>
|
|
|
|
|
|
2014-12-30 12:02:13 -08:00
|
|
|
#include "LiveWindow/LiveWindow.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;
|
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void PowerDistributionPanel::UpdateTable() {
|
2017-09-02 00:17:43 -07:00
|
|
|
for (size_t i = 0; i < sizeof(m_chanEntry) / sizeof(m_chanEntry[0]); ++i) {
|
|
|
|
|
if (m_chanEntry[i]) m_chanEntry[i].SetDouble(GetCurrent(i));
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2017-09-02 00:17:43 -07:00
|
|
|
if (m_voltageEntry) m_voltageEntry.SetDouble(GetVoltage());
|
|
|
|
|
if (m_totalCurrentEntry) m_totalCurrentEntry.SetDouble(GetTotalCurrent());
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PowerDistributionPanel::StartLiveWindowMode() {}
|
|
|
|
|
|
|
|
|
|
void PowerDistributionPanel::StopLiveWindowMode() {}
|
2014-12-30 12:02:13 -08:00
|
|
|
|
2015-06-19 17:23:54 -07:00
|
|
|
std::string PowerDistributionPanel::GetSmartDashboardType() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
return "PowerDistributionPanel";
|
2014-12-30 12:02:13 -08:00
|
|
|
}
|
|
|
|
|
|
2017-09-02 00:17:43 -07:00
|
|
|
void PowerDistributionPanel::InitTable(
|
|
|
|
|
std::shared_ptr<nt::NetworkTable> subTable) {
|
2015-06-25 15:07:55 -04:00
|
|
|
m_table = subTable;
|
2017-09-02 00:17:43 -07:00
|
|
|
if (m_table != nullptr) {
|
|
|
|
|
for (size_t i = 0; i < sizeof(m_chanEntry) / sizeof(m_chanEntry[0]); ++i) {
|
|
|
|
|
llvm::SmallString<32> buf;
|
|
|
|
|
llvm::raw_svector_ostream oss(buf);
|
|
|
|
|
oss << "Chan" << i;
|
|
|
|
|
m_chanEntry[i] = m_table->GetEntry(oss.str());
|
|
|
|
|
}
|
|
|
|
|
m_voltageEntry = m_table->GetEntry("Voltage");
|
|
|
|
|
m_totalCurrentEntry = m_table->GetEntry("TotalCurrent");
|
|
|
|
|
UpdateTable();
|
|
|
|
|
} else {
|
|
|
|
|
for (size_t i = 0; i < sizeof(m_chanEntry) / sizeof(m_chanEntry[0]); ++i) {
|
|
|
|
|
m_chanEntry[i] = nt::NetworkTableEntry();
|
|
|
|
|
}
|
|
|
|
|
m_voltageEntry = nt::NetworkTableEntry();
|
|
|
|
|
m_totalCurrentEntry = nt::NetworkTableEntry();
|
|
|
|
|
}
|
2014-12-30 12:02:13 -08:00
|
|
|
}
|
|
|
|
|
|
2017-09-02 00:17:43 -07:00
|
|
|
std::shared_ptr<nt::NetworkTable> PowerDistributionPanel::GetTable() const {
|
2016-05-20 17:30:37 -07:00
|
|
|
return m_table;
|
|
|
|
|
}
|