Remove sendables from SendableRegistry when close() is called (#1917)

This only affected Java (C++ RAII doesn't have the same problem).

Needed to add close/AutoCloseable to several classes to add this behavior.
This commit is contained in:
Peter Johnson
2019-10-05 23:42:53 -07:00
committed by GitHub
parent a9f0e46680
commit 10deba8546
36 changed files with 110 additions and 15 deletions

View File

@@ -113,6 +113,7 @@ public class ADXL345_I2C implements Accelerometer, Sendable, AutoCloseable {
@Override
public void close() {
SendableRegistry.remove(this);
if (m_i2c != null) {
m_i2c.close();
m_i2c = null;

View File

@@ -98,6 +98,7 @@ public class ADXL345_SPI implements Accelerometer, Sendable, AutoCloseable {
@Override
public void close() {
SendableRegistry.remove(this);
if (m_spi != null) {
m_spi.close();
m_spi = null;

View File

@@ -135,6 +135,7 @@ public class ADXL362 implements Accelerometer, Sendable, AutoCloseable {
@Override
public void close() {
SendableRegistry.remove(this);
if (m_spi != null) {
m_spi.close();
m_spi = null;

View File

@@ -175,6 +175,7 @@ public class ADXRS450_Gyro extends GyroBase implements Gyro, PIDSource, Sendable
*/
@Override
public void close() {
SendableRegistry.remove(this);
if (m_spi != null) {
m_spi.close();
m_spi = null;

View File

@@ -71,6 +71,7 @@ public class AnalogAccelerometer implements PIDSource, Sendable, AutoCloseable {
*/
@Override
public void close() {
SendableRegistry.remove(this);
if (m_analogChannel != null && m_allocatedChannel) {
m_analogChannel.close();
}

View File

@@ -121,6 +121,7 @@ public class AnalogGyro extends GyroBase implements Gyro, PIDSource, Sendable, A
*/
@Override
public void close() {
SendableRegistry.remove(this);
if (m_analog != null && m_channelAllocated) {
m_analog.close();
}

View File

@@ -55,6 +55,7 @@ public class AnalogInput implements PIDSource, Sendable, AutoCloseable {
@Override
public void close() {
SendableRegistry.remove(this);
AnalogJNI.freeAnalogInputPort(m_port);
m_port = 0;
m_channel = 0;

View File

@@ -39,6 +39,7 @@ public class AnalogOutput implements Sendable, AutoCloseable {
@Override
public void close() {
SendableRegistry.remove(this);
AnalogJNI.freeAnalogOutputPort(m_port);
m_port = 0;
m_channel = 0;

View File

@@ -157,6 +157,7 @@ public class AnalogPotentiometer implements Potentiometer, Sendable, AutoCloseab
@Override
public void close() {
SendableRegistry.remove(this);
if (m_initAnalogInput) {
m_analogInput.close();
m_analogInput = null;

View File

@@ -78,6 +78,7 @@ public class AnalogTrigger implements Sendable, AutoCloseable {
@Override
public void close() {
SendableRegistry.remove(this);
AnalogJNI.cleanAnalogTrigger(m_port);
m_port = 0;
if (m_ownsAnalog && m_analogInput != null) {

View File

@@ -20,7 +20,7 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
*
* <p>This class allows access to the roboRIO's internal accelerometer.
*/
public class BuiltInAccelerometer implements Accelerometer, Sendable {
public class BuiltInAccelerometer implements Accelerometer, Sendable, AutoCloseable {
/**
* Constructor.
*
@@ -39,6 +39,11 @@ public class BuiltInAccelerometer implements Accelerometer, Sendable {
this(Range.k8G);
}
@Override
public void close() {
SendableRegistry.remove(this);
}
@Override
public void setRange(Range range) {
AccelerometerJNI.setAccelerometerActive(false);

View File

@@ -24,7 +24,7 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
* the safety provided by using the pressure switch and closed loop control. You can only turn off
* closed loop control, thereby stopping the compressor from operating.
*/
public class Compressor implements Sendable {
public class Compressor implements Sendable, AutoCloseable {
private int m_compressorHandle;
private byte m_module;
@@ -53,6 +53,11 @@ public class Compressor implements Sendable {
this(SensorUtil.getDefaultSolenoidModule());
}
@Override
public void close() {
SendableRegistry.remove(this);
}
/**
* Start the compressor running in closed loop control mode.
*

View File

@@ -183,6 +183,8 @@ public class Counter implements CounterBase, PIDSource, Sendable, AutoCloseable
@Override
public void close() {
SendableRegistry.remove(this);
setUpdateWhenEmpty(true);
clearUpSource();

View File

@@ -43,6 +43,7 @@ public class DigitalGlitchFilter implements Sendable, AutoCloseable {
@Override
public void close() {
SendableRegistry.remove(this);
if (m_channelIndex >= 0) {
synchronized (m_mutex) {
m_filterAllocated[m_channelIndex] = false;

View File

@@ -42,6 +42,7 @@ public class DigitalInput extends DigitalSource implements Sendable, AutoCloseab
@Override
public void close() {
super.close();
SendableRegistry.remove(this);
if (m_interrupt != 0) {
cancelInterrupts();
}

View File

@@ -44,6 +44,7 @@ public class DigitalOutput implements Sendable, AutoCloseable {
@Override
public void close() {
SendableRegistry.remove(this);
// Disable the pwm only if we have allocated it
if (m_pwmGenerator != invalidPwmGenerator) {
disablePWM();

View File

@@ -86,6 +86,7 @@ public class DoubleSolenoid extends SolenoidBase implements Sendable, AutoClosea
@Override
public synchronized void close() {
SendableRegistry.remove(this);
SolenoidJNI.freeSolenoidPort(m_forwardHandle);
SolenoidJNI.freeSolenoidPort(m_reverseHandle);
}

View File

@@ -294,6 +294,7 @@ public class Encoder implements CounterBase, PIDSource, Sendable, AutoCloseable
@Override
public void close() {
SendableRegistry.remove(this);
if (m_aSource != null && m_allocatedA) {
m_aSource.close();
m_allocatedA = false;

View File

@@ -50,6 +50,7 @@ public class NidecBrushless extends MotorSafety implements SpeedController, Send
@Override
public void close() {
SendableRegistry.remove(this);
m_dio.close();
m_pwm.close();
}

View File

@@ -28,7 +28,7 @@ import static edu.wpi.first.wpilibj.util.ErrorMessages.requireNonNullParam;
* given set of PID constants.
*/
@SuppressWarnings("PMD.TooManyFields")
public class PIDBase implements PIDInterface, PIDOutput, Sendable {
public class PIDBase implements PIDInterface, PIDOutput, Sendable, AutoCloseable {
public static final double kDefaultPeriod = 0.05;
private static int instances;
@@ -191,6 +191,11 @@ public class PIDBase implements PIDInterface, PIDOutput, Sendable {
this(Kp, Ki, Kd, 0.0, source, output);
}
@Override
public void close() {
SendableRegistry.remove(this);
}
/**
* Read the input, calculate the output accordingly, and write to the output. This should only be
* called by the PIDTask and is created during initialization.

View File

@@ -74,6 +74,7 @@ public class PWM extends MotorSafety implements Sendable, AutoCloseable {
*/
@Override
public void close() {
SendableRegistry.remove(this);
if (m_handle == 0) {
return;
}

View File

@@ -17,7 +17,7 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
* Class for getting voltage, current, temperature, power and energy from the Power Distribution
* Panel over CAN.
*/
public class PowerDistributionPanel implements Sendable {
public class PowerDistributionPanel implements Sendable, AutoCloseable {
private final int m_handle;
/**
@@ -40,6 +40,11 @@ public class PowerDistributionPanel implements Sendable {
this(0);
}
@Override
public void close() {
SendableRegistry.remove(this);
}
/**
* Query the input voltage of the PDP.
*

View File

@@ -141,6 +141,7 @@ public class Relay extends MotorSafety implements Sendable, AutoCloseable {
@Override
public void close() {
SendableRegistry.remove(this);
freeRelay();
}

View File

@@ -37,5 +37,7 @@ public abstract class SendableBase implements Sendable, AutoCloseable {
}
@Override
public void close() {}
public void close() {
SendableRegistry.remove(this);
}
}

View File

@@ -54,6 +54,7 @@ public class Solenoid extends SolenoidBase implements Sendable, AutoCloseable {
@Override
public void close() {
SendableRegistry.remove(this);
SolenoidJNI.freeSolenoidPort(m_solenoidHandle);
m_solenoidHandle = 0;
}

View File

@@ -13,7 +13,7 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
/**
* Allows multiple {@link SpeedController} objects to be linked together.
*/
public class SpeedControllerGroup implements SpeedController, Sendable {
public class SpeedControllerGroup implements SpeedController, Sendable, AutoCloseable {
private boolean m_isInverted;
private final SpeedController[] m_speedControllers;
private static int instances;
@@ -37,6 +37,11 @@ public class SpeedControllerGroup implements SpeedController, Sendable {
SendableRegistry.addLW(this, "tSpeedControllerGroup", instances);
}
@Override
public void close() {
SendableRegistry.remove(this);
}
@Override
public void set(double speed) {
for (SpeedController speedController : m_speedControllers) {

View File

@@ -206,6 +206,7 @@ public class Ultrasonic implements PIDSource, Sendable, AutoCloseable {
*/
@Override
public synchronized void close() {
SendableRegistry.remove(this);
final boolean wasAutomaticMode = m_automaticEnabled;
setAutomaticMode(false);
if (m_allocatedChannels) {

View File

@@ -41,7 +41,7 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
* @see IllegalUseOfCommandException
*/
@SuppressWarnings("PMD.TooManyMethods")
public abstract class Command implements Sendable {
public abstract class Command implements Sendable, AutoCloseable {
/**
* The time since this command was initialized.
*/
@@ -204,6 +204,11 @@ public abstract class Command implements Sendable {
requires(subsystem);
}
@Override
public void close() {
SendableRegistry.remove(this);
}
/**
* Sets the timeout of this command.
*

View File

@@ -32,7 +32,8 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
*
* @see Command
*/
public final class Scheduler implements Sendable {
@SuppressWarnings("PMD.TooManyMethods")
public final class Scheduler implements Sendable, AutoCloseable {
/**
* The Singleton Instance.
*/
@@ -99,6 +100,11 @@ public final class Scheduler implements Sendable {
SendableRegistry.addLW(this, "Scheduler");
}
@Override
public void close() {
SendableRegistry.remove(this);
}
/**
* Adds the command to the {@link Scheduler}. This will not add the {@link Command} immediately,
* but will instead wait for the proper time in the {@link Scheduler#run()} loop before doing so.

View File

@@ -27,7 +27,7 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
*
* @see Command
*/
public abstract class Subsystem implements Sendable {
public abstract class Subsystem implements Sendable, AutoCloseable {
/**
* Whether or not getDefaultCommand() was called.
*/
@@ -64,6 +64,11 @@ public abstract class Subsystem implements Sendable {
m_currentCommandChanged = true;
}
@Override
public void close() {
SendableRegistry.remove(this);
}
/**
* Initialize the default command for a subsystem By default subsystems have no default command,
* but if they do, the default command is set with this method. It is called on all Subsystems by

View File

@@ -18,7 +18,7 @@ import edu.wpi.first.wpiutil.math.MathUtils;
* Implements a PID control loop.
*/
@SuppressWarnings("PMD.TooManyFields")
public class PIDController implements Sendable {
public class PIDController implements Sendable, AutoCloseable {
private static int instances;
// Factor for "proportional" control
@@ -103,6 +103,11 @@ public class PIDController implements Sendable {
HAL.report(tResourceType.kResourceType_PIDController, instances);
}
@Override
public void close() {
SendableRegistry.remove(this);
}
/**
* Sets the PID Controller gain parameters.
*

View File

@@ -96,7 +96,7 @@ import edu.wpi.first.wpiutil.math.MathUtils;
* {@link edu.wpi.first.wpilibj.RobotDrive#drive(double, double)} with the addition of a quick turn
* mode. However, it is not designed to give exactly the same response.
*/
public class DifferentialDrive extends RobotDriveBase implements Sendable {
public class DifferentialDrive extends RobotDriveBase implements Sendable, AutoCloseable {
public static final double kDefaultQuickStopThreshold = 0.2;
public static final double kDefaultQuickStopAlpha = 0.1;
@@ -127,6 +127,11 @@ public class DifferentialDrive extends RobotDriveBase implements Sendable {
SendableRegistry.addLW(this, "DifferentialDrive", instances);
}
@Override
public void close() {
SendableRegistry.remove(this);
}
/**
* Verifies that all motors are nonnull, throwing a NullPointerException if any of them are.
* The exception's error message will specify all null motors, e.g. {@code

View File

@@ -42,7 +42,7 @@ import edu.wpi.first.wpiutil.math.MathUtils;
* points down. Rotations follow the right-hand rule, so clockwise rotation around the Z axis is
* positive.
*/
public class KilloughDrive extends RobotDriveBase implements Sendable {
public class KilloughDrive extends RobotDriveBase implements Sendable, AutoCloseable {
public static final double kDefaultLeftMotorAngle = 60.0;
public static final double kDefaultRightMotorAngle = 120.0;
public static final double kDefaultBackMotorAngle = 270.0;
@@ -109,6 +109,11 @@ public class KilloughDrive extends RobotDriveBase implements Sendable {
SendableRegistry.addLW(this, "KilloughDrive", instances);
}
@Override
public void close() {
SendableRegistry.remove(this);
}
/**
* Verifies that all motors are nonnull, throwing a NullPointerException if any of them are.
* The exception's error message will specify all null motors, e.g. {@code

View File

@@ -61,7 +61,7 @@ import edu.wpi.first.wpiutil.math.MathUtils;
* {@link edu.wpi.first.wpilibj.RobotDrive#mecanumDrive_Polar(double, double, double)} if a
* deadband of 0 is used.
*/
public class MecanumDrive extends RobotDriveBase implements Sendable {
public class MecanumDrive extends RobotDriveBase implements Sendable, AutoCloseable {
private static int instances;
private final SpeedController m_frontLeftMotor;
@@ -92,6 +92,11 @@ public class MecanumDrive extends RobotDriveBase implements Sendable {
SendableRegistry.addLW(this, "MecanumDrive", instances);
}
@Override
public void close() {
SendableRegistry.remove(this);
}
/**
* Verifies that all motors are nonnull, throwing a NullPointerException if any of them are.
* The exception's error message will specify all null motors, e.g. {@code

View File

@@ -18,7 +18,7 @@ import edu.wpi.first.wpilibj.smartdashboard.SendableRegistry;
/**
* A wrapper to make video sources sendable and usable from Shuffleboard.
*/
public final class SendableCameraWrapper implements Sendable {
public final class SendableCameraWrapper implements Sendable, AutoCloseable {
private static final String kProtocol = "camera_server://";
private static Map<VideoSource, SendableCameraWrapper> m_wrappers = new WeakHashMap<>();
@@ -37,6 +37,11 @@ public final class SendableCameraWrapper implements Sendable {
m_uri = kProtocol + name;
}
@Override
public void close() {
SendableRegistry.remove(this);
}
/**
* Gets a sendable wrapper object for the given video source, creating the wrapper if one does
* not already exist for the source.

View File

@@ -32,7 +32,7 @@ import static edu.wpi.first.wpilibj.util.ErrorMessages.requireNonNullParam;
*
* @param <V> The type of the values to be stored
*/
public class SendableChooser<V> implements Sendable {
public class SendableChooser<V> implements Sendable, AutoCloseable {
/**
* The key for the default value.
*/
@@ -69,6 +69,11 @@ public class SendableChooser<V> implements Sendable {
SendableRegistry.add(this, "SendableChooser", m_instance);
}
@Override
public void close() {
SendableRegistry.remove(this);
}
/**
* Adds the given object to the list of options. On the {@link SmartDashboard} on the desktop, the
* object will appear as the given name.