@@ -48,12 +48,10 @@ public final class PositionalMotor<P extends Enum<P> & Supplier<Angle>> implemen
4848 * @param periodicRegistry periodic registry that the motor should use
4949 * @param motor the motor to control
5050 * @param encoder the encoder providing feedback
51- * @param initialPosition the initial position of the controller
5251 * @return builder instance
5352 */
54- public static <P extends Enum <P > & Supplier <Angle >> Builder <P > builder (
55- PeriodicRegistry periodicRegistry , Motor motor , Encoder encoder , P initialPosition ) {
56- return new Builder <>(periodicRegistry , motor , encoder , initialPosition );
53+ public static Builder builder (PeriodicRegistry periodicRegistry , Motor motor , Encoder encoder ) {
54+ return new Builder (periodicRegistry , motor , encoder );
5755 }
5856
5957 /**
@@ -63,36 +61,30 @@ public static <P extends Enum<P> & Supplier<Angle>> Builder<P> builder(
6361 *
6462 * @param periodicRegistry periodic registry that the motor should use
6563 * @param motor the integrated motor controller
66- * @param initialPosition the initial position of the controller
6764 * @return builder instance
6865 */
69- public static <P extends Enum <P > & Supplier <Angle >> Builder <P > builder (
70- PeriodicRegistry periodicRegistry , PIDMotor motor , P initialPosition ) {
71- return new Builder <>(periodicRegistry , motor , motor , initialPosition );
66+ public static Builder builder (PeriodicRegistry periodicRegistry , PIDMotor motor ) {
67+ return new Builder (periodicRegistry , motor , motor );
7268 }
7369
7470 /** A builder for a {@code PositionalMotor}. */
75- public static class Builder < P extends Enum < P > & Supplier < Angle >> {
71+ public static class Builder {
7672
7773 private final PeriodicRegistry periodicRegistry ;
7874 private final Motor motor ;
7975 private final Encoder encoder ;
80- private double initialPosition ;
8176 private ControlMode controlMode = ControlMode .DUTY_CYCLE ;
8277 private AngleUnit rotationUnit = Units .Rotations ;
8378 private PIDController controller ;
8479 private double acceptableError = DEFAULT_ERROR ;
8580 private Clamper clamper = value -> value ;
8681 private NetworkTable networkTable ;
8782
88- private Builder (
89- PeriodicRegistry periodicRegistry , Motor motor , Encoder encoder , P initialPosition ) {
83+ private Builder (PeriodicRegistry periodicRegistry , Motor motor , Encoder encoder ) {
9084 this .periodicRegistry =
9185 Objects .requireNonNull (periodicRegistry , "periodicRegistry should not be null" );
9286 this .motor = Objects .requireNonNull (motor , "motor should not be null" );
9387 this .encoder = Objects .requireNonNull (encoder , "encoder should not be null" );
94- Objects .requireNonNull (initialPosition , "initialPosition should not be null" );
95- this .initialPosition = initialPosition .get ().in (rotationUnit );
9688 controller = new PIDController (0 , 0 , 0 );
9789 }
9890
@@ -102,7 +94,7 @@ private Builder(
10294 * @param controller The PID controller
10395 * @return {@code this} for chaining
10496 */
105- public Builder < P > controller (PIDController controller ) {
97+ public Builder controller (PIDController controller ) {
10698 this .controller = Objects .requireNonNull (controller , "controller should not be null" );
10799 return this ;
108100 }
@@ -115,7 +107,7 @@ public Builder<P> controller(PIDController controller) {
115107 * @return {@code this} for chaining
116108 * @throws IllegalArgumentException If {@code controlMode} is for positional control
117109 */
118- public Builder < P > controlMode (ControlMode controlMode ) {
110+ public Builder controlMode (ControlMode controlMode ) {
119111 if (controlMode .isPositionalControl ()) {
120112 throw new IllegalArgumentException (
121113 String .format (
@@ -135,7 +127,7 @@ public Builder<P> controlMode(ControlMode controlMode) {
135127 * @param d the derivative
136128 * @return {@code this} for chaining
137129 */
138- public Builder < P > PID (double p , double i , double d ) {
130+ public Builder PID (double p , double i , double d ) {
139131 controller .setPID (p , i , d );
140132 return this ;
141133 }
@@ -145,7 +137,7 @@ public Builder<P> PID(double p, double i, double d) {
145137 *
146138 * @return {@code this} for chaining
147139 */
148- public Builder < P > acceptableError (double error ) {
140+ public Builder acceptableError (double error ) {
149141 this .acceptableError = error ;
150142 return this ;
151143 }
@@ -156,7 +148,7 @@ public Builder<P> acceptableError(double error) {
156148 * @param clamper Function to use to clamp output values
157149 * @return {@code this} for chaining
158150 */
159- public Builder < P > clamper (Clamper clamper ) {
151+ public Builder clamper (Clamper clamper ) {
160152 this .clamper = Objects .requireNonNull (clamper , "clamper should not be null" );
161153 return this ;
162154 }
@@ -166,8 +158,7 @@ public Builder<P> clamper(Clamper clamper) {
166158 *
167159 * @param rotationUnit The angle unit to use for calculations
168160 */
169- public Builder <P > rotationUnit (AngleUnit rotationUnit ) {
170- initialPosition = rotationUnit .convertFrom (initialPosition , this .rotationUnit );
161+ public Builder rotationUnit (AngleUnit rotationUnit ) {
171162 this .rotationUnit = rotationUnit ;
172163 return this ;
173164 }
@@ -178,18 +169,23 @@ public Builder<P> rotationUnit(AngleUnit rotationUnit) {
178169 * @param networkTable Table to publish to
179170 * @return {@code this} for chaining
180171 */
181- public Builder < P > publishTo (NetworkTable networkTable ) {
172+ public Builder publishTo (NetworkTable networkTable ) {
182173 this .networkTable = Objects .requireNonNull (networkTable , "networkTable should not be null" );
183174 return this ;
184175 }
185176
186- /** Builds a {@code PositionalMotor} using the current values of this builder. */
187- public PositionalMotor <P > build () {
188- return new PositionalMotor <>(this );
177+ /**
178+ * Builds a {@code PositionalMotor} using the current values of this builder.
179+ *
180+ * @param initialPosition the initial position of the controller
181+ */
182+ public <P extends Enum <P > & Supplier <Angle >> PositionalMotor <P > build (P initialPosition ) {
183+ Objects .requireNonNull (initialPosition , "initialPosition should not be null" );
184+ return new PositionalMotor <>(this , initialPosition );
189185 }
190186 }
191187
192- private PositionalMotor (Builder < P > builder ) {
188+ private PositionalMotor (Builder builder , P initialPosition ) {
193189 controller = builder .controller ;
194190 acceptableError = builder .acceptableError ;
195191 motor = builder .motor ;
@@ -205,10 +201,9 @@ private PositionalMotor(Builder<P> builder) {
205201 }
206202
207203 controller .setTolerance (builder .acceptableError );
208- controller .setSetpoint (builder .initialPosition );
209- encoder .setPosition (rotationUnit .of (builder .initialPosition ));
210-
211- // TODO: Should we update the position of the controller using controller.calculate()?
204+ double initialPositionAsDouble = initialPosition .get ().in (builder .rotationUnit );
205+ controller .setSetpoint (initialPositionAsDouble );
206+ encoder .setPosition (rotationUnit .of (initialPositionAsDouble ));
212207
213208 builder .periodicRegistry .addPeriodic (robotState -> this .periodic ());
214209 }
0 commit comments