Time-Based Movement (OnBot Java)
What is Time-Based Movement?
Time-based movement is the simplest way to make your robot move autonomously: you set the motor power, wait for a certain amount of time, then stop the motors. This method does not use sensors or encoders, so it is easy to implement but not very precise.
Implementing Time-Based Movement
To implement time-based movement in OnBot Java, you use the
setPower() method to start the motors, then sleep() to wait for a set duration, and finally stop the motors. Always use waitForStart() before starting any movement.Time-Based Movement Example
@Autonomous(name="TimeBasedMove")
public class TimeBasedMove extends LinearOpMode {
private DcMotor leftDrive;
private DcMotor rightDrive;
@Override
public void runOpMode() {
leftDrive = hardwareMap.get(DcMotor.class, "left_drive");
rightDrive = hardwareMap.get(DcMotor.class, "right_drive");
telemetry.addData("Status", "Initialized");
telemetry.update();
waitForStart();
leftDrive.setPower(0.5);
rightDrive.setPower(0.5);
sleep(1000); // Move forward for 1 second
leftDrive.setPower(0);
rightDrive.setPower(0);
telemetry.addData("Status", "Stopped");
telemetry.update();
}
}Limitations and Use Cases
Time-based movement is easy to implement but not very accurate. The robot's movement can vary due to battery voltage, floor friction, or motor inconsistencies. Use this method for simple tasks or when precision is not critical. For more reliable movement, consider using encoders or sensors.
Safety and Best Practices
- Always stop your motors after movement to prevent damage.
- Use telemetry to monitor robot status.
- Test your code in a safe environment before running on the field.
- Avoid running motors at full power for long periods.
Practice Exercise: Time-Based Turn
Write an autonomous OpMode that turns the robot in place for 0.5 seconds, then stops. Use telemetry to display status before, during, and after the turn.
- Map both left and right drive motors.
- Set one motor forward and the other backward to turn.
- Use sleep() to control the turn duration.
- Stop both motors and update telemetry.