2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-11-01 23:41:30 -07:00
|
|
|
|
|
|
|
|
#include "frc/DutyCycle.h"
|
|
|
|
|
|
|
|
|
|
#include <hal/DutyCycle.h>
|
|
|
|
|
#include <hal/FRCUsageReporting.h>
|
2021-05-26 07:24:53 -07:00
|
|
|
#include <wpi/NullDeleter.h>
|
2019-11-01 23:41:30 -07:00
|
|
|
|
|
|
|
|
#include "frc/DigitalSource.h"
|
2021-04-18 20:35:29 -07:00
|
|
|
#include "frc/Errors.h"
|
2019-11-01 23:41:30 -07:00
|
|
|
#include "frc/smartdashboard/SendableBuilder.h"
|
|
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
|
|
|
|
DutyCycle::DutyCycle(DigitalSource* source)
|
2021-05-26 07:24:53 -07:00
|
|
|
: m_source{source, wpi::NullDeleter<DigitalSource>()} {
|
2021-04-18 20:35:29 -07:00
|
|
|
if (!m_source) {
|
2021-05-23 19:33:33 -07:00
|
|
|
throw FRC_MakeError(err::NullParameter, "{}", "source");
|
2019-11-01 23:41:30 -07:00
|
|
|
}
|
2021-04-18 20:35:29 -07:00
|
|
|
InitDutyCycle();
|
2019-11-01 23:41:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DutyCycle::DutyCycle(DigitalSource& source)
|
2021-05-26 07:24:53 -07:00
|
|
|
: m_source{&source, wpi::NullDeleter<DigitalSource>()} {
|
2019-11-01 23:41:30 -07:00
|
|
|
InitDutyCycle();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DutyCycle::DutyCycle(std::shared_ptr<DigitalSource> source)
|
|
|
|
|
: m_source{std::move(source)} {
|
2021-04-18 20:35:29 -07:00
|
|
|
if (!m_source) {
|
2021-05-23 19:33:33 -07:00
|
|
|
throw FRC_MakeError(err::NullParameter, "{}", "source");
|
2019-11-01 23:41:30 -07:00
|
|
|
}
|
2021-04-18 20:35:29 -07:00
|
|
|
InitDutyCycle();
|
2019-11-01 23:41:30 -07:00
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
DutyCycle::~DutyCycle() {
|
|
|
|
|
HAL_FreeDutyCycle(m_handle);
|
|
|
|
|
}
|
2019-11-01 23:41:30 -07:00
|
|
|
|
|
|
|
|
void DutyCycle::InitDutyCycle() {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
m_handle =
|
|
|
|
|
HAL_InitializeDutyCycle(m_source->GetPortHandleForRouting(),
|
|
|
|
|
static_cast<HAL_AnalogTriggerType>(
|
|
|
|
|
m_source->GetAnalogTriggerTypeForRouting()),
|
|
|
|
|
&status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", GetSourceChannel());
|
2019-11-01 23:41:30 -07:00
|
|
|
int index = GetFPGAIndex();
|
|
|
|
|
HAL_Report(HALUsageReporting::kResourceType_DutyCycle, index + 1);
|
|
|
|
|
SendableRegistry::GetInstance().AddLW(this, "Duty Cycle", index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int DutyCycle::GetFPGAIndex() const {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
auto retVal = HAL_GetDutyCycleFPGAIndex(m_handle, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", GetSourceChannel());
|
2019-11-01 23:41:30 -07:00
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int DutyCycle::GetFrequency() const {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
auto retVal = HAL_GetDutyCycleFrequency(m_handle, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", GetSourceChannel());
|
2019-11-01 23:41:30 -07:00
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double DutyCycle::GetOutput() const {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
auto retVal = HAL_GetDutyCycleOutput(m_handle, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", GetSourceChannel());
|
2019-11-01 23:41:30 -07:00
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned int DutyCycle::GetOutputRaw() const {
|
|
|
|
|
int32_t status = 0;
|
2019-11-06 14:05:25 -08:00
|
|
|
auto retVal = HAL_GetDutyCycleOutputRaw(m_handle, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", GetSourceChannel());
|
2019-11-01 23:41:30 -07:00
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsigned int DutyCycle::GetOutputScaleFactor() const {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
auto retVal = HAL_GetDutyCycleOutputScaleFactor(m_handle, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", GetSourceChannel());
|
2019-11-01 23:41:30 -07:00
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
int DutyCycle::GetSourceChannel() const {
|
|
|
|
|
return m_source->GetChannel();
|
|
|
|
|
}
|
2019-11-14 22:51:33 -08:00
|
|
|
|
2019-11-01 23:41:30 -07:00
|
|
|
void DutyCycle::InitSendable(SendableBuilder& builder) {
|
|
|
|
|
builder.SetSmartDashboardType("Duty Cycle");
|
2020-06-27 20:39:00 -07:00
|
|
|
builder.AddDoubleProperty(
|
|
|
|
|
"Frequency", [this] { return this->GetFrequency(); }, nullptr);
|
|
|
|
|
builder.AddDoubleProperty(
|
|
|
|
|
"Output", [this] { return this->GetOutput(); }, nullptr);
|
2019-11-01 23:41:30 -07:00
|
|
|
}
|