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
- 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
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.
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.