mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-04 03:11:43 +00:00
Adds JNI Simulator interface and updated Sim API (#1002)
The simulator was generated by https://github.com/ThadHouse/SimulatorGenerator
This commit is contained in:
committed by
Peter Johnson
parent
1046371349
commit
337e89cf6e
@@ -9,6 +9,7 @@ package edu.wpi.first.wpilibj;
|
||||
|
||||
import edu.wpi.first.wpilibj.hal.AnalogJNI;
|
||||
import edu.wpi.first.wpilibj.hal.FRCNetComm.tResourceType;
|
||||
import edu.wpi.first.wpilibj.sim.AnalogInSim;
|
||||
import edu.wpi.first.wpilibj.hal.HAL;
|
||||
import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder;
|
||||
import edu.wpi.first.wpilibj.util.AllocationException;
|
||||
@@ -350,4 +351,8 @@ public class AnalogInput extends SensorBase implements PIDSource, Sendable {
|
||||
builder.setSmartDashboardType("Analog Input");
|
||||
builder.addDoubleProperty("Value", this::getAverageVoltage, null);
|
||||
}
|
||||
|
||||
public AnalogInSim getSimObject() {
|
||||
return new AnalogInSim(m_channel);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ package edu.wpi.first.wpilibj;
|
||||
|
||||
import edu.wpi.first.wpilibj.hal.AnalogJNI;
|
||||
import edu.wpi.first.wpilibj.hal.FRCNetComm.tResourceType;
|
||||
import edu.wpi.first.wpilibj.sim.AnalogOutSim;
|
||||
import edu.wpi.first.wpilibj.hal.HAL;
|
||||
import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder;
|
||||
|
||||
@@ -66,4 +67,8 @@ public class AnalogOutput extends SendableBase implements Sendable {
|
||||
builder.setSmartDashboardType("Analog Output");
|
||||
builder.addDoubleProperty("Value", this::getVoltage, this::setVoltage);
|
||||
}
|
||||
|
||||
public AnalogOutSim getSimObject() {
|
||||
return new AnalogOutSim(m_channel);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import edu.wpi.first.wpilibj.hal.AccelerometerJNI;
|
||||
import edu.wpi.first.wpilibj.hal.FRCNetComm.tResourceType;
|
||||
import edu.wpi.first.wpilibj.hal.HAL;
|
||||
import edu.wpi.first.wpilibj.interfaces.Accelerometer;
|
||||
import edu.wpi.first.wpilibj.sim.AccelerometerSim;
|
||||
import edu.wpi.first.wpilibj.smartdashboard.SendableBuilder;
|
||||
|
||||
/**
|
||||
@@ -97,4 +98,8 @@ public class BuiltInAccelerometer extends SensorBase implements Accelerometer, S
|
||||
builder.addDoubleProperty("Y", this::getY, null);
|
||||
builder.addDoubleProperty("Z", this::getZ, null);
|
||||
}
|
||||
|
||||
public AccelerometerSim getSimObject() {
|
||||
return new AccelerometerSim();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2018 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.sim;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import edu.wpi.first.wpilibj.AnalogInput;
|
||||
import edu.wpi.first.wpilibj.hal.HAL;
|
||||
|
||||
public class AnalogInputSimTest {
|
||||
static class DoubleStore {
|
||||
public boolean wasTriggered = false;
|
||||
public boolean wasCorrectType = false;
|
||||
public double setValue = 0;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetCallback() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
|
||||
AnalogInput input = new AnalogInput(5);
|
||||
|
||||
AnalogInSim inputSim = input.getSimObject();
|
||||
|
||||
for (double i = 0; i < 5.0; i+=0.1) {
|
||||
inputSim.setVoltage(0);
|
||||
|
||||
assertEquals(input.getVoltage(), 0, 0.001);
|
||||
|
||||
inputSim.setVoltage(i);
|
||||
|
||||
assertEquals(input.getVoltage(), i, 0.001);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2018 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.sim;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import edu.wpi.first.wpilibj.AnalogOutput;
|
||||
import edu.wpi.first.wpilibj.hal.HAL;
|
||||
|
||||
public class AnalogOutputSimTest {
|
||||
static class DoubleStore {
|
||||
public boolean wasTriggered = false;
|
||||
public boolean wasCorrectType = false;
|
||||
public double setValue = -1;
|
||||
|
||||
public void reset() {
|
||||
wasCorrectType = false;
|
||||
wasTriggered = false;
|
||||
setValue = -1;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetCallback() {
|
||||
HAL.initialize(500, 0);
|
||||
|
||||
|
||||
AnalogOutput output = new AnalogOutput(0);
|
||||
output.setVoltage(0.5);
|
||||
|
||||
AnalogOutSim outputSim = output.getSimObject();
|
||||
|
||||
DoubleStore store = new DoubleStore();
|
||||
|
||||
try (CallbackStore cb = outputSim.registerVoltageCallback((s, v) -> {
|
||||
store.wasTriggered = true;
|
||||
store.wasCorrectType = true;
|
||||
store.setValue = v.getDouble();
|
||||
}, false)) {
|
||||
assertFalse(store.wasTriggered);
|
||||
|
||||
for (double i = 0.1; i < 5.0; i+=0.1) {
|
||||
store.reset();
|
||||
|
||||
output.setVoltage(0);
|
||||
|
||||
assertTrue(store.wasTriggered);
|
||||
assertEquals(store.setValue, 0, 0.001);
|
||||
|
||||
store.reset();
|
||||
|
||||
output.setVoltage(i);
|
||||
|
||||
assertTrue(store.wasTriggered);
|
||||
assertEquals(store.setValue, i, 0.001);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user