[hal, wpilib] Remove digital source from encoder (#7740)

This commit is contained in:
Thad House
2025-01-28 05:43:09 -08:00
committed by GitHub
parent 3b345fe218
commit b799b285b3
8 changed files with 60 additions and 543 deletions

View File

@@ -20,44 +20,7 @@ using namespace frc;
Encoder::Encoder(int aChannel, int bChannel, bool reverseDirection,
EncodingType encodingType) {
m_aSource = std::make_shared<DigitalInput>(aChannel);
m_bSource = std::make_shared<DigitalInput>(bChannel);
InitEncoder(reverseDirection, encodingType);
wpi::SendableRegistry::AddChild(this, m_aSource.get());
wpi::SendableRegistry::AddChild(this, m_bSource.get());
}
Encoder::Encoder(DigitalSource* aSource, DigitalSource* bSource,
bool reverseDirection, EncodingType encodingType)
: m_aSource(aSource, wpi::NullDeleter<DigitalSource>()),
m_bSource(bSource, wpi::NullDeleter<DigitalSource>()) {
if (!m_aSource) {
throw FRC_MakeError(err::NullParameter, "aSource");
}
if (!m_bSource) {
throw FRC_MakeError(err::NullParameter, "bSource");
}
InitEncoder(reverseDirection, encodingType);
}
Encoder::Encoder(DigitalSource& aSource, DigitalSource& bSource,
bool reverseDirection, EncodingType encodingType)
: m_aSource(&aSource, wpi::NullDeleter<DigitalSource>()),
m_bSource(&bSource, wpi::NullDeleter<DigitalSource>()) {
InitEncoder(reverseDirection, encodingType);
}
Encoder::Encoder(std::shared_ptr<DigitalSource> aSource,
std::shared_ptr<DigitalSource> bSource, bool reverseDirection,
EncodingType encodingType)
: m_aSource(std::move(aSource)), m_bSource(std::move(bSource)) {
if (!m_aSource) {
throw FRC_MakeError(err::NullParameter, "aSource");
}
if (!m_bSource) {
throw FRC_MakeError(err::NullParameter, "bSource");
}
InitEncoder(reverseDirection, encodingType);
InitEncoder(aChannel, bChannel, reverseDirection, encodingType);
}
int Encoder::Get() const {
@@ -172,24 +135,6 @@ int Encoder::GetSamplesToAverage() const {
return result;
}
void Encoder::SetIndexSource(int channel, Encoder::IndexingType type) {
// Force digital input if just given an index
m_indexSource = std::make_shared<DigitalInput>(channel);
wpi::SendableRegistry::AddChild(this, m_indexSource.get());
SetIndexSource(*m_indexSource.get(), type);
}
void Encoder::SetIndexSource(const DigitalSource& source,
Encoder::IndexingType type) {
int32_t status = 0;
HAL_SetEncoderIndexSource(m_encoder, source.GetPortHandleForRouting(),
static_cast<HAL_AnalogTriggerType>(
source.GetAnalogTriggerTypeForRouting()),
static_cast<HAL_EncoderIndexingType>(type),
&status);
FRC_CheckErrorStatus(status, "SetIndexSource");
}
void Encoder::SetSimDevice(HAL_SimDeviceHandle device) {
HAL_SetEncoderSimDevice(m_encoder, device);
}
@@ -219,22 +164,17 @@ void Encoder::InitSendable(wpi::SendableBuilder& builder) {
nullptr);
}
void Encoder::InitEncoder(bool reverseDirection, EncodingType encodingType) {
void Encoder::InitEncoder(int aChannel, int bChannel, bool reverseDirection,
EncodingType encodingType) {
int32_t status = 0;
m_encoder = HAL_InitializeEncoder(
m_aSource->GetPortHandleForRouting(),
static_cast<HAL_AnalogTriggerType>(
m_aSource->GetAnalogTriggerTypeForRouting()),
m_bSource->GetPortHandleForRouting(),
static_cast<HAL_AnalogTriggerType>(
m_bSource->GetAnalogTriggerTypeForRouting()),
reverseDirection, static_cast<HAL_EncoderEncodingType>(encodingType),
&status);
aChannel, bChannel, reverseDirection,
static_cast<HAL_EncoderEncodingType>(encodingType), &status);
FRC_CheckErrorStatus(status, "InitEncoder");
HAL_Report(HALUsageReporting::kResourceType_Encoder, GetFPGAIndex() + 1,
encodingType);
wpi::SendableRegistry::Add(this, "Encoder", m_aSource->GetChannel());
// wpi::SendableRegistry::Add(this, "Encoder", m_aSource->GetChannel());
}
double Encoder::DecodingScaleFactor() const {

View File

@@ -37,20 +37,6 @@ class Encoder : public CounterBase,
public wpi::Sendable,
public wpi::SendableHelper<Encoder> {
public:
/**
* Encoder indexing types.
*/
enum IndexingType {
/// Reset while the signal is high.
kResetWhileHigh,
/// Reset while the signal is low.
kResetWhileLow,
/// Reset on falling edge of the signal.
kResetOnFallingEdge,
/// Reset on rising edge of the signal.
kResetOnRisingEdge
};
/**
* Encoder constructor.
*
@@ -77,62 +63,6 @@ class Encoder : public CounterBase,
Encoder(int aChannel, int bChannel, bool reverseDirection = false,
EncodingType encodingType = k4X);
/**
* Encoder constructor.
*
* Construct a Encoder given a and b channels as digital inputs. This is used
* in the case where the digital inputs are shared. The Encoder class will not
* allocate the digital inputs and assume that they already are counted.
*
* The counter will start counting immediately.
*
* @param aSource The source that should be used for the a channel.
* @param bSource the source that should be used for the b channel.
* @param reverseDirection represents the orientation of the encoder and
* inverts the output values if necessary so forward
* represents positive values.
* @param encodingType either k1X, k2X, or k4X to indicate 1X, 2X or 4X
* decoding. If 4X is selected, then an encoder FPGA
* object is used and the returned counts will be 4x
* the encoder spec'd value since all rising and
* falling edges are counted. If 1X or 2X are selected
* then a counter object will be used and the returned
* value will either exactly match the spec'd count or
* be double (2x) the spec'd count.
*/
Encoder(DigitalSource* aSource, DigitalSource* bSource,
bool reverseDirection = false, EncodingType encodingType = k4X);
/**
* Encoder constructor.
*
* Construct a Encoder given a and b channels as digital inputs. This is used
* in the case where the digital inputs are shared. The Encoder class will not
* allocate the digital inputs and assume that they already are counted.
*
* The counter will start counting immediately.
*
* @param aSource The source that should be used for the a channel.
* @param bSource the source that should be used for the b channel.
* @param reverseDirection represents the orientation of the encoder and
* inverts the output values if necessary so forward
* represents positive values.
* @param encodingType either k1X, k2X, or k4X to indicate 1X, 2X or 4X
* decoding. If 4X is selected, then an encoder FPGA
* object is used and the returned counts will be 4x
* the encoder spec'd value since all rising and
* falling edges are counted. If 1X or 2X are selected
* then a counter object will be used and the returned
* value will either exactly match the spec'd count or
* be double (2x) the spec'd count.
*/
Encoder(DigitalSource& aSource, DigitalSource& bSource,
bool reverseDirection = false, EncodingType encodingType = k4X);
Encoder(std::shared_ptr<DigitalSource> aSource,
std::shared_ptr<DigitalSource> bSource, bool reverseDirection = false,
EncodingType encodingType = k4X);
Encoder(Encoder&&) = default;
Encoder& operator=(Encoder&&) = default;
@@ -312,27 +242,6 @@ class Encoder : public CounterBase,
*/
int GetSamplesToAverage() const;
/**
* Set the index source for the encoder.
*
* When this source is activated, the encoder count automatically resets.
*
* @param channel A DIO channel to set as the encoder index
* @param type The state that will cause the encoder to reset
*/
void SetIndexSource(int channel, IndexingType type = kResetOnRisingEdge);
/**
* Set the index source for the encoder.
*
* When this source is activated, the encoder count automatically resets.
*
* @param source A digital source to set as the encoder index
* @param type The state that will cause the encoder to reset
*/
void SetIndexSource(const DigitalSource& source,
IndexingType type = kResetOnRisingEdge);
/**
* Indicates this encoder is used by a simulated device.
*
@@ -350,7 +259,8 @@ class Encoder : public CounterBase,
*
* This code allocates resources for Encoders and is common to all
* constructors. The counter will start counting immediately.
*
* @param aChannel The a channel.
* @param bChannel The b channel.
* @param reverseDirection If true, counts down instead of up (this is all
* relative)
* @param encodingType either k1X, k2X, or k4X to indicate 1X, 2X or 4X
@@ -362,7 +272,8 @@ class Encoder : public CounterBase,
* value will either exactly match the spec'd count or
* be double (2x) the spec'd count.
*/
void InitEncoder(bool reverseDirection, EncodingType encodingType);
void InitEncoder(int aChannel, int bChannel, bool reverseDirection,
EncodingType encodingType);
/**
* The scale needed to convert a raw counter value into a number of encoder
@@ -370,9 +281,6 @@ class Encoder : public CounterBase,
*/
double DecodingScaleFactor() const;
std::shared_ptr<DigitalSource> m_aSource; // The A phase of the quad encoder
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;
};