Skip to content

Commit 2037fbd

Browse files
committed
Add isValidateSetpoint()
1 parent 20c4139 commit 2037fbd

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

lib/src/main/java/com/team2813/lib2813/subsystems/MotorSubsystem.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,46 @@ protected MotorSubsystem(MotorSubsystemConfiguration builder) {
106106
/**
107107
* Sets the desired setpoint to the provided value, and enables the PID control.
108108
*
109+
* <p>This method will call {@link #isValidateSetpoint(Supplier)} before it makes any changes to
110+
* this subsystem. If that method call returns {@code false} then this method will silently do
111+
* nothing.
112+
*
109113
* @param position the position to go to.
110114
*/
111115
public final void setSetpoint(T position) {
116+
if (!isValidateSetpoint(position)) {
117+
return;
118+
}
119+
112120
if (!isEnabled()) {
113121
enable();
114122
}
115123
double setpoint = position.get().in(rotationUnit);
116124
controller.setSetpoint(setpoint);
117125
}
118126

127+
/**
128+
* Determines if the given position is a valid setpoint, given the current state of the subsystem.
129+
*
130+
* <p>This is an extension point that allows subclasses to prevent unsafe movements. This method
131+
* is called by {@link #setSetpoint(Supplier)} before that method makes any changes to the
132+
* subsystem. Subclasses that want to prevent movement to positions that are unsafe can override
133+
* this method and have it return {@code false} to indicate that {@link #setSetpoint(Supplier)}
134+
* should silently ignore the request to change the setpoint.
135+
*
136+
* <p>Subclasses that override this method <em>may</em> choose to report a warning to the user
137+
* before returning {@code false}, but <em>should not</em> perform any actions that would lead to
138+
* an observable behavior change on this subsystem.
139+
*
140+
* <p>The default implementation returns {@code true}.
141+
*
142+
* @param position the position passed to {@link #setSetpoint(Supplier)}.
143+
* @return {@code true} if the position should be considered valid, otherwise {@code false}.
144+
*/
145+
protected boolean isValidateSetpoint(T position) {
146+
return true;
147+
}
148+
119149
/**
120150
* Returns a command that sets the desired setpoint to the provided value.
121151
*

0 commit comments

Comments
 (0)