diff --git a/build.gradle b/build.gradle index fde63186..0f95b6e0 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,7 @@ plugins { id "java-library" id "org.carlmontrobotics.webots" version "0.8" - id "edu.wpi.first.GradleRIO" version "2021.1.1-beta-1" + id "edu.wpi.first.GradleRIO" version "2021.1.1-beta-2" } group 'org.carlmontrobotics' diff --git a/src/main/java/frc/robot/lib/sim/SimRegisterer.java b/src/main/java/frc/robot/lib/sim/SimRegisterer.java index 04aba38f..486b893c 100644 --- a/src/main/java/frc/robot/lib/sim/SimRegisterer.java +++ b/src/main/java/frc/robot/lib/sim/SimRegisterer.java @@ -21,7 +21,7 @@ public class SimRegisterer { static { // Register Initalized Callbacks for Misc Devices - SimUtils.registerSimDeviceCreatedCallback("", MISC_DEVICE_CALLBACK, true); + SimDeviceSim.registerDeviceCreatedCallback("", MISC_DEVICE_CALLBACK, true); // Register Initalized Callbacks for PWM Devices registerDeviceType("PWM", SensorUtil.kPwmChannels, PWMSim::new, PWMSim::getInitialized, PWMSim::registerInitializedCallback); } diff --git a/src/main/java/frc/robot/lib/sim/SimUtils.java b/src/main/java/frc/robot/lib/sim/SimUtils.java index 63a661ea..16cdc860 100644 --- a/src/main/java/frc/robot/lib/sim/SimUtils.java +++ b/src/main/java/frc/robot/lib/sim/SimUtils.java @@ -1,16 +1,11 @@ package frc.robot.lib.sim; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; -import java.util.List; -import java.util.function.Consumer; import edu.wpi.first.hal.HALValue; import edu.wpi.first.hal.SimValue; -import edu.wpi.first.hal.simulation.SimDeviceCallback; import edu.wpi.first.hal.simulation.SimValueCallback; -import edu.wpi.first.hal.simulation.SimDeviceDataJNI.SimDeviceInfo; import edu.wpi.first.wpilibj.simulation.SimDeviceSim; /** @@ -18,11 +13,6 @@ * These are mostly (only) to patch flaws in WPILib for which fixes are still being implemented */ public final class SimUtils { - - // Map Device Callbacks to their Prefixes - private static final HashMap prefixes = new HashMap<>(); - // Keep Track of Devices Known to Each Device Callback - private static final HashMap> knownDevices = new HashMap<>(); // Keep Track of Values Watched by Each Value Callback private static final HashMap valueCallbacks = new HashMap<>(); // Cache the Previous State of SimValues for New Callbacks @@ -33,19 +23,6 @@ public final class SimUtils { Simulation.registerPeriodicMethod(SimUtils::periodic); } - // Custom implementations of WPILib methods. These are required due to a bug in WPILib which hinderes their functionality (https:// github.com/wpilibsuite/allwpilib/issues/2832) - // The origional WPILib methods produce a crash in native code when their invoking state is triggered (i.e. When a device or value is created) - // To make migration to the WPILib methods easier, these methods have similar type signatures to their corresponding WPILib methods - // Notable differences: - // The WPILib methods are called asyncronously. These methods work by contnually polling WPILib for changes, so they notify their callbacks during the robot's periodic loop - // The value name and readOnly state cannot be obtained from the value object and the handle is ambiguous as to whether its a device or value handle, so value callbacks are called with "null" versions of these arguments - - // Custom implementation of {@link SimDeviceSim#registerSimDeviceCreatedCallback} - public static void registerSimDeviceCreatedCallback(String prefix, SimDeviceCallback callback, boolean initialNotify) { - prefixes.put(callback, prefix); - knownDevices.put(callback, new ArrayList<>()); - } - // Custom implementation of {@link SimDeviceSim#registerValueChangedCallback} public static void registerValueChangedCallback(SimDeviceSim device, String valueName, SimValueCallback callback, boolean initialNotify) { // Get the Corresponding SimValue @@ -60,28 +37,9 @@ public static void registerValueChangedCallback(SimDeviceSim device, String valu callback.callback("", -1, false, lastData.get(value)); } } - - // Notify callback and mark the device as known to the callback - private static final Consumer performDeviceCallback(SimDeviceCallback callback) { return (info) -> { - callback.callback(info.name, info.handle);knownDevices.get(callback).add(info.name);};} // Notify registered callbacks of new devices public static void periodic() { - // Get existing devices - List devices = Arrays.asList(SimDeviceSim.enumerateDevices("")); - // For each device callback - for(SimDeviceCallback callback : prefixes.keySet()) { - String prefix = prefixes.get(callback); - ArrayList kDevs = knownDevices.get(callback); - // For each device - devices.stream() - // If the callback has not already been not already been notifed of this device - .filter((info) -> !kDevs.contains(info.name)) - // And the device name starts with the prefix - .filter((info) -> info.name.startsWith(prefix)) - // Call the callback - .forEach(performDeviceCallback(callback)); - } ArrayList updatedValues = new ArrayList<>(); // For all cached data lastData.forEach((simValue, value) -> {