From d874fa5c8c16e54aca763ea723b366bf58a11ba3 Mon Sep 17 00:00:00 2001 From: Leek <134633159+Leek865@users.noreply.github.com> Date: Fri, 8 Mar 2024 22:46:45 -0800 Subject: [PATCH] Remove device object cache so there aren't CAN id conflicts --- .../java/com/team766/hal/RobotProvider.java | 11 --- .../team766/hal/mock/TestRobotProvider.java | 50 +++-------- .../simulator/SimulationRobotProvider.java | 50 +++-------- .../team766/hal/wpilib/WPIRobotProvider.java | 87 +++++++------------ 4 files changed, 50 insertions(+), 148 deletions(-) diff --git a/src/main/java/com/team766/hal/RobotProvider.java b/src/main/java/com/team766/hal/RobotProvider.java index e7477fee2..a18705ec2 100755 --- a/src/main/java/com/team766/hal/RobotProvider.java +++ b/src/main/java/com/team766/hal/RobotProvider.java @@ -21,17 +21,6 @@ public abstract class RobotProvider { public static RobotProvider instance; - protected EncoderReader[] encoders = new EncoderReader[20]; - protected SolenoidController[] solenoids = new SolenoidController[20]; - protected GyroReader[] gyros = new GyroReader[13]; - protected HashMap cams = new HashMap(); - protected JoystickReader[] joysticks = new JoystickReader[8]; - protected DigitalInputReader[] digInputs = new DigitalInputReader[8]; - protected AnalogInputReader[] angInputs = new AnalogInputReader[5]; - protected RelayOutput[] relays = new RelayOutput[5]; - protected PositionReader positionSensor = null; - protected BeaconReader beaconSensor = null; - private HashMap motorDeviceIdNames = new HashMap(); private HashMap motorPortNames = new HashMap(); private HashMap digitalIoNames = new HashMap(); diff --git a/src/main/java/com/team766/hal/mock/TestRobotProvider.java b/src/main/java/com/team766/hal/mock/TestRobotProvider.java index 1bac8f38d..84fc2cedf 100755 --- a/src/main/java/com/team766/hal/mock/TestRobotProvider.java +++ b/src/main/java/com/team766/hal/mock/TestRobotProvider.java @@ -42,10 +42,7 @@ public MotorController getMotor( @Override public EncoderReader getEncoder(final int index1, final int index2) { - if (encoders[index1] == null) { - encoders[index1] = new MockEncoder(); - } - return encoders[index1]; + return new MockEncoder(); } @Override @@ -55,42 +52,27 @@ public EncoderReader getEncoder(final int index1, String configPrefix) { @Override public SolenoidController getSolenoid(final int index) { - if (solenoids[index] == null) { - solenoids[index] = new MockSolenoid(index); - } - return solenoids[index]; + return new MockSolenoid(index); } @Override public GyroReader getGyro(final int index, String configPrefix) { - if (gyros[0] == null) { - gyros[0] = new MockGyro(); - } - return gyros[0]; + return new MockGyro(); } @Override public CameraReader getCamera(final String id, final String value) { - if (!cams.containsKey(id)) { - cams.put(id, new MockCamera()); - } - return cams.get(id); + return new MockCamera(); } @Override public JoystickReader getJoystick(final int index) { - if (joysticks[index] == null) { - joysticks[index] = new MockJoystick(); - } - return joysticks[index]; + return new MockJoystick(); } @Override public DigitalInputReader getDigitalInput(final int index) { - if (digInputs[index] == null) { - digInputs[index] = new MockDigitalInput(); - } - return digInputs[index]; + return new MockDigitalInput(); } @Override @@ -100,33 +82,21 @@ public CameraInterface getCamServer() { @Override public AnalogInputReader getAnalogInput(final int index) { - if (angInputs[index] == null) { - angInputs[index] = new MockAnalogInput(); - } - return angInputs[index]; + return new MockAnalogInput(); } public RelayOutput getRelay(final int index) { - if (relays[index] == null) { - relays[index] = new MockRelay(index); - } - return relays[index]; + return new MockRelay(index); } @Override public PositionReader getPositionSensor() { - if (positionSensor == null) { - positionSensor = new MockPositionSensor(); - } - return positionSensor; + return new MockPositionSensor(); } @Override public BeaconReader getBeaconSensor() { - if (beaconSensor == null) { - beaconSensor = new MockBeaconSensor(); - } - return beaconSensor; + return new MockBeaconSensor(); } @Override diff --git a/src/main/java/com/team766/hal/simulator/SimulationRobotProvider.java b/src/main/java/com/team766/hal/simulator/SimulationRobotProvider.java index faa9a9669..ce2c4d848 100755 --- a/src/main/java/com/team766/hal/simulator/SimulationRobotProvider.java +++ b/src/main/java/com/team766/hal/simulator/SimulationRobotProvider.java @@ -43,42 +43,27 @@ public MotorController getMotor( @Override public EncoderReader getEncoder(final int index1, final int index2) { - if (encoders[index1] == null) { - encoders[index1] = new Encoder(index1); - } - return encoders[index1]; + return new Encoder(index1); } @Override public EncoderReader getEncoder(final int index1, String configPrefix) { - if (encoders[index1] == null) { - encoders[index1] = new Encoder(index1); - } - return encoders[index1]; + return new Encoder(index1); } @Override public SolenoidController getSolenoid(final int index) { - if (solenoids[index] == null) { - solenoids[index] = new Solenoid(index); - } - return solenoids[index]; + return new Solenoid(index); } @Override public GyroReader getGyro(final int index, String configPrefix) { - if (gyros[0] == null) { - gyros[0] = new Gyro(); - } - return gyros[0]; + return new Gyro(); } @Override public CameraReader getCamera(final String id, final String value) { - if (!cams.containsKey(id)) { - cams.put(id, new Camera()); - } - return cams.get(id); + return new Camera(); } @Override @@ -88,10 +73,7 @@ public JoystickReader getJoystick(final int index) { @Override public DigitalInputReader getDigitalInput(final int index) { - if (digInputs[index] == null) { - digInputs[index] = new DigitalInput(index); - } - return digInputs[index]; + return new DigitalInput(index); } @Override @@ -101,33 +83,21 @@ public CameraInterface getCamServer() { @Override public AnalogInputReader getAnalogInput(final int index) { - if (angInputs[index] == null) { - angInputs[index] = new AnalogInput(index); - } - return angInputs[index]; + return new AnalogInput(index); } public RelayOutput getRelay(final int index) { - if (relays[index] == null) { - relays[index] = new Relay(index); - } - return relays[index]; + return new Relay(index); } @Override public PositionReader getPositionSensor() { - if (positionSensor == null) { - positionSensor = new PositionSensor(); - } - return positionSensor; + return new PositionSensor(); } @Override public BeaconReader getBeaconSensor() { - if (beaconSensor == null) { - beaconSensor = new BeaconSensor(); - } - return beaconSensor; + return new BeaconSensor(); } @Override diff --git a/src/main/java/com/team766/hal/wpilib/WPIRobotProvider.java b/src/main/java/com/team766/hal/wpilib/WPIRobotProvider.java index 595f851f1..c72164ffc 100755 --- a/src/main/java/com/team766/hal/wpilib/WPIRobotProvider.java +++ b/src/main/java/com/team766/hal/wpilib/WPIRobotProvider.java @@ -172,10 +172,7 @@ public MotorController getMotor( @Override public EncoderReader getEncoder(int index1, int index2) { - if (encoders[index1] == null) { - encoders[index1] = new Encoder(index1, index2); - } - return encoders[index1]; + return new Encoder(index1, index2); } @Override @@ -210,10 +207,7 @@ public EncoderReader getEncoder(int index1, String configPrefix) { @Override public SolenoidController getSolenoid(int index) { - if (solenoids[index] == null) { - solenoids[index] = new Solenoid(index); - } - return solenoids[index]; + return new Solenoid(index); } @Override @@ -234,24 +228,21 @@ public GyroReader getGyro(final int index, String configPrefix) { } } - if (gyros[index + 2] == null) { - if (index < -2) { - Logger.get(Category.CONFIGURATION) - .logRaw( - Severity.ERROR, - "Invalid gyro port " - + index - + ". Must be -2, -1, or a non-negative integer"); - return new MockGyro(); - } else if (index == -2) { - gyros[index + 2] = new NavXGyro(I2C.Port.kOnboard); - } else if (index == -1) { - gyros[index + 2] = new ADXRS450_Gyro(SPI.Port.kOnboardCS0); - } else { - gyros[index + 2] = new AnalogGyro(index); - } + if (index < -2) { + Logger.get(Category.CONFIGURATION) + .logRaw( + Severity.ERROR, + "Invalid gyro port " + + index + + ". Must be -2, -1, or a non-negative integer"); + return new MockGyro(); + } else if (index == -2) { + return new NavXGyro(I2C.Port.kOnboard); + } else if (index == -1) { + return new ADXRS450_Gyro(SPI.Port.kOnboardCS0); + } else { + return new AnalogGyro(index); } - return gyros[index + 2]; } @Override @@ -262,10 +253,7 @@ public CameraReader getCamera(final String id, final String value) { @Override public JoystickReader getJoystick(int index) { - if (joysticks[index] == null) { - joysticks[index] = new Joystick(index); - } - return joysticks[index]; + return new Joystick(index); } @Override @@ -275,50 +263,35 @@ public CameraInterface getCamServer() { @Override public DigitalInputReader getDigitalInput(final int index) { - if (digInputs[index] == null) { - digInputs[index] = new DigitalInput(index); - } - return digInputs[index]; + return new DigitalInput(index); } @Override public AnalogInputReader getAnalogInput(int index) { - if (angInputs[index] == null) { - angInputs[index] = new AnalogInput(index); - } - return angInputs[index]; + return new AnalogInput(index); } @Override public RelayOutput getRelay(int index) { - if (relays[index] == null) { - relays[index] = new Relay(index); - } - return relays[index]; + return new Relay(index); } @Override public PositionReader getPositionSensor() { - if (positionSensor == null) { - positionSensor = new MockPositionSensor(); - Logger.get(Category.CONFIGURATION) - .logRaw( - Severity.ERROR, - "Position sensor does not exist on real robots. Using mock position sensor instead - it will always return a position of 0"); - } - return positionSensor; + Logger.get(Category.CONFIGURATION) + .logRaw( + Severity.ERROR, + "Position sensor does not exist on real robots. Using mock position sensor instead - it will always return a position of 0"); + return new MockPositionSensor(); } @Override public BeaconReader getBeaconSensor() { - if (beaconSensor == null) { - beaconSensor = new MockBeaconSensor(); - Logger.get(Category.CONFIGURATION) - .logRaw( - Severity.ERROR, - "Beacon sensor does not exist on real robots. Using mock beacon sensor instead - it will always return no beacons"); - } - return beaconSensor; + Logger.get(Category.CONFIGURATION) + .logRaw( + Severity.ERROR, + "Beacon sensor does not exist on real robots. Using mock beacon sensor instead - it will always return no beacons"); + return new MockBeaconSensor(); } @Override