Skip to content

Commit

Permalink
[hal, wpilib] Remove digital source from encoder (#7740)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse authored Jan 28, 2025
1 parent 3b345fe commit b799b28
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 544 deletions.
28 changes: 3 additions & 25 deletions hal/src/main/java/edu/wpi/first/hal/EncoderJNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,15 @@ public class EncoderJNI extends JNIWrapper {
/**
* Initializes an encoder.
*
* @param digitalSourceHandleA the A source handle (either a digital or analog trigger)
* @param analogTriggerTypeA the analog trigger type of the A source if it is an analog trigger
* @param digitalSourceHandleB the B source handle (either a digital or analog trigger)
* @param analogTriggerTypeB the analog trigger type of the B source if it is an analog trigger
* @param aChannel the A channel
* @param bChannel the B channel
* @param reverseDirection true to reverse the counting direction from standard, otherwise false
* @param encodingType the encoding type
* @return the created encoder handle
* @see "HAL_InitializeEncoder"
*/
public static native int initializeEncoder(
int digitalSourceHandleA,
int analogTriggerTypeA,
int digitalSourceHandleB,
int analogTriggerTypeB,
boolean reverseDirection,
int encodingType);
int aChannel, int bChannel, boolean reverseDirection, int encodingType);

/**
* Frees an encoder.
Expand Down Expand Up @@ -212,21 +205,6 @@ public static native int initializeEncoder(
*/
public static native int getEncoderSamplesToAverage(int encoderHandle);

/**
* Sets the source for an index pulse on the encoder.
*
* <p>The index pulse can be used to cause an encoder to reset based on an external input.
*
* @param encoderHandle the encoder handle
* @param digitalSourceHandle the index source handle (either a HAL_AnalogTriggerHandle or a
* HAL_DigitalHandle)
* @param analogTriggerType the analog trigger type if the source is an analog trigger
* @param indexingType the index triggering type
* @see "HAL_SetEncoderIndexSource"
*/
public static native void setEncoderIndexSource(
int encoderHandle, int digitalSourceHandle, int analogTriggerType, int indexingType);

/**
* Gets the FPGA index of the encoder.
*
Expand Down
34 changes: 6 additions & 28 deletions hal/src/main/native/cpp/jni/EncoderJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,17 @@ extern "C" {
/*
* Class: edu_wpi_first_hal_EncoderJNI
* Method: initializeEncoder
* Signature: (IIIIZI)I
* Signature: (IIZI)I
*/
JNIEXPORT jint JNICALL
Java_edu_wpi_first_hal_EncoderJNI_initializeEncoder
(JNIEnv* env, jclass, jint digitalSourceHandleA, jint analogTriggerTypeA,
jint digitalSourceHandleB, jint analogTriggerTypeB,
jboolean reverseDirection, jint encodingType)
(JNIEnv* env, jclass, jint aChannel, jint bChannel, jboolean reverseDirection,
jint encodingType)
{
int32_t status = 0;
auto encoder = HAL_InitializeEncoder(
(HAL_Handle)digitalSourceHandleA,
(HAL_AnalogTriggerType)analogTriggerTypeA,
(HAL_Handle)digitalSourceHandleB,
(HAL_AnalogTriggerType)analogTriggerTypeB, reverseDirection,
(HAL_EncoderEncodingType)encodingType, &status);
auto encoder =
HAL_InitializeEncoder(aChannel, bChannel, reverseDirection,
(HAL_EncoderEncodingType)encodingType, &status);
CheckStatusForceThrow(env, status);
return (jint)encoder;
}
Expand Down Expand Up @@ -286,24 +282,6 @@ Java_edu_wpi_first_hal_EncoderJNI_getEncoderSamplesToAverage
return returnValue;
}

/*
* Class: edu_wpi_first_hal_EncoderJNI
* Method: setEncoderIndexSource
* Signature: (IIII)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_EncoderJNI_setEncoderIndexSource
(JNIEnv* env, jclass, jint id, jint digitalSourceHandle,
jint analogTriggerType, jint type)
{
int32_t status = 0;
HAL_SetEncoderIndexSource((HAL_EncoderHandle)id,
(HAL_Handle)digitalSourceHandle,
(HAL_AnalogTriggerType)analogTriggerType,
(HAL_EncoderIndexingType)type, &status);
CheckStatus(env, status);
}

/*
* Class: edu_wpi_first_hal_EncoderJNI
* Method: getEncoderFPGAIndex
Expand Down
39 changes: 6 additions & 33 deletions hal/src/main/native/include/hal/Encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,18 @@ extern "C" {
/**
* Initializes an encoder.
*
* @param[in] digitalSourceHandleA the A source (either a HAL_DigitalHandle or a
* HAL_AnalogTriggerHandle)
* @param[in] analogTriggerTypeA the analog trigger type of the A source if it
* is an analog trigger
* @param[in] digitalSourceHandleB the B source (either a HAL_DigitalHandle or a
* HAL_AnalogTriggerHandle)
* @param[in] analogTriggerTypeB the analog trigger type of the B source if it
* is an analog trigger
* @param[in] aChannel the A channel
* @param[in] bChannel the B channel
* @param[in] reverseDirection true to reverse the counting direction from
* standard, otherwise false
* @param[in] encodingType the encoding type
* @param[out] status Error status variable. 0 on success.
* @return the created encoder handle
*/
HAL_EncoderHandle HAL_InitializeEncoder(
HAL_Handle digitalSourceHandleA, HAL_AnalogTriggerType analogTriggerTypeA,
HAL_Handle digitalSourceHandleB, HAL_AnalogTriggerType analogTriggerTypeB,
HAL_Bool reverseDirection, HAL_EncoderEncodingType encodingType,
int32_t* status);
HAL_EncoderHandle HAL_InitializeEncoder(int32_t aChannel, int32_t bChannel,
HAL_Bool reverseDirection,
HAL_EncoderEncodingType encodingType,
int32_t* status);

