1515 * An advanced motor wrapper that enhances {@link DcMotorEx} by adding several useful features:
1616 * <ul>
1717 * <li><b>Caching:</b> Improves performance by reducing repeated hardware calls through caching via {@link CachingDcMotorEx}.</li>
18+ * <li><b>Current Limiting:</b> Allows setting a current threshold, above which power will be automatically cut for protection of the motor.</li>
1819 * <li><b>Custom PID Control:</b> Supports optional {@link PIDFController}-based custom control for precise positioning.</li>
1920 * <li><b>Linked Motors:</b> Synchronizes one or more additional {@link DcMotorEx} instances with the primary motor.</li>
2021 * <li><b>Ticks-to-Units Conversion:</b> Converts between encoder ticks and user-defined physical units like inches or degrees.</li>
@@ -33,6 +34,7 @@ public class AdvancedDcMotor extends CachingDcMotorEx {
3334 private double unitsPerTick = 0.0 ;
3435
3536 private boolean autoUpdate = true ; // Controls whether this motor auto-updates PID
37+ private boolean currentLimiting = true ; // Controls whether this motor has current limiting
3638
3739 /**
3840 * Constructs an {@code AdvancedDcMotor} using the primary motor and optional linked motors.
@@ -63,8 +65,6 @@ public void setMode(RunMode mode) {
6365
6466 @ Override
6567 public void setPower (double power ) {
66- if (isOverCurrent ()) return ;
67-
6868 super .setPower (power );
6969
7070 for (DcMotorEx motor : linkedMotors ) {
@@ -89,14 +89,25 @@ public boolean isOverCurrent() {
8989 if (motor .isOverCurrent ()) overCurrent = true ;
9090 }
9191
92- if (overCurrent ) {
93- setTargetPosition (getCurrentPosition ());
94- setPower (0 );
95- }
96-
9792 return overCurrent ;
9893 }
9994
95+ /**
96+ * Sets a current limit for the motor and its linked motors. If any motor exceeds this threshold,
97+ * it will be detected by {@link #isOverCurrent()}, and power will automatically cut to prevent damage to the motor.
98+ * <p>
99+ * This requires calling {@link AdvancedDcMotor#updateAll()} in each loop iteration.
100+ * <p>
101+ * This method is functionally identical to {@link #setCurrentAlert(double, CurrentUnit)},
102+ * but the name is changed to more clearly express its purpose as a current limiting mechanism.
103+ *
104+ * @param current the maximum allowed current
105+ * @param unit the unit of current (e.g., AMPS)
106+ */
107+ public void setCurrentLimit (double current , CurrentUnit unit ) {
108+ setCurrentAlert (current , unit );
109+ }
110+
100111 /**
101112 * Sets the motors to mirror the primary motor.
102113 *
@@ -172,7 +183,11 @@ public void setCustomPIDFController(CustomPIDFController controller) {
172183 * Throws {@link RuntimeException} if coefficients are not initialized.
173184 */
174185 public void update () {
175- isOverCurrent ();
186+ if (currentLimiting && isOverCurrent ()) {
187+ setPower (0 );
188+ return ;
189+ }
190+
176191 fakeRunToPosition ();
177192
178193 if (runningCustomPIDF ) {
@@ -276,15 +291,23 @@ public int unitsToTicks(double units) {
276291 }
277292
278293 /**
279- * Controls whether this motor updates its PID controller automatically
280- * when {@link #updateAll()} is called.
294+ * Enables or disables auto-update during {@link #updateAll()}.
281295 *
282- * @param autoUpdate true to enable automatic update, false to disable
296+ * @param autoUpdate true to auto-call {@link # update()} in {@link #updateAll()} , false to disable
283297 */
284298 public void setAutoUpdate (boolean autoUpdate ) {
285299 this .autoUpdate = autoUpdate ;
286300 }
287301
302+ /**
303+ * Enables or disables current limiting during updates.
304+ *
305+ * @param currentLimiting true to cut power if over current limit, false to ignore
306+ */
307+ public void setCurrentLimiting (boolean currentLimiting ) {
308+ this .currentLimiting = currentLimiting ;
309+ }
310+
288311 /**
289312 * Stops auto updating and removes this motor from the global registry.
290313 * Call this if you want to clean up.
0 commit comments