[hal, wpilib] Remove DigitalGlitchFilter (#7725)

This commit is contained in:
Thad House
2025-01-23 21:44:18 -08:00
committed by GitHub
parent 5a6c895b87
commit e2b6beb28a
10 changed files with 0 additions and 722 deletions

View File

@@ -1,65 +0,0 @@
// 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.
package edu.wpi.first.hal;
/**
* Digital Glitch Filter JNI functions.
*
* @see "hal/DIO.h"
*/
public class DigitalGlitchFilterJNI extends JNIWrapper {
/**
* Writes the filter index from the FPGA.
*
* <p>Set the filter index used to filter out short pulses.
*
* @param digitalPortHandle the digital port handle
* @param filterIndex the filter index (Must be in the range 0 - 3, where 0 means "none" and 1 - 3
* means filter # filterIndex - 1)
* @see "HAL_SetFilterSelect"
*/
public static native void setFilterSelect(int digitalPortHandle, int filterIndex);
/**
* Reads the filter index from the FPGA.
*
* <p>Gets the filter index used to filter out short pulses.
*
* @param digitalPortHandle the digital port handle
* @return the filter index (Must be in the range 0 - 3, where 0 means "none" and 1 - 3 means
* filter # filterIndex - 1)
* @see "HAL_GetFilterSelect"
*/
public static native int getFilterSelect(int digitalPortHandle);
/**
* Sets the filter period for the specified filter index.
*
* <p>Sets the filter period in FPGA cycles. Even though there are 2 different filter index
* domains (MXP vs HDR), ignore that distinction for now since it complicates the interface. That
* can be changed later.
*
* @param filterIndex the filter index, 0 - 2
* @param fpgaCycles the number of cycles that the signal must not transition to be counted as a
* transition.
* @see "HAL_SetFilterPeriod"
*/
public static native void setFilterPeriod(int filterIndex, int fpgaCycles);
/**
* Gets the filter period for the specified filter index.
*
* <p>Gets the filter period in FPGA cycles. Even though there are 2 different filter index
* domains (MXP vs HDR), ignore that distinction for now since it complicates the interface.
*
* @param filterIndex the filter index, 0 - 2
* @return The number of FPGA cycles of the filter period.
* @see "HAL_GetFilterPeriod"
*/
public static native int getFilterPeriod(int filterIndex);
/** Utility class. */
private DigitalGlitchFilterJNI() {}
}

View File

@@ -1,79 +0,0 @@
// 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.
#include <jni.h>
#include "HALUtil.h"
#include "edu_wpi_first_hal_DigitalGlitchFilterJNI.h"
#include "hal/DIO.h"
using namespace hal;
extern "C" {
/*
* Class: edu_wpi_first_hal_DigitalGlitchFilterJNI
* Method: setFilterSelect
* Signature: (II)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_DigitalGlitchFilterJNI_setFilterSelect
(JNIEnv* env, jclass, jint id, jint filter_index)
{
int32_t status = 0;
HAL_SetFilterSelect(static_cast<HAL_DigitalHandle>(id), filter_index,
&status);
CheckStatus(env, status);
}
/*
* Class: edu_wpi_first_hal_DigitalGlitchFilterJNI
* Method: getFilterSelect
* Signature: (I)I
*/
JNIEXPORT jint JNICALL
Java_edu_wpi_first_hal_DigitalGlitchFilterJNI_getFilterSelect
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
jint result =
HAL_GetFilterSelect(static_cast<HAL_DigitalHandle>(id), &status);
CheckStatus(env, status);
return result;
}
/*
* Class: edu_wpi_first_hal_DigitalGlitchFilterJNI
* Method: setFilterPeriod
* Signature: (II)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_DigitalGlitchFilterJNI_setFilterPeriod
(JNIEnv* env, jclass, jint filter_index, jint fpga_cycles)
{
int32_t status = 0;
HAL_SetFilterPeriod(filter_index, fpga_cycles, &status);
CheckStatus(env, status);
}
/*
* Class: edu_wpi_first_hal_DigitalGlitchFilterJNI
* Method: getFilterPeriod
* Signature: (I)I
*/
JNIEXPORT jint JNICALL
Java_edu_wpi_first_hal_DigitalGlitchFilterJNI_getFilterPeriod
(JNIEnv* env, jclass, jint filter_index)
{
int32_t status = 0;
jint result = HAL_GetFilterPeriod(filter_index, &status);
CheckStatus(env, status);
return result;
}
} // extern "C"

