[hal] Add frequency support to DutyCycle (#8076)

This commit is contained in:
Thad House
2025-07-14 23:46:17 -07:00
committed by GitHub
parent ef24c1df97
commit 3497a7d09f
31 changed files with 153 additions and 355 deletions

View File

@@ -35,18 +35,11 @@ void DutyCycle::InitDutyCycle() {
wpi::SendableRegistry::Add(this, "Duty Cycle", m_channel);
}
int DutyCycle::GetFPGAIndex() const {
int32_t status = 0;
auto retVal = HAL_GetDutyCycleFPGAIndex(m_handle, &status);
FRC_CheckErrorStatus(status, "Channel {}", GetSourceChannel());
return retVal;
}
int DutyCycle::GetFrequency() const {
units::hertz_t DutyCycle::GetFrequency() const {
int32_t status = 0;
auto retVal = HAL_GetDutyCycleFrequency(m_handle, &status);
FRC_CheckErrorStatus(status, "Channel {}", GetSourceChannel());
return retVal;
return units::hertz_t{retVal};
}
double DutyCycle::GetOutput() const {
@@ -63,13 +56,6 @@ units::second_t DutyCycle::GetHighTime() const {
return units::nanosecond_t{static_cast<double>(retVal)};
}
unsigned int DutyCycle::GetOutputScaleFactor() const {
int32_t status = 0;
auto retVal = HAL_GetDutyCycleOutputScaleFactor(m_handle, &status);
FRC_CheckErrorStatus(status, "Channel {}", GetSourceChannel());
return retVal;
}
int DutyCycle::GetSourceChannel() const {
return m_channel;
}
@@ -77,7 +63,7 @@ int DutyCycle::GetSourceChannel() const {
void DutyCycle::InitSendable(wpi::SendableBuilder& builder) {
builder.SetSmartDashboardType("Duty Cycle");
builder.AddDoubleProperty(
"Frequency", [this] { return this->GetFrequency(); }, nullptr);
"Frequency", [this] { return this->GetFrequency().value(); }, nullptr);
builder.AddDoubleProperty(
"Output", [this] { return this->GetOutput(); }, nullptr);
}

View File

@@ -122,7 +122,7 @@ void DutyCycleEncoder::SetDutyCycleRange(double min, double max) {
m_sensorMax = std::clamp(max, 0.0, 1.0);
}
int DutyCycleEncoder::GetFrequency() const {
units::hertz_t DutyCycleEncoder::GetFrequency() const {
return m_dutyCycle->GetFrequency();
}
@@ -133,9 +133,10 @@ bool DutyCycleEncoder::IsConnected() const {
return GetFrequency() > m_frequencyThreshold;
}
void DutyCycleEncoder::SetConnectedFrequencyThreshold(int frequency) {
if (frequency < 0) {
frequency = 0;
void DutyCycleEncoder::SetConnectedFrequencyThreshold(
units::hertz_t frequency) {
if (frequency < 0_Hz) {
frequency = 0_Hz;
}
m_frequencyThreshold = frequency;
}
@@ -152,10 +153,6 @@ void DutyCycleEncoder::SetAssumedFrequency(units::hertz_t frequency) {
}
}
int DutyCycleEncoder::GetFPGAIndex() const {
return m_dutyCycle->GetFPGAIndex();
}
int DutyCycleEncoder::GetSourceChannel() const {
return m_dutyCycle->GetSourceChannel();
}

View File

@@ -15,18 +15,10 @@ using namespace frc;
using namespace frc::sim;
DutyCycleSim::DutyCycleSim(const DutyCycle& dutyCycle)
: m_index{dutyCycle.GetFPGAIndex()} {}
: m_index{dutyCycle.GetSourceChannel()} {}
DutyCycleSim DutyCycleSim::CreateForChannel(int channel) {
int index = HALSIM_FindDutyCycleForChannel(channel);
if (index < 0) {
throw std::out_of_range("no duty cycle found for channel");
}
return DutyCycleSim{index};
}
DutyCycleSim DutyCycleSim::CreateForIndex(int index) {
return DutyCycleSim{index};
return DutyCycleSim{channel};
}
std::unique_ptr<CallbackStore> DutyCycleSim::RegisterInitializedCallback(
@@ -55,12 +47,12 @@ std::unique_ptr<CallbackStore> DutyCycleSim::RegisterFrequencyCallback(
return store;
}
int DutyCycleSim::GetFrequency() const {
return HALSIM_GetDutyCycleFrequency(m_index);
units::hertz_t DutyCycleSim::GetFrequency() const {
return units::hertz_t{HALSIM_GetDutyCycleFrequency(m_index)};
}
void DutyCycleSim::SetFrequency(int frequency) {
HALSIM_SetDutyCycleFrequency(m_index, frequency);
void DutyCycleSim::SetFrequency(units::hertz_t frequency) {
HALSIM_SetDutyCycleFrequency(m_index, frequency.value());
}
std::unique_ptr<CallbackStore> DutyCycleSim::RegisterOutputCallback(

View File

@@ -8,6 +8,7 @@
#include <hal/DutyCycle.h>
#include <hal/Types.h>
#include <units/frequency.h>
#include <units/time.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
@@ -17,11 +18,7 @@ namespace frc {
* Class to read a duty cycle PWM input.
*
* <p>PWM input signals are specified with a frequency and a ratio of high to
* low in that frequency. There are 8 of these in the roboRIO, and they can be
* attached to any DigitalSource.
*
* <p>These can be combined as the input of an AnalogTrigger to a Counter in
* order to implement rollover checking.
* low in that frequency. These can be attached to any SmartIO.
*
*/
class DutyCycle : public wpi::Sendable, public wpi::SendableHelper<DutyCycle> {
@@ -44,9 +41,9 @@ class DutyCycle : public wpi::Sendable, public wpi::SendableHelper<DutyCycle> {
/**
* Get the frequency of the duty cycle signal.
*
* @return frequency in Hertz
* @return frequency
*/
int GetFrequency() const;
units::hertz_t GetFrequency() const;
/**
* Get the output ratio of the duty cycle signal.
@@ -64,24 +61,6 @@ class DutyCycle : public wpi::Sendable, public wpi::SendableHelper<DutyCycle> {
*/
units::second_t GetHighTime() const;
/**
* Get the scale factor of the output.
*
* <p> An output equal to this value is always high, and then linearly scales
* down to 0. Divide a raw result by this in order to get the
* percentage between 0 and 1. Used by DMA.
*
* @return the output scale factor
*/
unsigned int GetOutputScaleFactor() const;
/**
* Get the FPGA index for the DutyCycle.
*
* @return the FPGA index
*/
int GetFPGAIndex() const;
/**
* Get the channel of the source.
*

View File

@@ -103,11 +103,11 @@ class DutyCycleEncoder : public wpi::Sendable,
DutyCycleEncoder& operator=(DutyCycleEncoder&&) = default;
/**
* Get the frequency in Hz of the duty cycle signal from the encoder.
* Get the frequency of the duty cycle signal from the encoder.
*
* @return duty cycle frequency in Hz
* @return duty cycle frequency
*/
int GetFrequency() const;
units::hertz_t GetFrequency() const;
/**
* Get if the sensor is connected
@@ -124,9 +124,9 @@ class DutyCycleEncoder : public wpi::Sendable,
* Change the frequency threshold for detecting connection used by
* IsConnected.
*
* @param frequency the minimum frequency in Hz.
* @param frequency the minimum frequency.
*/
void SetConnectedFrequencyThreshold(int frequency);
void SetConnectedFrequencyThreshold(units::hertz_t frequency);
/**
* Get the encoder value.
@@ -170,13 +170,6 @@ class DutyCycleEncoder : public wpi::Sendable,
*/
void SetInverted(bool inverted);
/**
* Get the FPGA index for the DutyCycleEncoder.
*
* @return the FPGA index
*/
int GetFPGAIndex() const;
/**
* Get the channel of the source.
*
@@ -191,7 +184,7 @@ class DutyCycleEncoder : public wpi::Sendable,
double MapSensorRange(double pos) const;
std::shared_ptr<DutyCycle> m_dutyCycle;
int m_frequencyThreshold = 100;
units::hertz_t m_frequencyThreshold = {100_Hz};
double m_fullRange;
double m_expectedZero;
units::second_t m_period{0_s};

View File

@@ -6,6 +6,8 @@
#include <memory>
#include <units/frequency.h>
#include "frc/simulation/CallbackStore.h"
namespace frc {
@@ -27,23 +29,13 @@ class DutyCycleSim {
explicit DutyCycleSim(const DutyCycle& dutyCycle);
/**
* Creates a DutyCycleSim for a digital input channel.
* Creates a DutyCycleSim for a SmartIO channel.
*
* @param channel digital input channel
* @param channel SmartIO channel
* @return Simulated object
* @throws std::out_of_range if no DutyCycle is configured for that channel
*/
static DutyCycleSim CreateForChannel(int channel);
/**
* Creates a DutyCycleSim for a simulated index.
* The index is incremented for each simulated DutyCycle.
*
* @param index simulator index
* @return Simulated object
*/
static DutyCycleSim CreateForIndex(int index);
/**
* Register a callback to be run when this duty cycle input is initialized.
*
@@ -85,14 +77,14 @@ class DutyCycleSim {
*
* @return the duty cycle frequency
*/
int GetFrequency() const;
units::hertz_t GetFrequency() const;
/**
* Change the duty cycle frequency.
*
* @param frequency the new frequency
*/
void SetFrequency(int frequency);
void SetFrequency(units::hertz_t frequency);
/**
* Register a callback to be run whenever the output changes.