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"
|
|
|
|
|
|
2025-01-15 11:57:31 -08:00
|
|
|
#include <string>
|
2024-09-20 17:43:39 -07:00
|
|
|
#include <utility>
|
|
|
|
|
|
2019-11-01 23:41:30 -07:00
|
|
|
#include <hal/DutyCycle.h>
|
2025-01-15 11:57:31 -08:00
|
|
|
#include <hal/HALBase.h>
|
2025-02-07 12:37:23 -08:00
|
|
|
#include <hal/UsageReporting.h>
|
2021-05-26 07:24:53 -07:00
|
|
|
#include <wpi/NullDeleter.h>
|
2025-01-15 11:57:31 -08:00
|
|
|
#include <wpi/StackTrace.h>
|
2021-06-13 16:38:05 -07:00
|
|
|
#include <wpi/sendable/SendableBuilder.h>
|
2019-11-01 23:41:30 -07:00
|
|
|
|
2021-04-18 20:35:29 -07:00
|
|
|
#include "frc/Errors.h"
|
2025-01-15 11:57:31 -08:00
|
|
|
#include "frc/SensorUtil.h"
|
2019-11-01 23:41:30 -07:00
|
|
|
|
|
|
|
|
using namespace frc;
|
|
|
|
|
|
2025-01-15 11:57:31 -08:00
|
|
|
DutyCycle::DutyCycle(int channel) : m_channel{channel} {
|
|
|
|
|
if (!SensorUtil::CheckDigitalChannel(channel)) {
|
|
|
|
|
throw FRC_MakeError(err::ChannelIndexOutOfRange, "Channel {}", channel);
|
2019-11-01 23:41:30 -07:00
|
|
|
}
|
2021-04-18 20:35:29 -07:00
|
|
|
InitDutyCycle();
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2019-11-01 23:41:30 -07:00
|
|
|
|
|
|
|
|
void DutyCycle::InitDutyCycle() {
|
|
|
|
|
int32_t status = 0;
|
2025-01-15 11:57:31 -08:00
|
|
|
std::string stackTrace = wpi::GetStackTrace(1);
|
2025-01-30 18:59:34 -08:00
|
|
|
m_handle = HAL_InitializeDutyCycle(m_channel, stackTrace.c_str(), &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", GetSourceChannel());
|
2025-02-07 12:37:23 -08:00
|
|
|
HAL_ReportUsage("IO", m_channel, "DutyCycle");
|
2025-01-25 10:52:19 -08:00
|
|
|
wpi::SendableRegistry::Add(this, "Duty Cycle", m_channel);
|
2019-11-01 23:41:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-12 10:15:09 -07:00
|
|
|
units::second_t DutyCycle::GetHighTime() const {
|
2019-11-01 23:41:30 -07:00
|
|
|
int32_t status = 0;
|
2022-10-12 10:15:09 -07:00
|
|
|
auto retVal = HAL_GetDutyCycleHighTime(m_handle, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", GetSourceChannel());
|
2022-10-12 10:15:09 -07:00
|
|
|
return units::nanosecond_t{static_cast<double>(retVal)};
|
2019-11-01 23:41:30 -07:00
|
|
|
}
|
|
|
|
|
|
2022-10-23 07:05:09 -07:00
|
|
|
unsigned int DutyCycle::GetOutputScaleFactor() const {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
auto retVal = HAL_GetDutyCycleOutputScaleFactor(m_handle, &status);
|
|
|
|
|
FRC_CheckErrorStatus(status, "Channel {}", GetSourceChannel());
|
|
|
|
|
return retVal;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 12:58:06 -08:00
|
|
|
int DutyCycle::GetSourceChannel() const {
|
2025-01-15 11:57:31 -08:00
|
|
|
return m_channel;
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2019-11-14 22:51:33 -08:00
|
|
|
|
2021-06-13 16:38:05 -07:00
|
|
|
void DutyCycle::InitSendable(wpi::SendableBuilder& builder) {
|
2019-11-01 23:41:30 -07:00
|
|
|
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
|
|
|
}
|