04 / Subsystems and Commands
Subsystem Lifecycle
Use read, periodic, write, and reset methods to organize mechanism code.
04 / Subsystems and Commands
Use read, periodic, write, and reset methods to organize mechanism code.
You will
This lesson is about making robot code scale. Architecture should reduce repeated decisions, not bury logic. Students should see why RobotHardware, subsystems, enums, state machines, and commands make the robot easier to test, reuse, and repair at an event.
A subsystem should represent one mechanism or tightly related mechanism group. It stores the current target, reads sensors, calculates outputs, writes to hardware, and exposes simple commands to OpModes.
The centerstage code uses a wrapper with read, periodic, write, and reset methods. That pattern makes it clear when data enters the subsystem, when decisions happen, and when outputs leave.
Introduce structure after students have felt the pain of duplication. Move one mechanism into a subsystem, one repeated behavior into a command, and one set of raw positions into named states. The final code should read more like robot intent and less like a wiring diagram.
For this specific lesson, students should first restate the goal in robot terms, then identify the value or behavior they expect to observe, then run the smallest test that proves the idea. The lesson should feel like a guided lab: predict, run, observe, explain, and only then extend.
WSubsystem.java · Java
public abstract class WSubsystem {
public abstract void read();
public abstract void periodic();
public abstract void write();
public abstract void reset();
}
public class LiftSubsystem extends WSubsystem {
private int targetTicks;
private int currentTicks;
public void goToHigh() {
targetTicks = 1800;
}
@Override public void read() {
currentTicks = RobotHardware.getInstance().lift.getCurrentPosition();
}
@Override public void periodic() {
telemetryError = targetTicks - currentTicks;
}
@Override public void write() {
RobotHardware.getInstance().lift.setTargetPosition(targetTicks);
}
}Architecture fails when responsibilities are unclear. If hardware is written from several places, commands fight. If states are scattered across booleans, impossible combinations appear. If lifecycle methods run out of order, telemetry and behavior disagree. The debugging path should follow ownership: input, command, subsystem state, hardware write.
Check your understanding
What is the main job of a subsystem?
0 of 1 answered
References
Finished reading?
You'll move on to “Enums and Finite State Machines” next.