2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2025-11-07 19:55:43 -05:00
|
|
|
package org.wpilib.simulation;
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2025-11-07 19:55:43 -05:00
|
|
|
import org.wpilib.hardware.hal.simulation.EncoderDataJNI;
|
|
|
|
|
import org.wpilib.hardware.hal.simulation.NotifyCallback;
|
|
|
|
|
import org.wpilib.hardware.rotation.Encoder;
|
2020-07-04 10:10:43 -07:00
|
|
|
import java.util.NoSuchElementException;
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Class to control a simulated encoder. */
|
2018-05-11 12:38:23 -07:00
|
|
|
public class EncoderSim {
|
2018-06-03 10:00:53 -07:00
|
|
|
private final int m_index;
|
2018-05-11 12:38:23 -07:00
|
|
|
|
2020-07-04 10:10:43 -07:00
|
|
|
/**
|
|
|
|
|
* Constructs from an Encoder object.
|
|
|
|
|
*
|
|
|
|
|
* @param encoder Encoder to simulate
|
|
|
|
|
*/
|
|
|
|
|
public EncoderSim(Encoder encoder) {
|
|
|
|
|
m_index = encoder.getFPGAIndex();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private EncoderSim(int index) {
|
2018-05-11 12:38:23 -07:00
|
|
|
m_index = index;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-04 10:10:43 -07:00
|
|
|
/**
|
2020-12-29 22:45:16 -08:00
|
|
|
* Creates an EncoderSim for a digital input channel. Encoders take two channels, so either one
|
|
|
|
|
* may be specified.
|
2020-07-04 10:10:43 -07:00
|
|
|
*
|
|
|
|
|
* @param channel digital input channel
|
|
|
|
|
* @return Simulated object
|
|
|
|
|
* @throws NoSuchElementException if no Encoder is configured for that channel
|
|
|
|
|
*/
|
|
|
|
|
public static EncoderSim createForChannel(int channel) {
|
|
|
|
|
int index = EncoderDataJNI.findForChannel(channel);
|
|
|
|
|
if (index < 0) {
|
|
|
|
|
throw new NoSuchElementException("no encoder found for channel " + channel);
|
|
|
|
|
}
|
|
|
|
|
return new EncoderSim(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-12-29 22:45:16 -08:00
|
|
|
* Creates an EncoderSim for a simulated index. The index is incremented for each simulated
|
|
|
|
|
* Encoder.
|
2020-07-04 10:10:43 -07:00
|
|
|
*
|
|
|
|
|
* @param index simulator index
|
|
|
|
|
* @return Simulated object
|
|
|
|
|
*/
|
|
|
|
|
public static EncoderSim createForIndex(int index) {
|
|
|
|
|
return new EncoderSim(index);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback on the Initialized property of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback that will be called whenever the Initialized property is changed
|
|
|
|
|
* @param initialNotify if true, the callback will be run on the initial value
|
2024-07-28 14:30:19 -07:00
|
|
|
* @return the {@link CallbackStore} object associated with this callback.
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public CallbackStore registerInitializedCallback(NotifyCallback callback, boolean initialNotify) {
|
|
|
|
|
int uid = EncoderDataJNI.registerInitializedCallback(m_index, callback, initialNotify);
|
|
|
|
|
return new CallbackStore(m_index, uid, EncoderDataJNI::cancelInitializedCallback);
|
|
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Read the Initialized value of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @return true if initialized
|
|
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public boolean getInitialized() {
|
|
|
|
|
return EncoderDataJNI.getInitialized(m_index);
|
|
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Change the Initialized value of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param initialized the new value
|
|
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public void setInitialized(boolean initialized) {
|
|
|
|
|
EncoderDataJNI.setInitialized(m_index, initialized);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback on the count property of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback that will be called whenever the count property is changed
|
|
|
|
|
* @param initialNotify if true, the callback will be run on the initial value
|
2024-07-28 14:30:19 -07:00
|
|
|
* @return the {@link CallbackStore} object associated with this callback.
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public CallbackStore registerCountCallback(NotifyCallback callback, boolean initialNotify) {
|
|
|
|
|
int uid = EncoderDataJNI.registerCountCallback(m_index, callback, initialNotify);
|
|
|
|
|
return new CallbackStore(m_index, uid, EncoderDataJNI::cancelCountCallback);
|
|
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Read the count of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @return the count
|
|
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public int getCount() {
|
|
|
|
|
return EncoderDataJNI.getCount(m_index);
|
|
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Change the count of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param count the new count
|
|
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public void setCount(int count) {
|
|
|
|
|
EncoderDataJNI.setCount(m_index, count);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback on the period of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback that will be called whenever the period is changed
|
|
|
|
|
* @param initialNotify if true, the callback will be run on the initial value
|
2024-07-28 14:30:19 -07:00
|
|
|
* @return the {@link CallbackStore} object associated with this callback.
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public CallbackStore registerPeriodCallback(NotifyCallback callback, boolean initialNotify) {
|
|
|
|
|
int uid = EncoderDataJNI.registerPeriodCallback(m_index, callback, initialNotify);
|
|
|
|
|
return new CallbackStore(m_index, uid, EncoderDataJNI::cancelPeriodCallback);
|
|
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Read the period of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @return the encoder period
|
|
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public double getPeriod() {
|
|
|
|
|
return EncoderDataJNI.getPeriod(m_index);
|
|
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Change the encoder period.
|
|
|
|
|
*
|
|
|
|
|
* @param period the new period
|
|
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public void setPeriod(double period) {
|
|
|
|
|
EncoderDataJNI.setPeriod(m_index, period);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback to be called whenever the encoder is reset.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback
|
|
|
|
|
* @param initialNotify whether to run the callback on the initial value
|
2024-07-28 14:30:19 -07:00
|
|
|
* @return the {@link CallbackStore} object associated with this callback.
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public CallbackStore registerResetCallback(NotifyCallback callback, boolean initialNotify) {
|
|
|
|
|
int uid = EncoderDataJNI.registerResetCallback(m_index, callback, initialNotify);
|
|
|
|
|
return new CallbackStore(m_index, uid, EncoderDataJNI::cancelResetCallback);
|
|
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Check if the encoder has been reset.
|
|
|
|
|
*
|
|
|
|
|
* @return true if reset
|
|
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public boolean getReset() {
|
|
|
|
|
return EncoderDataJNI.getReset(m_index);
|
|
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Change the reset property of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param reset the new value
|
|
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public void setReset(boolean reset) {
|
|
|
|
|
EncoderDataJNI.setReset(m_index, reset);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback to be run whenever the max period of the encoder is changed.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback
|
|
|
|
|
* @param initialNotify whether to run the callback on the initial value
|
2024-07-28 14:30:19 -07:00
|
|
|
* @return the {@link CallbackStore} object associated with this callback.
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public CallbackStore registerMaxPeriodCallback(NotifyCallback callback, boolean initialNotify) {
|
|
|
|
|
int uid = EncoderDataJNI.registerMaxPeriodCallback(m_index, callback, initialNotify);
|
|
|
|
|
return new CallbackStore(m_index, uid, EncoderDataJNI::cancelMaxPeriodCallback);
|
|
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Get the max period of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @return the max period of the encoder
|
|
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public double getMaxPeriod() {
|
|
|
|
|
return EncoderDataJNI.getMaxPeriod(m_index);
|
|
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Change the max period of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param maxPeriod the new value
|
|
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public void setMaxPeriod(double maxPeriod) {
|
|
|
|
|
EncoderDataJNI.setMaxPeriod(m_index, maxPeriod);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback on the direction of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback that will be called whenever the direction is changed
|
|
|
|
|
* @param initialNotify if true, the callback will be run on the initial value
|
2024-07-28 14:30:19 -07:00
|
|
|
* @return the {@link CallbackStore} object associated with this callback.
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public CallbackStore registerDirectionCallback(NotifyCallback callback, boolean initialNotify) {
|
|
|
|
|
int uid = EncoderDataJNI.registerDirectionCallback(m_index, callback, initialNotify);
|
|
|
|
|
return new CallbackStore(m_index, uid, EncoderDataJNI::cancelDirectionCallback);
|
|
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Get the direction of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @return the direction of the encoder
|
|
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public boolean getDirection() {
|
|
|
|
|
return EncoderDataJNI.getDirection(m_index);
|
|
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Set the direction of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param direction the new direction
|
|
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public void setDirection(boolean direction) {
|
|
|
|
|
EncoderDataJNI.setDirection(m_index, direction);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback on the reverse direction.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback that will be called whenever the reverse direction is changed
|
|
|
|
|
* @param initialNotify if true, the callback will be run on the initial value
|
2024-07-28 14:30:19 -07:00
|
|
|
* @return the {@link CallbackStore} object associated with this callback.
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2020-12-29 22:45:16 -08:00
|
|
|
public CallbackStore registerReverseDirectionCallback(
|
|
|
|
|
NotifyCallback callback, boolean initialNotify) {
|
2018-05-11 12:38:23 -07:00
|
|
|
int uid = EncoderDataJNI.registerReverseDirectionCallback(m_index, callback, initialNotify);
|
|
|
|
|
return new CallbackStore(m_index, uid, EncoderDataJNI::cancelReverseDirectionCallback);
|
|
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Get the reverse direction of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @return the reverse direction of the encoder
|
|
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public boolean getReverseDirection() {
|
|
|
|
|
return EncoderDataJNI.getReverseDirection(m_index);
|
|
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Set the reverse direction.
|
|
|
|
|
*
|
|
|
|
|
* @param reverseDirection the new value
|
|
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public void setReverseDirection(boolean reverseDirection) {
|
|
|
|
|
EncoderDataJNI.setReverseDirection(m_index, reverseDirection);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Register a callback on the samples-to-average value of this encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param callback the callback that will be called whenever the samples-to-average is changed
|
|
|
|
|
* @param initialNotify if true, the callback will be run on the initial value
|
2024-07-28 14:30:19 -07:00
|
|
|
* @return the {@link CallbackStore} object associated with this callback.
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2020-12-29 22:45:16 -08:00
|
|
|
public CallbackStore registerSamplesToAverageCallback(
|
|
|
|
|
NotifyCallback callback, boolean initialNotify) {
|
2018-05-11 12:38:23 -07:00
|
|
|
int uid = EncoderDataJNI.registerSamplesToAverageCallback(m_index, callback, initialNotify);
|
|
|
|
|
return new CallbackStore(m_index, uid, EncoderDataJNI::cancelSamplesToAverageCallback);
|
|
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Get the samples-to-average value.
|
|
|
|
|
*
|
|
|
|
|
* @return the samples-to-average value
|
|
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public int getSamplesToAverage() {
|
|
|
|
|
return EncoderDataJNI.getSamplesToAverage(m_index);
|
|
|
|
|
}
|
2020-12-29 22:45:16 -08:00
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Set the samples-to-average value.
|
|
|
|
|
*
|
|
|
|
|
* @param samplesToAverage the new value
|
|
|
|
|
*/
|
2018-05-11 12:38:23 -07:00
|
|
|
public void setSamplesToAverage(int samplesToAverage) {
|
|
|
|
|
EncoderDataJNI.setSamplesToAverage(m_index, samplesToAverage);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-11 14:43:56 -05:00
|
|
|
/**
|
|
|
|
|
* 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
|
2024-07-28 14:30:19 -07:00
|
|
|
* @return the {@link CallbackStore} object associated with this callback.
|
2023-01-11 14:43:56 -05:00
|
|
|
*/
|
|
|
|
|
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.
|
|
|
|
|
*
|
2024-04-04 09:21:05 -07:00
|
|
|
* @param distancePerPulse the new distancePerPulse
|
2023-01-11 14:43:56 -05:00
|
|
|
*/
|
2024-04-04 09:21:05 -07:00
|
|
|
public void setDistancePerPulse(double distancePerPulse) {
|
|
|
|
|
EncoderDataJNI.setDistancePerPulse(m_index, distancePerPulse);
|
2023-01-11 14:43:56 -05:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Change the encoder distance.
|
|
|
|
|
*
|
|
|
|
|
* @param distance the new distance
|
|
|
|
|
*/
|
2020-04-03 08:33:38 -07:00
|
|
|
public void setDistance(double distance) {
|
|
|
|
|
EncoderDataJNI.setDistance(m_index, distance);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Read the distance of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @return the encoder distance
|
|
|
|
|
*/
|
2020-04-03 08:33:38 -07:00
|
|
|
public double getDistance() {
|
|
|
|
|
return EncoderDataJNI.getDistance(m_index);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Change the rate of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @param rate the new rate
|
|
|
|
|
*/
|
2020-04-03 08:33:38 -07:00
|
|
|
public void setRate(double rate) {
|
|
|
|
|
EncoderDataJNI.setRate(m_index, rate);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Get the rate of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* @return the rate of change
|
|
|
|
|
*/
|
2020-04-03 08:33:38 -07:00
|
|
|
public double getRate() {
|
|
|
|
|
return EncoderDataJNI.getRate(m_index);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/** Resets all simulation data for this encoder. */
|
2018-05-11 12:38:23 -07:00
|
|
|
public void resetData() {
|
|
|
|
|
EncoderDataJNI.resetData(m_index);
|
|
|
|
|
}
|
|
|
|
|
}
|