From 9798702f494823ce1c5fa1272427640442329974 Mon Sep 17 00:00:00 2001 From: Veselin Dikov Date: Sun, 26 Oct 2025 20:10:46 -0700 Subject: [PATCH 1/6] Reorganize MotorSubsystem; clearly label overrides of Motor, Encoder, and Subsystem; use 'm_' prefix for MotorSubsystem members --- .../lib2813/subsystems/MotorSubsystem.java | 446 +++++++++--------- 1 file changed, 230 insertions(+), 216 deletions(-) diff --git a/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java b/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java index c33a905f..fcab9387 100644 --- a/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java +++ b/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java @@ -31,222 +31,18 @@ public abstract class MotorSubsystem> extends SubsystemBase implements Motor, Encoder { - protected final Motor motor; - protected final Encoder encoder; - protected final ControlMode controlMode; - protected final AngleUnit rotationUnit; - protected final double acceptableError; - protected final PIDController controller; - private final DoublePublisher positionPublisher; - private final BooleanPublisher atPositionPublisher; - private final DoublePublisher appliedCurrentPublisher; - private final DoublePublisher setpointPublisher; - - private boolean isEnabled; - - protected MotorSubsystem(MotorSubsystemConfiguration builder) { - this.controller = builder.controller; - this.controller.setTolerance(builder.acceptableError); - acceptableError = builder.acceptableError; - motor = builder.motor; - encoder = builder.encoder; - controlMode = builder.controlMode; - rotationUnit = builder.rotationUnit; - if (builder.ntInstance != null) { - NetworkTable networkTable = builder.ntInstance.getTable(getName()); - positionPublisher = networkTable.getDoubleTopic("position").publish(); - atPositionPublisher = networkTable.getBooleanTopic("at position").publish(); - appliedCurrentPublisher = networkTable.getDoubleTopic("applied current").publish(); - setpointPublisher = networkTable.getDoubleTopic("setpoint").publish(); - } else { - positionPublisher = null; - atPositionPublisher = null; - appliedCurrentPublisher = null; - setpointPublisher = null; - } - - this.controller.setSetpoint(builder.startingPosition); - } - - /** - * Sets the desired setpoint to the provided value, and enables the PID control. - * - * @param position the position to go to. - */ - public final void setSetpoint(T position) { - if (!isEnabled()) { - enable(); - } - double setpoint = position.get().in(rotationUnit); - controller.setSetpoint(setpoint); - } - - /** - * Returns a command that sets the desired setpoint to the provided value. - * - * @param setpoint the position to go to. - */ - public final Command setSetpointCommand(T setpoint) { - return new InstantCommand(() -> this.setSetpoint(setpoint), this); - } - - /** Returns the current setpoint as an angle. */ - public final Angle getSetpoint() { - return rotationUnit.of(controller.getSetpoint()); - } - - /** Determines if the motor is at the current setpoint, within the acceptable error. */ - public final boolean atPosition() { - return Math.abs(getMeasurement() - controller.getSetpoint()) <= acceptableError; - } - - /** - * {@inheritDoc} - * - *

Additionally, this method disables PID control of the subsystem - */ - @Override - public final void set(ControlMode mode, double demand, double feedForward) { - if (isEnabled()) { - disable(); - } - motor.set(mode, demand, feedForward); - } - - @Override - public final Current getAppliedCurrent() { - return motor.getAppliedCurrent(); - } - - /** - * Enables the motor - * - *

The motor voltage will be periodically updated to move the motor towards the current - * setupoint. - */ - public final void enable() { - isEnabled = true; - } - - /** - * Stops the motor. - * - *

The motor voltage will be set to zero, and the motor will not adjust to move towards the - * current setpoint. - */ - public final void disable() { - isEnabled = false; - motor.disable(); - } - - /** - * Returns whether the controller is enabled. If this is enabled, then PID control will be used. - * - * @return Whether the controller is enabled. - */ - public final boolean isEnabled() { - return isEnabled; - } - - /** - * {@inheritDoc} - * - *

Additionally, this method disables PID control of the subsystem. It does not clamp - * the provided value. - */ - @Override - public final void set(ControlMode mode, double demand) { - isEnabled = false; - motor.set(mode, demand); - } - - /** - * Clamps the given output value and provides it to the motor. - * - *

This was protected and non-final to allow subclasses to clamp the output. Subclasses should - * override {@link #clampOutput(double)}. - * - * @param output The output calculated by the PID algorithm. - * @param setpoint Ignored. - * @deprecated Subclasses should override {@link #clampOutput(double)}. - */ - @Deprecated - protected void useOutput(double output, double setpoint) { - motor.set(controlMode, clampOutput(output)); - } - - /** - * Clamps the given output value and provides it to the motor. - * - *

This is called by {@link #periodic()} if this subsystem is enabled. - */ - private void useOutput(double output) { - useOutput(output, controller.getSetpoint()); - } - - /** - * Extension point that allows subclasses to clamp the output. - * - *

The default implementation returns the provided value. - * - * @param output Output provided by the PID controller. - * @return Output to provide to the motor. - * @see edu.wpi.first.math.MathUtil#clamp(double, double, double) - */ - protected double clampOutput(double output) { - return output; - } - - protected final double getMeasurement() { - return encoder.getPositionMeasure().in(rotationUnit); - } - - @Override - @Deprecated(forRemoval = true) - public double position() { - return encoder.position(); - } - - @Override - public final Angle getPositionMeasure() { - return encoder.getPositionMeasure(); - } - - @Override - @Deprecated(forRemoval = true) - public void setPosition(double position) { - encoder.setPosition(position); - } - - @Override - public final void setPosition(Angle position) { - encoder.setPosition(position); - } - - @Override - @Deprecated(forRemoval = true) - public double getVelocity() { - return encoder.getVelocity(); - } - - @Override - public final AngularVelocity getVelocityMeasure() { - return encoder.getVelocityMeasure(); - } - - /** Applies the PID output to the motor if this subsystem is enabled. */ - @Override - public void periodic() { - if (isEnabled) { - useOutput(controller.calculate(getMeasurement())); - } - if (positionPublisher != null) { - positionPublisher.set(getPositionMeasure().in(Rotations)); - setpointPublisher.set(getSetpoint().in(Rotations)); - atPositionPublisher.set(atPosition()); - appliedCurrentPublisher.set(getAppliedCurrent().in(Amps)); - } - } + protected final Motor m_motor; + protected final Encoder m_encoder; + protected final ControlMode m_controlMode; + protected final AngleUnit m_rotationUnit; + protected final double m_acceptableError; + protected final PIDController m_pidController; + private final DoublePublisher m_positionPublisher; + private final BooleanPublisher m_atPositionPublisher; + private final DoublePublisher m_appliedCurrentPublisher; + private final DoublePublisher m_setpointPublisher; + + private boolean m_isEnabled; /** A configuration for a MotorSubsystem */ public static class MotorSubsystemConfiguration { @@ -256,8 +52,12 @@ public static class MotorSubsystemConfiguration { /** The default starting position if one is not provided. */ public static final double DEFAULT_STARTING_POSITION = 0.0; + /** Control mode for the MotorSubsystem motor. */ private ControlMode controlMode; + + /** */ private AngleUnit rotationUnit; + private final Motor motor; private final Encoder encoder; private PIDController controller; @@ -384,4 +184,218 @@ public MotorSubsystemConfiguration publishTo(NetworkTableInstance ntInstance) { return this; } } + + protected MotorSubsystem(MotorSubsystemConfiguration builder) { + m_pidController = builder.controller; + m_pidController.setTolerance(builder.acceptableError); + m_acceptableError = builder.acceptableError; + m_motor = builder.motor; + m_encoder = builder.encoder; + m_controlMode = builder.controlMode; + m_rotationUnit = builder.rotationUnit; + if (builder.ntInstance != null) { + NetworkTable networkTable = builder.ntInstance.getTable(getName()); + m_positionPublisher = networkTable.getDoubleTopic("position").publish(); + m_atPositionPublisher = networkTable.getBooleanTopic("at position").publish(); + m_appliedCurrentPublisher = networkTable.getDoubleTopic("applied current").publish(); + m_setpointPublisher = networkTable.getDoubleTopic("setpoint").publish(); + } else { + m_positionPublisher = null; + m_atPositionPublisher = null; + m_appliedCurrentPublisher = null; + m_setpointPublisher = null; + } + + m_pidController.setSetpoint(builder.startingPosition); + } + + /** + * Sets the desired setpoint to the provided value, and enables the PID control. + * + * @param position the position to go to. + */ + public final void setSetpoint(T position) { + if (!m_isEnabled) { + enable(); + } + double setpoint = position.get().in(m_rotationUnit); + m_pidController.setSetpoint(setpoint); + } + + /** + * Returns a command that sets the desired setpoint to the provided value. + * + * @param setpoint the position to go to. + */ + public final Command setSetpointCommand(T setpoint) { + return new InstantCommand(() -> this.setSetpoint(setpoint), this); + } + + /** Returns the current setpoint as an angle. */ + public final Angle getSetpoint() { + return m_rotationUnit.of(m_pidController.getSetpoint()); + } + + /** Determines if the motor is at the current setpoint, within the acceptable error. */ + public final boolean atPosition() { + return Math.abs(getMeasurement() - m_pidController.getSetpoint()) <= m_acceptableError; + } + + /** + * Enables the motor + * + *

The motor voltage will be periodically updated to move the motor towards the current + * setupoint. + */ + public final void enable() { + m_isEnabled = true; + } + + /** + * Stops the motor. + * + *

The motor voltage will be set to zero, and the motor will not adjust to move towards the + * current setpoint. + */ + public final void disable() { + m_isEnabled = false; + m_motor.set(m_controlMode, 0); + } + + /** + * Returns whether the controller is enabled. If this is enabled, then PID control will be used. + * + * @return Whether the controller is enabled. + */ + public final boolean isEnabled() { + return m_isEnabled; + } + + /** + * Clamps the given output value and provides it to the motor. + * + *

This was protected and non-final to allow subclasses to clamp the output. Subclasses should + * override {@link #clampOutput(double)}. + * + * @param output The output calculated by the PID algorithm. + * @param setpoint Ignored. + * @deprecated Subclasses should override {@link #clampOutput(double)}. + */ + @Deprecated + protected void useOutput(double output, double setpoint) { + m_motor.set(m_controlMode, clampOutput(output)); + } + + /** + * Clamps the given output value and provides it to the motor. + * + *

This is called by {@link #periodic()} if this subsystem is enabled. + */ + private void useOutput(double output) { + useOutput(output, m_pidController.getSetpoint()); + } + + /** + * Extension point that allows subclasses to clamp the output. + * + *

The default implementation returns the provided value. + * + * @param output Output provided by the PID controller. + * @return Output to provide to the motor. + * @see edu.wpi.first.math.MathUtil#clamp(double, double, double) + */ + protected double clampOutput(double output) { + return output; + } + + protected final double getMeasurement() { + return m_encoder.getPositionMeasure().in(m_rotationUnit); + } + + // `Motor` method overrides. + // ------------------------- + + /** + * {@inheritDoc} + * + *

Additionally, this method disables PID control of the subsystem + */ + @Override + public final void set(ControlMode mode, double demand, double feedForward) { + if (m_isEnabled) { + disable(); + } + m_motor.set(mode, demand, feedForward); + } + + /** + * {@inheritDoc} + * + *

Additionally, this method disables PID control of the subsystem. It does not clamp + * the provided value. + */ + @Override + public final void set(ControlMode mode, double demand) { + m_isEnabled = false; + m_motor.set(mode, demand); + } + + /** {@inheritDoc} */ + @Override + public final Current getAppliedCurrent() { + return m_motor.getAppliedCurrent(); + } + + // `Encoder` method overrides. + // --------------------------- + + @Override + @Deprecated(forRemoval = true) + public double position() { + return m_encoder.position(); + } + + @Override + public final Angle getPositionMeasure() { + return m_encoder.getPositionMeasure(); + } + + @Override + @Deprecated(forRemoval = true) + public void setPosition(double position) { + m_encoder.setPosition(position); + } + + @Override + public final void setPosition(Angle position) { + m_encoder.setPosition(position); + } + + @Override + @Deprecated(forRemoval = true) + public double getVelocity() { + return m_encoder.getVelocity(); + } + + @Override + public final AngularVelocity getVelocityMeasure() { + return m_encoder.getVelocityMeasure(); + } + + // `Subsystem` method overrides. + // ----------------------------- + + /** Applies the PID output to the motor if this subsystem is enabled. */ + @Override + public void periodic() { + if (m_isEnabled) { + useOutput(m_pidController.calculate(getMeasurement())); + } + if (m_positionPublisher != null) { + m_positionPublisher.set(getPositionMeasure().in(Rotations)); + m_setpointPublisher.set(getSetpoint().in(Rotations)); + m_atPositionPublisher.set(atPosition()); + m_appliedCurrentPublisher.set(getAppliedCurrent().in(Amps)); + } + } } From c35132c5837eda3605d722733f31682129f89268 Mon Sep 17 00:00:00 2001 From: Veselin Dikov Date: Sun, 26 Oct 2025 21:03:07 -0700 Subject: [PATCH 2/6] Extend javadoc in MotorSubsystem.java --- .../lib2813/subsystems/MotorSubsystem.java | 67 +++++++++++++++---- 1 file changed, 53 insertions(+), 14 deletions(-) diff --git a/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java b/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java index fcab9387..739498ff 100644 --- a/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java +++ b/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java @@ -24,7 +24,27 @@ import java.util.function.Supplier; /** - * Defines PID control over a motor, with values specified by an encoder + * Defines a generic subsystem comprising a motor and encoder. + * + *

The MotorSybsystem supports a dual operation mode: + * + *

+ * + *

The PID Mode is enabled by calling {@link setSetpoint(T)}. The subsystem starts moving + * toward the setpoint and maintains position at the setpoint under the control of the PID + * controller. The motor system's {@link isEnabled()} returns {@code true}. + * + *

The Direct User Input Mode is activated when the user calls the {@link + * set(ControlType,double,double)} or {@link set(ControlType,double)} method, where the user + * provides direct input of type ControlType (specified via {@link + * MotorSubsystemConfiguration#controlMode(ControlType)}). The PID Mode is interrupted and + * disengaged, and {@link isEnabled()} returns {@code false}. It can be re-engaged with the {@link + * enable()} method and will resume movement toward setpoint. * * @param the type of the {@link Supplier} used to specify setpoints. */ @@ -52,12 +72,9 @@ public static class MotorSubsystemConfiguration { /** The default starting position if one is not provided. */ public static final double DEFAULT_STARTING_POSITION = 0.0; - /** Control mode for the MotorSubsystem motor. */ + // See setter methods for descriptions of these member variables. private ControlMode controlMode; - - /** */ private AngleUnit rotationUnit; - private final Motor motor; private final Encoder encoder; private PIDController controller; @@ -95,7 +112,7 @@ public MotorSubsystemConfiguration(PIDMotor motor) { } /** - * Sets the controller used to calculate the next value + * Sets the controller that drives the motor to a user-specified setpoint. * * @param controller The PID controller * @return {@code this} for chaining @@ -126,7 +143,7 @@ public MotorSubsystemConfiguration controlMode(ControlMode controlMode) { } /** - * Sets the PID constants for the controller + * Sets the PID constants for the controller. * * @param p the proportional * @param i the integral @@ -139,7 +156,7 @@ public MotorSubsystemConfiguration PID(double p, double i, double d) { } /** - * Sets the initial setpoint of the controller + * Sets the initial setpoint of the controller. * * @param startingPosition the initial setpoint * @return {@code this} for chaining @@ -162,14 +179,19 @@ public MotorSubsystemConfiguration startingPosition(Supplier startingPosi return startingPosition(startingPositionSupplier.get()); } - /** Sets the acceptable position error. */ + /** + * Sets the acceptable position error. + * + *

This is the acceptable delta between the user-specified setpoint and the actual subsystem + * position where the system is reporting to be "in position" (see {@link atPosition}). + */ public MotorSubsystemConfiguration acceptableError(double error) { this.acceptableError = error; return this; } /** - * Sets the unit to use for PID calculations + * Sets the unit to use for PID calculations. * * @param rotationUnit The angle unit to use for calculations */ @@ -179,6 +201,7 @@ public MotorSubsystemConfiguration rotationUnit(AngleUnit rotationUnit) { return this; } + /** Sets the network table instance to report subsystem internal state to. */ public MotorSubsystemConfiguration publishTo(NetworkTableInstance ntInstance) { this.ntInstance = ntInstance; return this; @@ -242,10 +265,13 @@ public final boolean atPosition() { } /** - * Enables the motor + * Engages the PID Controller. * *

The motor voltage will be periodically updated to move the motor towards the current * setupoint. + * + *

If this method is called after the motor drive toward setpoint was interrupted by user + * interruption, the motor will resume its movement towards the last set setpoint. */ public final void enable() { m_isEnabled = true; @@ -263,9 +289,13 @@ public final void disable() { } /** - * Returns whether the controller is enabled. If this is enabled, then PID control will be used. + * Returns whether the PID controller is engaged. + * + *

When the PID controller is engaged, the motor system is moving toward the user-specified + * setpoint position (set via {@link setSetpoint(T)}), or, if it has already reached the setpoint, + * is maintaining that position. * - * @return Whether the controller is enabled. + * @return Whether the PID controller is engaged. */ public final boolean isEnabled() { return m_isEnabled; @@ -308,6 +338,9 @@ protected double clampOutput(double output) { return output; } + // Returns the current position of the motor subsystem in the units specified by m_rotationUnit. + // This position is used by the PID controller (when it is enabled) to determine if it is already + // at position or needs to adjust the subsystem position to reach a user-specified setpoint. protected final double getMeasurement() { return m_encoder.getPositionMeasure().in(m_rotationUnit); } @@ -318,7 +351,7 @@ protected final double getMeasurement() { /** * {@inheritDoc} * - *

Additionally, this method disables PID control of the subsystem + *

Additionally, this method disengages PID control of the subsystem. */ @Override public final void set(ControlMode mode, double demand, double feedForward) { @@ -349,34 +382,40 @@ public final Current getAppliedCurrent() { // `Encoder` method overrides. // --------------------------- + /** {@inheritDoc} */ @Override @Deprecated(forRemoval = true) public double position() { return m_encoder.position(); } + /** {@inheritDoc} */ @Override public final Angle getPositionMeasure() { return m_encoder.getPositionMeasure(); } + /** {@inheritDoc} */ @Override @Deprecated(forRemoval = true) public void setPosition(double position) { m_encoder.setPosition(position); } + /** {@inheritDoc} */ @Override public final void setPosition(Angle position) { m_encoder.setPosition(position); } + /** {@inheritDoc} */ @Override @Deprecated(forRemoval = true) public double getVelocity() { return m_encoder.getVelocity(); } + /** {@inheritDoc} */ @Override public final AngularVelocity getVelocityMeasure() { return m_encoder.getVelocityMeasure(); From bfda0bafa27c067a63bafbbd1f75f8814d7c0d49 Mon Sep 17 00:00:00 2001 From: Veselin Dikov Date: Sun, 26 Oct 2025 22:00:32 -0700 Subject: [PATCH 3/6] Revert renaming of MotorSubsystem members, remove 'm_' prefixes; subclasses inheriting from MotorSubsystem directly access these fields and that PR started creating breakages --- .../lib2813/subsystems/MotorSubsystem.java | 101 +++++++++--------- 1 file changed, 51 insertions(+), 50 deletions(-) diff --git a/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java b/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java index 739498ff..85be7e57 100644 --- a/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java +++ b/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java @@ -51,18 +51,19 @@ public abstract class MotorSubsystem> extends SubsystemBase implements Motor, Encoder { - protected final Motor m_motor; - protected final Encoder m_encoder; - protected final ControlMode m_controlMode; - protected final AngleUnit m_rotationUnit; - protected final double m_acceptableError; - protected final PIDController m_pidController; - private final DoublePublisher m_positionPublisher; - private final BooleanPublisher m_atPositionPublisher; - private final DoublePublisher m_appliedCurrentPublisher; - private final DoublePublisher m_setpointPublisher; - + protected final Motor motor; + protected final Encoder encoder; + // Specifies if the mechanism is currently under the control of the PID controller. private boolean m_isEnabled; + protected final ControlMode controlMode; + protected final AngleUnit rotationUnit; + protected final double acceptableError; + protected final PIDController pidController; + private final DoublePublisher positionPublisher; + private final BooleanPublisher atPositionPublisher; + private final DoublePublisher appliedCurrentPublisher; + private final DoublePublisher setpointPublisher; + /** A configuration for a MotorSubsystem */ public static class MotorSubsystemConfiguration { @@ -209,27 +210,27 @@ public MotorSubsystemConfiguration publishTo(NetworkTableInstance ntInstance) { } protected MotorSubsystem(MotorSubsystemConfiguration builder) { - m_pidController = builder.controller; - m_pidController.setTolerance(builder.acceptableError); - m_acceptableError = builder.acceptableError; - m_motor = builder.motor; - m_encoder = builder.encoder; - m_controlMode = builder.controlMode; - m_rotationUnit = builder.rotationUnit; + pidController = builder.controller; + pidController.setTolerance(builder.acceptableError); + acceptableError = builder.acceptableError; + motor = builder.motor; + encoder = builder.encoder; + controlMode = builder.controlMode; + rotationUnit = builder.rotationUnit; if (builder.ntInstance != null) { NetworkTable networkTable = builder.ntInstance.getTable(getName()); - m_positionPublisher = networkTable.getDoubleTopic("position").publish(); - m_atPositionPublisher = networkTable.getBooleanTopic("at position").publish(); - m_appliedCurrentPublisher = networkTable.getDoubleTopic("applied current").publish(); - m_setpointPublisher = networkTable.getDoubleTopic("setpoint").publish(); + positionPublisher = networkTable.getDoubleTopic("position").publish(); + atPositionPublisher = networkTable.getBooleanTopic("at position").publish(); + appliedCurrentPublisher = networkTable.getDoubleTopic("applied current").publish(); + setpointPublisher = networkTable.getDoubleTopic("setpoint").publish(); } else { - m_positionPublisher = null; - m_atPositionPublisher = null; - m_appliedCurrentPublisher = null; - m_setpointPublisher = null; + positionPublisher = null; + atPositionPublisher = null; + appliedCurrentPublisher = null; + setpointPublisher = null; } - m_pidController.setSetpoint(builder.startingPosition); + pidController.setSetpoint(builder.startingPosition); } /** @@ -241,8 +242,8 @@ public final void setSetpoint(T position) { if (!m_isEnabled) { enable(); } - double setpoint = position.get().in(m_rotationUnit); - m_pidController.setSetpoint(setpoint); + double setpoint = position.get().in(rotationUnit); + pidController.setSetpoint(setpoint); } /** @@ -256,12 +257,12 @@ public final Command setSetpointCommand(T setpoint) { /** Returns the current setpoint as an angle. */ public final Angle getSetpoint() { - return m_rotationUnit.of(m_pidController.getSetpoint()); + return rotationUnit.of(pidController.getSetpoint()); } /** Determines if the motor is at the current setpoint, within the acceptable error. */ public final boolean atPosition() { - return Math.abs(getMeasurement() - m_pidController.getSetpoint()) <= m_acceptableError; + return Math.abs(getMeasurement() - pidController.getSetpoint()) <= acceptableError; } /** @@ -285,7 +286,7 @@ public final void enable() { */ public final void disable() { m_isEnabled = false; - m_motor.set(m_controlMode, 0); + motor.set(controlMode, 0); } /** @@ -313,7 +314,7 @@ public final boolean isEnabled() { */ @Deprecated protected void useOutput(double output, double setpoint) { - m_motor.set(m_controlMode, clampOutput(output)); + motor.set(controlMode, clampOutput(output)); } /** @@ -322,7 +323,7 @@ protected void useOutput(double output, double setpoint) { *

This is called by {@link #periodic()} if this subsystem is enabled. */ private void useOutput(double output) { - useOutput(output, m_pidController.getSetpoint()); + useOutput(output, pidController.getSetpoint()); } /** @@ -342,7 +343,7 @@ protected double clampOutput(double output) { // This position is used by the PID controller (when it is enabled) to determine if it is already // at position or needs to adjust the subsystem position to reach a user-specified setpoint. protected final double getMeasurement() { - return m_encoder.getPositionMeasure().in(m_rotationUnit); + return encoder.getPositionMeasure().in(rotationUnit); } // `Motor` method overrides. @@ -358,7 +359,7 @@ public final void set(ControlMode mode, double demand, double feedForward) { if (m_isEnabled) { disable(); } - m_motor.set(mode, demand, feedForward); + motor.set(mode, demand, feedForward); } /** @@ -370,13 +371,13 @@ public final void set(ControlMode mode, double demand, double feedForward) { @Override public final void set(ControlMode mode, double demand) { m_isEnabled = false; - m_motor.set(mode, demand); + motor.set(mode, demand); } /** {@inheritDoc} */ @Override public final Current getAppliedCurrent() { - return m_motor.getAppliedCurrent(); + return motor.getAppliedCurrent(); } // `Encoder` method overrides. @@ -386,39 +387,39 @@ public final Current getAppliedCurrent() { @Override @Deprecated(forRemoval = true) public double position() { - return m_encoder.position(); + return encoder.position(); } /** {@inheritDoc} */ @Override public final Angle getPositionMeasure() { - return m_encoder.getPositionMeasure(); + return encoder.getPositionMeasure(); } /** {@inheritDoc} */ @Override @Deprecated(forRemoval = true) public void setPosition(double position) { - m_encoder.setPosition(position); + encoder.setPosition(position); } /** {@inheritDoc} */ @Override public final void setPosition(Angle position) { - m_encoder.setPosition(position); + encoder.setPosition(position); } /** {@inheritDoc} */ @Override @Deprecated(forRemoval = true) public double getVelocity() { - return m_encoder.getVelocity(); + return encoder.getVelocity(); } /** {@inheritDoc} */ @Override public final AngularVelocity getVelocityMeasure() { - return m_encoder.getVelocityMeasure(); + return encoder.getVelocityMeasure(); } // `Subsystem` method overrides. @@ -428,13 +429,13 @@ public final AngularVelocity getVelocityMeasure() { @Override public void periodic() { if (m_isEnabled) { - useOutput(m_pidController.calculate(getMeasurement())); + useOutput(pidController.calculate(getMeasurement())); } - if (m_positionPublisher != null) { - m_positionPublisher.set(getPositionMeasure().in(Rotations)); - m_setpointPublisher.set(getSetpoint().in(Rotations)); - m_atPositionPublisher.set(atPosition()); - m_appliedCurrentPublisher.set(getAppliedCurrent().in(Amps)); + if (positionPublisher != null) { + positionPublisher.set(getPositionMeasure().in(Rotations)); + setpointPublisher.set(getSetpoint().in(Rotations)); + atPositionPublisher.set(atPosition()); + appliedCurrentPublisher.set(getAppliedCurrent().in(Amps)); } } } From 273d258348e79ea7f1fa412487b21f403b550b43 Mon Sep 17 00:00:00 2001 From: Veselin Dikov Date: Sun, 26 Oct 2025 22:02:05 -0700 Subject: [PATCH 4/6] spotlessApply --- .../java/com/team2813/lib2813/subsystems/MotorSubsystem.java | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java b/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java index 85be7e57..15fb1496 100644 --- a/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java +++ b/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java @@ -64,7 +64,6 @@ public abstract class MotorSubsystem> extends Subsyste private final DoublePublisher appliedCurrentPublisher; private final DoublePublisher setpointPublisher; - /** A configuration for a MotorSubsystem */ public static class MotorSubsystemConfiguration { /** The default acceptable position error. */ From c23cb95b475f4b952c0a74db2752e225d062c2b4 Mon Sep 17 00:00:00 2001 From: Veselin Dikov Date: Mon, 27 Oct 2025 22:29:27 +0000 Subject: [PATCH 5/6] Restore two more MotorSubsystem member names to their prior state; as it turned out, these are used by subclasses and cannot be changed without breaking user code --- .../lib2813/subsystems/MotorSubsystem.java | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java b/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java index 15fb1496..d2047ec7 100644 --- a/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java +++ b/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java @@ -54,11 +54,11 @@ public abstract class MotorSubsystem> extends Subsyste protected final Motor motor; protected final Encoder encoder; // Specifies if the mechanism is currently under the control of the PID controller. - private boolean m_isEnabled; + private boolean isEnabled; protected final ControlMode controlMode; protected final AngleUnit rotationUnit; protected final double acceptableError; - protected final PIDController pidController; + protected final PIDController controller; private final DoublePublisher positionPublisher; private final BooleanPublisher atPositionPublisher; private final DoublePublisher appliedCurrentPublisher; @@ -209,8 +209,8 @@ public MotorSubsystemConfiguration publishTo(NetworkTableInstance ntInstance) { } protected MotorSubsystem(MotorSubsystemConfiguration builder) { - pidController = builder.controller; - pidController.setTolerance(builder.acceptableError); + controller = builder.controller; + controller.setTolerance(builder.acceptableError); acceptableError = builder.acceptableError; motor = builder.motor; encoder = builder.encoder; @@ -229,7 +229,7 @@ protected MotorSubsystem(MotorSubsystemConfiguration builder) { setpointPublisher = null; } - pidController.setSetpoint(builder.startingPosition); + controller.setSetpoint(builder.startingPosition); } /** @@ -238,11 +238,11 @@ protected MotorSubsystem(MotorSubsystemConfiguration builder) { * @param position the position to go to. */ public final void setSetpoint(T position) { - if (!m_isEnabled) { + if (!isEnabled) { enable(); } double setpoint = position.get().in(rotationUnit); - pidController.setSetpoint(setpoint); + controller.setSetpoint(setpoint); } /** @@ -256,12 +256,12 @@ public final Command setSetpointCommand(T setpoint) { /** Returns the current setpoint as an angle. */ public final Angle getSetpoint() { - return rotationUnit.of(pidController.getSetpoint()); + return rotationUnit.of(controller.getSetpoint()); } /** Determines if the motor is at the current setpoint, within the acceptable error. */ public final boolean atPosition() { - return Math.abs(getMeasurement() - pidController.getSetpoint()) <= acceptableError; + return Math.abs(getMeasurement() - controller.getSetpoint()) <= acceptableError; } /** @@ -274,7 +274,7 @@ public final boolean atPosition() { * interruption, the motor will resume its movement towards the last set setpoint. */ public final void enable() { - m_isEnabled = true; + isEnabled = true; } /** @@ -284,7 +284,7 @@ public final void enable() { * current setpoint. */ public final void disable() { - m_isEnabled = false; + isEnabled = false; motor.set(controlMode, 0); } @@ -298,7 +298,7 @@ public final void disable() { * @return Whether the PID controller is engaged. */ public final boolean isEnabled() { - return m_isEnabled; + return isEnabled; } /** @@ -322,7 +322,7 @@ protected void useOutput(double output, double setpoint) { *

This is called by {@link #periodic()} if this subsystem is enabled. */ private void useOutput(double output) { - useOutput(output, pidController.getSetpoint()); + useOutput(output, controller.getSetpoint()); } /** @@ -355,7 +355,7 @@ protected final double getMeasurement() { */ @Override public final void set(ControlMode mode, double demand, double feedForward) { - if (m_isEnabled) { + if (isEnabled) { disable(); } motor.set(mode, demand, feedForward); @@ -369,7 +369,7 @@ public final void set(ControlMode mode, double demand, double feedForward) { */ @Override public final void set(ControlMode mode, double demand) { - m_isEnabled = false; + isEnabled = false; motor.set(mode, demand); } @@ -427,8 +427,8 @@ public final AngularVelocity getVelocityMeasure() { /** Applies the PID output to the motor if this subsystem is enabled. */ @Override public void periodic() { - if (m_isEnabled) { - useOutput(pidController.calculate(getMeasurement())); + if (isEnabled) { + useOutput(controller.calculate(getMeasurement())); } if (positionPublisher != null) { positionPublisher.set(getPositionMeasure().in(Rotations)); From 2a0982f536896194ec28555935d999813a91e0ab Mon Sep 17 00:00:00 2001 From: Veselin Dikov Date: Mon, 27 Oct 2025 22:32:54 +0000 Subject: [PATCH 6/6] Restore few more upstream changes or state that got inadvertendedly changed in this branch --- .../com/team2813/lib2813/subsystems/MotorSubsystem.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java b/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java index d2047ec7..d8033320 100644 --- a/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java +++ b/lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java @@ -238,7 +238,7 @@ protected MotorSubsystem(MotorSubsystemConfiguration builder) { * @param position the position to go to. */ public final void setSetpoint(T position) { - if (!isEnabled) { + if (!isEnabled()) { enable(); } double setpoint = position.get().in(rotationUnit); @@ -285,7 +285,7 @@ public final void enable() { */ public final void disable() { isEnabled = false; - motor.set(controlMode, 0); + motor.disable(); } /** @@ -355,7 +355,7 @@ protected final double getMeasurement() { */ @Override public final void set(ControlMode mode, double demand, double feedForward) { - if (isEnabled) { + if (isEnabled()) { disable(); } motor.set(mode, demand, feedForward); @@ -427,7 +427,7 @@ public final AngularVelocity getVelocityMeasure() { /** Applies the PID output to the motor if this subsystem is enabled. */ @Override public void periodic() { - if (isEnabled) { + if (isEnabled()) { useOutput(controller.calculate(getMeasurement())); } if (positionPublisher != null) {