Skip to content

Code Organization Scheme

Kyle Hollars edited this page Oct 21, 2021 · 2 revisions

Systems, Subsystems, and Opmodes

Our code is organized into 3 main types of files: systems, subsystems, and opmodes.

Opmodes

Opmodes are the programs that are "really" run. An opmode contains the code run when the driver presses "play" on the drivers' station. There are two types of opmodes:

  • Driver controlled opmodes have 1 main job: interpret signals from the gamepad and call upon subsystems of the robot to interact with the hardware.
  • Autonomous opmodes function similarly to driver controlled opmodes, except they don't interface with the gamepad. They still call upon subsystems to control hardware components, but using pre-written code instead of gamepad input. This is one of two places where most of the "real code" will be written.

The following is a small, sample Opmode, whose only function is to pass input from the gamepad to the rest of the robot:

@TeleOp(name="OneDriver",group="Tungsteel 21-22")
public class OneDriver extends LinearOpMode {
    @Override
    public void runOpMode() {
        Robot robot = new Robot(hardwareMap);
        waitForStart();
        while (opModeIsActive()) {
            robot.tank.drive(gamepad1.left_stick_x, gamepad1.right_stick_y);
            telemetry.update();

        }
    }
}

Systems

Although a system may sound more important than a subsystem, in reality it isn't. All a system does is organize subsystems into one object. There is only one system in a project, and it contains very little code. The following is an example of a system:

public class Robot {
    public Drivetrain tank;
    public Robot(HardwareMap hwMap){
        tank = new Drivetrain(hwMap);
    }

Subsystems

This is arguably the most important of all the types of files. Subsystems are the only files that communicate directly with hardware on the robot. Each component on the robot (this year will likely be separated into drivetrain, intake, lift/claw, and turner) has it's own subsystem to control it's hardware. Within the subsystem, you can write methods that control the robot. This is the other place where most of the "real code" will be written. The following is a sample subsystem to control the drivetrain of a robot with 2 motors (a tank drive):

public class Drivetrain extends Subsystem {
    float speedMod = 1;
    DcMotorEx left, right;
    public Drivetrain(HardwareMap hwMap) {
        right = hwMap.get(DcMotorEx.class, "right"); //Define what each motor is called..
        left = hwMap.get(DcMotorEx.class, "left");   //..in the configuration
        left.setDirection(DcMotorEx.Direction.FORWARD); //Just so you don't have to constantly...
        right.setDirection(DcMotorEx.Direction.REVERSE); //...keep negative values in mind
    }
    public void drive(double m, double r) {
        // Calculate the power
        // Range.clip() makes sure the value is always between -1 and 1
        double leftPower = Range.clip(m + r, -1.0, 1.0) / speedMod;
        double rightPower = Range.clip(m - r, -1.0, 1.0) / speedMod;
        // Send calculated power to wheels
        left.setPower(leftPower);
        right.setPower(rightPower);
    }
}
Clone this wiki locally