/**
* Frees an encoder.
Expand Down Expand Up @@ -260,26 +253,6 @@ void HAL_SetEncoderSamplesToAverage(HAL_EncoderHandle encoderHandle,
int32_t HAL_GetEncoderSamplesToAverage(HAL_EncoderHandle encoderHandle,
int32_t* status);

/**
* Sets the source for an index pulse on the encoder.
*
* The index pulse can be used to cause an encoder to reset based on an external
* input.
*
* @param[in] encoderHandle the encoder handle
* @param[in] digitalSourceHandle the index source handle (either a
* HAL_AnalogTriggerHandle or a
* HAL_DigitalHandle)
* @param[in] analogTriggerType the analog trigger type if the source is an
* analog trigger
* @param[in] type the index triggering type
* @param[out] status Error status variable. 0 on success.
*/
void HAL_SetEncoderIndexSource(HAL_EncoderHandle encoderHandle,
HAL_Handle digitalSourceHandle,
HAL_AnalogTriggerType analogTriggerType,
HAL_EncoderIndexingType type, int32_t* status);

/**
* Gets the FPGA index of the encoder.
*
Expand Down
20 changes: 6 additions & 14 deletions hal/src/main/native/sim/Encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,10 @@ bool GetEncoderBaseHandle(HAL_EncoderHandle handle,
} // namespace hal

extern "C" {
HAL_EncoderHandle HAL_InitializeEncoder(
HAL_Handle digitalSourceHandleA, HAL_AnalogTriggerType analogTriggerTypeA,
HAL_Handle digitalSourceHandleB, HAL_AnalogTriggerType analogTriggerTypeB,
HAL_Bool reverseDirection, HAL_EncoderEncodingType encodingType,
int32_t* status) {
HAL_EncoderHandle HAL_InitializeEncoder(int32_t aChannel, int32_t bChannel,
HAL_Bool reverseDirection,
HAL_EncoderEncodingType encodingType,
int32_t* status) {
hal::init::CheckInit();
HAL_Handle nativeHandle = HAL_kInvalidHandle;
if (encodingType == HAL_EncoderEncodingType::HAL_Encoder_k4X) {
Expand All @@ -95,8 +94,8 @@ HAL_EncoderHandle HAL_InitializeEncoder(
return HAL_kInvalidHandle;
}
int16_t index = getHandleIndex(handle);
SimEncoderData[index].digitalChannelA = getHandleIndex(digitalSourceHandleA);
SimEncoderData[index].digitalChannelB = getHandleIndex(digitalSourceHandleB);
SimEncoderData[index].digitalChannelA = aChannel;
SimEncoderData[index].digitalChannelB = bChannel;
SimEncoderData[index].initialized = true;
SimEncoderData[index].reverseDirection = reverseDirection;
SimEncoderData[index].simDevice = 0;
Expand Down Expand Up @@ -328,13 +327,6 @@ int32_t HAL_GetEncoderSamplesToAverage(HAL_EncoderHandle encoderHandle,
return SimEncoderData[encoder->index].samplesToAverage;
}

void HAL_SetEncoderIndexSource(HAL_EncoderHandle encoderHandle,
HAL_Handle digitalSourceHandle,
HAL_AnalogTriggerType analogTriggerType,
HAL_EncoderIndexingType type, int32_t* status) {
// Not implemented yet
}

int32_t HAL_GetEncoderFPGAIndex(HAL_EncoderHandle encoderHandle,
int32_t* status) {
auto encoder = encoderHandles->Get(encoderHandle);
Expand Down
30 changes: 6 additions & 24 deletions hal/src/main/native/systemcore/Encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,15 @@

using namespace hal;

namespace hal {

namespace init {
namespace hal::init {
void InitializeEncoder() {}
} // namespace init

bool GetEncoderBaseHandle(HAL_EncoderHandle handle,
HAL_FPGAEncoderHandle* fpgaHandle,
HAL_CounterHandle* counterHandle) {
return false;
}
} // namespace hal
} // namespace hal::init

extern "C" {
HAL_EncoderHandle HAL_InitializeEncoder(
HAL_Handle digitalSourceHandleA, HAL_AnalogTriggerType analogTriggerTypeA,
HAL_Handle digitalSourceHandleB, HAL_AnalogTriggerType analogTriggerTypeB,
HAL_Bool reverseDirection, HAL_EncoderEncodingType encodingType,
int32_t* status) {
HAL_EncoderHandle HAL_InitializeEncoder(int32_t aChannel, int32_t bChannel,
HAL_Bool reverseDirection,
HAL_EncoderEncodingType encodingType,
int32_t* status) {
hal::init::CheckInit();
*status = HAL_HANDLE_ERROR;
return HAL_kInvalidHandle;
Expand Down Expand Up @@ -150,14 +140,6 @@ HAL_EncoderEncodingType HAL_GetEncoderEncodingType(
return HAL_Encoder_k4X;
}

void HAL_SetEncoderIndexSource(HAL_EncoderHandle encoderHandle,
HAL_Handle digitalSourceHandle,
HAL_AnalogTriggerType analogTriggerType,
HAL_EncoderIndexingType type, int32_t* status) {
*status = HAL_HANDLE_ERROR;
return;
}

int32_t HAL_GetEncoderFPGAIndex(HAL_EncoderHandle encoderHandle,
int32_t* status) {
*status = HAL_HANDLE_ERROR;
Expand Down
72 changes: 6 additions & 66 deletions wpilibc/src/main/native/cpp/Encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit b799b28

Please sign in to comment.