Skip to main content

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

  1. To declare a motor variable at the class scope, you use the __________ data type.
  2. The DcMotor.class parameter in the get() method ensures __________ safety by telling the SDK what type of hardware to return.
  3. After mapping, you can reverse the motor's physical bias by invoking the __________ method.
Show answers
  1. DcMotor
  2. type safety
  3. setDirection()

Simulator Challenge

Use the simulator below to practice mapping multiple motors and correcting drivetrain direction immediately after registration.

Telemark Unit 7 Simulator
Supports multiple Java files: hover over the tabs and choose + to create a separate mechanism or configuration class, or to import existing code.
Shows mapping errors, type mismatches, direction settings, and missing initialization hints.
Best for practicing `hardwareMap.get()`, device classes, naming consistency, and mapping structure.

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.