-
Notifications
You must be signed in to change notification settings - Fork 0
/
redLeftAutoTruss.java
143 lines (118 loc) · 6.01 KB
/
redLeftAutoTruss.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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.util.ElapsedTime;
@Autonomous(name="redLeftAutoTruss", group="Robot")
public class redLeftAutoTruss extends LinearOpMode {
private DcMotor frontLeft = null;
private DcMotor frontRight = null;
private DcMotor backLeft = null;
private DcMotor backRight = null;
private ElapsedTime runtime = new ElapsedTime();
static final double COUNTS_PER_MOTOR_REV = 537.7 ;
static final double DRIVE_GEAR_REDUCTION = 1.0 ;
static final double WHEEL_DIAMETER_INCHES = 4.0 ;
static final double COUNTS_PER_INCH = (COUNTS_PER_MOTOR_REV * DRIVE_GEAR_REDUCTION) /
(WHEEL_DIAMETER_INCHES * 3.14159);
static final double DRIVE_SPEED = 0.6;
static final double TURN_SPEED = 0.5;
@Override
public void runOpMode() {
frontLeft = hardwareMap.get(DcMotor.class, "frontLeft");
frontRight = hardwareMap.get(DcMotor.class, "frontRight");
backLeft = hardwareMap.get(DcMotor.class, "backLeft");
backRight = hardwareMap.get(DcMotor.class, "backRight");
frontLeft.setDirection(DcMotor.Direction.REVERSE);
frontRight.setDirection(DcMotor.Direction.FORWARD);
backLeft.setDirection(DcMotor.Direction.REVERSE);
backRight.setDirection(DcMotor.Direction.FORWARD);
frontLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
frontRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
backLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
backRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
frontLeft.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
frontRight.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
backLeft.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
backRight.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
telemetry.addData("Starting at", "%7d :%7d :%7d :%7d",
frontLeft.getCurrentPosition(),
frontRight.getCurrentPosition(),
backLeft.getCurrentPosition(),
backRight.getCurrentPosition());
telemetry.update();
waitForStart();
// Values are frontLeft, frontRight, backLeft, backRight. All are in inches, except turning which is in degrees.
// Green
forward(28);
backward(27);
turnRight(90);
forward(78);
telemetry.addData("Path", "Complete");
telemetry.update();
sleep(1000);
}
public void encoderDrive(double speed,
double frontLeftInches, double frontRightInches,
double backLeftInches, double backRightInches,
double timeoutS) {
int newFrontLeftTarget;
int newFrontRightTarget;
int newBackLeftTarget;
int newBackRightTarget;
if (opModeIsActive()) {
newFrontLeftTarget = frontLeft.getCurrentPosition() + (int)(frontLeftInches * COUNTS_PER_INCH);
newFrontRightTarget = frontRight.getCurrentPosition() + (int)(frontRightInches * COUNTS_PER_INCH);
newBackLeftTarget = backLeft.getCurrentPosition() + (int)(backLeftInches * COUNTS_PER_INCH);
newBackRightTarget = backRight.getCurrentPosition() + (int)(backRightInches * COUNTS_PER_INCH);
frontLeft.setTargetPosition(newFrontLeftTarget);
frontRight.setTargetPosition(newFrontRightTarget);
backLeft.setTargetPosition(newBackLeftTarget);
backRight.setTargetPosition(newBackRightTarget);
frontLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION);
frontRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
backLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION);
backRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
runtime.reset();
frontLeft.setPower(Math.abs(speed));
frontRight.setPower(Math.abs(speed));
backLeft.setPower(Math.abs(speed));
backRight.setPower(Math.abs(speed));
while (opModeIsActive() &&
(runtime.seconds() < timeoutS) &&
(frontLeft.isBusy() && frontRight.isBusy() && backLeft.isBusy() && backRight.isBusy())) {
telemetry.addData("Running to", " %7d :%7d :%7d :%7d", newFrontLeftTarget, newFrontRightTarget, newBackLeftTarget, newBackRightTarget);
telemetry.addData("Currently at", " at %7d :%7d :%7d :%7d",
frontLeft.getCurrentPosition(), frontRight.getCurrentPosition(), backLeft.getCurrentPosition(), backRight.getCurrentPosition());
telemetry.update();
}
frontLeft.setPower(0);
frontRight.setPower(0);
backLeft.setPower(0);
backRight.setPower(0);
frontLeft.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
frontRight.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
backLeft.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
backRight.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
sleep(250);
}
}
void forward(float distance){
encoderDrive(DRIVE_SPEED, distance, distance, distance, distance, 5.0);
}
void backward(float distance){
encoderDrive(DRIVE_SPEED, -distance, -distance, -distance, -distance, 5.0);
}
void strafeLeft(float distance){
encoderDrive(DRIVE_SPEED, -distance, distance, distance, -distance, 5.0);
}
void strafeRight(float distance){
encoderDrive(DRIVE_SPEED, distance, -distance, -distance, distance, 5.0);
}
void turnLeft(float angle){
encoderDrive(TURN_SPEED, -(angle/4.435), angle/4.435, -(angle/4.435), angle/4.435, 4.0);
}
void turnRight(float angle){
encoderDrive(TURN_SPEED, angle/4.435, -(angle/4.435), angle/4.435, -(angle/4.435), 4.0);
}
}