View File

@@ -194,60 +194,6 @@ HAL_Bool HAL_IsPulsing(HAL_DigitalHandle dioPortHandle, int32_t* status);
* @return true if a pulse on some line is in progress
*/
HAL_Bool HAL_IsAnyPulsing(int32_t* status);
/**
* Writes the filter index from the FPGA.
*
* Set the filter index used to filter out short pulses.
*
* @param[in] dioPortHandle the digital port handle
* @param[in] filterIndex the filter index (Must be in the range 0 - 3, where
* 0 means "none" and 1 - 3 means filter # filterIndex
* - 1)
* @param[out] status Error status variable. 0 on success.
*/
void HAL_SetFilterSelect(HAL_DigitalHandle dioPortHandle, int32_t filterIndex,
int32_t* status);
/**
* Reads the filter index from the FPGA.
*
* Gets the filter index used to filter out short pulses.
*
* @param[in] dioPortHandle the digital port handle
* @param[out] status Error status variable. 0 on success.
* @return filterIndex the filter index (Must be in the range 0 - 3, where 0
* means "none" and 1 - 3 means filter # filterIndex - 1)
*/
int32_t HAL_GetFilterSelect(HAL_DigitalHandle dioPortHandle, int32_t* status);
/**
* Sets the filter period for the specified filter index.
*
* Sets the filter period in FPGA cycles. Even though there are 2 different
* filter index domains (MXP vs HDR), ignore that distinction for now since it
* complicates the interface. That can be changed later.
*
* @param[in] filterIndex the filter index, 0 - 2
* @param[in] value the number of cycles that the signal must not
* transition to be counted as a transition.
* @param[out] status Error status variable. 0 on success.
*/
void HAL_SetFilterPeriod(int32_t filterIndex, int64_t value, int32_t* status);
/**
* Gets the filter period for the specified filter index.
*
* Gets the filter period in FPGA cycles. Even though there are 2 different
* filter index domains (MXP vs HDR), ignore that distinction for now since it
* complicates the interface. Set status to NiFpga_Status_SoftwareFault if the
* filter values mismatch.
*
* @param[in] filterIndex the filter index, 0 - 2
* @param[out] status Error status variable. 0 on success.
* @return The number of FPGA cycles of the filter period.
*/
int64_t HAL_GetFilterPeriod(int32_t filterIndex, int32_t* status);
#ifdef __cplusplus
} // extern "C"
#endif

View File

@@ -269,32 +269,4 @@ HAL_Bool HAL_IsPulsing(HAL_DigitalHandle dioPortHandle, int32_t* status) {
HAL_Bool HAL_IsAnyPulsing(int32_t* status) {
return false; // TODO(Thad) Figure this out
}
void HAL_SetFilterSelect(HAL_DigitalHandle dioPortHandle, int32_t filterIndex,
int32_t* status) {
auto port = digitalChannelHandles->Get(dioPortHandle, HAL_HandleEnum::DIO);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return;
}
// mimics athena HAL
port->filterIndex = filterIndex % 4;
}
int32_t HAL_GetFilterSelect(HAL_DigitalHandle dioPortHandle, int32_t* status) {
auto port = digitalChannelHandles->Get(dioPortHandle, HAL_HandleEnum::DIO);
if (port == nullptr) {
*status = HAL_HANDLE_ERROR;
return 0;
}
return port->filterIndex;
}
void HAL_SetFilterPeriod(int32_t filterIndex, int64_t value, int32_t* status) {
// TODO(Thad) figure this out
}
int64_t HAL_GetFilterPeriod(int32_t filterIndex, int32_t* status) {
return 0; // TODO(Thad) figure this out
}
} // extern "C"

