STEM Robotics 4 All

STEM Resources for all

Creating a Simple RC OpMode

By the end of this lesson, you will have a RC Robot Controlling OpMode.

Before starting this lesson, you need to know how to create an OpMode. Review that here.

We will use a simple OpMode as opposed to LinearOpMode for this lesson, though porting this program to that framework is quite simple.

Start by creating a OpMode with the name “SimpleRCOpMode” and make it an OpMode. It should look like this:

We start by declaring the motor objects outside of the init or loop methods:

Make sure you import com.qualcomm.robotcore.hardware.DcMotor;

Then, in the init method, we instantiate the objects we just declared using the hardwareMap:

Make sure the green string matches what is declared in the HardwareMap on the Control Hub or Robot Controller phone. If it is not the same (case too) then this will crash!

Once we have our instantiations, we need to configure them a bit. Depending on your robot, you may need to reverse several of the motors, and you always want to set their “ZeroPowerBehavior” to BRAKE since you want to be precise. Thus we add the following lines:

Now our motors are all set up for use, let’s move onto the loop method.

In the loop, we want to set the motor power function of gamepad input. Here is how we will do it:

Lets take a closer look. The gamepad controls are as follows:

Source: Java for FTC

Bolded labels are doubles from either 0-1 (triggers) or -1 to 1 (joysticks). We in this case are using joysticks. Also, the sticks_x is positive to the left and sticks_y is positive down, hence all the minus signs in our code.

The end behavior we get from this code is:

  • L Stick Up/Down: Forward/Backward
  • L Stick Left/Right: Strafe Left/Right
  • R Stick Left/Right: Turn Left/Right

Note that these only work for a robot with mecanum wheels; otherwise you cannot strafe (move side to side). However, in a nonmecanum robot you can still use the other two functions (forward/backward and turn L/R).

And now you have your entire RC OpMode! This is probably the simplest OpMode you will ever have 🙂

Here is the entire code:

package org.firstinspires.ftc.teamcode;

import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorSimple;

public class SimpleRCOpMode extends OpMode {

    DcMotor LF, RF, LB, RB;

    @Override
    public void init() {
        LF = hardwareMap.get(DcMotor.class, "LeftFront");
        RF = hardwareMap.get(DcMotor.class, "RightFront");
        LB = hardwareMap.get(DcMotor.class, "LeftBack");
        RB = hardwareMap.get(DcMotor.class, "RightBack");

        LF.setDirection(DcMotorSimple.Direction.REVERSE);
        RB.setDirection(DcMotorSimple.Direction.REVERSE);

        LF.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
        RF.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
        LB.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
        RB.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);

    }

    @Override
    public void loop() {
        LF.setPower(0.6 * (-gamepad1.left_stick_y - gamepad1.left_stick_x + gamepad1.right_stick_x));
        RF.setPower(0.6 * (-gamepad1.left_stick_y + gamepad1.left_stick_x - gamepad1.right_stick_x));
        LB.setPower(0.6 * (-gamepad1.left_stick_y + gamepad1.left_stick_x + gamepad1.right_stick_x));
        RB.setPower(0.6 * (-gamepad1.left_stick_y - gamepad1.left_stick_x - gamepad1.right_stick_x));
    }
}

And the file:

© The RoboMentors (Marc and Anne-Sarah)

%d bloggers like this: