forked from FIRST-Tech-Challenge/FtcRobotController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwoFeetAutoBasketProgramBase.java
More file actions
240 lines (205 loc) · 9.18 KB
/
twoFeetAutoBasketProgramBase.java
File metadata and controls
240 lines (205 loc) · 9.18 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package org.firstinspires.ftc.teamcode;
import com.qualcomm.hardware.rev.RevHubOrientationOnRobot;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.hardware.DcMotorEx;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.IMU;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.util.ElapsedTime;
import com.qualcomm.hardware.bosch.BNO055IMU;
import com.qualcomm.robotcore.hardware.DistanceSensor;
import org.firstinspires.ftc.robotcore.external.navigation.DistanceUnit;
@Autonomous(name = "twoFeetAutoBasketProgram")
public class twoFeetAutoBasketProgramBase extends LinearOpMode {
private DcMotorEx frontLeft, frontRight, backLeft, backRight, leftLiftMotor, rightLiftMotor;
private Servo basketServo, intakePivotServo;
private static ElapsedTime myStopwatch = new ElapsedTime();
private static ElapsedTime basketTimer = new ElapsedTime();
private boolean noHighBasket = true;
private boolean isliftUp = false;
private int i = 0;
private boolean basket1stPos = false;
private boolean basket2ndPos = false;
private boolean reachedLeft = false;
private boolean reachedForward = false;
private boolean rotatedTarget = false;
private static final double wheelDiameter = 75;
private static final double ticksPerRev = 100;
private static final int targetRotation = 820;
private static final int forwardMovement = 400;
private static final int backwardMovement = 400;
private static final int leftwardMovement = 775;
public void basket() {
if (basket1stPos == false) {
basketServo.setPosition(1.0);
}
else if ((basketServo.getPosition() > 0.45) && (basket1stPos == false)) {
basket1stPos = true;
}
sleep(3000);
basketServo.setPosition(0.0);
if (basketServo.getPosition() < 0.075) {
basket2ndPos = true;
basket1stPos = true;
}
sleep(3000);
}
public void rotateRight() {
frontLeft.setTargetPosition(frontLeft.getCurrentPosition() + targetRotation);
frontRight.setTargetPosition(frontRight.getCurrentPosition() - targetRotation);
backLeft.setTargetPosition(backLeft.getCurrentPosition() - targetRotation);
backRight.setTargetPosition(backRight.getCurrentPosition() + targetRotation);
frontLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontLeft.setVelocity(1000);
frontRight.setVelocity(1000);
backLeft.setVelocity(1000);
backRight.setVelocity(1000);
if (frontLeft.getCurrentPosition() == targetRotation) {
rotatedTarget = true;
resetMotors();
}
}
public void strafeLeft() {
frontLeft.setTargetPosition(frontLeft.getCurrentPosition() - leftwardMovement);
frontRight.setTargetPosition(frontRight.getCurrentPosition() + leftwardMovement);
backLeft.setTargetPosition(backLeft.getCurrentPosition() + leftwardMovement);
backRight.setTargetPosition(backRight.getCurrentPosition() - leftwardMovement);
frontLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontLeft.setVelocity(1000);
frontRight.setVelocity(1000);
backLeft.setVelocity(1000);
backRight.setVelocity(1000);
if (frontLeft.getCurrentPosition() == leftwardMovement) {
reachedLeft = true;
resetMotors();
}
}
public void forward() {
frontLeft.setTargetPosition(frontLeft.getCurrentPosition() + forwardMovement);
frontRight.setTargetPosition(frontRight.getCurrentPosition() + forwardMovement);
backLeft.setTargetPosition(backLeft.getCurrentPosition() + forwardMovement);
backRight.setTargetPosition(backRight.getCurrentPosition() + forwardMovement);
frontLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontLeft.setVelocity(1000);
frontRight.setVelocity(1000);
backLeft.setVelocity(1000);
backRight.setVelocity(1000);
if (frontLeft.getCurrentPosition() == forwardMovement) {
reachedForward = true;
resetMotors();
}
}
public void backward() {
frontLeft.setTargetPosition(frontLeft.getCurrentPosition() - backwardMovement);
frontRight.setTargetPosition(frontRight.getCurrentPosition() - backwardMovement);
backLeft.setTargetPosition(backLeft.getCurrentPosition() - backwardMovement);
backRight.setTargetPosition(backRight.getCurrentPosition() - backwardMovement);
frontLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backLeft.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
backRight.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
frontLeft.setVelocity(1000);
frontRight.setVelocity(1000);
backLeft.setVelocity(1000);
backRight.setVelocity(1000);
}
public void setLiftPosition(int targetPosition) {
leftLiftMotor.setTargetPosition(-targetPosition);
rightLiftMotor.setTargetPosition(targetPosition);
leftLiftMotor.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
rightLiftMotor.setMode(DcMotorEx.RunMode.RUN_TO_POSITION);
leftLiftMotor.setVelocity(1000);
rightLiftMotor.setVelocity(1000);
telemetry.addData("Lift value:", rightLiftMotor.getCurrentPosition());
if (rightLiftMotor.getCurrentPosition() > 2890) {
isliftUp = true;
resetMotors();
}
}
private void highBasketProcess() {
if (isliftUp == false) {
setLiftPosition(3266); // low basket encoder positions
// telemetry.addData("Lift up", rightLiftMotor.getCurrentPosition());
// telemetry.update();
}
else if ((isliftUp == true) && (basket2ndPos == false)) {
basket();
// telemetry.addData("Basket", rightLiftMotor.getCurrentPosition());
// telemetry.update();
}
else if (basket2ndPos == true) {
i += 1;
if (i == 1) {
forward();
stopMotors();
}
setLiftPosition(0); // low basket encoder positions
}
else {
noHighBasket = false;
}
}
@Override
public void runOpMode() throws InterruptedException {
// Initialize the hardware
frontLeft = hardwareMap.get(DcMotorEx.class, "frontLeft");
frontRight = hardwareMap.get(DcMotorEx.class, "frontRight");
backLeft = hardwareMap.get(DcMotorEx.class, "backLeft");
backRight = hardwareMap.get(DcMotorEx.class, "backRight");
basketServo = hardwareMap.get(Servo.class, "basketServo");
intakePivotServo = hardwareMap.get(Servo.class, "intakePivotServo");
leftLiftMotor = hardwareMap.get(DcMotorEx.class, "leftLiftMotor");
rightLiftMotor = hardwareMap.get(DcMotorEx.class, "rightLiftMotor");
// Wait for the start signal
waitForStart();
frontLeft.setDirection(DcMotorEx.Direction.FORWARD);
frontRight.setDirection(DcMotorEx.Direction.REVERSE);
backLeft.setDirection(DcMotorEx.Direction.FORWARD);
backRight.setDirection(DcMotorEx.Direction.REVERSE);
resetMotors();
frontLeft.setMode(DcMotorEx.RunMode.RUN_USING_ENCODER);
frontRight.setMode(DcMotorEx.RunMode.RUN_USING_ENCODER);
backLeft.setMode(DcMotorEx.RunMode.RUN_USING_ENCODER);
backRight.setMode(DcMotorEx.RunMode.RUN_USING_ENCODER);
leftLiftMotor.setMode(DcMotorEx.RunMode.RUN_USING_ENCODER);
rightLiftMotor.setMode(DcMotorEx.RunMode.RUN_USING_ENCODER);
myStopwatch.reset();
while (reachedForward == false) {
forward();
}
while (reachedLeft == false) {
strafeLeft();
}
while (rotatedTarget == false) {
rotateRight();
}
intakePivotServo.setPosition(-.2);
while (noHighBasket) {
highBasketProcess();
}
stopMotors();
}
private void stopMotors() {
frontLeft.setPower(0);
frontRight.setPower(0);
backLeft.setPower(0);
backRight.setPower(0);
}
private void resetMotors() {
frontLeft.setMode(DcMotorEx.RunMode.STOP_AND_RESET_ENCODER);
frontRight.setMode(DcMotorEx.RunMode.STOP_AND_RESET_ENCODER);
backLeft.setMode(DcMotorEx.RunMode.STOP_AND_RESET_ENCODER);
backRight.setMode(DcMotorEx.RunMode.STOP_AND_RESET_ENCODER);
leftLiftMotor.setMode(DcMotorEx.RunMode.STOP_AND_RESET_ENCODER);
rightLiftMotor.setMode(DcMotorEx.RunMode.STOP_AND_RESET_ENCODER);
}
}