mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[hal, wpilib] Remove DigitalGlitchFilter (#7725)
This commit is contained in:
@@ -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&) {}
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user