[hal, wpilibj] Add missing distance per pulse functions to EncoderSim (#4928)

Also fix C++ and Java EncoderSim.setDistancePerPulse() not propagating value to SimEncoderData.
This commit is contained in:
Ryan Blue
2023-01-11 14:43:56 -05:00
committed by GitHub
parent 530ae40614
commit 53d8d33bca
6 changed files with 112 additions and 2 deletions

View File

@@ -303,6 +303,38 @@ public class EncoderSim {
EncoderDataJNI.setSamplesToAverage(m_index, samplesToAverage);
}
/**
* Register a callback on the distance per pulse value of this encoder.
*
* @param callback the callback that will be called whenever the distance per pulse is changed
* @param initialNotify if true, the callback will be run on the initial value
* @return the {@link CallbackStore} object associated with this callback. Save a reference to
* this object so GC doesn't cancel the callback.
*/
public CallbackStore registerDistancePerPulseCallback(
NotifyCallback callback, boolean initialNotify) {
int uid = EncoderDataJNI.registerDistancePerPulseCallback(m_index, callback, initialNotify);
return new CallbackStore(m_index, uid, EncoderDataJNI::cancelDistancePerPulseCallback);
}
/**
* Get the distance per pulse value.
*
* @return the distance per pulse value
*/
public double getDistancePerPulse() {
return EncoderDataJNI.getDistancePerPulse(m_index);
}
/**
* Set the distance per pulse value.
*
* @param samplesToAverage the new value
*/
public void setDistancePerPulse(double samplesToAverage) {
EncoderDataJNI.setDistancePerPulse(m_index, samplesToAverage);
}
/**
* Change the encoder distance.
*

View File

@@ -89,4 +89,23 @@ class EncoderSimTest {
}
}
}
@Test
void testDistancePerPulse() {
HAL.initialize(500, 0);
try (Encoder encoder = new Encoder(0, 1)) {
EncoderSim sim = new EncoderSim(encoder);
sim.resetData();
DoubleCallback callback = new DoubleCallback();
try (CallbackStore cb = sim.registerDistancePerPulseCallback(callback, false)) {
sim.setDistancePerPulse(0.03405);
assertEquals(0.03405, sim.getDistancePerPulse());
assertEquals(0.03405, encoder.getDistancePerPulse());
assertTrue(callback.wasTriggered());
assertEquals(0.03405, callback.getSetValue());
}
}
}
}