View File

@@ -201,25 +201,4 @@ HAL_Bool HAL_IsAnyPulsing(int32_t* status) {
return false;
}
void HAL_SetFilterSelect(HAL_DigitalHandle dioPortHandle, int32_t filterIndex,
int32_t* status) {
*status = HAL_HANDLE_ERROR;
return;
}
int32_t HAL_GetFilterSelect(HAL_DigitalHandle dioPortHandle, int32_t* status) {
*status = HAL_HANDLE_ERROR;
return 0;
}
void HAL_SetFilterPeriod(int32_t filterIndex, int64_t value, int32_t* status) {
*status = HAL_HANDLE_ERROR;
return;
}
int64_t HAL_GetFilterPeriod(int32_t filterIndex, int32_t* status) {
*status = HAL_HANDLE_ERROR;
return 0;
}
} // extern "C"

View File

@@ -1,138 +0,0 @@
// 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.
#include "frc/DigitalGlitchFilter.h"
#include <algorithm>
#include <array>
#include <utility>
#include <hal/Constants.h>
#include <hal/DIO.h>
#include <hal/FRCUsageReporting.h>
#include <wpi/sendable/SendableRegistry.h>
#include "frc/Counter.h"
#include "frc/Encoder.h"
#include "frc/Errors.h"
#include "frc/SensorUtil.h"
using namespace frc;
std::array<bool, 3> DigitalGlitchFilter::m_filterAllocated = {
{false, false, false}};
wpi::mutex DigitalGlitchFilter::m_mutex;
DigitalGlitchFilter::DigitalGlitchFilter() {
m_channelIndex = AllocateFilterIndex();
if (m_channelIndex < 0) {
throw FRC_MakeError(err::NoAvailableResources,
"No filters available to allocate.");
}
HAL_Report(HALUsageReporting::kResourceType_DigitalGlitchFilter,
m_channelIndex + 1);
wpi::SendableRegistry::AddLW(this, "DigitalGlitchFilter", m_channelIndex);
}
DigitalGlitchFilter::~DigitalGlitchFilter() {
if (m_channelIndex >= 0) {
std::scoped_lock lock(m_mutex);
m_filterAllocated[m_channelIndex] = false;
}
}
int DigitalGlitchFilter::AllocateFilterIndex() {
std::scoped_lock lock{m_mutex};
auto filter =
std::find(m_filterAllocated.begin(), m_filterAllocated.end(), false);
if (filter == m_filterAllocated.end()) {
return -1;
}
*filter = true;
return std::distance(m_filterAllocated.begin(), filter);
}
void DigitalGlitchFilter::Add(DigitalSource* input) {
DoAdd(input, m_channelIndex + 1);
}
void DigitalGlitchFilter::DoAdd(DigitalSource* input, int requestedIndex) {
// Some sources from Counters and Encoders are null. By pushing the check
// here, we catch the issue more generally.
if (input) {
// We don't support GlitchFilters on AnalogTriggers.
if (input->IsAnalogTrigger()) {
throw FRC_MakeError(
-1, "Analog Triggers not supported for DigitalGlitchFilters");
}
int32_t status = 0;
HAL_SetFilterSelect(input->GetPortHandleForRouting(), requestedIndex,
&status);
FRC_CheckErrorStatus(status, "requested index {}", requestedIndex);
// Validate that we set it correctly.
int actualIndex =
HAL_GetFilterSelect(input->GetPortHandleForRouting(), &status);
FRC_CheckErrorStatus(status, "requested index {}", requestedIndex);
FRC_AssertMessage(actualIndex == requestedIndex,
"HAL_SetFilterSelect({}) failed -> {}", requestedIndex,
actualIndex);
}
}
void DigitalGlitchFilter::Add(Encoder* input) {
Add(input->m_aSource.get());
Add(input->m_bSource.get());
}
void DigitalGlitchFilter::Add(Counter* input) {
Add(input->m_upSource.get());
Add(input->m_downSource.get());
}
void DigitalGlitchFilter::Remove(DigitalSource* input) {
DoAdd(input, 0);
}
void DigitalGlitchFilter::Remove(Encoder* input) {
Remove(input->m_aSource.get());
Remove(input->m_bSource.get());
}
void DigitalGlitchFilter::Remove(Counter* input) {
Remove(input->m_upSource.get());
Remove(input->m_downSource.get());
}
void DigitalGlitchFilter::SetPeriodCycles(int fpgaCycles) {
int32_t status = 0;
HAL_SetFilterPeriod(m_channelIndex, fpgaCycles, &status);
FRC_CheckErrorStatus(status, "Channel {}", m_channelIndex);
}
void DigitalGlitchFilter::SetPeriodNanoSeconds(uint64_t nanoseconds) {
int32_t status = 0;
int fpgaCycles =
nanoseconds * HAL_GetSystemClockTicksPerMicrosecond() / 4 / 1000;
HAL_SetFilterPeriod(m_channelIndex, fpgaCycles, &status);
FRC_CheckErrorStatus(status, "Channel {}", m_channelIndex);
}
int DigitalGlitchFilter::GetPeriodCycles() {
int32_t status = 0;
int fpgaCycles = HAL_GetFilterPeriod(m_channelIndex, &status);
FRC_CheckErrorStatus(status, "Channel {}", m_channelIndex);
return fpgaCycles;
}
uint64_t DigitalGlitchFilter::GetPeriodNanoSeconds() {
int32_t status = 0;
int fpgaCycles = HAL_GetFilterPeriod(m_channelIndex, &status);
FRC_CheckErrorStatus(status, "Channel {}", m_channelIndex);
return static_cast<uint64_t>(fpgaCycles) * 1000L /
static_cast<uint64_t>(HAL_GetSystemClockTicksPerMicrosecond() / 4);
}
void DigitalGlitchFilter::InitSendable(wpi::SendableBuilder&) {}

