Files
allwpilib/wpilibc/src/main/native/cpp/hardware/rotation/DutyCycle.cpp

69 lines
2.2 KiB
C++
Raw Normal View History

// 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
2025-11-07 19:56:21 -05:00
#include "wpi/hardware/rotation/DutyCycle.hpp"
2019-11-01 23:41:30 -07:00
#include <string>
2024-09-20 17:43:39 -07:00
#include <utility>
#include "wpi/hal/DutyCycle.h"
2025-11-07 19:56:21 -05:00
#include "wpi/hal/HALBase.h"
#include "wpi/hal/UsageReporting.h"
2025-11-07 19:57:55 -05:00
#include "wpi/system/Errors.hpp"
2025-11-07 19:56:21 -05:00
#include "wpi/util/NullDeleter.hpp"
2025-11-07 19:57:55 -05:00
#include "wpi/util/SensorUtil.hpp"
2025-11-07 19:56:21 -05:00
#include "wpi/util/StackTrace.hpp"
#include "wpi/util/sendable/SendableBuilder.hpp"
2019-11-01 23:41:30 -07:00
2025-11-07 20:00:05 -05:00
using namespace wpi;
2019-11-01 23:41:30 -07:00
DutyCycle::DutyCycle(int channel) : m_channel{channel} {
if (!SensorUtil::CheckDigitalChannel(channel)) {
2025-11-07 20:00:43 -05:00
throw WPILIB_MakeError(err::ChannelIndexOutOfRange, "Channel {}", channel);
2019-11-01 23:41:30 -07:00
}
InitDutyCycle();
}
2019-11-01 23:41:30 -07:00
void DutyCycle::InitDutyCycle() {
int32_t status = 0;
2025-11-07 20:00:05 -05:00
std::string stackTrace = wpi::util::GetStackTrace(1);
2025-01-30 18:59:34 -08:00
m_handle = HAL_InitializeDutyCycle(m_channel, stackTrace.c_str(), &status);
2025-11-07 20:00:43 -05:00
WPILIB_CheckErrorStatus(status, "Channel {}", GetSourceChannel());
HAL_ReportUsage("IO", m_channel, "DutyCycle");
2025-11-07 20:00:05 -05:00
wpi::util::SendableRegistry::Add(this, "Duty Cycle", m_channel);
2019-11-01 23:41:30 -07:00
}
2025-11-07 20:00:05 -05:00
wpi::units::hertz_t DutyCycle::GetFrequency() const {
2019-11-01 23:41:30 -07:00
int32_t status = 0;
auto retVal = HAL_GetDutyCycleFrequency(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_CheckErrorStatus(status, "Channel {}", GetSourceChannel());
2025-11-07 20:00:05 -05:00
return wpi::units::hertz_t{retVal};
2019-11-01 23:41:30 -07:00
}
double DutyCycle::GetOutput() const {
int32_t status = 0;
auto retVal = HAL_GetDutyCycleOutput(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_CheckErrorStatus(status, "Channel {}", GetSourceChannel());
2019-11-01 23:41:30 -07:00
return retVal;
}
2025-11-07 20:00:05 -05:00
wpi::units::second_t DutyCycle::GetHighTime() const {
2019-11-01 23:41:30 -07:00
int32_t status = 0;
auto retVal = HAL_GetDutyCycleHighTime(m_handle, &status);
2025-11-07 20:00:43 -05:00
WPILIB_CheckErrorStatus(status, "Channel {}", GetSourceChannel());
2025-11-07 20:00:05 -05:00
return wpi::units::nanosecond_t{static_cast<double>(retVal)};
2019-11-01 23:41:30 -07:00
}
int DutyCycle::GetSourceChannel() const {
return m_channel;
}
2025-11-07 20:00:05 -05:00
void DutyCycle::InitSendable(wpi::util::SendableBuilder& builder) {
2019-11-01 23:41:30 -07:00
builder.SetSmartDashboardType("Duty Cycle");
builder.AddDoubleProperty(
"Frequency", [this] { return this->GetFrequency().value(); }, nullptr);
builder.AddDoubleProperty(
"Output", [this] { return this->GetOutput(); }, nullptr);
2019-11-01 23:41:30 -07:00
}