Lesson 7.2: Mapping DcMotors with the Right Names and Ports
Technical Context
Motors are high-draw components connected to the 4-pin motor ports on the Control Hub or Expansion Hub. Proper mapping ensures that the SDK applies the correct PID coefficients and encoder logic to the specific motor registered at that port. Mapping the wrong port will send voltage to the wrong mechanism — a critical failure during a competition match.
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().
A common professional practice is to set the motor's direction and run mode immediately after mapping, so the hardware is always in a known state before the match begins. This prevents issues like an arm motor suddenly lurching when the OpMode starts.
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()
Template Challenge
Robot Scenario: Your robot uses two motors for a tank drive configuration. Register "left_drive" and "right_drive" in the initialization block, and reverse the right motor so both sides drive forward with positive power.
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="Tank_Map_Challenge")
public class TankMap extends OpMode {
// INSERT CODE HERE: Declare two DcMotor member variables
// named 'leftDrive' and 'rightDrive'
@Override
public void init() {
// INSERT CODE HERE: Map both motors using the hardwareMap
// INSERT CODE HERE: Reverse the right motor direction
}
@Override
public void loop() {
leftDrive.setPower(-gamepad1.left_stick_y);
rightDrive.setPower(-gamepad1.right_stick_y);
}
}
Show answer
private DcMotor leftDrive;
private DcMotor rightDrive;
@Override
public void init() {
leftDrive = hardwareMap.get(DcMotor.class, "left_drive");
rightDrive = hardwareMap.get(DcMotor.class, "right_drive");
rightDrive.setDirection(DcMotorSimple.Direction.REVERSE);
telemetry.addData("Status", "Tank drive mapped");
}
Ready to move on?
Sign in with Google to save your progress with Telemark, or continue without saving.