View File

@@ -1,138 +0,0 @@
// 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.
#pragma once
#include <stdint.h>
#include <array>
#include <wpi/mutex.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
#include "frc/DigitalSource.h"
namespace frc {
class Encoder;
class Counter;
/**
* Class to enable glitch filtering on a set of digital inputs.
*
* This class will manage adding and removing digital inputs from a FPGA glitch
* filter. The filter lets the user configure the time that an input must remain
* high or low before it is classified as high or low.
*/
class DigitalGlitchFilter : public wpi::Sendable,
public wpi::SendableHelper<DigitalGlitchFilter> {
public:
DigitalGlitchFilter();
~DigitalGlitchFilter() override;
DigitalGlitchFilter(DigitalGlitchFilter&&) = default;
DigitalGlitchFilter& operator=(DigitalGlitchFilter&&) = default;
/**
* Assigns the DigitalSource to this glitch filter.
*
* @param input The DigitalSource to add.
*/
void Add(DigitalSource* input);
/**
* Assigns the Encoder to this glitch filter.
*
* @param input The Encoder to add.
*/
void Add(Encoder* input);
/**
* Assigns the Counter to this glitch filter.
*
* @param input The Counter to add.
*/
void Add(Counter* input);
/**
* Removes a digital input from this filter.
*
* Removes the DigitalSource from this glitch filter and re-assigns it to
* the default filter.
*
* @param input The DigitalSource to remove.
*/
void Remove(DigitalSource* input);
/**
* Removes an encoder from this filter.
*
* Removes the Encoder from this glitch filter and re-assigns it to
* the default filter.
*
* @param input The Encoder to remove.
*/
void Remove(Encoder* input);
/**
* Removes a counter from this filter.
*
* Removes the Counter from this glitch filter and re-assigns it to
* the default filter.
*
* @param input The Counter to remove.
*/
void Remove(Counter* input);
/**
* Sets the number of cycles that the input must not change state for.
*
* @param fpgaCycles The number of FPGA cycles.
*/
void SetPeriodCycles(int fpgaCycles);
/**
* Sets the number of nanoseconds that the input must not change state for.
*
* @param nanoseconds The number of nanoseconds.
*/
void SetPeriodNanoSeconds(uint64_t nanoseconds);
/**
* Gets the number of cycles that the input must not change state for.
*
* @return The number of cycles.
*/
int GetPeriodCycles();
/**
* Gets the number of nanoseconds that the input must not change state for.
*
* @return The number of nanoseconds.
*/
uint64_t GetPeriodNanoSeconds();
void InitSendable(wpi::SendableBuilder& builder) override;
private:
int m_channelIndex;
// Sets the filter for the input to be the requested index. A value of 0
// disables the filter, and the filter value must be between 1 and 3,
// inclusive.
static void DoAdd(DigitalSource* input, int requested_index);
/**
* Allocates the next available filter index, or -1 if there are no filters
* available.
* @return the filter index
*/
static int AllocateFilterIndex();
static wpi::mutex m_mutex;
static std::array<bool, 3> m_filterAllocated;
};
} // namespace frc

