FTC Programming

Modular Code

Objects in FTC Java

What is modular code?

Modular code comes from creating object files that you can use in your OpModes. Let’s create one for hardware mapping.

Creating an Object

Create a new file, but not as an op mode, and name it HardwareRobot. All of our hardware mapping will go here.

Let’s get the core of the file completed.

package org.firstinspires.ftc.teamcode;
public class HardwareRobot {
    public DcMotor leftFront, rightFront;
    public HardwareRobot() {}


Right now, we have a class name and a constructor. The constructor allows us to pull in other objects we need. For this file, we do not need any though. Also, make sure that you make public variables, so we can call them in files and other objects.

After the constructor, we can start creating functions within the object. Let’s create an initialize function.

public void init(HardwareMap hwMap) {
    hardwareMap = hwMap;
    leftFront = hardwareMap.get(DcMotor.class, "left_front");
    rightFront = hardwareMap.get(DcMotor.class, "right_front");

Now, in an OpMode, we need to create this object. Right after we create our main class, write:

HardwareRobot robot = new HardwareRobot();

We are creating a new HardwareRobot object named robot. Now, before our waitForStart();, we can add:


Now, you can modify one file for all your hardware mappings, rather than writing it for every file. You can create tons of functions within these objects, to make your opmodes smaller.

Creating movement object

Creating a movement object is also helpful. Create a new object just like HardwareRobot, and name it Movement. This time, we will be adding the HardwareRobot into the constructor, so we can use it throughout the file.

package org.firstinspires.ftc.teamcode;
public class Movement {
    HardwareRobot robot = null;
    public Movement(HardwareRobot arobot) {
        robot = arobot;

By creating a HardwareRobot inside this object, we can now use everything from there. Let’s create a basic movement function inside of our Movement Object.

public void drive(double lpower, double rpower){

Notice when we want to use variables from another object, we must start it with the object name, then “.” then what we want.