mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-04 03:11:43 +00:00
[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:
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user