Lesson 7.1: Using hardwareMap to Find the Devices You Configured
Technical Context
The Control Hub maintains an internal XML configuration file that maps physical ports to user-defined strings. If the Java code attempts to instantiate a component that does not exist in this registry, the program will throw a NullPointerException or a MissingHardwareException, causing the robot to fail during the initialization phase of a match.
How hardwareMap Connects Code to Real Hardware
The hardwareMap object is an instance of the HardwareMap class provided by the SDK. It acts as a lookup service. When you invoke the get() method, the SDK searches the configuration registry for a device matching the specified class type and name string. This process performs the dependency injection required to link your logical Java objects to the physical I/O ports on the Control Hub.
The get() method requires two parameters:
- The hardware class — tells the SDK what type of device to return (e.g.,
DcMotor.class) - The configuration name string — must match the name defined in the Robot Controller app exactly, character for character
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;
@TeleOp(name="HardwareMap_Demo")
public class HardwareMapDemo extends OpMode {
private DcMotor armMotor;
@Override
public void init() {
// The SDK performs a lookup in the Hub's XML configuration registry
// Parameters: (Hardware Class Type, "Exact Configuration Name")
armMotor = hardwareMap.get(DcMotor.class, "arm_motor");
telemetry.addData("Status", "arm_motor mapped successfully");
}
@Override
public void loop() {
armMotor.setPower(0.5);
}
}
Fill-in-the-Blank Practice
- The
hardwareMapobject is used to perform a__________in the robot's configuration registry. - The
get()method requires two parameters: the__________of the device and the exact string name from the configuration. - Attempting to access a hardware device that is not registered in the configuration will result in a
__________during OpMode initialization.
Show answers
- lookup (dependency injection / registry search)
- class type (e.g.,
DcMotor.class) NullPointerException(orMissingHardwareException)
Template Challenge
Robot Scenario: You have a new robot build. You need to map a motor that controls a "lift" mechanism. The motor is named "lift_motor" in the Robot Controller configuration.
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;
@TeleOp(name="Lift_Map_Challenge")
public class LiftMap extends OpMode {
private DcMotor lift;
@Override
public void init() {
// INSERT CODE HERE: Use hardwareMap to instantiate the lift motor
// using the configuration name "lift_motor"
}
@Override
public void loop() {
lift.setPower(-gamepad1.left_stick_y);
}
}
Show answer
@Override
public void init() {
lift = hardwareMap.get(DcMotor.class, "lift_motor");
telemetry.addData("Status", "Lift motor mapped");
}
Ready to move on?
Sign in with Google to save your progress with Telemark, or continue without saving.