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 (decimals). In the FTC SDK, the setPower() method specifically expects a double parameter. While a float also handles decimals, it is only 32-bit; the JVM on the Control Hub defaults to double for maximum accuracy when calculating velocity or interpreting analog joystick data. Assigning a power level to a double variable allows you to perform math — such as halving the speed for a "Slow Mode" — without losing the decimal remainder.
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
- To store a number with a decimal point for motor power, you should use the
__________datatype. - The
doubledatatype occupies__________bits of memory, making it more accurate than afloat. - The valid range of values for a motor's
setPower()method is from__________to1.0.
Show answers
double- 64
- -1.0
Template 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.
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.