Lesson 7.2: Mapping DcMotors with the Right Names and Ports
Technical Context
Motors connect to the motor ports on a Control Hub or Expansion Hub. Mapping gives your Java variable access to the device name stored in the Robot Controller configuration. If the name points to the wrong configured port, later commands can move the wrong mechanism.
What Good Motor Mapping Looks Like
In Java, you declare a member variable of type DcMotor at the class level. During the init() phase, you use hardwareMap.get(DcMotor.class, "name") to assign the physical motor to that variable. Once instantiated, the motor object gains access to methods like setPower(), setDirection(), and setMode().
Set the motor's direction, run mode, and other required defaults during initialization so the mechanism begins each OpMode in a known software state. A safe initialization sequence should still keep motor power at zero until movement is intended.
Annotated Code
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
@TeleOp(name="DcMotor_Mapping_Demo")
public class DcMotorMappingDemo extends OpMode {
private DcMotor frontLeft;
private DcMotor frontRight;
@Override
public void init() {
// Instantiating motors for a differential drivetrain
frontLeft = hardwareMap.get(DcMotor.class, "fl_motor");
frontRight = hardwareMap.get(DcMotor.class, "fr_motor");
// Setting logical orientation immediately after mapping
// Right side is physically reversed on most robot builds
frontRight.setDirection(DcMotorSimple.Direction.REVERSE);
// Ensure motors start at zero power in a known state
frontLeft.setPower(0);
frontRight.setPower(0);
telemetry.addData("Status", "Drivetrain mapped and ready");
}
@Override
public void loop() {
frontLeft.setPower(-gamepad1.left_stick_y);
frontRight.setPower(-gamepad1.right_stick_y);
}
}
Fill-in-the-Blank Practice
- To declare a motor variable at the class scope, you use the
__________data type. - The
DcMotor.classparameter in theget()method ensures__________safety by telling the SDK what type of hardware to return. - After mapping, you can reverse the motor's physical bias by invoking the
__________method.
Show answers
DcMotor- type safety
setDirection()
Simulator Challenge
Use the simulator below to practice mapping multiple motors and correcting drivetrain direction immediately after registration.
Ready to move on?
Only mark complete if you genuinely understand the material. Your progress will be saved in this browser.
Stuck on this lesson?
Ask about anything on this page. It can see which lesson you have open and which part you are reading.