HardwareMap
Connect your Java code to the physical robot hardware.
The HardwareMap connects your code to the physical devices on your robot.
Before your code can control a motor, servo, or sensor, it must get a reference to that device from the HardwareMap.
Robot Configuration Names
Every piece of hardware on your robot has a configuration name.
Example:
leftMotor
rightMotor
clawServo
imuYour code must use the exact same name.
Getting Hardware
Hardware is retrieved using:
hardwareMap.get()The format is:
deviceType variable = hardwareMap.get(deviceType.class, "name");Example:
DcMotor motor;
motor = hardwareMap.get(DcMotor.class, "leftMotor");Mapping Variables
Usually, hardware is initialized at the beginning of your OpMode.
@Override
public void runOpMode() {
DcMotor leftMotor;
leftMotor = hardwareMap.get(DcMotor.class, "leftMotor");
}Common Hardware Types
Motors
DcMotor motor;Servos
Servo servo;Sensors
DistanceSensor sensor;Common Errors
Wrong Device Name
Example configuration:
left_driveCode:
hardwareMap.get(DcMotor.class, "leftMotor");The names do not match, causing an error.
Missing Hardware
If a device is not connected or configured, initialization will fail.
NullPointerException
A common mistake is using hardware before initializing it.
Bad:
DcMotor motor;
motor.setPower(1);The motor does not reference anything.
Good:
DcMotor motor;
motor = hardwareMap.get(DcMotor.class, "motor");
motor.setPower(1);Best Practices
- Use descriptive names.
- Keep configuration names consistent.
- Initialize hardware before using it.
- Avoid changing names during the season.
Key Takeaways
- HardwareMap connects software to hardware.
- Names must exactly match the robot configuration.
- Always initialize hardware before using it.
- Most hardware errors come from incorrect configuration.
Was this resource helpful?
