[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

@@ -34,7 +34,7 @@ public class DutyCycleJNI extends JNIWrapper {
* @return frequency in Hertz
* @see "HAL_GetDutyCycleFrequency"
*/
public static native int getFrequency(int handle);
public static native double getFrequency(int handle);
/**
* Get the output ratio of the duty cycle signal.
@@ -56,27 +56,6 @@ public class DutyCycleJNI extends JNIWrapper {
*/
public static native int getHighTime(int handle);
/**
* 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.
*
* @param handle the duty cycle handle
* @return the output scale factor
* @see "HAL_GetDutyCycleOutputScaleFactor"
*/
public static native int getOutputScaleFactor(int handle);
/**
* Get the FPGA index for the DutyCycle.
*
* @param handle the duty cycle handle
* @return the FPGA index
* @see "HAL_GetDutyCycleFPGAIndex"
*/
public static native int getFPGAIndex(int handle);
/** Utility class. */
private DutyCycleJNI() {}
}

View File

@@ -22,9 +22,9 @@ public class DutyCycleDataJNI extends JNIWrapper {
public static native void cancelFrequencyCallback(int index, int uid);
public static native int getFrequency(int index);
public static native double getFrequency(int index);
public static native void setFrequency(int index, int frequency);
public static native void setFrequency(int index, double frequency);
public static native int registerOutputCallback(
int index, NotifyCallback callback, boolean initialNotify);
@@ -37,8 +37,6 @@ public class DutyCycleDataJNI extends JNIWrapper {
public static native void resetData(int index);
public static native int findForChannel(int channel);
/** Utility class. */
private DutyCycleDataJNI() {}
}

View File

@@ -46,9 +46,9 @@ Java_edu_wpi_first_hal_DutyCycleJNI_free
/*
* Class: edu_wpi_first_hal_DutyCycleJNI
* Method: getFrequency
* Signature: (I)I
* Signature: (I)D
*/
JNIEXPORT jint JNICALL
JNIEXPORT jdouble JNICALL
Java_edu_wpi_first_hal_DutyCycleJNI_getFrequency
(JNIEnv* env, jclass, jint handle)
{
@@ -91,36 +91,4 @@ Java_edu_wpi_first_hal_DutyCycleJNI_getHighTime
return retVal;
}
/*
* Class: edu_wpi_first_hal_DutyCycleJNI
* Method: getOutputScaleFactor
* Signature: (I)I
*/
JNIEXPORT jint JNICALL
Java_edu_wpi_first_hal_DutyCycleJNI_getOutputScaleFactor
(JNIEnv* env, jclass, jint handle)
{
int32_t status = 0;
auto retVal = HAL_GetDutyCycleOutputScaleFactor(
static_cast<HAL_DutyCycleHandle>(handle), &status);
CheckStatus(env, status);
return retVal;
}
/*
* Class: edu_wpi_first_hal_DutyCycleJNI
* Method: getFPGAIndex
* Signature: (I)I
*/
JNIEXPORT jint JNICALL
Java_edu_wpi_first_hal_DutyCycleJNI_getFPGAIndex
(JNIEnv* env, jclass, jint handle)
{
int32_t status = 0;
auto retVal = HAL_GetDutyCycleFPGAIndex(
static_cast<HAL_DutyCycleHandle>(handle), &status);
CheckStatus(env, status);
return retVal;
}
} // extern "C"

View File

@@ -91,9 +91,9 @@ Java_edu_wpi_first_hal_simulation_DutyCycleDataJNI_cancelFrequencyCallback
/*
* Class: edu_wpi_first_hal_simulation_DutyCycleDataJNI
* Method: getFrequency
* Signature: (I)I
* Signature: (I)D
*/
JNIEXPORT jint JNICALL
JNIEXPORT jdouble JNICALL
Java_edu_wpi_first_hal_simulation_DutyCycleDataJNI_getFrequency
(JNIEnv*, jclass, jint index)
{
@@ -103,11 +103,11 @@ Java_edu_wpi_first_hal_simulation_DutyCycleDataJNI_getFrequency
/*
* Class: edu_wpi_first_hal_simulation_DutyCycleDataJNI
* Method: setFrequency
* Signature: (II)V
* Signature: (ID)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_simulation_DutyCycleDataJNI_setFrequency
(JNIEnv*, jclass, jint index, jint value)
(JNIEnv*, jclass, jint index, jdouble value)
{
HALSIM_SetDutyCycleFrequency(index, value);
}
@@ -174,16 +174,4 @@ Java_edu_wpi_first_hal_simulation_DutyCycleDataJNI_resetData
HALSIM_ResetDutyCycleData(index);
}
/*
* Class: edu_wpi_first_hal_simulation_DutyCycleDataJNI
* Method: findForChannel
* Signature: (I)I
*/
JNIEXPORT jint JNICALL
Java_edu_wpi_first_hal_simulation_DutyCycleDataJNI_findForChannel
(JNIEnv*, jclass, jint channel)
{
return HALSIM_FindDutyCycleForChannel(channel);
}
} // extern "C"

View File

@@ -52,8 +52,8 @@ void HAL_SetDutyCycleSimDevice(HAL_DutyCycleHandle handle,
* @param[out] status Error status variable. 0 on success.
* @return frequency in Hertz
*/
int32_t HAL_GetDutyCycleFrequency(HAL_DutyCycleHandle dutyCycleHandle,
int32_t* status);
double HAL_GetDutyCycleFrequency(HAL_DutyCycleHandle dutyCycleHandle,
int32_t* status);
/**
* Get the output ratio of the duty cycle signal.
@@ -77,30 +77,6 @@ double HAL_GetDutyCycleOutput(HAL_DutyCycleHandle dutyCycleHandle,
int32_t HAL_GetDutyCycleHighTime(HAL_DutyCycleHandle dutyCycleHandle,
int32_t* status);
/**
* 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.
*
* @param[in] dutyCycleHandle the duty cycle handle
* @param[out] status Error status variable. 0 on success.
* @return the output scale factor
*/
int32_t HAL_GetDutyCycleOutputScaleFactor(HAL_DutyCycleHandle dutyCycleHandle,
int32_t* status);
/**
* Get the FPGA index for the DutyCycle.
*
* @param[in] dutyCycleHandle the duty cycle handle
* @param[out] status Error status variable. 0 on success.
* @return the FPGA index
*/
int32_t HAL_GetDutyCycleFPGAIndex(HAL_DutyCycleHandle dutyCycleHandle,
int32_t* status);
#ifdef __cplusplus
} // extern "C"
#endif

View File

@@ -11,10 +11,7 @@
extern "C" {
#endif
int32_t HALSIM_FindDutyCycleForChannel(int32_t channel);
void HALSIM_ResetDutyCycleData(int32_t index);
int32_t HALSIM_GetDutyCycleDigitalChannel(int32_t index);
int32_t HALSIM_RegisterDutyCycleInitializedCallback(int32_t index,
HAL_NotifyCallback callback,
@@ -39,8 +36,8 @@ int32_t HALSIM_RegisterDutyCycleFrequencyCallback(int32_t index,
void* param,
HAL_Bool initialNotify);
void HALSIM_CancelDutyCycleFrequencyCallback(int32_t index, int32_t uid);
int32_t HALSIM_GetDutyCycleFrequency(int32_t index);
void HALSIM_SetDutyCycleFrequency(int32_t index, int32_t frequency);
double HALSIM_GetDutyCycleFrequency(int32_t index);
void HALSIM_SetDutyCycleFrequency(int32_t index, double frequency);
void HALSIM_RegisterDutyCycleAllCallbacks(int32_t index,
HAL_NotifyCallback callback,

View File

@@ -4,11 +4,14 @@
#include "hal/DutyCycle.h"
#include <string>
#include "HALInitializer.h"
#include "HALInternal.h"
#include "PortsInternal.h"
#include "hal/Errors.h"
#include "hal/handles/HandlesInternal.h"
#include "hal/handles/LimitedHandleResource.h"
#include "hal/handles/IndexedHandleResource.h"
#include "mockdata/DutyCycleDataInternal.h"
using namespace hal;
@@ -16,16 +19,17 @@ using namespace hal;
namespace {
struct DutyCycle {
uint8_t index;
std::string previousAllocation;
};
struct Empty {};
} // namespace
static LimitedHandleResource<HAL_DutyCycleHandle, DutyCycle, kNumDutyCycles,
static IndexedHandleResource<HAL_DutyCycleHandle, DutyCycle, kNumDutyCycles,
HAL_HandleEnum::DutyCycle>* dutyCycleHandles;
namespace hal::init {
void InitializeDutyCycle() {
static LimitedHandleResource<HAL_DutyCycleHandle, DutyCycle, kNumDutyCycles,
static IndexedHandleResource<HAL_DutyCycleHandle, DutyCycle, kNumDutyCycles,
HAL_HandleEnum::DutyCycle>
dcH;
dutyCycleHandles = &dcH;
@@ -38,23 +42,25 @@ HAL_DutyCycleHandle HAL_InitializeDutyCycle(int32_t channel,
int32_t* status) {
hal::init::CheckInit();
HAL_DutyCycleHandle handle = dutyCycleHandles->Allocate();
if (handle == HAL_kInvalidHandle) {
*status = NO_AVAILABLE_RESOURCES;
return HAL_kInvalidHandle;
}
HAL_DutyCycleHandle handle = HAL_kInvalidHandle;
auto dutyCycle = dutyCycleHandles->Allocate(channel, &handle, status);
auto dutyCycle = dutyCycleHandles->Get(handle);
if (dutyCycle == nullptr) { // would only occur on thread issue
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
if (*status != 0) {
if (dutyCycle) {
hal::SetLastErrorPreviouslyAllocated(status, "SmartIo", channel,
dutyCycle->previousAllocation);
} else {
hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for Duty Cycle",
0, kNumDutyCycles, channel);
}
return HAL_kInvalidHandle; // failed to allocate. Pass error back.
}
int16_t index = getHandleIndex(handle);
SimDutyCycleData[index].digitalChannel = channel;
SimDutyCycleData[index].initialized = true;
SimDutyCycleData[index].simDevice = 0;
dutyCycle->index = index;
dutyCycle->previousAllocation = allocationLocation ? allocationLocation : "";
return handle;
}
void HAL_FreeDutyCycle(HAL_DutyCycleHandle dutyCycleHandle) {
@@ -75,8 +81,8 @@ void HAL_SetDutyCycleSimDevice(HAL_EncoderHandle handle,
SimDutyCycleData[dutyCycle->index].simDevice = device;
}
int32_t HAL_GetDutyCycleFrequency(HAL_DutyCycleHandle dutyCycleHandle,
int32_t* status) {
double HAL_GetDutyCycleFrequency(HAL_DutyCycleHandle dutyCycleHandle,
int32_t* status) {
auto dutyCycle = dutyCycleHandles->Get(dutyCycleHandle);
if (dutyCycle == nullptr) {
*status = HAL_HANDLE_ERROR;
@@ -110,19 +116,4 @@ int32_t HAL_GetDutyCycleHighTime(HAL_DutyCycleHandle dutyCycleHandle,
double period = 1e9 / SimDutyCycleData[dutyCycle->index].frequency; // ns
return period * SimDutyCycleData[dutyCycle->index].output;
}
int32_t HAL_GetDutyCycleOutputScaleFactor(HAL_DutyCycleHandle dutyCycleHandle,
int32_t* status) {
return 4e7 - 1;
}
int32_t HAL_GetDutyCycleFPGAIndex(HAL_DutyCycleHandle dutyCycleHandle,
int32_t* status) {
auto dutyCycle = dutyCycleHandles->Get(dutyCycleHandle);
if (dutyCycle == nullptr) {
*status = HAL_HANDLE_ERROR;
return -1;
}
return dutyCycle->index;
}
} // extern "C"

View File

@@ -31,7 +31,7 @@ constexpr int32_t kNumREVPDHModules = 63;
constexpr int32_t kNumREVPDHChannels = 24;
constexpr int32_t kNumPDSimModules = kNumREVPDHModules;
constexpr int32_t kNumPDSimChannels = kNumREVPDHChannels;
constexpr int32_t kNumDutyCycles = 8;
constexpr int32_t kNumDutyCycles = 6;
constexpr int32_t kNumAddressableLEDs = 1;
constexpr int32_t kNumREVPHModules = 63;
constexpr int32_t kNumREVPHChannels = 16;

View File

@@ -17,7 +17,6 @@ void InitializeDutyCycleData() {
DutyCycleData* hal::SimDutyCycleData;
void DutyCycleData::ResetData() {
digitalChannel = 0;
initialized.Reset(false);
simDevice = 0;
frequency.Reset(0);
@@ -25,22 +24,10 @@ void DutyCycleData::ResetData() {
}
extern "C" {
int32_t HALSIM_FindDutyCycleForChannel(int32_t channel) {
for (int i = 0; i < kNumDutyCycles; ++i) {
if (SimDutyCycleData[i].initialized &&
SimDutyCycleData[i].digitalChannel == channel) {
return i;
}
}
return -1;
}
void HALSIM_ResetDutyCycleData(int32_t index) {
SimDutyCycleData[index].ResetData();
}
int32_t HALSIM_GetDutyCycleDigitalChannel(int32_t index) {
return SimDutyCycleData[index].digitalChannel;
}
HAL_SimDeviceHandle HALSIM_GetDutyCycleSimDevice(int32_t index) {
return SimDutyCycleData[index].simDevice;
@@ -51,7 +38,7 @@ HAL_SimDeviceHandle HALSIM_GetDutyCycleSimDevice(int32_t index) {
SimDutyCycleData, LOWERNAME)
DEFINE_CAPI(HAL_Bool, Initialized, initialized)
DEFINE_CAPI(int32_t, Frequency, frequency)
DEFINE_CAPI(double, Frequency, frequency)
DEFINE_CAPI(double, Output, output)
#define REGISTER(NAME) \

View File

@@ -17,11 +17,10 @@ class DutyCycleData {
HAL_SIMDATAVALUE_DEFINE_NAME(Frequency)
public:
std::atomic<int32_t> digitalChannel{0};
SimDataValue<HAL_Bool, HAL_MakeBoolean, GetInitializedName> initialized{
false};
std::atomic<HAL_SimDeviceHandle> simDevice;
SimDataValue<int32_t, HAL_MakeInt, GetFrequencyName> frequency{0};
SimDataValue<double, HAL_MakeDouble, GetFrequencyName> frequency{0};
SimDataValue<double, HAL_MakeDouble, GetOutputName> output{0};
virtual void ResetData();

View File

@@ -88,16 +88,47 @@ void HAL_FreeDutyCycle(HAL_DutyCycleHandle dutyCycleHandle) {
void HAL_SetDutyCycleSimDevice(HAL_EncoderHandle handle,
HAL_SimDeviceHandle device) {}
int32_t HAL_GetDutyCycleFrequency(HAL_DutyCycleHandle dutyCycleHandle,
int32_t* status) {
*status = HAL_HANDLE_ERROR;
return 0;
double HAL_GetDutyCycleFrequency(HAL_DutyCycleHandle dutyCycleHandle,
int32_t* status) {
auto port = smartIoHandles->Get(dutyCycleHandle, HAL_HandleEnum::DutyCycle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
uint16_t ret = 0;
*status = port->GetPwmInputPeriodMicroseconds(&ret);
if (ret == 0) {
return 0.0;
}
return 1000000.0 / ret;
}
double HAL_GetDutyCycleOutput(HAL_DutyCycleHandle dutyCycleHandle,
int32_t* status) {
*status = HAL_HANDLE_ERROR;
return 0;
auto port = smartIoHandles->Get(dutyCycleHandle, HAL_HandleEnum::DutyCycle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0.0;
}
uint16_t highTime = 0;
*status = port->GetPwmInputMicroseconds(&highTime);
uint16_t period = 0;
*status = port->GetPwmInputPeriodMicroseconds(&period);
if (period == 0) {
return 0.0;
}
if (highTime >= period) {
return 1.0;
}
return static_cast<double>(highTime) / static_cast<double>(period);
}
int32_t HAL_GetDutyCycleHighTime(HAL_DutyCycleHandle dutyCycleHandle,
@@ -105,23 +136,11 @@ int32_t HAL_GetDutyCycleHighTime(HAL_DutyCycleHandle dutyCycleHandle,
auto port = smartIoHandles->Get(dutyCycleHandle, HAL_HandleEnum::DutyCycle);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return false;
return 0;
}
uint16_t ret = 0;
*status = port->GetPwmInputMicroseconds(&ret);
return static_cast<int32_t>(ret) * 1000;
}
int32_t HAL_GetDutyCycleOutputScaleFactor(HAL_DutyCycleHandle dutyCycleHandle,
int32_t* status) {
*status = HAL_HANDLE_ERROR;
return 0;
}
int32_t HAL_GetDutyCycleFPGAIndex(HAL_DutyCycleHandle dutyCycleHandle,
int32_t* status) {
*status = HAL_HANDLE_ERROR;
return 0;
}
} // extern "C"

View File

@@ -35,11 +35,11 @@ int32_t SmartIo::InitializeMode(SmartIoMode mode) {
modePublisher = inst.GetIntegerTopic(subTableString + "type").Publish();
getSubscriber =
inst.GetIntegerTopic(subTableString + "valget").Subscribe(0, options);
frequencySubscriber =
inst.GetIntegerTopic(subTableString + "freqget").Subscribe(0, options);
periodGetSubscriber =
inst.GetIntegerTopic(subTableString + "periodget").Subscribe(0, options);
setPublisher =
inst.GetIntegerTopic(subTableString + "valset").Publish(options);
periodPublisher =
periodSetPublisher =
inst.GetIntegerTopic(subTableString + "periodset").Publish(options);
currentMode = mode;
@@ -107,6 +107,17 @@ int32_t SmartIo::GetPwmInputMicroseconds(uint16_t* microseconds) {
return 0;
}
int32_t SmartIo::GetPwmInputPeriodMicroseconds(uint16_t* microseconds) {
if (currentMode != SmartIoMode::PwmInput) {
return INCOMPATIBLE_STATE;
}
int val = periodGetSubscriber.Get();
*microseconds = val;
return 0;
}
int32_t SmartIo::SetPwmOutputPeriod(PwmOutputPeriod period) {
if (currentMode != SmartIoMode::PwmOutput) {
return INCOMPATIBLE_STATE;
@@ -117,7 +128,7 @@ int32_t SmartIo::SetPwmOutputPeriod(PwmOutputPeriod period) {
case PwmOutputPeriod::k10ms:
case PwmOutputPeriod::k5ms:
case PwmOutputPeriod::k2ms:
periodPublisher.Set(static_cast<int>(period));
periodSetPublisher.Set(static_cast<int>(period));
return 0;
default:

View File

@@ -19,12 +19,12 @@ constexpr int32_t kPwmAlwaysHigh = 0xFFFF;
enum class SmartIoMode {
DigitalInput = 0,
DigitalOutput,
AnalogInput,
PwmInput,
PwmOutput,
SingleCounterRising,
SingleCounterFalling,
DigitalOutput = 1,
AnalogInput = 2,
PwmInput = 3,
PwmOutput = 4,
SingleCounterRising = 5,
SingleCounterFalling = 6,
};
enum class PwmOutputPeriod {
@@ -43,8 +43,8 @@ struct SmartIo {
nt::IntegerPublisher setPublisher;
nt::IntegerSubscriber getSubscriber;
nt::IntegerPublisher periodPublisher;
nt::IntegerSubscriber frequencySubscriber;
nt::IntegerPublisher periodSetPublisher;
nt::IntegerSubscriber periodGetSubscriber;
int32_t InitializeMode(SmartIoMode mode);
int32_t SwitchDioDirection(bool input);
@@ -53,6 +53,7 @@ struct SmartIo {
int32_t GetDigitalInput(bool* value);
int32_t GetPwmInputMicroseconds(uint16_t* microseconds);
int32_t GetPwmInputPeriodMicroseconds(uint16_t* microseconds);
int32_t SetPwmOutputPeriod(PwmOutputPeriod period);

View File

@@ -7,16 +7,9 @@
#include "hal/simulation/SimDataValue.h"
extern "C" {
int32_t HALSIM_FindDutyCycleForChannel(int32_t channel) {
return 0;
}
void HALSIM_ResetDutyCycleData(int32_t index) {}
int32_t HALSIM_GetDutyCycleDigitalChannel(int32_t index) {
return 0;
}
HAL_SimDeviceHandle HALSIM_GetDutyCycleSimDevice(int32_t index) {
return 0;
}
@@ -25,7 +18,7 @@ HAL_SimDeviceHandle HALSIM_GetDutyCycleSimDevice(int32_t index) {
HAL_SIMDATAVALUE_STUB_CAPI(TYPE, HALSIM, DutyCycle##CAPINAME, RETURN)
DEFINE_CAPI(HAL_Bool, Initialized, false)
DEFINE_CAPI(int32_t, Frequency, 0)
DEFINE_CAPI(double, Frequency, 0)
DEFINE_CAPI(double, Output, 0)
void HALSIM_RegisterDutyCycleAllCallbacks(int32_t index,

View File

@@ -187,7 +187,7 @@ void DIOsSimModel::Update() {
auto& model = m_dutyCycleModels[i];
if (HALSIM_GetDutyCycleInitialized(i)) {
if (!model) {
int channel = HALSIM_GetDutyCycleDigitalChannel(i);
int channel = i;
if (channel >= 0 && channel < numDIO && m_dioModels[channel]) {
model = std::make_unique<DutyCycleSimModel>(i);
m_dioModels[channel]->SetDutyCycle(model.get());

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.

View File

@@ -15,7 +15,7 @@ namespace frc::sim {
TEST(DutyCycleSimTest, Initialization) {
HAL_Initialize(500, 0);
DutyCycleSim sim = DutyCycleSim::CreateForIndex(0);
DutyCycleSim sim = DutyCycleSim::CreateForChannel(2);
EXPECT_FALSE(sim.GetInitialized());
BooleanCallback callback;
@@ -38,12 +38,12 @@ TEST(DutyCycleSimTest, SetFrequency) {
DutyCycle dc{2};
DutyCycleSim sim(dc);
IntCallback callback;
DoubleCallback callback;
auto cb = sim.RegisterFrequencyCallback(callback.GetCallback(), false);
sim.SetFrequency(191);
EXPECT_EQ(191, sim.GetFrequency());
EXPECT_EQ(191, dc.GetFrequency());
sim.SetFrequency(191_Hz);
EXPECT_EQ(191_Hz, sim.GetFrequency());
EXPECT_EQ(191_Hz, dc.GetFrequency());
EXPECT_TRUE(callback.WasTriggered());
EXPECT_EQ(191, callback.GetLastValue());
}

View File

@@ -35,7 +35,7 @@ TEST(SimInitializationTest, AllInitialize) {
PWMSim pwmsim{0};
RoboRioSim rrsim;
(void)rrsim;
DutyCycleSim dcsim = DutyCycleSim::CreateForIndex(0);
DutyCycleSim dcsim = DutyCycleSim::CreateForChannel(0);
(void)dcsim;
AddressableLEDSim adLED;
}

View File

@@ -60,7 +60,7 @@ class Robot : public frc::TimedRobot {
fullRange - percentOfRange);
frc::SmartDashboard::PutBoolean("Connected", connected);
frc::SmartDashboard::PutNumber("Frequency", frequency);
frc::SmartDashboard::PutNumber("Frequency", frequency.value());
frc::SmartDashboard::PutNumber("Output", output);
frc::SmartDashboard::PutNumber("ShiftedOutput", shiftedOutput);
}

View File

@@ -21,7 +21,7 @@ class Robot : public frc::TimedRobot {
// 1 is fully on, 0 is fully off
auto output = m_dutyCycle.GetOutput();
frc::SmartDashboard::PutNumber("Frequency", frequency);
frc::SmartDashboard::PutNumber("Frequency", frequency.value());
frc::SmartDashboard::PutNumber("Duty Cycle", output);
}
};

View File

@@ -50,7 +50,7 @@ public class DutyCycle implements Sendable, AutoCloseable {
*
* @return frequency in Hertz
*/
public int getFrequency() {
public double getFrequency() {
return DutyCycleJNI.getFrequency(m_handle);
}
@@ -74,27 +74,6 @@ public class DutyCycle implements Sendable, AutoCloseable {
return DutyCycleJNI.getHighTime(m_handle);
}
/**
* 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
*/
public int getOutputScaleFactor() {
return DutyCycleJNI.getOutputScaleFactor(m_handle);
}
/**
* Get the FPGA index for the DutyCycle.
*
* @return the FPGA index
*/
public final int getFPGAIndex() {
return DutyCycleJNI.getFPGAIndex(m_handle);
}
/**
* Get the channel of the source.
*

View File

@@ -19,7 +19,7 @@ import edu.wpi.first.util.sendable.SendableRegistry;
public class DutyCycleEncoder implements Sendable, AutoCloseable {
private final DutyCycle m_dutyCycle;
private boolean m_ownsDutyCycle;
private int m_frequencyThreshold = 100;
private double m_frequencyThreshold = 100;
private double m_fullRange;
private double m_expectedZero;
private double m_periodNanos;
@@ -165,7 +165,7 @@ public class DutyCycleEncoder implements Sendable, AutoCloseable {
*
* @return duty cycle frequency in Hz
*/
public int getFrequency() {
public double getFrequency() {
return m_dutyCycle.getFrequency();
}
@@ -190,7 +190,7 @@ public class DutyCycleEncoder implements Sendable, AutoCloseable {
*
* @param frequency the minimum frequency in Hz.
*/
public void setConnectedFrequencyThreshold(int frequency) {
public void setConnectedFrequencyThreshold(double frequency) {
if (frequency < 0) {
frequency = 0;
}
@@ -235,15 +235,6 @@ public class DutyCycleEncoder implements Sendable, AutoCloseable {
}
}
/**
* Get the FPGA index for the DutyCycleEncoder.
*
* @return the FPGA index
*/
public int getFPGAIndex() {
return m_dutyCycle.getFPGAIndex();
}
/**
* Get the channel of the source.
*

View File

@@ -7,7 +7,6 @@ package edu.wpi.first.wpilibj.simulation;
import edu.wpi.first.hal.simulation.DutyCycleDataJNI;
import edu.wpi.first.hal.simulation.NotifyCallback;
import edu.wpi.first.wpilibj.DutyCycle;
import java.util.NoSuchElementException;
/** Class to control a simulated duty cycle digital input. */
public class DutyCycleSim {
@@ -19,7 +18,7 @@ public class DutyCycleSim {
* @param dutyCycle DutyCycle to simulate
*/
public DutyCycleSim(DutyCycle dutyCycle) {
m_index = dutyCycle.getFPGAIndex();
m_index = dutyCycle.getSourceChannel();
}
private DutyCycleSim(int index) {
@@ -27,29 +26,13 @@ public class DutyCycleSim {
}
/**
* 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 NoSuchElementException if no DutyCycle is configured for that channel
*/
public static DutyCycleSim createForChannel(int channel) {
int index = DutyCycleDataJNI.findForChannel(channel);
if (index < 0) {
throw new NoSuchElementException("no duty cycle found for channel " + channel);
}
return new DutyCycleSim(index);
}
/**
* Creates a DutyCycleSim for a simulated index. The index is incremented for each simulated
* DutyCycle.
*
* @param index simulator index
* @return Simulated object
*/
public static DutyCycleSim createForIndex(int index) {
return new DutyCycleSim(index);
return new DutyCycleSim(channel);
}
/**
@@ -99,7 +82,7 @@ public class DutyCycleSim {
*
* @return the duty cycle frequency
*/
public int getFrequency() {
public double getFrequency() {
return DutyCycleDataJNI.getFrequency(m_index);
}
@@ -108,7 +91,7 @@ public class DutyCycleSim {
*
* @param frequency the new frequency
*/
public void setFrequency(int frequency) {
public void setFrequency(double frequency) {
DutyCycleDataJNI.setFrequency(m_index, frequency);
}

View File

@@ -12,13 +12,12 @@ import edu.wpi.first.hal.HAL;
import edu.wpi.first.wpilibj.DutyCycle;
import edu.wpi.first.wpilibj.simulation.testutils.BooleanCallback;
import edu.wpi.first.wpilibj.simulation.testutils.DoubleCallback;
import edu.wpi.first.wpilibj.simulation.testutils.IntCallback;
import org.junit.jupiter.api.Test;
class DutyCycleSimTest {
@Test
void testInitialization() {
DutyCycleSim sim = DutyCycleSim.createForIndex(0);
DutyCycleSim sim = DutyCycleSim.createForChannel(2);
assertFalse(sim.getInitialized());
BooleanCallback callback = new BooleanCallback();
@@ -36,7 +35,7 @@ class DutyCycleSimTest {
HAL.initialize(500, 0);
try (DutyCycle dc = new DutyCycle(2)) {
IntCallback callback = new IntCallback();
DoubleCallback callback = new DoubleCallback();
DutyCycleSim sim = new DutyCycleSim(dc);
try (CallbackStore cb = sim.registerFrequencyCallback(callback, false)) {
sim.setFrequency(191);

View File

@@ -45,7 +45,7 @@ public class Robot extends TimedRobot {
boolean connected = m_dutyCycleEncoder.isConnected();
// Duty Cycle Frequency in Hz
int frequency = m_dutyCycleEncoder.getFrequency();
double frequency = m_dutyCycleEncoder.getFrequency();
// Output of encoder
double output = m_dutyCycleEncoder.get();

View File

@@ -18,7 +18,7 @@ public class Robot extends TimedRobot {
@Override
public void robotPeriodic() {
// Duty Cycle Frequency in Hz
int frequency = m_dutyCycle.getFrequency();
double frequency = m_dutyCycle.getFrequency();
// Output of duty cycle, ranging from 0 to 1
// 1 is fully on, 0 is fully off