mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-26 01:51:41 +00:00
[wpilib] DutyCycleEncoderSim: Expand API (#5443)
This commit is contained in:
@@ -246,6 +246,9 @@ public class DutyCycleEncoder implements Sendable, AutoCloseable {
|
||||
if (m_counter != null) {
|
||||
m_counter.reset();
|
||||
}
|
||||
if (m_simPosition != null) {
|
||||
m_simPosition.set(0);
|
||||
}
|
||||
m_positionOffset = getAbsolutePosition();
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import edu.wpi.first.hal.SimBoolean;
|
||||
import edu.wpi.first.hal.SimDouble;
|
||||
import edu.wpi.first.wpilibj.DutyCycleEncoder;
|
||||
|
||||
@@ -11,6 +12,8 @@ import edu.wpi.first.wpilibj.DutyCycleEncoder;
|
||||
public class DutyCycleEncoderSim {
|
||||
private final SimDouble m_simPosition;
|
||||
private final SimDouble m_simDistancePerRotation;
|
||||
private final SimDouble m_simAbsolutePosition;
|
||||
private final SimBoolean m_simIsConnected;
|
||||
|
||||
/**
|
||||
* Constructs from an DutyCycleEncoder object.
|
||||
@@ -30,6 +33,17 @@ public class DutyCycleEncoderSim {
|
||||
SimDeviceSim wrappedSimDevice = new SimDeviceSim("DutyCycle:DutyCycleEncoder", channel);
|
||||
m_simPosition = wrappedSimDevice.getDouble("position");
|
||||
m_simDistancePerRotation = wrappedSimDevice.getDouble("distance_per_rot");
|
||||
m_simAbsolutePosition = wrappedSimDevice.getDouble("absPosition");
|
||||
m_simIsConnected = wrappedSimDevice.getBoolean("connected");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the position in turns.
|
||||
*
|
||||
* @return The position.
|
||||
*/
|
||||
public double get() {
|
||||
return m_simPosition.get();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,11 +56,65 @@ public class DutyCycleEncoderSim {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the position.
|
||||
* Get the distance.
|
||||
*
|
||||
* @param distance The position.
|
||||
* @return The distance.
|
||||
*/
|
||||
public double getDistance() {
|
||||
return m_simPosition.get() * m_simDistancePerRotation.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the distance.
|
||||
*
|
||||
* @param distance The distance.
|
||||
*/
|
||||
public void setDistance(double distance) {
|
||||
m_simPosition.set(distance / m_simDistancePerRotation.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the absolute position.
|
||||
*
|
||||
* @return The absolute position
|
||||
*/
|
||||
public double getAbsolutePosition() {
|
||||
return m_simAbsolutePosition.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the absolute position.
|
||||
*
|
||||
* @param position The absolute position
|
||||
*/
|
||||
public void setAbsolutePosition(double position) {
|
||||
m_simAbsolutePosition.set(position);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the distance per rotation for this encoder.
|
||||
*
|
||||
* @return The scale factor that will be used to convert rotation to useful units.
|
||||
*/
|
||||
public double getDistancePerRotation() {
|
||||
return m_simDistancePerRotation.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get if the encoder is connected.
|
||||
*
|
||||
* @return true if the encoder is connected.
|
||||
*/
|
||||
public boolean getConnected() {
|
||||
return m_simIsConnected.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set if the encoder is connected.
|
||||
*
|
||||
* @param isConnected Whether or not the sensor is connected.
|
||||
*/
|
||||
public void setConnected(boolean isConnected) {
|
||||
m_simIsConnected.set(isConnected);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user