View File

@@ -12,8 +12,6 @@
namespace frc {
class DigitalGlitchFilter;
/**
* Class to read a digital input.
*
@@ -81,8 +79,6 @@ class DigitalInput : public DigitalSource,
private:
int m_channel;
hal::Handle<HAL_DigitalHandle, HAL_FreeDIOPort> m_handle;
friend class DigitalGlitchFilter;
};
} // namespace frc

View File

@@ -17,7 +17,6 @@
namespace frc {
class DigitalSource;
class DigitalGlitchFilter;
/**
* Class to read quad encoders.
@@ -375,8 +374,6 @@ class Encoder : public CounterBase,
std::shared_ptr<DigitalSource> m_bSource; // The B phase of the quad encoder
std::shared_ptr<DigitalSource> m_indexSource = nullptr;
hal::Handle<HAL_EncoderHandle, HAL_FreeEncoder> m_encoder;
friend class DigitalGlitchFilter;
};
} // namespace frc

View File

@@ -1,192 +0,0 @@
// 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.
package edu.wpi.first.wpilibj;
import edu.wpi.first.hal.DigitalGlitchFilterJNI;
import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.hal.util.AllocationException;
import edu.wpi.first.util.sendable.Sendable;
import edu.wpi.first.util.sendable.SendableBuilder;
import edu.wpi.first.util.sendable.SendableRegistry;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Class to enable glitch filtering on a set of digital inputs. This class will manage adding and
* removing digital inputs from an FPGA glitch filter. The filter lets the user configure the time
* that an input must remain high or low before it is classified as high or low.
*/
public class DigitalGlitchFilter implements Sendable, AutoCloseable {
/** Configures the Digital Glitch Filter to its default settings. */
@SuppressWarnings("this-escape")
public DigitalGlitchFilter() {
m_channelIndex = allocateFilterIndex();
if (m_channelIndex < 0) {
throw new AllocationException("No filters available to allocate.");
}
HAL.report(tResourceType.kResourceType_DigitalGlitchFilter, m_channelIndex + 1, 0);
SendableRegistry.addLW(this, "DigitalGlitchFilter", m_channelIndex);
}
@Override
public void close() {
SendableRegistry.remove(this);
if (m_channelIndex >= 0) {
m_mutex.lock();
try {
m_filterAllocated[m_channelIndex] = false;
} finally {
m_mutex.unlock();
}
m_channelIndex = -1;
}
}
/**
* Allocates the next available filter index, or -1 if there are no filters available.
*
* @return the filter index
*/
private static int allocateFilterIndex() {
m_mutex.lock();
try {
for (int index = 0; index < m_filterAllocated.length; index++) {
if (!m_filterAllocated[index]) {
m_filterAllocated[index] = true;
return index;
}
}
} finally {
m_mutex.unlock();
}
return -1;
}
private static void setFilter(DigitalSource input, int channelIndex) {
if (input != null) { // Counter might have just one input
// analog triggers are not supported for DigitalGlitchFilters
if (input.isAnalogTrigger()) {
throw new IllegalStateException("Analog Triggers not supported for DigitalGlitchFilters");
}
DigitalGlitchFilterJNI.setFilterSelect(input.getPortHandleForRouting(), channelIndex);
int selected = DigitalGlitchFilterJNI.getFilterSelect(input.getPortHandleForRouting());
if (selected != channelIndex) {
throw new IllegalStateException(
"DigitalGlitchFilterJNI.setFilterSelect(" + channelIndex + ") failed -> " + selected);
}
}
}
/**
* Assigns the DigitalSource to this glitch filter.
*
* @param input The DigitalSource to add.
*/
public void add(DigitalSource input) {
setFilter(input, m_channelIndex + 1);
}
/**
* Assigns the Encoder to this glitch filter.
*
* @param input The Encoder to add.
*/
public void add(Encoder input) {
add(input.m_aSource);
add(input.m_bSource);
}
/**
* Assigns the Counter to this glitch filter.
*
* @param input The Counter to add.
*/
public void add(Counter input) {
add(input.m_upSource);
add(input.m_downSource);
}
/**
* Removes this filter from the given digital input.
*
* @param input The DigitalSource to stop filtering.
*/
public void remove(DigitalSource input) {
setFilter(input, 0);
}
/**
* Removes this filter from the given Encoder.
*
* @param input the Encoder to stop filtering.
*/
public void remove(Encoder input) {
remove(input.m_aSource);
remove(input.m_bSource);
}
/**
* Removes this filter from the given Counter.
*
* @param input The Counter to stop filtering.
*/
public void remove(Counter input) {
remove(input.m_upSource);
remove(input.m_downSource);
}
/**
* Sets the number of FPGA cycles that the input must hold steady to pass through this glitch
* filter.
*
* @param fpgaCycles The number of FPGA cycles.
*/
public void setPeriodCycles(int fpgaCycles) {
DigitalGlitchFilterJNI.setFilterPeriod(m_channelIndex, fpgaCycles);
}
/**
* Sets the number of nanoseconds that the input must hold steady to pass through this glitch
* filter.
*
* @param nanoseconds The number of nanoseconds.
*/
public void setPeriodNanoSeconds(long nanoseconds) {
int fpgaCycles = (int) (nanoseconds * SensorUtil.kSystemClockTicksPerMicrosecond / 4 / 1000);
setPeriodCycles(fpgaCycles);
}
/**
* Gets the number of FPGA cycles that the input must hold steady to pass through this glitch
* filter.
*
* @return The number of cycles.
*/
public int getPeriodCycles() {
return DigitalGlitchFilterJNI.getFilterPeriod(m_channelIndex);
}
/**
* Gets the number of nanoseconds that the input must hold steady to pass through this glitch
* filter.
*
* @return The number of nanoseconds.
*/
public long getPeriodNanoSeconds() {
int fpgaCycles = getPeriodCycles();
return fpgaCycles * 1000L / (SensorUtil.kSystemClockTicksPerMicrosecond / 4);
}
@Override
public void initSendable(SendableBuilder builder) {}
private int m_channelIndex;
private static final Lock m_mutex = new ReentrantLock(true);
private static final boolean[] m_filterAllocated = new boolean[3];
}