44import com .team2813 .lib2813 .control .Encoder ;
55import com .team2813 .lib2813 .control .Motor ;
66import com .team2813 .lib2813 .control .PIDMotor ;
7+ import com .team2813 .lib2813 .util .PeriodicRegistry ;
78import edu .wpi .first .math .controller .PIDController ;
89import edu .wpi .first .networktables .BooleanPublisher ;
910import edu .wpi .first .networktables .DoublePublisher ;
1819/**
1920 * A wrapper around a motor and a PID controller that supports predefined positions.
2021 *
21- * <p>The creator is responsible for calling {@link #periodic()} periodically.
22- *
2322 * <p>When the motor is created, PID control is disabled. To enable PID control, call {@link
2423 * #setSetpoint(P)}; the motor will moving toward the setpoint and maintains position at the
2524 * setpoint under the control of the PID controller. To stop the motor, call {@link #stopMotor()}.
@@ -46,33 +45,36 @@ public final class PositionalMotor<P extends Enum<P> & Supplier<Angle>> implemen
4645 *
4746 * <p>The default acceptable error is {@value #DEFAULT_ERROR} and the PID constants are set to 0.
4847 *
48+ * @param periodicRegistry periodic registry that the motor should use
4949 * @param motor the motor to control
5050 * @param encoder the encoder providing feedback
5151 * @param initialPosition the initial position of the controller
5252 * @return builder instance
5353 */
5454 public static <P extends Enum <P > & Supplier <Angle >> Builder <P > builder (
55- Motor motor , Encoder encoder , P initialPosition ) {
56- return new Builder <>(motor , encoder , initialPosition );
55+ PeriodicRegistry periodicRegistry , Motor motor , Encoder encoder , P initialPosition ) {
56+ return new Builder <>(periodicRegistry , motor , encoder , initialPosition );
5757 }
5858
5959 /**
6060 * Creates a new builder for a {@code PositionalMotor} using a motor that has a built-in encoder.
6161 *
6262 * <p>The default acceptable error is {@value #DEFAULT_ERROR} and the PID constants are set to 0.
6363 *
64+ * @param periodicRegistry periodic registry that the motor should use
6465 * @param motor the integrated motor controller
6566 * @param initialPosition the initial position of the controller
6667 * @return builder instance
6768 */
6869 public static <P extends Enum <P > & Supplier <Angle >> Builder <P > builder (
69- PIDMotor motor , P initialPosition ) {
70- return new Builder <>(motor , motor , initialPosition );
70+ PeriodicRegistry periodicRegistry , PIDMotor motor , P initialPosition ) {
71+ return new Builder <>(periodicRegistry , motor , motor , initialPosition );
7172 }
7273
7374 /** A builder for a {@code PositionalMotor}. */
7475 public static class Builder <P extends Enum <P > & Supplier <Angle >> {
7576
77+ private final PeriodicRegistry periodicRegistry ;
7678 private final Motor motor ;
7779 private final Encoder encoder ;
7880 private final double initialPosition ;
@@ -82,7 +84,10 @@ public static class Builder<P extends Enum<P> & Supplier<Angle>> {
8284 private Clamper clamper = value -> value ;
8385 private NetworkTable networkTable ;
8486
85- private Builder (Motor motor , Encoder encoder , P initialPosition ) {
87+ private Builder (
88+ PeriodicRegistry periodicRegistry , Motor motor , Encoder encoder , P initialPosition ) {
89+ this .periodicRegistry =
90+ Objects .requireNonNull (periodicRegistry , "periodicRegistry should not be null" );
8691 this .motor = Objects .requireNonNull (motor , "motor should not be null" );
8792 this .encoder = Objects .requireNonNull (encoder , "encoder should not be null" );
8893 Objects .requireNonNull (initialPosition , "initialPosition should not be null" );
@@ -190,6 +195,8 @@ private PositionalMotor(Builder<P> builder) {
190195
191196 // TODO: Should we update the position of the controller using controller.calculate()?
192197 // TODO: Should we update the position of the encoder using encoder.setPosition()?
198+
199+ builder .periodicRegistry .addPeriodic (robotState -> this .periodic ());
193200 }
194201
195202 /**
@@ -258,12 +265,8 @@ public boolean atPosition() {
258265 return Math .abs (getMeasurement () - controller .getSetpoint ()) <= acceptableError ;
259266 }
260267
261- /**
262- * Applies the PID output to the motor if this motor is enabled.
263- *
264- * <p>Should be called from {@link edu.wpi.first.wpilibj2.command.Subsystem#periodic()}.
265- */
266- public void periodic () {
268+ /** Applies the PID output to the motor if this motor is enabled. */
269+ private void periodic () {
267270 if (isPIDControlEnabled ) {
268271 double nextOutput = controller .calculate (getMeasurement ());
269272 motor .set (controlMode , clamper .clampValue (nextOutput ));
0 commit comments