Robot Class
Connect subsystems together and provide a central structure for your robot's code.
The Robot class is responsible for connecting all of the robot's components together.
It creates, stores, and manages the subsystems that make up the robot.
A typical Robot class looks like:
public class Robot {
public DriveSubsystem drive;
public ShooterSubsystem shooter;
public IntakeSubsystem intake;
public Robot() {
drive = new DriveSubsystem();
shooter = new ShooterSubsystem();
intake = new IntakeSubsystem();
}
}The Robot class does not contain the logic for driving, shooting, or intaking. Those responsibilities belong to the subsystems.
Instead, it acts as the central structure that connects all of the robot's components.
Managing Subsystems
Each subsystem has its own initialization and update methods.
The init() method is responsible for setting up hardware, while the update() method runs repeatedly to update the subsystem's behavior.
Without a Robot class, every OpMode would need to manually manage every subsystem:
drive.init(hardwareMap);
shooter.init(hardwareMap);
intake.init(hardwareMap);
drive.update();
shooter.update();
intake.update();This creates unnecessary repetition and makes OpModes harder to maintain.
Instead, the Robot class manages the lifecycle of every subsystem:
public class Robot {
public DriveSubsystem drive;
public ShooterSubsystem shooter;
public IntakeSubsystem intake;
public Robot() {
drive = new DriveSubsystem();
shooter = new ShooterSubsystem();
intake = new IntakeSubsystem();
}
public void init(HardwareMap hardwareMap) {
drive.init(hardwareMap);
shooter.init(hardwareMap);
intake.init(hardwareMap);
}
public void update() {
drive.update();
shooter.update();
intake.update();
}
}Now the OpMode only needs to interact with the Robot class:
Robot robot;
@Override
public void init() {
robot = new Robot();
robot.init(hardwareMap);
}
@Override
public void loop() {
robot.update();
}The OpMode no longer needs to know how each individual mechanism works.
Instead, it can simply use the subsystems:
robot.drive.drive();
robot.shooter.shoot();This keeps OpModes focused on controlling the robot rather than managing its internal structure.
Responsibilities
The Robot class should:
- Create and store subsystems
- Initialize all subsystems
- Update all subsystems
- Provide access to robot components
The Robot class should not:
- Contain mechanism logic
- Directly control motors or servos
- Replace individual subsystems
Think of the Robot class as the coordinator that connects all parts of the robot together.
Was this resource helpful?
