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
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/PowerDistributionPanel.h"
|
2016-09-05 13:55:31 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include <hal/HAL.h>
|
|
|
|
|
#include <hal/PDP.h>
|
|
|
|
|
#include <hal/Ports.h>
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/SmallString.h>
|
|
|
|
|
#include <wpi/raw_ostream.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/SensorUtil.h"
|
|
|
|
|
#include "frc/WPIErrors.h"
|
|
|
|
|
#include "frc/smartdashboard/SendableBuilder.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
|
|
|
|
2018-06-07 22:31:26 -07:00
|
|
|
/**
|
|
|
|
|
* Initialize the PDP.
|
|
|
|
|
*/
|
|
|
|
|
PowerDistributionPanel::PowerDistributionPanel(int module) {
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t status = 0;
|
2018-06-07 22:31:26 -07:00
|
|
|
m_handle = HAL_InitializePDP(module, &status);
|
2016-07-12 10:45:14 -07:00
|
|
|
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
|
|
|
return;
|
|
|
|
|
}
|
2018-06-25 02:48:22 -04:00
|
|
|
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_PDP, module);
|
2017-12-04 23:28:33 -08:00
|
|
|
SetName("PowerDistributionPanel", module);
|
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;
|
|
|
|
|
|
2018-06-07 22:31:26 -07:00
|
|
|
double voltage = HAL_GetPDPVoltage(m_handle, &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-11-20 07:25:03 -08:00
|
|
|
double PowerDistributionPanel::GetTemperature() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
|
|
|
|
|
2018-06-07 22:31:26 -07:00
|
|
|
double temperature = HAL_GetPDPTemperature(m_handle, &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-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;
|
|
|
|
|
|
2018-05-23 20:22:30 -07:00
|
|
|
if (!SensorUtil::CheckPDPChannel(channel)) {
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::SmallString<32> str;
|
|
|
|
|
wpi::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
|
|
|
}
|
|
|
|
|
|
2018-06-07 22:31:26 -07:00
|
|
|
double current = HAL_GetPDPChannelCurrent(m_handle, 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
|
|
|
}
|
|
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double PowerDistributionPanel::GetTotalCurrent() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
|
|
|
|
|
2018-06-07 22:31:26 -07:00
|
|
|
double current = HAL_GetPDPTotalCurrent(m_handle, &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-11-20 07:25:03 -08:00
|
|
|
double PowerDistributionPanel::GetTotalPower() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
|
|
|
|
|
2018-06-07 22:31:26 -07:00
|
|
|
double power = HAL_GetPDPTotalPower(m_handle, &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-11-20 07:25:03 -08:00
|
|
|
double PowerDistributionPanel::GetTotalEnergy() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
|
|
|
|
|
2018-06-07 22:31:26 -07:00
|
|
|
double energy = HAL_GetPDPTotalEnergy(m_handle, &status);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
if (status) {
|
|
|
|
|
wpi_setWPIErrorWithContext(Timeout, "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return energy;
|
2014-11-25 00:52:41 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void PowerDistributionPanel::ResetTotalEnergy() {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
|
2018-06-07 22:31:26 -07:00
|
|
|
HAL_ResetPDPTotalEnergy(m_handle, &status);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
if (status) {
|
|
|
|
|
wpi_setWPIErrorWithContext(Timeout, "");
|
|
|
|
|
}
|
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
|
|
|
|
2018-06-07 22:31:26 -07:00
|
|
|
HAL_ClearPDPStickyFaults(m_handle, &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");
|
2018-05-23 20:22:30 -07:00
|
|
|
for (int i = 0; i < SensorUtil::kPDPChannels; ++i) {
|
2018-04-29 23:33:19 -07:00
|
|
|
builder.AddDoubleProperty("Chan" + wpi::Twine(i),
|
2017-12-04 23:28:33 -08:00
|
|
|
[=]() { 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
|
|
|
}
|