Lesson 13.7: Build MotorMechanism.java and Lift.java
The lift shares basic motor setup with other powered mechanisms. Put that shared behavior in MotorMechanism, then let Lift add encoder targets and lift-specific stopping.
The Parent Class
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.HardwareMap;
public class MotorMechanism {
protected DcMotor motor;
public void init(HardwareMap hardwareMap, String name) {
motor = hardwareMap.get(DcMotor.class, name);
motor.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
stop();
}
public void stop() {
motor.setPower(0.0);
}
}
protected allows Lift to use the motor while keeping unrelated OpMode code out.
The Lift Class
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.HardwareMap;
public class Lift extends MotorMechanism {
public void init(HardwareMap hardwareMap) {
super.init(hardwareMap, RobotConfig.LIFT_NAME);
motor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
motor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
}
public void moveToScore() {
motor.setTargetPosition(RobotConfig.LIFT_SCORE_TICKS);
motor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
motor.setPower(RobotConfig.LIFT_POWER);
}
public boolean isAtTarget() {
return !motor.isBusy();
}
public void update() {
if (isAtTarget()) stop();
}
@Override
public void stop() {
motor.setPower(0.0);
}
}
moveToScore() starts motion and returns. It does not wait in a loop. The OpMode can continue updating its drivetrain, vision, and intake while the lift moves.
update() checks progress during each OpMode cycle. This same non-blocking contract will matter in the final autonomous state machine.
Project Practice
Complete the lift target command and verify the inheritance, override, and status methods.
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.