Lesson 5.3: Using Comparison Operators to Protect Hardware Limits
Technical Context
Comparison operators allow the robot to interpret analog data — such as encoder ticks or sensor voltage — to enforce physical safety limits. Implementing "Soft Limits" via comparison logic prevents the robot from attempting to drive a mechanism past its physical hard-stop, which would otherwise result in stalled motors, stripped gears, or blown fuses on the Control Hub.
How Comparisons Create Safe Operating Zones
Hardware sensors return numerical values that must be compared against thresholds. Java provides several conditional operators for this purpose: == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to). In the case of an arm mechanism, the programmer compares the current DcMotor encoder position against a maximum "Safe-Zone" integer. If the position exceeds the limit, the logic overrides driver input to force the power to zero.
Annotated Code
@Override
public void loop() {
double liftPower = -1 * gamepad1.left_stick_y;
int currentPos = liftMotor.getCurrentPosition();
// Hardware Protection Logic: Max Height is 2000 ticks
if (currentPos > 2000 && liftPower > 0) {
// Overriding power to prevent mechanical over-extension
liftPower = 0.0;
telemetry.addData("Safety", "MAX LIMIT REACHED");
}
liftMotor.setPower(liftPower);
}
Fill-in-the-Blank Practice
- To test if two numerical hardware values are exactly equal, the
__________operator must be used. - The
__________operator is used to check if a sensor value is "not equal" to a specific constant. - A common error is accidentally using the single
=operator, which is for__________, instead of the double==operator.
Show answers
==!=- assignment
Template Challenge
Robot Scenario: Your team is using a Distance Sensor to prevent the robot from hitting walls. If the distance reading is less than 10.0 cm, the motor power should be set to 0.0 immediately.
Show answer
@Override
public void loop() {
double dist = distanceSensor.getDistance(DistanceUnit.CM);
if (dist < 10.0) {
driveMotor.setPower(0.0);
} else {
driveMotor.setPower(-1 * gamepad1.left_stick_y);
}
}
Ready to move on?
Sign in with Google to save your progress with Telemark, or continue without saving.