Opmodes - ftc8380/coding-guide GitHub Wiki

I'm going to now assume some familiarity with the FTC Driver Station app. If you're confused by anything I say about it, go back here with CTRL+F as your friend.

What is an Opmode?

Opmodes are basically sets of instructions for the robot to execute. As our new programmers, you are going to be writing opmodes for the robot to run. Here are some examples of opmodes:

  • Teleop
  • BlueSideAuto
  • RedSideAuto
  • RedSideAutoDelayed

etc etc.

Making opmodes

When you make your own Opmode, you're extending the FTC SDK's built-in OpMode class. I assume a basic understanding of Java, but if you're shaky, then you can think of this like:

  • The OpMode class is provided by FIRST but it's completely empty and useless
  • When you extend it, you use it as a template to make your own version that does stuff

Remember that GM0 taught you how to make classes. Android Studio asks you for a name and they come out looking like this:

package org.firstinspires.ftc.teamcode;

public class TheNameYouTypedIn {
}

That's not very helpful! Let's make our class extend the OpMode class:

package org.firstinspires.ftc.teamcode;

public class TheNameYouTypedIn extends OpMode {
    @Override
    public void init() {

    }

    @Override
    public void loop() {

    }
}

Okay. We've made it so that our new Opmode extends its FTC-provided counterpart, and we've added two methods (or functions as you might know them): init and loop.

Methods: init and loop

@Override

Do you see the @Override on top of each method? This means we're overriding methods from the OpMode class. Remember that the OpMode class comes with its own init and loop functions - but they're empty! We are overriding them with our own custom ones.

You can ignore the public void stuff for now - just know that they need to be there to work properly.

init

Inside those curly braces you can define what happens after you select your opmode on the driver station (phone or brick) and press INIT.

Now is really where you start to think about your opmodes? What do you want to do after INIT, but before Play? To me, that consists mostly of setting up the hardware. I would want to do that once and only once. More on that later.

loop

The code in here will loop repeatedly while your Opmode is playing but before it stops. What things might you want to do over and over? Well, personally, I'd listen for controller input. I can't just do that once, that would be pointless. I'd also control the motors and choose what speed they're at.

Takeaways

This is an introduction to an Opmode. I talked vaguely about setting up hardware and listening for controller input, but I just want you to know what an Opmode is, and how it's structured. Once you start thinking in terms of

  • what do I want to do once
  • what do I want to do repeatedly

you can move on to the next page.