Lesson 5.5: Challenge — Build Intake Logic for Multi-Stage Game Piece Sorting
Technical Context
A "Logic Tree" is a complex arrangement of decisions that allows the robot to handle variable field conditions autonomously. This prevents failures in game element management by creating specific behaviors for different scenarios — such as varying motor power based on whether a Color Sensor detects a friendly or opposing alliance element.
How to Design a Multi-Stage Intake Decision Tree
This challenge requires nesting if/else statements within each other, or using a complex else if chain, to manage a hardware subsystem. The goal is to implement a state-dependent controller. For example, the intake mechanism should run at 100% if empty, 50% if one item is detected, and 0% (Stop) if the distance sensor indicates the intake is full. By structuring this logic correctly, you minimize the driver's cognitive load and maximize the robot's cycle efficiency.
Annotated Code
@Override
public void loop() {
double dist = board.getDistance(DistanceUnit.CM);
int redValue = board.getAmountRed();
if (dist < 5.0) {
// Logic Level 1: Intake is full — stop immediately
intake.setPower(0.0);
telemetry.addData("Intake", "FULL - STOPPED");
} else if (redValue > 200) {
// Logic Level 2: Wrong alliance element detected — eject
intake.setPower(-1.0);
telemetry.addData("Intake", "WRONG ELEMENT - EJECTING");
} else {
// Logic Level 3: Ready to intake — follow driver input
intake.setPower(gamepad1.right_trigger);
telemetry.addData("Intake", "READY");
}
}
Fill-in-the-Blank Practice
- When one
ifstatement is placed inside anotherifstatement's block, it is referred to as a/an__________conditional. - Building a multi-stage logic tree helps robots handle variable conditions in the
__________phase of a match. - A well-designed logic tree ensures that the
setPower()method always receives a/an__________value between-1.0and1.0.
Show answers
- nested
- autonomous
- valid (unambiguous / single)
Template Challenge
Robot Scenario: Create a sorting logic. If the Color Sensor sees Blue (blue() > 100), set Servo 1 to 0.0. If it sees Red (red() > 100), set Servo 1 to 1.0. If it sees neither, set Servo 1 to 0.5.
Show answer
@Override
public void loop() {
if (sensor.blue() > 100) {
sorter.setPosition(0.0);
} else if (sensor.red() > 100) {
sorter.setPosition(1.0);
} else {
sorter.setPosition(0.5);
}
}
Ready to move on?
Sign in with Google to save your progress with Telemark, or continue without saving.