-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutonomousCodeTest.java
94 lines (74 loc) · 3.8 KB
/
AutonomousCodeTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
import com.qualcomm.robotcore.util.ElapsedTime;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.IMU;
import com.qualcomm.robotcore.util.ElapsedTime;
@Autonomous(name = "Autonomous_Test")
public class AutonomousCodeTest extends LinearOpMode {
private DcMotor leftFrontDrive = null;
private DcMotor leftBackDrive = null;
private DcMotor rightFrontDrive = null;
private DcMotor rightBackDrive = null;
//Convert from the counts per revolution of the encoder to counts per inch
static final double HD_COUNTS_PER_REV = 28;
static final double DRIVE_GEAR_REDUCTION = 19.2;
static final double WHEEL_CIRCUMFERENCE_MM = 90 * Math.PI;
static final double DRIVE_COUNTS_PER_MM = (HD_COUNTS_PER_REV * DRIVE_GEAR_REDUCTION) / WHEEL_CIRCUMFERENCE_MM;
static final double DRIVE_COUNTS_PER_IN = DRIVE_COUNTS_PER_MM * 25.4;
//Create elapsed time variable and an instance of elapsed time
private ElapsedTime runtime = new ElapsedTime();
// Drive function with 3 parameters
private void drive(double power, double leftInches, double rightInches) {
int rightTarget;
int leftTarget;
if (opModeIsActive()) {
// Create target positions
rightTarget = ((rightBackDrive.getCurrentPosition() + rightFrontDrive.getCurrentPosition())/2) + (int) (rightInches * DRIVE_COUNTS_PER_IN);
leftTarget = ((leftBackDrive.getCurrentPosition() + leftFrontDrive.getCurrentPosition())/2) + (int) (leftInches * DRIVE_COUNTS_PER_IN);
// set target position
leftFrontDrive.setTargetPosition(leftTarget);
leftBackDrive.setTargetPosition(leftTarget);
rightFrontDrive.setTargetPosition(rightTarget);
rightBackDrive.setTargetPosition(rightTarget);
//switch to run to position mode
leftBackDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
leftFrontDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightBackDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightFrontDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
//run to position at the desiginated power
leftFrontDrive.setPower(power);
leftBackDrive.setPower(power);
rightFrontDrive.setPower(power);
rightBackDrive.setPower(power);
// wait until both motors are no longer busy running to position
while (opModeIsActive() && (leftBackDrive.isBusy() || rightBackDrive.isBusy())) {
}
// set motor power back to 0
leftBackDrive.setPower(0);
leftFrontDrive.setPower(0);
rightBackDrive.setPower(0);
rightFrontDrive.setPower(0);
}
}
@Override
public void runOpMode() {
rightBackDrive = hardwareMap.get(DcMotor.class, "RR");
rightFrontDrive = hardwareMap.get(DcMotor.class, "RF");
leftBackDrive = hardwareMap.get(DcMotor.class, "LR");
leftFrontDrive = hardwareMap.get(DcMotor.class, "LF");
leftFrontDrive.setDirection(DcMotorSimple.Direction.REVERSE);
leftBackDrive.setDirection(DcMotorSimple.Direction.REVERSE);
waitForStart();
if (opModeIsActive()) {
runtime.reset(); // reset elapsed time timer
//segment 1
drive(0.1, 10, 0);
}
}
}