Lesson 3.5: Challenge — Build Dynamic Power Logic with Multiple Variables
Technical Context
A competitive robot rarely runs at a static speed. By combining multiple variables (inputs, multipliers, and offsets), you can create dynamic control systems. For instance, scaling joystick input by a "Battery Multiplier" variable ensures consistent robot performance even as the 12V battery voltage drops during a match.
How to Think Through Multi-Variable Power Logic
This challenge requires manipulating different variable types in a single expression. When an int and a double are used in the same math operation, Java "promotes" the result to a double to maintain precision. You must be careful with assignment operators; using = updates the variable's value, while using *= or += allows you to modify the current value relative to its previous state.
Annotated Code
@TeleOp(name="Dynamic_Control")
public class DynamicControl extends OpMode {
DcMotor drive;
// Multi-variable setup
double baseSpeed = 0.5;
double boostMultiplier = 2.0;
int gearSetting = 1;
@Override
public void init() {
drive = hardwareMap.get(DcMotor.class, "motor");
}
@Override
public void loop() {
double finalPower;
if (gamepad1.right_trigger > 0.5) {
// Math combining double inputs and multipliers
finalPower = baseSpeed * boostMultiplier;
} else {
finalPower = baseSpeed;
}
drive.setPower(finalPower);
telemetry.addData("Gear", gearSetting);
telemetry.addData("Output Power", finalPower);
}
}
Fill-in-the-Blank Practice
- The operator used to assign a new value to a variable is
__________. - If you multiply an
intby adouble, the resulting datatype will be a/an__________. - To perform math and update a variable in one step (e.g., adding 5 to the current value), you use the
__________operator.
Show answers
=double(Java promotes the result to the wider type)+=
Template Challenge
Robot Scenario: Create a "Precision Mode." If the driver holds the left bumper, the motor power should be exactly half of the joystick input. Use variables for input, scaleFactor, and finalPower.
Show answer
@Override
public void loop() {
double input = -gamepad1.left_stick_y;
double finalPower;
if (gamepad1.left_bumper) {
finalPower = input * scaleFactor;
} else {
finalPower = input;
}
motor.setPower(finalPower);
}
Ready to move on?
Sign in with Google to save your progress with Telemark, or continue without saving.