Skip to main content

Lesson 7.1: Using hardwareMap to Find the Devices You Configured


Technical Context

The active Robot Controller configuration maps physical ports to user-defined names. HardwareMap.get(Class, String) returns a matching configured device, but throws an exception if no such device is found. It does not return null. For optional hardware, HardwareMap.tryGet(Class, String) performs a lookup that returns null when no matching device is available. See the HardwareMap SDK Javadoc.


How hardwareMap Connects Code to Real Hardware

The hardwareMap object is an instance of the SDK's HardwareMap class. It acts as a lookup service. When you call get(), the SDK searches the active Robot Controller configuration for a device with the requested type and name, then returns the Java object used to communicate with that configured device.

The get() method requires two parameters:

  1. The hardware class: tells the SDK what type of device to return (e.g., DcMotor.class)
  2. 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);
}
}

For a genuinely optional device, use tryGet() and test its result before calling methods on it:

DcMotor testMotor = hardwareMap.tryGet(DcMotor.class, "test_motor");
if (testMotor != null) {
testMotor.setPower(0.25);
}

Fill-in-the-Blank Practice

  1. The hardwareMap object is used to perform a __________ in the robot's configuration registry.
  2. The get() method requires two parameters: the __________ of the device and the exact string name from the configuration.
  3. Calling get() for a device that is not present in the active configuration will result in an __________ during OpMode initialization.
Show answers
  1. lookup (configuration registry search)
  2. class type (e.g., DcMotor.class)
  3. exception. Use tryGet() when a missing device should instead produce null.

Simulator Challenge

Use the simulator below to practice mapping a single motor from the Robot Controller configuration into Java.

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.
Connects to the mechanical track

The names you request here have to match the labels on the physical wiring.

See Lesson 9.1: The FTC Control System Layout and Port Map.

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.