EasyStateSubsystem
#EasyStateSubsystem
EasyStateSubsystem<S extends Enum<S>> extends EasySubsystemBase and keeps track of what state the mechanism is in. The states are the values of an enum that you define.
#Creating a State Subsystem
Define the enum, pass it as the type parameter, and give the starting state to the constructor:
public class Arm extends EasyStateSubsystem<Arm.State> {
public enum State { STOWED, INTAKE, SCORE }
public Arm() {
super(State.STOWED);
}
@Override
public void init(HardwareMap hardwareMap) {
// get the hardware here
}
}The constructor is protected, so only subclasses can call it. The state and the previous state both start as the initial state.
#Reading the State
| Method | Returns |
|---|---|
getState() | The current state. |
getPreviousState() | The state before the last change. It equals the current state until the first change. |
isIn(state) | true if the current state is the given one. |
if (arm.isIn(Arm.State.SCORE)) {
// the arm is at the scoring state
}#Changing the State
arm.setState(Arm.State.SCORE);setState() does nothing if the subsystem is already in that state. Otherwise it saves the current state as the previous state, sets the new state, and then calls onStateChanged().
Override onStateChanged() to move the mechanism when the state changes. It receives the old and the new state, and getState() already returns the new one:
@Override
protected void onStateChanged(State from, State to) {
// for example, set a new target position
}The hook is empty by default. It is not called for the initial state, because the constructor sets it directly. Apply the initial state in init().
Do not pass null to setState(). The value is not checked.
#State Command
setStateCommand(state) returns an InstantCommand that calls setState(). Use it inside command groups and decorators:
EasyCommand scoreSequence = arm.setStateCommand(Arm.State.SCORE)
.andThen(new WaitCommand(5@@))
.andThen(arm.setStateCommand(Arm.State.STOWED));The command requires the subsystem, so scheduling it takes the subsystem from any other command that is using it. When you are not inside a command, call setState() directly.
#Complete Example
This arm moves to a target position that depends on its state. Each state stores its encoder target, and onStateChanged() gives the controller the new target.
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorEx;
import com.qualcomm.robotcore.hardware.HardwareMap;
import dev.easyfamily.EasyStack.subsytem.EasyStateSubsystem;
// Also import EasyPIDController from your EasyStack package.
public class Arm extends EasyStateSubsystem<Arm.State> {
public enum State {
STOWED(0),
INTAKE(120),
SCORE(480);
public final int ticks;
State(int ticks) {
this.ticks = ticks;
}
}
private DcMotorEx motor;
private final EasyPIDController controller =
new EasyPIDController(0.01, 0.0, 0.0002);
public Arm() {
super(State.STOWED);
}
@Override
public void init(HardwareMap hardwareMap) {
motor = hardwareMap.get(DcMotorEx.class, "arm");
motor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
motor.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
controller.setTargetPosition(getState().ticks);
}
@Override
protected void onStateChanged(State from, State to) {
controller.setTargetPosition(to.ticks);
controller.reset();
}
@Override
public void loop() {
double power = controller.calculate(motor.getCurrentPosition());
motor.setPower(Math.max(-0.8, Math.min(0.8, power)));
}
}In an OpMode, create the subsystem once and change its state from the gamepad:
Arm arm = new Arm();
if (pad.aPressed()) {
arm.setState(Arm.State.SCORE);
} else if (pad.bPressed()) {
arm.setState(Arm.State.STOWED);
}
telemetry.addData("Arm state", arm.getState());In this example:
- The enum holds the target of each state, so adding a state means adding one line.
init()applies the starting target, becauseonStateChanged()does not run for the initial state.onStateChanged()callsreset()so the old integral does not carry over to the new target.loop()limits the final power to −0.8 to 0.8, because the controller does not limit its output.
The PID gains are illustrative and must be tuned for your arm. Configure the motor so that positive power increases the encoder count.
Última actualización 2026-11-16