mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
[wpilib] Add DutyCycleEncoderSim (#2798)
This commit is contained in:
@@ -32,6 +32,7 @@ public class DutyCycleEncoder implements Sendable, AutoCloseable {
|
||||
|
||||
protected SimDevice m_simDevice;
|
||||
protected SimDouble m_simPosition;
|
||||
protected SimDouble m_simDistancePerRotation;
|
||||
protected SimBoolean m_simIsConnected;
|
||||
|
||||
/**
|
||||
@@ -72,6 +73,7 @@ public class DutyCycleEncoder implements Sendable, AutoCloseable {
|
||||
|
||||
if (m_simDevice != null) {
|
||||
m_simPosition = m_simDevice.createDouble("Position", false, 0.0);
|
||||
m_simDistancePerRotation = m_simDevice.createDouble("DistancePerRotation", false, 1.0);
|
||||
m_simIsConnected = m_simDevice.createBoolean("Connected", false, true);
|
||||
} else {
|
||||
m_counter = new Counter();
|
||||
@@ -140,6 +142,10 @@ public class DutyCycleEncoder implements Sendable, AutoCloseable {
|
||||
*/
|
||||
public void setDistancePerRotation(double distancePerRotation) {
|
||||
m_distancePerRotation = distancePerRotation;
|
||||
|
||||
if (m_simDistancePerRotation != null) {
|
||||
m_simDistancePerRotation.set(distancePerRotation);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -230,6 +236,15 @@ public class DutyCycleEncoder implements Sendable, AutoCloseable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channel of the source.
|
||||
*
|
||||
* @return the source channel
|
||||
*/
|
||||
public int getSourceChannel() {
|
||||
return m_dutyCycle.getSourceChannel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initSendable(SendableBuilder builder) {
|
||||
builder.setSmartDashboardType("AbsoluteEncoder");
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2020 FIRST. All Rights Reserved. */
|
||||
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
||||
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
||||
/* the project. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import edu.wpi.first.hal.SimDouble;
|
||||
import edu.wpi.first.wpilibj.DutyCycleEncoder;
|
||||
|
||||
/**
|
||||
* Class to control a simulated duty cycle encoder.
|
||||
*/
|
||||
public class DutyCycleEncoderSim {
|
||||
private final SimDouble m_simPosition;
|
||||
private final SimDouble m_simDistancePerRotation;
|
||||
|
||||
/**
|
||||
* Constructs from an DutyCycleEncoder object.
|
||||
*
|
||||
* @param encoder DutyCycleEncoder to simulate
|
||||
*/
|
||||
public DutyCycleEncoderSim(DutyCycleEncoder encoder) {
|
||||
SimDeviceSim wrappedSimDevice = new SimDeviceSim("DutyCycleEncoder" + "[" + encoder.getSourceChannel() + "]");
|
||||
m_simPosition = wrappedSimDevice.getDouble("Position");
|
||||
m_simDistancePerRotation = wrappedSimDevice.getDouble("DistancePerRotation");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the position in turns.
|
||||
*
|
||||
* @param turns The position.
|
||||
*/
|
||||
public void set(double turns) {
|
||||
m_simPosition.set(turns);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the position.
|
||||
*/
|
||||
public void setDistance(double distance) {
|
||||
m_simPosition.set(distance / m_simDistancePerRotation.get());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user