Skip to main content

Lesson 3.2: Using double for Motor Power and Precise Math


Technical Context

Motor voltage control requires high-precision values to achieve smooth acceleration. Using the double datatype allows the robot to handle the fractional values (-1.0 to 1.0) required by the setPower() method, preventing the jerky movement or rounding errors associated with lower-precision types.


Why FTC Code Uses double So Often

The double is a 64-bit primitive datatype used for floating-point numbers. In the FTC SDK, methods such as setPower() accept a double, and Java treats decimal literals such as 0.5 as doubles by default. Using a double for joystick values, sensor measurements, and power calculations preserves fractional values that an int would discard.


Annotated Code

@TeleOp(name="Precision_Power")
public class PrecisionPower extends OpMode {

DcMotor drive;

// Initializing a double variable for fine-grained speed control
double targetSpeed = 0.35;

@Override
public void init() {
drive = hardwareMap.get(DcMotor.class, "motor");
}

@Override
public void loop() {
// The SDK setPower method receives the double variable
drive.setPower(targetSpeed);
telemetry.addData("Current Power", targetSpeed);
}
}

Fill-in-the-Blank Practice

  1. To store a number with a decimal point for motor power, you should use the __________ datatype.
  2. The double datatype occupies __________ bits of memory, making it more accurate than a float.
  3. The valid range of values for a motor's setPower() method is from __________ to 1.0.
Show answers
  1. double
  2. 64
  3. -1.0

Simulator Challenge

Robot Scenario: Your driver wants a specific "Auton Speed" of 0.45. Create a double variable to store this and apply it to a motor.

Telemark Unit 3 Simulator
Tracks `String`, `double`, `boolean`, and `int` variables as you edit.
Supports telemetry, `gamepad1`, simple `if` blocks, and basic `setPower()` math.
Does not simulate full hardware behavior or complex Java control flow.
Show answer
@Override
public void loop() {
double autonSpeed = 0.45;
motor.setPower(autonSpeed);
}

Ready to move on?

Sign in with Google to save your progress with Telemark, or continue without saving.

Stuck on this lesson?

Ask about anything on this page. It can see which lesson you have open and which part you are reading.