[hal] Add SimValue reset() function (#3064)

This enables correct behavior for resetting incremental sensor values like
encoder counts or gyro accumulated angle with WebSockets.
This commit is contained in:
Peter Johnson
2021-01-12 00:38:58 -08:00
committed by GitHub
parent 9c3b51ca0f
commit bc80c55353
20 changed files with 324 additions and 11 deletions

View File

@@ -165,10 +165,7 @@ public class ADXRS450_Gyro extends GyroBase implements Gyro, PIDSource, Sendable
@Override
public void reset() {
if (m_simAngle != null) {
m_simAngle.set(0.0);
}
if (m_simRate != null) {
m_simRate.set(0.0);
m_simAngle.reset();
}
if (m_spi != null) {
m_spi.resetAccumulator();

View File

@@ -13,6 +13,7 @@ import edu.wpi.first.hal.SimValue;
import edu.wpi.first.hal.simulation.SimDeviceCallback;
import edu.wpi.first.hal.simulation.SimDeviceDataJNI;
import edu.wpi.first.hal.simulation.SimValueCallback;
import edu.wpi.first.hal.simulation.SimValueCallback2;
/** Class to control the simulation side of a SimDevice. */
public class SimDeviceSim {
@@ -169,6 +170,38 @@ public class SimDeviceSim {
return new CallbackStore(uid, SimDeviceDataJNI::cancelSimValueChangedCallback);
}
/**
* Register a callback to be run every time a value is changed on this device.
*
* @param callback the callback
* @param initialNotify should the callback be run with the initial state
* @return the {@link CallbackStore} object associated with this callback. Save a reference to
* this object so GC doesn't cancel the callback.
*/
public CallbackStore registerValueChangedCallback2(
SimValue value, SimValueCallback2 callback, boolean initialNotify) {
int uid =
SimDeviceDataJNI.registerSimValueChangedCallback2(
value.getNativeHandle(), callback, initialNotify);
return new CallbackStore(uid, SimDeviceDataJNI::cancelSimValueChangedCallback);
}
/**
* Register a callback for SimDouble.reset() and similar functions. The callback is called with
* the old value.
*
* @param value simulated value
* @param callback callback
* @param initialNotify ignored (present for consistency)
*/
public CallbackStore registerValueResetCallback(
SimValue value, SimValueCallback2 callback, boolean initialNotify) {
int uid =
SimDeviceDataJNI.registerSimValueResetCallback(
value.getNativeHandle(), callback, initialNotify);
return new CallbackStore(uid, SimDeviceDataJNI::cancelSimValueResetCallback);
}
/**
* Get all sim devices with the given prefix.
*