mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-25 01:41:43 +00:00
SCRIPT Move java files
This commit is contained in:
committed by
Peter Johnson
parent
7ca1be9bae
commit
c350c5f112
@@ -0,0 +1,38 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.wpilibj.ADXL345_I2C;
|
||||
import edu.wpi.first.wpilibj.I2C;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.EnumSource;
|
||||
|
||||
class ADXL345SimTest {
|
||||
@ParameterizedTest
|
||||
@EnumSource(ADXL345_I2C.Range.class)
|
||||
void testInitI2C(ADXL345_I2C.Range range) {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
try (ADXL345_I2C accel = new ADXL345_I2C(I2C.Port.kPort0, range)) {
|
||||
ADXL345Sim sim = new ADXL345Sim(accel);
|
||||
|
||||
sim.setX(1.91);
|
||||
sim.setY(-3.405);
|
||||
sim.setZ(2.29);
|
||||
|
||||
assertEquals(1.91, accel.getX());
|
||||
assertEquals(-3.405, accel.getY());
|
||||
assertEquals(2.29, accel.getZ());
|
||||
|
||||
ADXL345_I2C.AllAxes allAccel = accel.getAccelerations();
|
||||
assertEquals(1.91, allAccel.XAxis);
|
||||
assertEquals(-3.405, allAccel.YAxis);
|
||||
assertEquals(2.29, allAccel.ZAxis);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.wpilibj.AddressableLED;
|
||||
import edu.wpi.first.wpilibj.AddressableLEDBuffer;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.BooleanCallback;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.BufferCallback;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.IntCallback;
|
||||
import java.util.Arrays;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class AddressableLEDSimTest {
|
||||
@Test
|
||||
void testInitialization() {
|
||||
HAL.initialize(500, 0);
|
||||
AddressableLEDSim sim = new AddressableLEDSim(0);
|
||||
assertFalse(sim.getInitialized());
|
||||
|
||||
BooleanCallback initializedCallback = new BooleanCallback();
|
||||
|
||||
try (CallbackStore cb = sim.registerInitializedCallback(initializedCallback, false);
|
||||
AddressableLED led = new AddressableLED(0)) {
|
||||
assertTrue(sim.getInitialized());
|
||||
assertTrue(initializedCallback.wasTriggered());
|
||||
assertTrue(initializedCallback.getSetValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLength() {
|
||||
AddressableLEDSim sim = new AddressableLEDSim(0);
|
||||
IntCallback callback = new IntCallback();
|
||||
try (CallbackStore cb = sim.registerLengthCallback(callback, false);
|
||||
AddressableLED led = new AddressableLED(0)) {
|
||||
assertEquals(0, sim.getLength()); // Defaults to 0 leds
|
||||
|
||||
AddressableLEDBuffer ledData = new AddressableLEDBuffer(50);
|
||||
led.setLength(ledData.getLength());
|
||||
led.setData(ledData);
|
||||
|
||||
assertEquals(50, sim.getLength());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(50, callback.getSetValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetData() {
|
||||
AddressableLEDSim sim = new AddressableLEDSim(0);
|
||||
BufferCallback callback = new BufferCallback();
|
||||
|
||||
try (AddressableLED led = new AddressableLED(0);
|
||||
CallbackStore cb = AddressableLEDSim.registerDataCallback(callback)) {
|
||||
assertEquals(0, sim.getLength()); // Defaults to 0 leds
|
||||
|
||||
AddressableLEDBuffer ledData = new AddressableLEDBuffer(3);
|
||||
led.setLength(ledData.getLength());
|
||||
|
||||
ledData.setRGB(0, 255, 0, 0);
|
||||
ledData.setRGB(1, 0, 255, 0);
|
||||
ledData.setRGB(2, 0, 0, 255);
|
||||
led.setData(ledData);
|
||||
|
||||
byte[] data = sim.getData();
|
||||
System.out.println(Arrays.toString(data));
|
||||
assertEquals(9, data.length);
|
||||
|
||||
assertEquals((byte) 255, data[0]);
|
||||
assertEquals((byte) 0, data[1]);
|
||||
assertEquals((byte) 0, data[2]);
|
||||
|
||||
assertEquals((byte) 0, data[3]);
|
||||
assertEquals((byte) 255, data[4]);
|
||||
assertEquals((byte) 0, data[5]);
|
||||
|
||||
assertEquals((byte) 0, data[6]);
|
||||
assertEquals((byte) 0, data[7]);
|
||||
assertEquals((byte) 255, data[8]);
|
||||
|
||||
assertTrue(callback.wasTriggered());
|
||||
data = callback.getSetValue();
|
||||
|
||||
assertEquals((byte) 255, data[0]);
|
||||
assertEquals((byte) 0, data[1]);
|
||||
assertEquals((byte) 0, data[2]);
|
||||
|
||||
assertEquals((byte) 0, data[3]);
|
||||
assertEquals((byte) 255, data[4]);
|
||||
assertEquals((byte) 0, data[5]);
|
||||
|
||||
assertEquals((byte) 0, data[6]);
|
||||
assertEquals((byte) 0, data[7]);
|
||||
assertEquals((byte) 255, data[8]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import edu.wpi.first.wpilibj.AnalogEncoder;
|
||||
import edu.wpi.first.wpilibj.AnalogInput;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class AnalogEncoderSimTest {
|
||||
@Test
|
||||
void testBasic() {
|
||||
try (var analogInput = new AnalogInput(0);
|
||||
var analogEncoder = new AnalogEncoder(analogInput, 360, 0)) {
|
||||
var encoderSim = new AnalogEncoderSim(analogEncoder);
|
||||
|
||||
encoderSim.set(180);
|
||||
assertEquals(analogEncoder.get(), 180, 1E-8);
|
||||
assertEquals(encoderSim.get(), 180, 1E-8);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.wpilibj.AnalogInput;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.BooleanCallback;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.DoubleCallback;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class AnalogInputSimTest {
|
||||
@Test
|
||||
void setInitializeTest() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
AnalogInputSim sim = new AnalogInputSim(5);
|
||||
BooleanCallback callback = new BooleanCallback();
|
||||
|
||||
try (CallbackStore cb = sim.registerInitializedCallback(callback, false);
|
||||
AnalogInput input = new AnalogInput(5)) {
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertTrue(callback.getSetValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetVoltage() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
AnalogInputSim sim = new AnalogInputSim(5);
|
||||
DoubleCallback callback = new DoubleCallback();
|
||||
|
||||
try (CallbackStore cb = sim.registerVoltageCallback(callback, false);
|
||||
AnalogInput input = new AnalogInput(5)) {
|
||||
// Bootstrap the voltage to be non-zero
|
||||
sim.setVoltage(1.0);
|
||||
|
||||
for (double i = 0; i < 5.0; i += 0.1) {
|
||||
callback.reset();
|
||||
|
||||
sim.setVoltage(0);
|
||||
assertEquals(input.getVoltage(), 0, 0.001);
|
||||
|
||||
callback.reset();
|
||||
sim.setVoltage(i);
|
||||
assertEquals(input.getVoltage(), i, 0.001);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetOverSampleBits() {
|
||||
HAL.initialize(500, 0);
|
||||
try (AnalogInput input = new AnalogInput(5)) {
|
||||
input.setOversampleBits(3504);
|
||||
assertEquals(3504, input.getOversampleBits());
|
||||
}
|
||||
}
|
||||
}
|
||||
168
wpilibj/src/test/java/org/wpilib/simulation/CTREPCMSimTest.java
Normal file
168
wpilibj/src/test/java/org/wpilib/simulation/CTREPCMSimTest.java
Normal file
@@ -0,0 +1,168 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.wpilibj.CompressorConfigType;
|
||||
import edu.wpi.first.wpilibj.DoubleSolenoid;
|
||||
import edu.wpi.first.wpilibj.PneumaticsControlModule;
|
||||
import edu.wpi.first.wpilibj.PneumaticsModuleType;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.BooleanCallback;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.DoubleCallback;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CTREPCMSimTest {
|
||||
@Test
|
||||
void testInitialization() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
CTREPCMSim sim = new CTREPCMSim(0);
|
||||
sim.resetData();
|
||||
assertFalse(sim.getInitialized());
|
||||
|
||||
BooleanCallback callback = new BooleanCallback();
|
||||
|
||||
try (CallbackStore cb = sim.registerInitializedCallback(callback, false);
|
||||
PneumaticsControlModule pcm = new PneumaticsControlModule(0)) {
|
||||
assertTrue(sim.getInitialized());
|
||||
}
|
||||
assertFalse(sim.getInitialized());
|
||||
}
|
||||
|
||||
@Test
|
||||
void solenoidOutputTest() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
try (PneumaticsControlModule pcm = new PneumaticsControlModule(0);
|
||||
DoubleSolenoid doubleSolenoid = new DoubleSolenoid(0, PneumaticsModuleType.CTREPCM, 3, 4)) {
|
||||
CTREPCMSim sim = new CTREPCMSim(0);
|
||||
sim.resetData();
|
||||
|
||||
BooleanCallback callback3 = new BooleanCallback();
|
||||
BooleanCallback callback4 = new BooleanCallback();
|
||||
|
||||
try (CallbackStore cb3 = sim.registerSolenoidOutputCallback(3, callback3, false);
|
||||
CallbackStore cb4 = sim.registerSolenoidOutputCallback(4, callback4, false)) {
|
||||
// Reverse
|
||||
callback3.reset();
|
||||
callback4.reset();
|
||||
doubleSolenoid.set(DoubleSolenoid.Value.kReverse);
|
||||
assertFalse(callback3.wasTriggered());
|
||||
assertNull(callback3.getSetValue());
|
||||
assertTrue(callback4.wasTriggered());
|
||||
assertTrue(callback4.getSetValue());
|
||||
assertFalse(sim.getSolenoidOutput(3));
|
||||
assertTrue(sim.getSolenoidOutput(4));
|
||||
assertEquals(0x10, pcm.getSolenoids());
|
||||
|
||||
// Forward
|
||||
callback3.reset();
|
||||
callback4.reset();
|
||||
doubleSolenoid.set(DoubleSolenoid.Value.kForward);
|
||||
assertTrue(callback3.wasTriggered());
|
||||
assertTrue(callback3.getSetValue());
|
||||
assertTrue(callback4.wasTriggered());
|
||||
assertFalse(callback4.getSetValue());
|
||||
assertTrue(sim.getSolenoidOutput(3));
|
||||
assertFalse(sim.getSolenoidOutput(4));
|
||||
assertEquals(0x8, pcm.getSolenoids());
|
||||
|
||||
// Off
|
||||
callback3.reset();
|
||||
callback4.reset();
|
||||
doubleSolenoid.set(DoubleSolenoid.Value.kOff);
|
||||
assertTrue(callback3.wasTriggered());
|
||||
assertFalse(callback3.getSetValue());
|
||||
assertFalse(callback4.wasTriggered());
|
||||
assertNull(callback4.getSetValue());
|
||||
assertFalse(sim.getSolenoidOutput(3));
|
||||
assertFalse(sim.getSolenoidOutput(4));
|
||||
assertEquals(0x0, pcm.getSolenoids());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void setCompressorOnTest() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
CTREPCMSim sim = new CTREPCMSim(0);
|
||||
BooleanCallback callback = new BooleanCallback();
|
||||
|
||||
try (PneumaticsControlModule pcm = new PneumaticsControlModule(0);
|
||||
CallbackStore cb = sim.registerCompressorOnCallback(callback, false)) {
|
||||
assertFalse(pcm.getCompressor());
|
||||
assertFalse(sim.getCompressorOn());
|
||||
sim.setCompressorOn(true);
|
||||
assertTrue(pcm.getCompressor());
|
||||
assertTrue(sim.getCompressorOn());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertTrue(callback.getSetValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void setEnableDigital() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
CTREPCMSim sim = new CTREPCMSim(0);
|
||||
BooleanCallback callback = new BooleanCallback();
|
||||
|
||||
try (PneumaticsControlModule pcm = new PneumaticsControlModule(0);
|
||||
CallbackStore cb = sim.registerClosedLoopEnabledCallback(callback, false)) {
|
||||
pcm.disableCompressor();
|
||||
assertEquals(pcm.getCompressorConfigType(), CompressorConfigType.Disabled);
|
||||
|
||||
pcm.enableCompressorDigital();
|
||||
assertTrue(sim.getClosedLoopEnabled());
|
||||
assertEquals(pcm.getCompressorConfigType(), CompressorConfigType.Digital);
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertTrue(callback.getSetValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void setPressureSwitchEnabledTest() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
CTREPCMSim sim = new CTREPCMSim(0);
|
||||
BooleanCallback callback = new BooleanCallback();
|
||||
|
||||
try (PneumaticsControlModule pcm = new PneumaticsControlModule(0);
|
||||
CallbackStore cb = sim.registerPressureSwitchCallback(callback, false)) {
|
||||
assertFalse(pcm.getPressureSwitch());
|
||||
|
||||
sim.setPressureSwitch(true);
|
||||
assertTrue(sim.getPressureSwitch());
|
||||
assertTrue(pcm.getPressureSwitch());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertTrue(callback.getSetValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void setCompressorCurrentTest() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
CTREPCMSim sim = new CTREPCMSim(0);
|
||||
DoubleCallback callback = new DoubleCallback();
|
||||
|
||||
try (PneumaticsControlModule pcm = new PneumaticsControlModule(0);
|
||||
CallbackStore cb = sim.registerCompressorCurrentCallback(callback, false)) {
|
||||
assertFalse(pcm.getPressureSwitch());
|
||||
|
||||
sim.setCompressorCurrent(35.04);
|
||||
assertEquals(35.04, sim.getCompressorCurrent());
|
||||
assertEquals(35.04, pcm.getCompressorCurrent());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(35.04, callback.getSetValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import edu.wpi.first.math.controller.PIDController;
|
||||
import edu.wpi.first.math.numbers.N1;
|
||||
import edu.wpi.first.math.numbers.N2;
|
||||
import edu.wpi.first.math.system.LinearSystem;
|
||||
import edu.wpi.first.math.system.plant.DCMotor;
|
||||
import edu.wpi.first.math.system.plant.LinearSystemId;
|
||||
import edu.wpi.first.wpilibj.Encoder;
|
||||
import edu.wpi.first.wpilibj.RobotController;
|
||||
import edu.wpi.first.wpilibj.motorcontrol.PWMVictorSPX;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class DCMotorSimTest {
|
||||
@Test
|
||||
void testVoltageSteadyState() {
|
||||
RoboRioSim.resetData();
|
||||
|
||||
DCMotor gearbox = DCMotor.getNEO(1);
|
||||
LinearSystem<N2, N1, N2> plant = LinearSystemId.createDCMotorSystem(gearbox, 0.0005, 1);
|
||||
DCMotorSim sim = new DCMotorSim(plant, gearbox);
|
||||
|
||||
try (var motor = new PWMVictorSPX(0);
|
||||
var encoder = new Encoder(0, 1)) {
|
||||
var encoderSim = new EncoderSim(encoder);
|
||||
encoderSim.resetData();
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
motor.setVoltage(12);
|
||||
|
||||
// ------ SimulationPeriodic() happens after user code -------
|
||||
RoboRioSim.setVInVoltage(
|
||||
BatterySim.calculateDefaultBatteryLoadedVoltage(sim.getCurrentDraw()));
|
||||
sim.setInputVoltage(motor.get() * RobotController.getBatteryVoltage());
|
||||
sim.update(0.020);
|
||||
encoderSim.setRate(sim.getAngularVelocity());
|
||||
}
|
||||
|
||||
assertEquals(gearbox.Kv * 12, encoder.getRate(), 0.1);
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
motor.setVoltage(0);
|
||||
|
||||
// ------ SimulationPeriodic() happens after user code -------
|
||||
RoboRioSim.setVInVoltage(
|
||||
BatterySim.calculateDefaultBatteryLoadedVoltage(sim.getCurrentDraw()));
|
||||
sim.setInputVoltage(motor.get() * RobotController.getBatteryVoltage());
|
||||
sim.update(0.020);
|
||||
encoderSim.setRate(sim.getAngularVelocity());
|
||||
}
|
||||
|
||||
assertEquals(0, encoder.getRate(), 0.1);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPositionFeedbackControl() {
|
||||
RoboRioSim.resetData();
|
||||
|
||||
DCMotor gearbox = DCMotor.getNEO(1);
|
||||
LinearSystem<N2, N1, N2> plant = LinearSystemId.createDCMotorSystem(gearbox, 0.0005, 1);
|
||||
DCMotorSim sim = new DCMotorSim(plant, gearbox);
|
||||
|
||||
try (var motor = new PWMVictorSPX(0);
|
||||
var encoder = new Encoder(0, 1);
|
||||
var controller = new PIDController(0.04, 0.0, 0.001)) {
|
||||
var encoderSim = new EncoderSim(encoder);
|
||||
encoderSim.resetData();
|
||||
|
||||
for (int i = 0; i < 140; i++) {
|
||||
motor.set(controller.calculate(encoder.getDistance(), 750));
|
||||
|
||||
// ------ SimulationPeriodic() happens after user code -------
|
||||
RoboRioSim.setVInVoltage(
|
||||
BatterySim.calculateDefaultBatteryLoadedVoltage(sim.getCurrentDraw()));
|
||||
sim.setInputVoltage(motor.get() * RobotController.getBatteryVoltage());
|
||||
sim.update(0.020);
|
||||
encoderSim.setDistance(sim.getAngularPosition());
|
||||
encoderSim.setRate(sim.getAngularVelocity());
|
||||
}
|
||||
|
||||
assertEquals(750, encoder.getDistance(), 1.0);
|
||||
assertEquals(0, encoder.getRate(), 0.1);
|
||||
}
|
||||
}
|
||||
}
|
||||
85
wpilibj/src/test/java/org/wpilib/simulation/DIOSimTest.java
Normal file
85
wpilibj/src/test/java/org/wpilib/simulation/DIOSimTest.java
Normal file
@@ -0,0 +1,85 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.wpilibj.DigitalInput;
|
||||
import edu.wpi.first.wpilibj.DigitalOutput;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.BooleanCallback;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class DIOSimTest {
|
||||
@Test
|
||||
void testInitialization() {
|
||||
DIOSim sim = new DIOSim(2);
|
||||
assertFalse(sim.getInitialized());
|
||||
|
||||
BooleanCallback initializedCallback = new BooleanCallback();
|
||||
BooleanCallback isInputCallback = new BooleanCallback();
|
||||
|
||||
try (CallbackStore initializeCb = sim.registerInitializedCallback(initializedCallback, false);
|
||||
CallbackStore inputCb = sim.registerIsInputCallback(isInputCallback, false);
|
||||
DigitalOutput output = new DigitalOutput(2)) {
|
||||
assertTrue(sim.getInitialized());
|
||||
assertTrue(initializedCallback.wasTriggered());
|
||||
assertTrue(initializedCallback.getSetValue());
|
||||
|
||||
assertFalse(sim.getIsInput());
|
||||
assertTrue(isInputCallback.wasTriggered());
|
||||
assertFalse(isInputCallback.getSetValue());
|
||||
|
||||
initializedCallback.reset();
|
||||
sim.setInitialized(false);
|
||||
assertTrue(initializedCallback.wasTriggered());
|
||||
assertFalse(initializedCallback.getSetValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testInput() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
try (DigitalInput input = new DigitalInput(0)) {
|
||||
DIOSim sim = new DIOSim(input);
|
||||
assertTrue(sim.getIsInput());
|
||||
|
||||
BooleanCallback valueCallback = new BooleanCallback();
|
||||
|
||||
try (CallbackStore cb = sim.registerValueCallback(valueCallback, false)) {
|
||||
assertTrue(input.get());
|
||||
assertTrue(sim.getValue());
|
||||
|
||||
assertFalse(valueCallback.wasTriggered());
|
||||
sim.setValue(false);
|
||||
assertTrue(valueCallback.wasTriggered());
|
||||
assertFalse(valueCallback.getSetValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testOutput() {
|
||||
HAL.initialize(500, 0);
|
||||
try (DigitalOutput output = new DigitalOutput(0)) {
|
||||
DIOSim sim = new DIOSim(output);
|
||||
assertFalse(sim.getIsInput());
|
||||
|
||||
BooleanCallback valueCallback = new BooleanCallback();
|
||||
|
||||
try (CallbackStore cb = sim.registerValueCallback(valueCallback, false)) {
|
||||
assertTrue(output.get());
|
||||
assertTrue(sim.getValue());
|
||||
|
||||
assertFalse(valueCallback.wasTriggered());
|
||||
output.set(false);
|
||||
assertTrue(valueCallback.wasTriggered());
|
||||
assertFalse(valueCallback.getSetValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import edu.wpi.first.math.Matrix;
|
||||
import edu.wpi.first.math.Nat;
|
||||
import edu.wpi.first.math.VecBuilder;
|
||||
import edu.wpi.first.math.Vector;
|
||||
import edu.wpi.first.math.controller.LTVUnicycleController;
|
||||
import edu.wpi.first.math.controller.LinearPlantInversionFeedforward;
|
||||
import edu.wpi.first.math.geometry.Pose2d;
|
||||
import edu.wpi.first.math.geometry.Rotation2d;
|
||||
import edu.wpi.first.math.kinematics.DifferentialDriveKinematics;
|
||||
import edu.wpi.first.math.numbers.N1;
|
||||
import edu.wpi.first.math.numbers.N7;
|
||||
import edu.wpi.first.math.system.NumericalIntegration;
|
||||
import edu.wpi.first.math.system.plant.DCMotor;
|
||||
import edu.wpi.first.math.system.plant.LinearSystemId;
|
||||
import edu.wpi.first.math.trajectory.TrajectoryConfig;
|
||||
import edu.wpi.first.math.trajectory.TrajectoryGenerator;
|
||||
import edu.wpi.first.math.trajectory.constraint.DifferentialDriveKinematicsConstraint;
|
||||
import edu.wpi.first.math.util.Units;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class DifferentialDrivetrainSimTest {
|
||||
@Test
|
||||
void testConvergence() {
|
||||
var motor = DCMotor.getNEO(2);
|
||||
var plant =
|
||||
LinearSystemId.createDrivetrainVelocitySystem(
|
||||
motor, 50, Units.inchesToMeters(2), Units.inchesToMeters(12), 0.5, 1.0);
|
||||
|
||||
var kinematics = new DifferentialDriveKinematics(Units.inchesToMeters(24));
|
||||
var sim =
|
||||
new DifferentialDrivetrainSim(
|
||||
plant,
|
||||
motor,
|
||||
1,
|
||||
kinematics.trackwidth,
|
||||
Units.inchesToMeters(2),
|
||||
VecBuilder.fill(0, 0, 0.0001, 0.1, 0.1, 0.005, 0.005));
|
||||
|
||||
var feedforward = new LinearPlantInversionFeedforward<>(plant, 0.020);
|
||||
var feedback = new LTVUnicycleController(0.020);
|
||||
feedforward.reset(VecBuilder.fill(0, 0));
|
||||
|
||||
// ground truth
|
||||
Matrix<N7, N1> groundTruthX = new Vector<>(Nat.N7());
|
||||
|
||||
var traj =
|
||||
TrajectoryGenerator.generateTrajectory(
|
||||
Pose2d.kZero,
|
||||
List.of(),
|
||||
new Pose2d(2, 2, Rotation2d.kZero),
|
||||
new TrajectoryConfig(1, 1)
|
||||
.addConstraint(new DifferentialDriveKinematicsConstraint(kinematics, 1)));
|
||||
|
||||
for (double t = 0; t < traj.getTotalTime(); t += 0.020) {
|
||||
var state = traj.sample(t);
|
||||
var feedbackOut = feedback.calculate(sim.getPose(), state);
|
||||
|
||||
var wheelSpeeds = kinematics.toWheelSpeeds(feedbackOut);
|
||||
|
||||
var voltages = feedforward.calculate(VecBuilder.fill(wheelSpeeds.left, wheelSpeeds.right));
|
||||
|
||||
// Sim periodic code
|
||||
sim.setInputs(voltages.get(0, 0), voltages.get(1, 0));
|
||||
sim.update(0.020);
|
||||
|
||||
// Update our ground truth
|
||||
groundTruthX = NumericalIntegration.rkdp(sim::getDynamics, groundTruthX, voltages, 0.020);
|
||||
}
|
||||
|
||||
// 2 inch tolerance is OK since our ground truth is an approximation of the
|
||||
// ODE solution using RKDP anyway
|
||||
assertEquals(
|
||||
groundTruthX.get(DifferentialDrivetrainSim.State.kX.value, 0),
|
||||
sim.getState(DifferentialDrivetrainSim.State.kX),
|
||||
0.05);
|
||||
assertEquals(
|
||||
groundTruthX.get(DifferentialDrivetrainSim.State.kY.value, 0),
|
||||
sim.getState(DifferentialDrivetrainSim.State.kY),
|
||||
0.05);
|
||||
assertEquals(
|
||||
groundTruthX.get(DifferentialDrivetrainSim.State.kHeading.value, 0),
|
||||
sim.getState(DifferentialDrivetrainSim.State.kHeading),
|
||||
0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCurrent() {
|
||||
var motor = DCMotor.getNEO(2);
|
||||
var plant =
|
||||
LinearSystemId.createDrivetrainVelocitySystem(
|
||||
motor, 50, Units.inchesToMeters(2), Units.inchesToMeters(12), 0.5, 1.0);
|
||||
var kinematics = new DifferentialDriveKinematics(Units.inchesToMeters(24));
|
||||
var sim =
|
||||
new DifferentialDrivetrainSim(
|
||||
plant, motor, 1, kinematics.trackwidth, Units.inchesToMeters(2), null);
|
||||
|
||||
sim.setInputs(-12, -12);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
sim.update(0.020);
|
||||
}
|
||||
assertTrue(sim.getCurrentDraw() > 0);
|
||||
|
||||
sim.setInputs(12, 12);
|
||||
for (int i = 0; i < 20; i++) {
|
||||
sim.update(0.020);
|
||||
}
|
||||
assertTrue(sim.getCurrentDraw() > 0);
|
||||
|
||||
sim.setInputs(-12, 12);
|
||||
for (int i = 0; i < 30; i++) {
|
||||
sim.update(0.020);
|
||||
}
|
||||
assertTrue(sim.getCurrentDraw() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testModelStability() {
|
||||
var motor = DCMotor.getNEO(2);
|
||||
var plant =
|
||||
LinearSystemId.createDrivetrainVelocitySystem(
|
||||
motor, 50, Units.inchesToMeters(2), Units.inchesToMeters(12), 2.0, 5.0);
|
||||
|
||||
var kinematics = new DifferentialDriveKinematics(Units.inchesToMeters(24));
|
||||
var sim =
|
||||
new DifferentialDrivetrainSim(
|
||||
plant,
|
||||
motor,
|
||||
5,
|
||||
kinematics.trackwidth,
|
||||
Units.inchesToMeters(2),
|
||||
VecBuilder.fill(0, 0, 0.0001, 0.1, 0.1, 0.005, 0.005));
|
||||
|
||||
sim.setInputs(2, 4);
|
||||
|
||||
// 10 seconds should be enough time to verify stability
|
||||
for (int i = 0; i < 500; i++) {
|
||||
sim.update(0.020);
|
||||
}
|
||||
|
||||
assertTrue(Math.abs(sim.getPose().getTranslation().getNorm()) < 100);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.wpilibj.DigitalOutput;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.BooleanCallback;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.DoubleCallback;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.IntCallback;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class DigitalPWMSimTest {
|
||||
@Test
|
||||
void testInitialization() {
|
||||
try (DigitalOutput output = new DigitalOutput(0)) {
|
||||
DigitalPWMSim sim = new DigitalPWMSim(output);
|
||||
assertFalse(sim.getInitialized());
|
||||
|
||||
BooleanCallback initializeCallback = new BooleanCallback();
|
||||
DoubleCallback dutyCycleCallback = new DoubleCallback();
|
||||
try (CallbackStore initCb = sim.registerInitializedCallback(initializeCallback, false);
|
||||
CallbackStore dutyCycleCb = sim.registerDutyCycleCallback(dutyCycleCallback, false)) {
|
||||
final double kTestDutyCycle = 0.191;
|
||||
output.enablePWM(kTestDutyCycle);
|
||||
|
||||
assertTrue(sim.getInitialized());
|
||||
assertTrue(initializeCallback.wasTriggered());
|
||||
assertTrue(initializeCallback.getSetValue());
|
||||
|
||||
assertEquals(kTestDutyCycle, sim.getDutyCycle());
|
||||
assertTrue(dutyCycleCallback.wasTriggered());
|
||||
assertEquals(kTestDutyCycle, dutyCycleCallback.getSetValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void setPinTest() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
try (DigitalOutput output = new DigitalOutput(0)) {
|
||||
DigitalPWMSim sim = new DigitalPWMSim(output);
|
||||
IntCallback callback = new IntCallback();
|
||||
try (CallbackStore cb = sim.registerPinCallback(callback, false)) {
|
||||
sim.setPin(191);
|
||||
assertEquals(191, sim.getPin());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(191, callback.getSetValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import edu.wpi.first.hal.AllianceStationID;
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.wpilibj.DriverStation;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.BooleanCallback;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.DoubleCallback;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.EnumCallback;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.EnumSource;
|
||||
|
||||
class DriverStationSimTest {
|
||||
@Test
|
||||
void testEnabled() {
|
||||
HAL.initialize(500, 0);
|
||||
DriverStationSim.resetData();
|
||||
|
||||
assertFalse(DriverStation.isEnabled());
|
||||
BooleanCallback callback = new BooleanCallback();
|
||||
try (CallbackStore cb = DriverStationSim.registerEnabledCallback(callback, false)) {
|
||||
DriverStationSim.setEnabled(true);
|
||||
DriverStationSim.notifyNewData();
|
||||
assertTrue(DriverStationSim.getEnabled());
|
||||
assertTrue(DriverStation.isEnabled());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertTrue(callback.getSetValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAutonomous() {
|
||||
HAL.initialize(500, 0);
|
||||
DriverStationSim.resetData();
|
||||
|
||||
assertFalse(DriverStation.isAutonomous());
|
||||
BooleanCallback callback = new BooleanCallback();
|
||||
try (CallbackStore cb = DriverStationSim.registerAutonomousCallback(callback, false)) {
|
||||
DriverStationSim.setAutonomous(true);
|
||||
DriverStationSim.notifyNewData();
|
||||
assertTrue(DriverStationSim.getAutonomous());
|
||||
assertTrue(DriverStation.isAutonomous());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertTrue(callback.getSetValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTest() {
|
||||
HAL.initialize(500, 0);
|
||||
DriverStationSim.resetData();
|
||||
|
||||
assertFalse(DriverStation.isTest());
|
||||
BooleanCallback callback = new BooleanCallback();
|
||||
try (CallbackStore cb = DriverStationSim.registerTestCallback(callback, false)) {
|
||||
DriverStationSim.setTest(true);
|
||||
DriverStationSim.notifyNewData();
|
||||
assertTrue(DriverStationSim.getTest());
|
||||
assertTrue(DriverStation.isTest());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertTrue(callback.getSetValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEstop() {
|
||||
HAL.initialize(500, 0);
|
||||
DriverStationSim.resetData();
|
||||
|
||||
assertFalse(DriverStation.isEStopped());
|
||||
BooleanCallback callback = new BooleanCallback();
|
||||
try (CallbackStore cb = DriverStationSim.registerEStopCallback(callback, false)) {
|
||||
DriverStationSim.setEStop(true);
|
||||
DriverStationSim.notifyNewData();
|
||||
assertTrue(DriverStationSim.getEStop());
|
||||
assertTrue(DriverStation.isEStopped());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertTrue(callback.getSetValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFmsAttached() {
|
||||
HAL.initialize(500, 0);
|
||||
DriverStationSim.resetData();
|
||||
|
||||
assertFalse(DriverStation.isFMSAttached());
|
||||
BooleanCallback callback = new BooleanCallback();
|
||||
try (CallbackStore cb = DriverStationSim.registerFmsAttachedCallback(callback, false)) {
|
||||
DriverStationSim.setFmsAttached(true);
|
||||
DriverStationSim.notifyNewData();
|
||||
assertTrue(DriverStationSim.getFmsAttached());
|
||||
assertTrue(DriverStation.isFMSAttached());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertTrue(callback.getSetValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDsAttached() {
|
||||
HAL.initialize(500, 0);
|
||||
DriverStationSim.resetData();
|
||||
DriverStation.refreshData();
|
||||
|
||||
assertFalse(DriverStationSim.getDsAttached());
|
||||
assertFalse(DriverStation.isDSAttached());
|
||||
DriverStationSim.notifyNewData();
|
||||
assertTrue(DriverStationSim.getDsAttached());
|
||||
assertTrue(DriverStation.isDSAttached());
|
||||
|
||||
BooleanCallback callback = new BooleanCallback();
|
||||
try (CallbackStore cb = DriverStationSim.registerDsAttachedCallback(callback, false)) {
|
||||
DriverStationSim.setDsAttached(false);
|
||||
DriverStation.refreshData();
|
||||
assertFalse(DriverStationSim.getDsAttached());
|
||||
assertFalse(DriverStation.isDSAttached());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertFalse(callback.getSetValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAllianceStationId() {
|
||||
HAL.initialize(500, 0);
|
||||
DriverStationSim.resetData();
|
||||
|
||||
EnumCallback callback = new EnumCallback();
|
||||
|
||||
AllianceStationID allianceStation = AllianceStationID.Blue2;
|
||||
DriverStationSim.setAllianceStationId(allianceStation);
|
||||
|
||||
try (CallbackStore cb = DriverStationSim.registerAllianceStationIdCallback(callback, false)) {
|
||||
// Unknown
|
||||
allianceStation = AllianceStationID.Unknown;
|
||||
DriverStationSim.setAllianceStationId(allianceStation);
|
||||
DriverStationSim.notifyNewData();
|
||||
assertEquals(allianceStation, DriverStationSim.getAllianceStationId());
|
||||
assertFalse(DriverStation.getAlliance().isPresent());
|
||||
assertFalse(DriverStation.getLocation().isPresent());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(allianceStation.ordinal(), callback.getSetValue());
|
||||
|
||||
// B1
|
||||
allianceStation = AllianceStationID.Blue1;
|
||||
DriverStationSim.setAllianceStationId(allianceStation);
|
||||
DriverStationSim.notifyNewData();
|
||||
assertEquals(allianceStation, DriverStationSim.getAllianceStationId());
|
||||
assertEquals(DriverStation.Alliance.Blue, DriverStation.getAlliance().get());
|
||||
assertEquals(1, DriverStation.getLocation().getAsInt());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(allianceStation.ordinal(), callback.getSetValue());
|
||||
|
||||
// B2
|
||||
allianceStation = AllianceStationID.Blue2;
|
||||
DriverStationSim.setAllianceStationId(allianceStation);
|
||||
DriverStationSim.notifyNewData();
|
||||
assertEquals(allianceStation, DriverStationSim.getAllianceStationId());
|
||||
assertEquals(DriverStation.Alliance.Blue, DriverStation.getAlliance().get());
|
||||
assertEquals(2, DriverStation.getLocation().getAsInt());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(allianceStation.ordinal(), callback.getSetValue());
|
||||
|
||||
// B3
|
||||
allianceStation = AllianceStationID.Blue3;
|
||||
DriverStationSim.setAllianceStationId(allianceStation);
|
||||
DriverStationSim.notifyNewData();
|
||||
assertEquals(allianceStation, DriverStationSim.getAllianceStationId());
|
||||
assertEquals(DriverStation.Alliance.Blue, DriverStation.getAlliance().get());
|
||||
assertEquals(3, DriverStation.getLocation().getAsInt());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(allianceStation.ordinal(), callback.getSetValue());
|
||||
|
||||
// R1
|
||||
allianceStation = AllianceStationID.Red1;
|
||||
DriverStationSim.setAllianceStationId(allianceStation);
|
||||
DriverStationSim.notifyNewData();
|
||||
assertEquals(allianceStation, DriverStationSim.getAllianceStationId());
|
||||
assertEquals(DriverStation.Alliance.Red, DriverStation.getAlliance().get());
|
||||
assertEquals(1, DriverStation.getLocation().getAsInt());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(allianceStation.ordinal(), callback.getSetValue());
|
||||
|
||||
// R2
|
||||
allianceStation = AllianceStationID.Red2;
|
||||
DriverStationSim.setAllianceStationId(allianceStation);
|
||||
DriverStationSim.notifyNewData();
|
||||
assertEquals(allianceStation, DriverStationSim.getAllianceStationId());
|
||||
assertEquals(DriverStation.Alliance.Red, DriverStation.getAlliance().get());
|
||||
assertEquals(2, DriverStation.getLocation().getAsInt());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(allianceStation.ordinal(), callback.getSetValue());
|
||||
|
||||
// R3
|
||||
allianceStation = AllianceStationID.Red3;
|
||||
DriverStationSim.setAllianceStationId(allianceStation);
|
||||
DriverStationSim.notifyNewData();
|
||||
assertEquals(allianceStation, DriverStationSim.getAllianceStationId());
|
||||
assertEquals(DriverStation.Alliance.Red, DriverStation.getAlliance().get());
|
||||
assertEquals(3, DriverStation.getLocation().getAsInt());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(allianceStation.ordinal(), callback.getSetValue());
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@EnumSource(DriverStation.MatchType.class)
|
||||
void testMatchType(DriverStation.MatchType matchType) {
|
||||
HAL.initialize(500, 0);
|
||||
DriverStationSim.resetData();
|
||||
|
||||
DriverStationSim.setMatchType(matchType);
|
||||
DriverStationSim.notifyNewData();
|
||||
assertEquals(matchType, DriverStation.getMatchType());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testReplayNumber() {
|
||||
HAL.initialize(500, 0);
|
||||
DriverStationSim.resetData();
|
||||
|
||||
DriverStationSim.setReplayNumber(4);
|
||||
DriverStationSim.notifyNewData();
|
||||
assertEquals(4, DriverStation.getReplayNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMatchNumber() {
|
||||
HAL.initialize(500, 0);
|
||||
DriverStationSim.resetData();
|
||||
|
||||
DriverStationSim.setMatchNumber(3);
|
||||
DriverStationSim.notifyNewData();
|
||||
assertEquals(3, DriverStation.getMatchNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMatchTime() {
|
||||
HAL.initialize(500, 0);
|
||||
DriverStationSim.resetData();
|
||||
|
||||
DoubleCallback callback = new DoubleCallback();
|
||||
try (CallbackStore cb = DriverStationSim.registerMatchTimeCallback(callback, false)) {
|
||||
final double testTime = 19.174;
|
||||
DriverStationSim.setMatchTime(testTime);
|
||||
DriverStationSim.notifyNewData();
|
||||
assertEquals(testTime, DriverStationSim.getMatchTime());
|
||||
assertEquals(testTime, DriverStation.getMatchTime());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(testTime, callback.getSetValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetGameSpecificMessage() {
|
||||
HAL.initialize(500, 0);
|
||||
DriverStationSim.resetData();
|
||||
|
||||
final String message = "Hello World!";
|
||||
DriverStationSim.setGameSpecificMessage(message);
|
||||
DriverStationSim.notifyNewData();
|
||||
assertEquals(message, DriverStation.getGameSpecificMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetEventName() {
|
||||
HAL.initialize(500, 0);
|
||||
DriverStationSim.resetData();
|
||||
|
||||
final String message = "The Best Event";
|
||||
DriverStationSim.setEventName(message);
|
||||
DriverStationSim.notifyNewData();
|
||||
assertEquals(message, DriverStation.getEventName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.wpilibj.DutyCycleEncoder;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class DutyCycleEncoderSimTest {
|
||||
@Test
|
||||
void setTest() {
|
||||
try (DutyCycleEncoder encoder = new DutyCycleEncoder(0, 5.67, 0)) {
|
||||
DutyCycleEncoderSim sim = new DutyCycleEncoderSim(encoder);
|
||||
|
||||
sim.set(5.67);
|
||||
assertEquals(5.67, encoder.get());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void setIsConnectedTest() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
try (DutyCycleEncoder encoder = new DutyCycleEncoder(0)) {
|
||||
DutyCycleEncoderSim sim = new DutyCycleEncoderSim(encoder);
|
||||
|
||||
sim.setConnected(true);
|
||||
assertTrue(encoder.isConnected());
|
||||
sim.setConnected(false);
|
||||
assertFalse(encoder.isConnected());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.wpilibj.DutyCycle;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.BooleanCallback;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.DoubleCallback;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class DutyCycleSimTest {
|
||||
@Test
|
||||
void testInitialization() {
|
||||
DutyCycleSim sim = DutyCycleSim.createForChannel(2);
|
||||
assertFalse(sim.getInitialized());
|
||||
|
||||
BooleanCallback callback = new BooleanCallback();
|
||||
|
||||
try (CallbackStore cb = sim.registerInitializedCallback(callback, false);
|
||||
DutyCycle dc = new DutyCycle(2)) {
|
||||
assertTrue(sim.getInitialized());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertTrue(callback.getSetValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void setFrequencyTest() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
try (DutyCycle dc = new DutyCycle(2)) {
|
||||
DoubleCallback callback = new DoubleCallback();
|
||||
DutyCycleSim sim = new DutyCycleSim(dc);
|
||||
try (CallbackStore cb = sim.registerFrequencyCallback(callback, false)) {
|
||||
sim.setFrequency(191);
|
||||
assertEquals(191, sim.getFrequency());
|
||||
assertEquals(191, dc.getFrequency());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(191, callback.getSetValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void setOutputTest() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
try (DutyCycle dc = new DutyCycle(2)) {
|
||||
DoubleCallback callback = new DoubleCallback();
|
||||
DutyCycleSim sim = new DutyCycleSim(dc);
|
||||
try (CallbackStore cb = sim.registerOutputCallback(callback, false)) {
|
||||
sim.setOutput(229.174);
|
||||
assertEquals(229.174, sim.getOutput());
|
||||
assertEquals(229.174, dc.getOutput());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(229.174, callback.getSetValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
127
wpilibj/src/test/java/org/wpilib/simulation/ElevatorSimTest.java
Normal file
127
wpilibj/src/test/java/org/wpilib/simulation/ElevatorSimTest.java
Normal file
@@ -0,0 +1,127 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import edu.wpi.first.math.VecBuilder;
|
||||
import edu.wpi.first.math.controller.PIDController;
|
||||
import edu.wpi.first.math.system.plant.DCMotor;
|
||||
import edu.wpi.first.math.system.plant.LinearSystemId;
|
||||
import edu.wpi.first.math.util.Units;
|
||||
import edu.wpi.first.wpilibj.Encoder;
|
||||
import edu.wpi.first.wpilibj.RobotController;
|
||||
import edu.wpi.first.wpilibj.motorcontrol.PWMVictorSPX;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ElevatorSimTest {
|
||||
@Test
|
||||
void testStateSpaceSimWithElevator() {
|
||||
RoboRioSim.resetData();
|
||||
|
||||
@SuppressWarnings("resource")
|
||||
var controller = new PIDController(10, 0, 0);
|
||||
|
||||
var sim =
|
||||
new ElevatorSim(
|
||||
DCMotor.getVex775Pro(4),
|
||||
14.67,
|
||||
8,
|
||||
0.75 * 25.4 / 1000.0,
|
||||
0.0,
|
||||
3.0,
|
||||
true,
|
||||
0.0,
|
||||
0.01,
|
||||
0.0);
|
||||
|
||||
try (var motor = new PWMVictorSPX(0);
|
||||
var encoder = new Encoder(0, 1)) {
|
||||
var encoderSim = new EncoderSim(encoder);
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
controller.setSetpoint(2.0);
|
||||
|
||||
double nextVoltage = controller.calculate(encoderSim.getDistance());
|
||||
|
||||
double currentBatteryVoltage = RobotController.getBatteryVoltage();
|
||||
motor.set(nextVoltage / currentBatteryVoltage);
|
||||
|
||||
// ------ SimulationPeriodic() happens after user code -------
|
||||
|
||||
var u = VecBuilder.fill(motor.get() * currentBatteryVoltage);
|
||||
sim.setInput(u);
|
||||
sim.update(0.020);
|
||||
var y = sim.getOutput();
|
||||
encoderSim.setDistance(y.get(0, 0));
|
||||
}
|
||||
|
||||
assertEquals(controller.getSetpoint(), sim.getPosition(), 0.2);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testInitialState() {
|
||||
double startingHeightMeters = 0.5;
|
||||
var sim =
|
||||
new ElevatorSim(
|
||||
DCMotor.getKrakenX60(2), 20, 8.0, 0.1, 0.0, 1.0, true, startingHeightMeters, 0.01, 0.0);
|
||||
|
||||
assertEquals(startingHeightMeters, sim.getPosition());
|
||||
assertEquals(0, sim.getVelocity());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMinMax() {
|
||||
var sim =
|
||||
new ElevatorSim(
|
||||
DCMotor.getVex775Pro(4),
|
||||
14.67,
|
||||
8.0,
|
||||
0.75 * 25.4 / 1000.0,
|
||||
0.0,
|
||||
1.0,
|
||||
true,
|
||||
0.0,
|
||||
0.01,
|
||||
0.0);
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
sim.setInput(VecBuilder.fill(0));
|
||||
sim.update(0.020);
|
||||
var height = sim.getPosition();
|
||||
assertTrue(height >= -0.05);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
sim.setInput(VecBuilder.fill(12.0));
|
||||
sim.update(0.020);
|
||||
var height = sim.getPosition();
|
||||
assertTrue(height <= 1.05);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStability() {
|
||||
var sim =
|
||||
new ElevatorSim(
|
||||
DCMotor.getVex775Pro(4), 100, 4, Units.inchesToMeters(0.5), 0, 10, false, 0.0);
|
||||
|
||||
sim.setState(VecBuilder.fill(0, 0));
|
||||
sim.setInput(12);
|
||||
for (int i = 0; i < 50; ++i) {
|
||||
sim.update(0.02);
|
||||
}
|
||||
|
||||
var system =
|
||||
LinearSystemId.createElevatorSystem(
|
||||
DCMotor.getVex775Pro(4), 4, Units.inchesToMeters(0.5), 100);
|
||||
assertEquals(
|
||||
system.calculateX(VecBuilder.fill(0, 0), VecBuilder.fill(12), 0.02 * 50.0).get(0, 0),
|
||||
sim.getPosition(),
|
||||
0.01);
|
||||
}
|
||||
}
|
||||
111
wpilibj/src/test/java/org/wpilib/simulation/EncoderSimTest.java
Normal file
111
wpilibj/src/test/java/org/wpilib/simulation/EncoderSimTest.java
Normal file
@@ -0,0 +1,111 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.wpilibj.Encoder;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.DoubleCallback;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.IntCallback;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class EncoderSimTest {
|
||||
private static final double DEFAULT_DISTANCE_PER_PULSE = 0.0005;
|
||||
|
||||
@Test
|
||||
void testRate() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
try (Encoder encoder = new Encoder(0, 1)) {
|
||||
EncoderSim sim = new EncoderSim(encoder);
|
||||
sim.resetData();
|
||||
|
||||
encoder.setDistancePerPulse(DEFAULT_DISTANCE_PER_PULSE);
|
||||
|
||||
sim.setRate(1.91);
|
||||
assertEquals(1.91, sim.getRate());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCount() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
try (Encoder encoder = new Encoder(0, 1)) {
|
||||
EncoderSim sim = new EncoderSim(encoder);
|
||||
sim.resetData();
|
||||
|
||||
encoder.setDistancePerPulse(DEFAULT_DISTANCE_PER_PULSE);
|
||||
|
||||
IntCallback callback = new IntCallback();
|
||||
try (CallbackStore cb = sim.registerCountCallback(callback, false)) {
|
||||
sim.setCount(3504);
|
||||
assertEquals(3504, sim.getCount());
|
||||
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(3504, encoder.get());
|
||||
assertEquals(3504, callback.getSetValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDistance() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
try (Encoder encoder = new Encoder(0, 1)) {
|
||||
EncoderSim sim = new EncoderSim(encoder);
|
||||
sim.resetData();
|
||||
|
||||
encoder.setDistancePerPulse(DEFAULT_DISTANCE_PER_PULSE);
|
||||
|
||||
sim.setDistance(229.174);
|
||||
assertEquals(229.174, sim.getDistance());
|
||||
assertEquals(229.174, encoder.getDistance());
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation") // Encoder.getPeriod()
|
||||
@Test
|
||||
void testPeriod() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
try (Encoder encoder = new Encoder(0, 1)) {
|
||||
EncoderSim sim = new EncoderSim(encoder);
|
||||
sim.resetData();
|
||||
|
||||
encoder.setDistancePerPulse(DEFAULT_DISTANCE_PER_PULSE);
|
||||
|
||||
DoubleCallback callback = new DoubleCallback();
|
||||
try (CallbackStore cb = sim.registerPeriodCallback(callback, false)) {
|
||||
sim.setPeriod(123.456);
|
||||
assertEquals(123.456, sim.getPeriod());
|
||||
assertEquals(123.456, encoder.getPeriod());
|
||||
assertEquals(DEFAULT_DISTANCE_PER_PULSE / 123.456, encoder.getRate());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDistancePerPulse() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
try (Encoder encoder = new Encoder(0, 1)) {
|
||||
EncoderSim sim = new EncoderSim(encoder);
|
||||
sim.resetData();
|
||||
|
||||
DoubleCallback callback = new DoubleCallback();
|
||||
try (CallbackStore cb = sim.registerDistancePerPulseCallback(callback, false)) {
|
||||
sim.setDistancePerPulse(0.03405);
|
||||
assertEquals(0.03405, sim.getDistancePerPulse());
|
||||
assertEquals(0.03405, encoder.getDistancePerPulse());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(0.03405, callback.getSetValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.wpilibj.motorcontrol.Spark;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class PWMMotorControllerSimTest {
|
||||
@Test
|
||||
void testMotor() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
try (Spark spark = new Spark(0)) {
|
||||
PWMMotorControllerSim sim = new PWMMotorControllerSim(spark);
|
||||
|
||||
spark.set(0);
|
||||
assertEquals(0, sim.getSpeed());
|
||||
|
||||
spark.set(0.354);
|
||||
assertEquals(0.354, sim.getSpeed());
|
||||
|
||||
spark.set(-0.785);
|
||||
assertEquals(-0.785, sim.getSpeed());
|
||||
}
|
||||
}
|
||||
}
|
||||
72
wpilibj/src/test/java/org/wpilib/simulation/PWMSimTest.java
Normal file
72
wpilibj/src/test/java/org/wpilib/simulation/PWMSimTest.java
Normal file
@@ -0,0 +1,72 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.wpilibj.PWM;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.BooleanCallback;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.IntCallback;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class PWMSimTest {
|
||||
@Test
|
||||
void testInitialize() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
PWMSim sim = new PWMSim(0);
|
||||
sim.resetData();
|
||||
assertFalse(sim.getInitialized());
|
||||
|
||||
BooleanCallback callback = new BooleanCallback();
|
||||
|
||||
try (CallbackStore cb = sim.registerInitializedCallback(callback, false);
|
||||
PWM pwm = new PWM(0)) {
|
||||
assertTrue(sim.getInitialized());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetPulseTime() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
PWMSim sim = new PWMSim(0);
|
||||
sim.resetData();
|
||||
assertFalse(sim.getInitialized());
|
||||
|
||||
IntCallback callback = new IntCallback();
|
||||
|
||||
try (CallbackStore cb = sim.registerPulseMicrosecondCallback(callback, false);
|
||||
PWM pwm = new PWM(0)) {
|
||||
sim.setPulseMicrosecond(2290);
|
||||
assertEquals(2290, sim.getPulseMicrosecond());
|
||||
assertEquals(2290, pwm.getPulseTimeMicroseconds());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(2290, callback.getSetValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetOutputPeriod() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
PWMSim sim = new PWMSim(0);
|
||||
sim.resetData();
|
||||
assertFalse(sim.getInitialized());
|
||||
|
||||
IntCallback callback = new IntCallback();
|
||||
|
||||
try (CallbackStore cb = sim.registerOutputPeriodCallback(callback, false);
|
||||
PWM pwm = new PWM(0)) {
|
||||
sim.setOutputPeriod(3504);
|
||||
assertEquals(3504, sim.getOutputPeriod());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(3504, callback.getSetValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
209
wpilibj/src/test/java/org/wpilib/simulation/REVPHSimTest.java
Normal file
209
wpilibj/src/test/java/org/wpilib/simulation/REVPHSimTest.java
Normal file
@@ -0,0 +1,209 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.wpilibj.CompressorConfigType;
|
||||
import edu.wpi.first.wpilibj.DoubleSolenoid;
|
||||
import edu.wpi.first.wpilibj.PneumaticHub;
|
||||
import edu.wpi.first.wpilibj.PneumaticsModuleType;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.BooleanCallback;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.DoubleCallback;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.EnumCallback;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class REVPHSimTest {
|
||||
@Test
|
||||
void testInitialization() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
REVPHSim sim = new REVPHSim(1);
|
||||
sim.resetData();
|
||||
assertFalse(sim.getInitialized());
|
||||
|
||||
BooleanCallback callback = new BooleanCallback();
|
||||
|
||||
try (CallbackStore cb = sim.registerInitializedCallback(callback, false);
|
||||
PneumaticHub ph = new PneumaticHub(1)) {
|
||||
assertTrue(sim.getInitialized());
|
||||
}
|
||||
assertFalse(sim.getInitialized());
|
||||
}
|
||||
|
||||
@Test
|
||||
void solenoidOutputTest() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
try (PneumaticHub ph = new PneumaticHub(1);
|
||||
DoubleSolenoid doubleSolenoid = new DoubleSolenoid(1, PneumaticsModuleType.REVPH, 3, 4)) {
|
||||
REVPHSim sim = new REVPHSim(ph);
|
||||
sim.resetData();
|
||||
|
||||
BooleanCallback callback3 = new BooleanCallback();
|
||||
BooleanCallback callback4 = new BooleanCallback();
|
||||
|
||||
try (CallbackStore cb3 = sim.registerSolenoidOutputCallback(3, callback3, false);
|
||||
CallbackStore cb4 = sim.registerSolenoidOutputCallback(4, callback4, false)) {
|
||||
// Reverse
|
||||
callback3.reset();
|
||||
callback4.reset();
|
||||
doubleSolenoid.set(DoubleSolenoid.Value.kReverse);
|
||||
assertFalse(callback3.wasTriggered());
|
||||
assertNull(callback3.getSetValue());
|
||||
assertTrue(callback4.wasTriggered());
|
||||
assertTrue(callback4.getSetValue());
|
||||
assertFalse(sim.getSolenoidOutput(3));
|
||||
assertTrue(sim.getSolenoidOutput(4));
|
||||
assertEquals(0x10, ph.getSolenoids());
|
||||
|
||||
// Forward
|
||||
callback3.reset();
|
||||
callback4.reset();
|
||||
doubleSolenoid.set(DoubleSolenoid.Value.kForward);
|
||||
assertTrue(callback3.wasTriggered());
|
||||
assertTrue(callback3.getSetValue());
|
||||
assertTrue(callback4.wasTriggered());
|
||||
assertFalse(callback4.getSetValue());
|
||||
assertTrue(sim.getSolenoidOutput(3));
|
||||
assertFalse(sim.getSolenoidOutput(4));
|
||||
assertEquals(0x8, ph.getSolenoids());
|
||||
|
||||
// Off
|
||||
callback3.reset();
|
||||
callback4.reset();
|
||||
doubleSolenoid.set(DoubleSolenoid.Value.kOff);
|
||||
assertTrue(callback3.wasTriggered());
|
||||
assertFalse(callback3.getSetValue());
|
||||
assertFalse(callback4.wasTriggered());
|
||||
assertNull(callback4.getSetValue());
|
||||
assertFalse(sim.getSolenoidOutput(3));
|
||||
assertFalse(sim.getSolenoidOutput(4));
|
||||
assertEquals(0x0, ph.getSolenoids());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void setCompressorOnTest() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
REVPHSim sim = new REVPHSim(1);
|
||||
BooleanCallback callback = new BooleanCallback();
|
||||
|
||||
try (PneumaticHub ph = new PneumaticHub(1);
|
||||
CallbackStore cb = sim.registerCompressorOnCallback(callback, false)) {
|
||||
assertFalse(ph.getCompressor());
|
||||
assertFalse(sim.getCompressorOn());
|
||||
sim.setCompressorOn(true);
|
||||
assertTrue(ph.getCompressor());
|
||||
assertTrue(sim.getCompressorOn());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertTrue(callback.getSetValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void setEnableDigital() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
REVPHSim sim = new REVPHSim(1);
|
||||
EnumCallback callback = new EnumCallback();
|
||||
|
||||
try (PneumaticHub ph = new PneumaticHub(1);
|
||||
CallbackStore cb = sim.registerCompressorConfigTypeCallback(callback, false)) {
|
||||
ph.disableCompressor();
|
||||
assertEquals(ph.getCompressorConfigType(), CompressorConfigType.Disabled);
|
||||
|
||||
ph.enableCompressorDigital();
|
||||
assertEquals(sim.getCompressorConfigType(), CompressorConfigType.Digital.getValue());
|
||||
assertEquals(ph.getCompressorConfigType(), CompressorConfigType.Digital);
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(callback.getSetValue(), CompressorConfigType.Digital.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void setEnableAnalog() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
REVPHSim sim = new REVPHSim(1);
|
||||
EnumCallback callback = new EnumCallback();
|
||||
|
||||
try (PneumaticHub ph = new PneumaticHub(1);
|
||||
CallbackStore cb = sim.registerCompressorConfigTypeCallback(callback, false)) {
|
||||
ph.disableCompressor();
|
||||
assertEquals(ph.getCompressorConfigType(), CompressorConfigType.Disabled);
|
||||
|
||||
ph.enableCompressorAnalog(1, 2);
|
||||
assertEquals(sim.getCompressorConfigType(), CompressorConfigType.Analog.getValue());
|
||||
assertEquals(ph.getCompressorConfigType(), CompressorConfigType.Analog);
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(callback.getSetValue(), CompressorConfigType.Analog.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void setEnableHybrid() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
REVPHSim sim = new REVPHSim(1);
|
||||
EnumCallback callback = new EnumCallback();
|
||||
|
||||
try (PneumaticHub ph = new PneumaticHub(1);
|
||||
CallbackStore cb = sim.registerCompressorConfigTypeCallback(callback, false)) {
|
||||
ph.disableCompressor();
|
||||
assertEquals(ph.getCompressorConfigType(), CompressorConfigType.Disabled);
|
||||
|
||||
ph.enableCompressorHybrid(1, 2);
|
||||
assertEquals(sim.getCompressorConfigType(), CompressorConfigType.Hybrid.getValue());
|
||||
assertEquals(ph.getCompressorConfigType(), CompressorConfigType.Hybrid);
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(callback.getSetValue(), CompressorConfigType.Hybrid.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void setPressureSwitchEnabledTest() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
REVPHSim sim = new REVPHSim(1);
|
||||
BooleanCallback callback = new BooleanCallback();
|
||||
|
||||
try (PneumaticHub ph = new PneumaticHub(1);
|
||||
CallbackStore cb = sim.registerPressureSwitchCallback(callback, false)) {
|
||||
assertFalse(ph.getPressureSwitch());
|
||||
|
||||
sim.setPressureSwitch(true);
|
||||
assertTrue(sim.getPressureSwitch());
|
||||
assertTrue(ph.getPressureSwitch());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertTrue(callback.getSetValue());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void setCompressorCurrentTest() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
REVPHSim sim = new REVPHSim(1);
|
||||
DoubleCallback callback = new DoubleCallback();
|
||||
|
||||
try (PneumaticHub ph = new PneumaticHub(1);
|
||||
CallbackStore cb = sim.registerCompressorCurrentCallback(callback, false)) {
|
||||
assertFalse(ph.getPressureSwitch());
|
||||
|
||||
sim.setCompressorCurrent(35.04);
|
||||
assertEquals(35.04, sim.getCompressorCurrent());
|
||||
assertEquals(35.04, ph.getCompressorCurrent());
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(35.04, callback.getSetValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
165
wpilibj/src/test/java/org/wpilib/simulation/RoboRioSimTest.java
Normal file
165
wpilibj/src/test/java/org/wpilib/simulation/RoboRioSimTest.java
Normal file
@@ -0,0 +1,165 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import edu.wpi.first.wpilibj.RobotController;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.BooleanCallback;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.DoubleCallback;
|
||||
import edu.wpi.first.wpilibj.simulation.testutils.IntCallback;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class RoboRioSimTest {
|
||||
@Test
|
||||
void testSetVin() {
|
||||
RoboRioSim.resetData();
|
||||
|
||||
DoubleCallback voltageCallback = new DoubleCallback();
|
||||
try (CallbackStore voltageCb = RoboRioSim.registerVInVoltageCallback(voltageCallback, false)) {
|
||||
final double kTestVoltage = 1.91;
|
||||
|
||||
RoboRioSim.setVInVoltage(kTestVoltage);
|
||||
assertTrue(voltageCallback.wasTriggered());
|
||||
assertEquals(kTestVoltage, voltageCallback.getSetValue());
|
||||
assertEquals(kTestVoltage, RoboRioSim.getVInVoltage());
|
||||
assertEquals(kTestVoltage, RobotController.getInputVoltage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSetBrownout() {
|
||||
RoboRioSim.resetData();
|
||||
|
||||
DoubleCallback voltageCallback = new DoubleCallback();
|
||||
try (CallbackStore voltageCb =
|
||||
RoboRioSim.registerBrownoutVoltageCallback(voltageCallback, false)) {
|
||||
final double kTestVoltage = 1.91;
|
||||
|
||||
RoboRioSim.setBrownoutVoltage(kTestVoltage);
|
||||
assertTrue(voltageCallback.wasTriggered());
|
||||
assertEquals(kTestVoltage, voltageCallback.getSetValue());
|
||||
assertEquals(kTestVoltage, RoboRioSim.getBrownoutVoltage());
|
||||
assertEquals(kTestVoltage, RobotController.getBrownoutVoltage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void test3V3() {
|
||||
RoboRioSim.resetData();
|
||||
|
||||
DoubleCallback voltageCallback = new DoubleCallback();
|
||||
DoubleCallback currentCallback = new DoubleCallback();
|
||||
BooleanCallback activeCallback = new BooleanCallback();
|
||||
IntCallback faultCallback = new IntCallback();
|
||||
try (CallbackStore voltageCb =
|
||||
RoboRioSim.registerUserVoltage3V3Callback(voltageCallback, false);
|
||||
CallbackStore currentCb =
|
||||
RoboRioSim.registerUserCurrent3V3Callback(currentCallback, false);
|
||||
CallbackStore activeCb = RoboRioSim.registerUserActive3V3Callback(activeCallback, false);
|
||||
CallbackStore faultsCb = RoboRioSim.registerUserFaults3V3Callback(faultCallback, false)) {
|
||||
final double kTestVoltage = 22.9;
|
||||
final double kTestCurrent = 174;
|
||||
final int kTestFaults = 229;
|
||||
|
||||
RoboRioSim.setUserVoltage3V3(kTestVoltage);
|
||||
assertTrue(voltageCallback.wasTriggered());
|
||||
assertEquals(kTestVoltage, voltageCallback.getSetValue());
|
||||
assertEquals(kTestVoltage, RoboRioSim.getUserVoltage3V3());
|
||||
assertEquals(kTestVoltage, RobotController.getVoltage3V3());
|
||||
|
||||
RoboRioSim.setUserCurrent3V3(kTestCurrent);
|
||||
assertTrue(currentCallback.wasTriggered());
|
||||
assertEquals(kTestCurrent, currentCallback.getSetValue());
|
||||
assertEquals(kTestCurrent, RoboRioSim.getUserCurrent3V3());
|
||||
assertEquals(kTestCurrent, RobotController.getCurrent3V3());
|
||||
|
||||
RoboRioSim.setUserActive3V3(false);
|
||||
assertTrue(activeCallback.wasTriggered());
|
||||
assertFalse(activeCallback.getSetValue());
|
||||
assertFalse(RoboRioSim.getUserActive3V3());
|
||||
assertFalse(RobotController.getEnabled3V3());
|
||||
|
||||
RoboRioSim.setUserFaults3V3(kTestFaults);
|
||||
assertTrue(faultCallback.wasTriggered());
|
||||
assertEquals(kTestFaults, faultCallback.getSetValue());
|
||||
assertEquals(kTestFaults, RoboRioSim.getUserFaults3V3());
|
||||
assertEquals(kTestFaults, RobotController.getFaultCount3V3());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCPUTemp() {
|
||||
RoboRioSim.resetData();
|
||||
|
||||
DoubleCallback callback = new DoubleCallback();
|
||||
|
||||
try (CallbackStore cb = RoboRioSim.registerCPUTempCallback(callback, false)) {
|
||||
final double kCPUTemp = 100.0;
|
||||
|
||||
RoboRioSim.setCPUTemp(kCPUTemp);
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(kCPUTemp, callback.getSetValue());
|
||||
assertEquals(kCPUTemp, RoboRioSim.getCPUTemp());
|
||||
assertEquals(kCPUTemp, RobotController.getCPUTemp());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testTeamNumber() {
|
||||
RoboRioSim.resetData();
|
||||
|
||||
IntCallback callback = new IntCallback();
|
||||
|
||||
try (CallbackStore cb = RoboRioSim.registerTeamNumberCallback(callback, false)) {
|
||||
final int kTeamNumber = 9999;
|
||||
|
||||
RoboRioSim.setTeamNumber(kTeamNumber);
|
||||
assertTrue(callback.wasTriggered());
|
||||
assertEquals(kTeamNumber, callback.getSetValue());
|
||||
assertEquals(kTeamNumber, RoboRioSim.getTeamNumber());
|
||||
assertEquals(kTeamNumber, RobotController.getTeamNumber());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSerialNumber() {
|
||||
RoboRioSim.resetData();
|
||||
|
||||
final String kSerialNumber = "Hello";
|
||||
|
||||
RoboRioSim.setSerialNumber(kSerialNumber);
|
||||
assertEquals(kSerialNumber, RoboRioSim.getSerialNumber());
|
||||
assertEquals(kSerialNumber, RobotController.getSerialNumber());
|
||||
|
||||
// Make sure it truncates at 8 characters properly
|
||||
final String kSerialNumberOverflow = "SerialNumber";
|
||||
final String kSerialNumberTruncated = kSerialNumberOverflow.substring(0, 8);
|
||||
RoboRioSim.setSerialNumber(kSerialNumberOverflow);
|
||||
assertEquals(kSerialNumberTruncated, RoboRioSim.getSerialNumber());
|
||||
assertEquals(kSerialNumberTruncated, RobotController.getSerialNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testComments() {
|
||||
RoboRioSim.resetData();
|
||||
|
||||
final String kComments = "Hello! These are comments in the roboRIO web interface!";
|
||||
|
||||
RoboRioSim.setComments(kComments);
|
||||
assertEquals(kComments, RoboRioSim.getComments());
|
||||
assertEquals(kComments, RobotController.getComments());
|
||||
|
||||
final String kCommentsOverflow =
|
||||
"Hello! These are comments in the roboRIO web interface!"
|
||||
+ " This comment exceeds 64 characters!";
|
||||
final String kCommentsTruncated = kCommentsOverflow.substring(0, 64);
|
||||
RoboRioSim.setComments(kCommentsOverflow);
|
||||
assertEquals(kCommentsTruncated, RoboRioSim.getComments());
|
||||
assertEquals(kCommentsTruncated, RobotController.getComments());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import edu.wpi.first.hal.SimBoolean;
|
||||
import edu.wpi.first.hal.SimDevice;
|
||||
import edu.wpi.first.hal.SimDevice.Direction;
|
||||
import edu.wpi.first.hal.SimValue;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SimDeviceSimTest {
|
||||
@Test
|
||||
void testBasic() {
|
||||
try (SimDevice dev = SimDevice.create("test")) {
|
||||
SimBoolean devBool = dev.createBoolean("bool", Direction.kBidir, false);
|
||||
|
||||
SimDeviceSim sim = new SimDeviceSim("test");
|
||||
SimBoolean simBool = sim.getBoolean("bool");
|
||||
|
||||
assertFalse(simBool.get());
|
||||
simBool.set(true);
|
||||
assertTrue(devBool.get());
|
||||
|
||||
assertEquals(dev.getName(), "test");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeviceCreatedCallback() {
|
||||
AtomicInteger callback1Counter = new AtomicInteger(0);
|
||||
AtomicInteger callback2Counter = new AtomicInteger(0);
|
||||
|
||||
try (SimDevice otherdev = SimDevice.create("testnotDC");
|
||||
SimDevice dev1 = SimDevice.create("testDC1")) {
|
||||
try (CallbackStore callback1 =
|
||||
SimDeviceSim.registerDeviceCreatedCallback(
|
||||
"testDC", (name, handle) -> callback1Counter.addAndGet(1), false);
|
||||
CallbackStore callback2 =
|
||||
SimDeviceSim.registerDeviceCreatedCallback(
|
||||
"testDC", (name, handle) -> callback2Counter.addAndGet(1), true)) {
|
||||
assertEquals(0, callback1Counter.get(), "Callback 1 called early");
|
||||
assertEquals(
|
||||
1,
|
||||
callback2Counter.get(),
|
||||
"Callback 2 called early or not initialized with existing devices");
|
||||
|
||||
SimDevice dev2 = SimDevice.create("testDC2");
|
||||
dev2.close();
|
||||
|
||||
assertEquals(
|
||||
1, callback1Counter.get(), "Callback 1 called either more than once or not at all");
|
||||
assertEquals(2, callback2Counter.get(), "Callback 2 called either more or less than twice");
|
||||
}
|
||||
|
||||
SimDevice dev3 = SimDevice.create("testDC3");
|
||||
dev3.close();
|
||||
|
||||
assertEquals(1, callback1Counter.get(), "Callback 1 called after closure");
|
||||
assertEquals(2, callback2Counter.get(), "Callback 2 called after closure");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeviceFreedCallback() {
|
||||
AtomicInteger counter = new AtomicInteger(0);
|
||||
|
||||
SimDevice dev1 = SimDevice.create("testDF1");
|
||||
try (CallbackStore callback =
|
||||
SimDeviceSim.registerDeviceFreedCallback(
|
||||
"testDF", (name, handle) -> counter.addAndGet(1), false)) {
|
||||
assertEquals(0, counter.get(), "Callback called early");
|
||||
dev1.close();
|
||||
assertEquals(1, counter.get(), "Callback called either more than once or not at all");
|
||||
}
|
||||
|
||||
SimDevice dev2 = SimDevice.create("testDF2");
|
||||
dev2.close();
|
||||
|
||||
assertEquals(1, counter.get(), "Callback called after closure");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testValueCreatedCallback() {
|
||||
AtomicInteger callback1Counter = new AtomicInteger(0);
|
||||
AtomicInteger callback2Counter = new AtomicInteger(0);
|
||||
|
||||
try (SimDevice dev1 = SimDevice.create("testVM1")) {
|
||||
dev1.createBoolean("v1", Direction.kBidir, false);
|
||||
SimDeviceSim sim = new SimDeviceSim("testVM1");
|
||||
try (CallbackStore callback1 =
|
||||
sim.registerValueCreatedCallback(
|
||||
(name, handle, readonly, value) -> callback1Counter.addAndGet(1), false);
|
||||
CallbackStore callback2 =
|
||||
sim.registerValueCreatedCallback(
|
||||
(name, handle, readonly, value) -> callback2Counter.addAndGet(1), true)) {
|
||||
assertEquals(0, callback1Counter.get(), "Callback 1 called early");
|
||||
assertEquals(
|
||||
1,
|
||||
callback2Counter.get(),
|
||||
"Callback 2 called early or not initialized with existing devices");
|
||||
|
||||
dev1.createDouble("v2", Direction.kBidir, 0);
|
||||
|
||||
assertEquals(
|
||||
1, callback1Counter.get(), "Callback 1 called either more than once or not at all");
|
||||
assertEquals(2, callback2Counter.get(), "Callback 2 called either more or less than twice");
|
||||
}
|
||||
dev1.createBoolean("v3", Direction.kBidir, false);
|
||||
|
||||
assertEquals(1, callback1Counter.get(), "Callback 1 called after closure");
|
||||
assertEquals(2, callback2Counter.get(), "Callback 2 called after closure");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testValueChangedCallback() {
|
||||
AtomicInteger callback1Counter = new AtomicInteger(0);
|
||||
AtomicInteger callback2Counter = new AtomicInteger(0);
|
||||
|
||||
try (SimDevice dev1 = SimDevice.create("testVM1")) {
|
||||
SimBoolean val = dev1.createBoolean("v1", Direction.kBidir, false);
|
||||
SimDeviceSim sim = new SimDeviceSim("testVM1");
|
||||
SimValue simVal = sim.getValue("v1");
|
||||
try (CallbackStore callback1 =
|
||||
sim.registerValueChangedCallback(
|
||||
simVal, (name, handle, readonly, value) -> callback1Counter.addAndGet(1), false);
|
||||
CallbackStore callback2 =
|
||||
sim.registerValueChangedCallback(
|
||||
simVal, (name, handle, readonly, value) -> callback2Counter.addAndGet(1), true)) {
|
||||
assertEquals(0, callback1Counter.get(), "Callback 1 called early");
|
||||
assertEquals(
|
||||
1,
|
||||
callback2Counter.get(),
|
||||
"Callback 2 called early or not initialized with existing devices");
|
||||
|
||||
val.set(true);
|
||||
|
||||
assertEquals(
|
||||
1, callback1Counter.get(), "Callback 1 called either more than once or not at all");
|
||||
assertEquals(2, callback2Counter.get(), "Callback 2 called either more or less than twice");
|
||||
}
|
||||
val.set(false);
|
||||
assertEquals(1, callback1Counter.get(), "Callback 1 called after closure");
|
||||
assertEquals(2, callback2Counter.get(), "Callback 2 called after closure");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import edu.wpi.first.math.VecBuilder;
|
||||
import edu.wpi.first.math.system.plant.DCMotor;
|
||||
import edu.wpi.first.math.util.Units;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SingleJointedArmSimTest {
|
||||
@Test
|
||||
void testArmDisabled() {
|
||||
SingleJointedArmSim sim =
|
||||
new SingleJointedArmSim(
|
||||
DCMotor.getVex775Pro(2),
|
||||
300,
|
||||
3.0,
|
||||
Units.inchesToMeters(30.0),
|
||||
-Math.PI,
|
||||
0.0,
|
||||
true,
|
||||
Math.PI / 2.0);
|
||||
|
||||
// Reset Arm angle to 0
|
||||
sim.setState(VecBuilder.fill(0.0, 0.0));
|
||||
|
||||
for (int i = 0; i < 12 / 0.02; i++) {
|
||||
sim.setInput(0.0);
|
||||
sim.update(0.020);
|
||||
}
|
||||
|
||||
// the arm should swing down
|
||||
assertEquals(-Math.PI / 2.0, sim.getAngle(), 0.1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testInitialState() {
|
||||
double startingAngleRads = Math.PI / 4.0;
|
||||
SingleJointedArmSim sim =
|
||||
new SingleJointedArmSim(
|
||||
DCMotor.getKrakenX60(2),
|
||||
125,
|
||||
3.0,
|
||||
Units.inchesToMeters(30.0),
|
||||
0,
|
||||
Math.PI / 2.0,
|
||||
true,
|
||||
startingAngleRads);
|
||||
|
||||
assertEquals(startingAngleRads, sim.getAngle());
|
||||
assertEquals(0, sim.getVelocity());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation.testutils;
|
||||
|
||||
import edu.wpi.first.hal.HALValue;
|
||||
|
||||
public class BooleanCallback extends CallbackHelperBase<Boolean> {
|
||||
@Override
|
||||
public void callback(String name, HALValue value) {
|
||||
if (value.getType() != HALValue.kBoolean) {
|
||||
throw new IllegalArgumentException("Wrong callback for type " + value.getType());
|
||||
}
|
||||
|
||||
m_wasTriggered = true;
|
||||
m_setValue = value.getBoolean();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation.testutils;
|
||||
|
||||
import edu.wpi.first.hal.simulation.ConstBufferCallback;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class BufferCallback implements ConstBufferCallback {
|
||||
private boolean m_wasTriggered;
|
||||
private byte[] m_setValue;
|
||||
|
||||
@Override
|
||||
public void callback(String name, byte[] buffer, int count) {
|
||||
m_wasTriggered = true;
|
||||
m_setValue = Arrays.copyOf(buffer, buffer.length);
|
||||
}
|
||||
|
||||
public boolean wasTriggered() {
|
||||
return m_wasTriggered;
|
||||
}
|
||||
|
||||
public byte[] getSetValue() {
|
||||
return Arrays.copyOf(m_setValue, m_setValue.length);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation.testutils;
|
||||
|
||||
import edu.wpi.first.hal.simulation.NotifyCallback;
|
||||
|
||||
public abstract class CallbackHelperBase<T> implements NotifyCallback {
|
||||
protected boolean m_wasTriggered;
|
||||
protected T m_setValue;
|
||||
|
||||
public final boolean wasTriggered() {
|
||||
return m_wasTriggered;
|
||||
}
|
||||
|
||||
public final T getSetValue() {
|
||||
return m_setValue;
|
||||
}
|
||||
|
||||
public final void reset() {
|
||||
m_wasTriggered = false;
|
||||
m_setValue = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation.testutils;
|
||||
|
||||
import edu.wpi.first.hal.HALValue;
|
||||
|
||||
public class DoubleCallback extends CallbackHelperBase<Double> {
|
||||
@Override
|
||||
public void callback(String name, HALValue value) {
|
||||
if (value.getType() != HALValue.kDouble) {
|
||||
throw new IllegalArgumentException("Wrong callback for type " + value.getType());
|
||||
}
|
||||
|
||||
m_wasTriggered = true;
|
||||
m_setValue = value.getDouble();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation.testutils;
|
||||
|
||||
import edu.wpi.first.hal.HALValue;
|
||||
|
||||
public class EnumCallback extends CallbackHelperBase<Long> {
|
||||
@Override
|
||||
public void callback(String name, HALValue value) {
|
||||
if (value.getType() != HALValue.kEnum) {
|
||||
throw new IllegalArgumentException("Wrong callback for type " + value.getType());
|
||||
}
|
||||
|
||||
m_wasTriggered = true;
|
||||
m_setValue = value.getLong();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.wpilibj.simulation.testutils;
|
||||
|
||||
import edu.wpi.first.hal.HALValue;
|
||||
|
||||
public class IntCallback extends CallbackHelperBase<Integer> {
|
||||
@Override
|
||||
public void callback(String name, HALValue value) {
|
||||
if (value.getType() != HALValue.kInt) {
|
||||
throw new IllegalArgumentException("Wrong callback for type " + value.getType());
|
||||
}
|
||||
|
||||
m_wasTriggered = true;
|
||||
m_setValue = (int) value.getLong();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user