Skip to documentation
Documentation / Programming

Shared hardware map

Access the current HardwareMap from any subsystem without passing it through constructors.

GlobalObjects.hardwareMapEverywhere

Import GlobalObjects and use the field directly, or use a static import for shorter code. CubicOpMode assigns the SDK HardwareMap before createLocalizer() and configure().

Java · inside a subsystem constructed in configure()
import dev.cubiclib.ftc.GlobalObjects;

DcMotor motor = GlobalObjects.hardwareMapEverywhere
    .get(DcMotor.class, "intake");

Use a static import

This works in a standalone subsystem class, not just an inner class of the OpMode. Import DcMotor from com.qualcomm.robotcore.hardware.

Java · subsystem field
import static dev.cubiclib.ftc.GlobalObjects.hardwareMapEverywhere;

private final DcMotor motor =
    hardwareMapEverywhere.get(DcMotor.class, "intake");

Construct subsystems during configure()

Create subsystems inside configure(), after the global map is set. Do not construct them in static initializers, static singletons, or OpMode field initializers. Recreate them for each OpMode; do not cache hardware between runs.

The field is available through onStopped(), command end callbacks and subsystem stop(). It is reset to null after cleanup, including failed initialization and loop errors. Treat the public field as read-only; do not assign it yourself.

Automatic binding requires CubicOpMode. An ordinary SDK OpMode does not populate it. For an ordinary OpMode, pass that OpMode’s hardwareMap into your subsystems explicitly. Global access does not make hardware thread-safe; use it on the OpMode thread.

Get a clear error outside the lifecycle

requireHardwareMap() returns the same map, but throws a descriptive IllegalStateException if accessed before Init or after Stop. Direct field access outside its lifecycle returns null.

Java
DcMotor motor = GlobalObjects.requireHardwareMap()
    .get(DcMotor.class, "intake");