Your First Motor
In the previous lesson, you wrote an OpMode that displayed messages on the Driver Station. That is a fine start, but robots are meant to move. In this lesson, you will learn how to access hardware devices and control a DC motor.
The Hardware Map
Every piece of hardware on your FTC robot -- motors, servos, sensors -- is registered in a hardware configuration on the Robot Controller. When your OpMode runs, the FTC SDK provides a hardwareMap object that lets you look up devices by name.
Here is how you retrieve a DC motor:
DcMotor motor = hardwareMap.get(DcMotor.class, "motorName");
Let's break this down:
hardwareMap.get()looks up a hardware device by its type and name.DcMotor.classtells the SDK you want a DC motor."motorName"is the name you gave the motor in your robot's hardware configuration. It must match exactly (case-sensitive).
waitForStart(), during the initialization phase. This way, if something is misconfigured, you will see the error before the match starts.
Controlling a Motor
Once you have a DcMotor reference, you can control it with setPower():
motor.setPower(0.5); // Half speed forward
The power value is a double that ranges from -1.0 to 1.0:
| Value | Effect |
|---|---|
1.0 | Full speed forward |
0.5 | Half speed forward |
0.0 | Stopped |
-0.5 | Half speed reverse |
-1.0 | Full speed reverse |
@TeleOp(name = "Motor Demo")
public class MotorDemo extends LinearOpMode {
@Override
public void runOpMode() {
DcMotor motor = hardwareMap.get(DcMotor.class, "myMotor");
telemetry.addData("Status", "Initialized");
telemetry.update();
waitForStart();
motor.setPower(0.5);
telemetry.addData("Status", "Motor running at 50%");
telemetry.update();
}
}
Important: Imports
To use DcMotor, you need to import it at the top of your file:
import com.qualcomm.robotcore.hardware.DcMotor;
The FTC SDK organizes hardware classes under com.qualcomm.robotcore.hardware. You will see this import pattern frequently.
A Word About Hardware Names
On a real robot, hardware names come from the configuration file you create on the Robot Controller phone or Control Hub. In this tutorial environment, we simulate the hardware for you, so the motor name is pre-configured as "testMotor".
In your own team's code, you would choose descriptive names like "leftFront", "intakeMotor", or "liftMotor" and configure them to match the physical ports.
Your Exercise
Your task is to:
- Get a
DcMotorfrom thehardwareMapwith the name"testMotor". - Wait for start.
- Set the motor's power to
0.5.
Try it in the editor below!