[sim] Add AnalogEncoderSim (#2647)

This commit is contained in:
Matt
2020-08-19 22:59:52 -07:00
committed by GitHub
parent 932bfcf374
commit 9398b6b55b
8 changed files with 237 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
/* Copyright (c) 2019-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. */
@@ -132,6 +132,15 @@ public class AnalogEncoder implements Sendable, AutoCloseable {
return get() * getDistancePerRotation();
}
/**
* Get the channel number.
*
* @return The channel number.
*/
public int getChannel() {
return m_analogInput.getChannel();
}
/**
* Reset the Encoder distance to zero.
*/

View File

@@ -0,0 +1,59 @@
/*----------------------------------------------------------------------------*/
/* 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.AnalogEncoder;
import edu.wpi.first.wpilibj.geometry.Rotation2d;
/**
* Class to control a simulated analog encoder.
*/
public class AnalogEncoderSim {
private final SimDouble m_simPosition;
/**
* Constructs from an AnalogEncoder object.
*
* @param encoder AnalogEncoder to simulate
*/
public AnalogEncoderSim(AnalogEncoder encoder) {
SimDeviceSim wrappedSimDevice = new SimDeviceSim("AnalogEncoder" + "[" + encoder.getChannel() + "]");
m_simPosition = wrappedSimDevice.getDouble("Position");
}
/**
* Set the position using an {@link Rotation2d}.
*/
public void setPosition(Rotation2d angle) {
setTurns(angle.getDegrees() / 360.0);
}
/**
* Set the position of the encoder.
*
* @param turns The position.
*/
public void setTurns(double turns) {
m_simPosition.set(turns);
}
/**
* Get the simulated position.
*/
public double getTurns() {
return m_simPosition.get();
}
/**
* Get the position as a {@link Rotation2d}.
*/
public Rotation2d getPosition() {
return Rotation2d.fromDegrees(getTurns() * 360.0);
}
}