Build a Reusable RobotHardware Template
A hardware class that every OpMode can trust.
A hardware class that every OpMode can trust.
In this lesson, you will:
RobotHardware is the team’s shared definition of the robot. It should map configured names once, set default hardware behavior once, and expose fields or helpers that other code can use without repeating setup.
Think of RobotHardware as the pit checklist in code. If a motor name, servo range, IMU orientation, or hub cache rule belongs to the whole robot, it belongs here rather than in one TeleOp.
Start with four drive motors, hub bulk caching, and a stopDrive helper. Add mechanisms only after each one has a test OpMode. Keep subsystem-specific behavior out of the hardware class until there is a clear owner.
RobotHardware.javaJava
public class RobotHardware {
public DcMotorEx frontLeft, frontRight, backLeft, backRight;
public void init(HardwareMap hardwareMap) {
frontLeft = hardwareMap.get(DcMotorEx.class, "front_left_drive");
frontRight = hardwareMap.get(DcMotorEx.class, "front_right_drive");
backLeft = hardwareMap.get(DcMotorEx.class, "back_left_drive");
backRight = hardwareMap.get(DcMotorEx.class, "back_right_drive");
// Example only: reverse the side your physical drivetrain test requires.
frontLeft.setDirection(DcMotorSimple.Direction.REVERSE);
backLeft.setDirection(DcMotorSimple.Direction.REVERSE);
for (DcMotorEx motor : Arrays.asList(frontLeft, frontRight, backLeft, backRight)) {
motor.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
motor.setPower(0.0);
}
}
public void stopDrive() {
for (DcMotorEx motor : Arrays.asList(frontLeft, frontRight, backLeft, backRight)) {
motor.setPower(0.0);
}
}
}If one OpMode drives correctly and another does not, hardware setup is duplicated. If init fails in every OpMode, the shared contract is wrong. If one mechanism needs special rules, move those rules into a subsystem rather than bloating RobotHardware.
Write a RobotHardware class for the drivetrain. Use it from a TeleOp and an autonomous test without duplicating motor lookups.
Check your understanding before moving on.
0 of 2 answered