Hardware Access - addonovan/ftc-ext GitHub Wiki

At least for me, the life cycle for the Qualcomm OpModes were a little odd. First, your OpMode is created, then you must wait until init() is called to even access hardwareMap because it is null before then, forcing all of your hardware objects to have separate declaration and instantiation. This reads rather poorly and also opens the OpMode up to NullPointerExceptions if you forgot to assign the variable during the init or if the hardware configuration was done incorrectly.

The life cycle for ftcext's OpModes is much simpler. The moment that your OpMode is created, you already have access to hardwareMap via getHardwareMap() (or simply hardwareMap in Kotlin), along with the telemetry and gamepads (accessible via getTelemetry(), getGamepad1(), and getGamepad2() respectively, or telemetry, gamepad1, and gamepad2 in Kotlin). This means you can immediately assign values to your hardware devices (motors, sensors, etc.) the moment that they are declared.

In order to get hardware devices, like motors and servos, there are some simple methods that are available to call, each is the equivalent of just doing getHardwareMap().{map_name}.get( "foo" ) (Kotlin: hardwareMap.{map_name}[ "foo" ]), however, they remove the "hardwareMap" and "get" parts, making the calls much shorter. The available methods are generally just their data types; however, there are a few exceptions, listed below.

DcMotorControllers are accessed via the motorController( String ) function, DcMotors by the motor( String ) function, IrSeekerSensors by irSensor( String ), AnalogInputs by analogIn( String ), and AnalogOutputs by analogOut( String ). The most notable part is the lack of the "dc" prefix in the motor function and the complete omission of Seeker from the irSensor function.

Here are some examples:
Java

private final DcMotor motor_left = motor( "left_motor" );
private final DcMotor motor_right = motor( "right_motor" );
private final AccelerationSensor sensor_acceleration = accelerationSensor( "acceleration" );

Kotlin

private val motor_left = motor( "left_motor" );
private val motor_right = motor( "right_motor" );
private val sensor_acceleration = accelerationSensor( "acceleration" );

Also available for Kotlin users in the inline-function getDevice< T : HardwareMap >( name: String ), which is useful if you dislike needing to write different functions. Due to Kotlin's type inference abilities, if the type of the variable is already declared, the type parameter can be dropped from the method call. For instance, the two below statements are identical when compiled.

private val motor_left = getDevice< DcMotor >( "left_motor" );
private val motor_left: DcMotor = getDevice( "left_motor" );

Using this method ends up being more versatile as you head into [advanced hardware types](Advanced Hardware).

⚠️ **GitHub.com Fallback** ⚠️