EasyStack Docs

EasyStateSubsystem

Esta página ainda não foi traduzida. Mostrando a versão em inglês.

#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:

Java
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

MethodReturns
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.
Java
if (arm.isIn(Arm.State.SCORE)) {
    // the arm is at the scoring state
}

#Changing the State

Java
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:

Java
@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:

Java
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.

Java
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:

Java
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, because onStateChanged() does not run for the initial state.
  • onStateChanged() calls reset() 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 atualização 2026-11-16

EasyStack - FTC Java library

This site records page views of visitors to understand how the documentation is used. Nothing is sold or shared with third parties.