1.2.2 OpModes - Brickwolves/CodeCampWiki GitHub Wiki

image

What is an OpMode?

An OpMode is a Java class that can be directly executed via the driver station. When you run a TeleOp in an FTC match, you would run an OpMode that includes all the robot initialization and user controls. In an autonomous, you would have a separate OpMode including your robot initialization and your pre-set instructions to execute. This sounds pretty abstract but just bear with me. There are two types of OpModes:

Linear vs Iterative OpModes

A LinearOpMode implements a single runOpMode() method from the LinearOpMode interface. Any code in this method runs once.

An IterativeOpMode implements multiple methods of the OpMode interface. The two important ones are the init() and the loop() methods. Just as it sounds, the init() method will execute when you click the [INIT] button and is where you declare and retrieve all your hardware. The loop() method is where your main controller code should be, and this will be repeated until [STOP] is pressed.

Example Autonomous

@Autonomous(name="LinearAuto", group="Autonomous Linear Opmode")
public class LinearAuto extends LinearOpMode
{
    // Declare OpMode members.
    private ElapsedTime runtime = new ElapsedTime();

    public void initialize(){
        setOpMode(this);

        multTelemetry.addData("Status", "Initalized");
        multTelemetry.update();
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void runOpMode()
    {

        initialize();

        multTelemetry.addLine("Waiting for start");
        multTelemetry.update();
        waitForStart();
        
        if (opModeIsActive()){

            /* Y O U R   C O D E   H E R E */

        }
   }
}

Example TeleOp

@TeleOp(name="Iterative TeleOp", group="Iterative Opmode")
public class IterativeTeleOp extends OpMode {

    // Declare OpMode members.
    private ElapsedTime runtime = new ElapsedTime();
    /*
     * Code to run ONCE when the driver hits INIT
     */
    @Override
    public void init() {
        setOpMode(this);
        
        /*
                    Y O U R   C O D E   H E R E
                                                   */

        multTelemetry.addData("Status", "Initialized");
        multTelemetry.update();
    }

    /*
     * Code to run REPEATEDLY after the driver hits INIT, but before they hit PLAY
     */
    @Override
    public void init_loop() {

        /*
                    Y O U R   C O D E   H E R E
                                                   */


        multTelemetry.addData("Status", "InitLoop");
        multTelemetry.update();
    }

    /*
     * Code to run ONCE when the driver hits PLAY
     */
    @Override
    public void start() {
        runtime.reset();


        /*
                    Y O U R   C O D E   H E R E
                                                   */

        multTelemetry.addData("Status", "Started");
        multTelemetry.update();
    }

    /*
     * Code to run REPEATEDLY after the driver hits PLAY but before they hit STOP
     */
    @Override
    public void loop() {

        /*
                    Y O U R   C O D E   H E R E
                                                    */
        
        /*
             ----------- L O G G I N G -----------
                                                */
        multTelemetry.addData("Status", "TeleOp Running");
        multTelemetry.update();
    }

    @Override
    public void stop() {

        /*
                    Y O U R   C O D E   H E R E
                                                   */

    }
}