Skip to content

Commit 106a67b

Browse files
author
BuildTools
committed
add current limiting to AdvancedDcMotor
1 parent 0afe535 commit 106a67b

2 files changed

Lines changed: 42 additions & 26 deletions

File tree

Marrow/src/main/java/com/skeletonarmy/marrow/AdvancedDcMotor.java

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
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.

Marrow/src/main/java/com/skeletonarmy/marrow/actions/MotorToPosition.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,24 @@
1010
import com.qualcomm.robotcore.util.ElapsedTime;
1111
import com.skeletonarmy.marrow.AdvancedDcMotor;
1212

13+
/**
14+
* An {@link Action} that commands a motor to move to a specific encoder position.
15+
* This action completes when the motor reaches the target position within the
16+
* motor's {@link DcMotorEx#getTargetPositionTolerance() target position tolerance},
17+
* or when an overcurrent condition is detected.
18+
*/
1319
@Config
1420
public class MotorToPosition implements Action {
1521
private boolean initialized = false;
1622

1723
private final DcMotorEx motor;
1824
private final int targetPos;
19-
private final boolean holdPosition;
20-
21-
private boolean reachedVelocity;
2225

2326
private final ElapsedTime timer = new ElapsedTime();
2427

25-
public MotorToPosition(DcMotorEx motor, int targetPos, boolean holdPosition) {
28+
public MotorToPosition(DcMotorEx motor, int targetPos) {
2629
this.motor = motor;
2730
this.targetPos = targetPos;
28-
this.holdPosition = holdPosition;
2931
}
3032

3133
@Override
@@ -44,15 +46,6 @@ public boolean run(@NonNull TelemetryPacket telemetryPacket) {
4446

4547
boolean isAtPosition = Math.abs(targetPos - motor.getCurrentPosition()) <= motor.getTargetPositionTolerance();
4648

47-
// Reached target position
48-
if (isAtPosition) {
49-
if (holdPosition) {
50-
motor.setTargetPosition(motor.getCurrentPosition());
51-
} else {
52-
motor.setPower(0);
53-
}
54-
}
55-
56-
return !isAtPosition;
49+
return !isAtPosition && !motor.isOverCurrent();
5750
}
5851
}

0 commit comments

Comments
 (0)