-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeleopIterative.java
More file actions
268 lines (231 loc) · 10.2 KB
/
TeleopIterative.java
File metadata and controls
268 lines (231 loc) · 10.2 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* Copyright (c) 2017 FIRST. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted (subject to the limitations in the disclaimer below) provided that
* the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* Neither the name of FIRST nor the names of its contributors may be used to endorse or
* promote products derived from this software without specific prior written permission.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
* LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.eventloop.opmode.OpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
import com.qualcomm.robotcore.hardware.Gamepad;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.util.ElapsedTime;
import com.qualcomm.robotcore.util.Range;
@TeleOp(name="Iterative OpMode", group="Teleop")
public class TeleopIterative extends OpMode
{
// Declare OpMode members.
private ElapsedTime runtime = new ElapsedTime();
//note that the motors are now all in a single row
private DcMotor blDrive = null;
private DcMotor brDrive = null;
private DcMotor flDrive = null;
private DcMotor frDrive = null;
private DcMotor intake = null;
private DcMotor linearSlide = null;//Tesrng btiyuc
private DcMotor carousel = null;
private Servo arm = null;
private int zero = 0; //linear slide encoders
private final int MAX = 5000;
private final int MIN = 0;
private int currentMax = MAX;
private int currentMin = MIN;
private int lastPos = 0;
private int stageTwo = FieldMeasurements.getStageHeight(1);
private int stageOne = FieldMeasurements.getStageHeight(2);
private int stageThree = FieldMeasurements.getStageHeight(3);
DcMotorSimple.Direction motF = DcMotorSimple.Direction.FORWARD;
DcMotorSimple.Direction motR = DcMotorSimple.Direction.REVERSE;
Servo.Direction serR = Servo.Direction.REVERSE;
Servo.Direction serF = Servo.Direction.FORWARD;
/*
* Code to run ONCE when the driver hits INIT
*/
@Override
public void init() {
telemetry.addData("Status", "Initialized");
this.blDrive = hardwareMap.get(DcMotor.class, "blDrive");
this.brDrive = hardwareMap.get(DcMotor.class, "brDrive");
this.flDrive = hardwareMap.get(DcMotor.class, "flDrive");
this.frDrive = hardwareMap.get(DcMotor.class, "frDrive");
this.intake = hardwareMap.get(DcMotor.class, "intake");
this.linearSlide = hardwareMap.get(DcMotor.class, "linearSlide");
this.carousel = hardwareMap.get(DcMotor.class, "carousel");
this.arm = hardwareMap.get(Servo.class, "arm");
//intake.resetDeviceConfigurationForOpMode();
this.blDrive.setDirection(motF);
this.brDrive.setDirection(motR);
this.flDrive.setDirection(motF);
this.frDrive.setDirection(motR);
this.linearSlide.setDirection(motR);
this.arm.setDirection(serR);
this.linearSlide.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
this.linearSlide.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
this.linearSlide.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
this.flDrive.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
this.frDrive.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
this.blDrive.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
this.brDrive.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
}
/*
* Code to run REPEATEDLY after the driver hits INIT, but before they hit PLAY
*/
@Override
public void init_loop() {
}
/*
* Code to run ONCE when the driver hits PLAY
*/
@Override
public void start() {
runtime.reset();
}
/*
* Code to run REPEATEDLY after the driver hits PLAY but before they hit STOP
*/
@Override
public void loop() {
double drive = (-gamepad1.left_stick_y);
double strafe = (gamepad1.left_stick_x);
double turn = (gamepad1.right_stick_x);
//drive measures forward and backwards - is unchanged for all motors
//strafe is subtracted from the front right and the back left
//turn needs to affect the right if the stick is moved right, left if moved left
//right stick right is positive
//so subtract turn from the right, and add to left
double frontLeftPower = (drive + strafe + turn);
double frontRightPower = (drive - strafe - turn);
double backLeftPower = (drive - strafe + turn);
double backRightPower = (drive + strafe - turn);
//to slow down the drive if needed
double slowDown = gamepad1.left_bumper ? 8.0 : 1.0;
slowDown = gamepad1.right_bumper ? 2.0 : slowDown;
flDrive.setPower((frontLeftPower)/(slowDown));
blDrive.setPower((backLeftPower)/(slowDown));
frDrive.setPower((frontRightPower)/(slowDown));
brDrive.setPower((backRightPower)/(slowDown));
intake(gamepad2);
linearPower(gamepad2);
armPower(gamepad2);
carouselPower(gamepad1);
// Show the elapsed game time and wheel power.
telemetry.addData("Status", "Run Time: " + runtime.toString());
telemetry.addData("Motors",
"back left (%.2f), back right (%.2f), front left (%.2f), front right (%.2f)",
(backLeftPower)/(slowDown), (backRightPower)/(slowDown), (frontLeftPower)/(slowDown), (frontRightPower)/(slowDown));
telemetry.addData("Game Pad 1: ", gamepad1.left_stick_y + " " +gamepad1.left_stick_x+ " "
+gamepad1.right_stick_x+ " " +gamepad1.right_stick_y + "\n\n");
telemetry.addData("Right bumper GP1: ", gamepad1.right_bumper);
telemetry.addData("Left bumper GP1: ", gamepad1.left_bumper);
telemetry.addData("GP2 a: ", gamepad2.a);
telemetry.addData("GP2 b: ", gamepad2.b);
telemetry.addData("GP2 y: ", gamepad2.y);
telemetry.addData("Intake: ", intake.getCurrentPosition() + "\n");
telemetry.addData("LinearSlide: ", +linearSlide.getPower() + "\n");
telemetry.addData("LinearSlide Encoder: ", +getSlideEncoder() + "\n");
if (isMax()) telemetry.addData("LINEAR SLIDE ERROR", "MAX REACHED");
if (isMin()) telemetry.addData("LINEAR SLIDE ERROR", "MIN REACHED");
telemetry.addData("Carousel: ", carousel.getPower() + "\n");
telemetry.addData("Current Max: ", currentMax);
telemetry.update();
}
/*
* Code to run ONCE after the driver hits STOP
*/
@Override
public void stop() {
}
//GamePad 2 Methods
public void intake(Gamepad gp) { //negative is push out || positive is take in
if (gp.right_stick_y != 0)
intake.setPower(gp.right_stick_y);
else
intake.setPower(0);
}
public void armPower(Gamepad gp) { //right is raise || left is lower
if (gp.right_bumper)
arm.setPosition(0.45);
else if (gp.left_bumper)
arm.setPosition(0.55);
else
arm.setPosition(0.5);
}
public boolean isMin(){
return getSlideEncoder() <= MIN;
}
public boolean isMax(){
return getSlideEncoder() >= MAX;
}
public int getSlideEncoder(){ //the "max" and "min" are too counterintuitive
return -1*linearSlide.getCurrentPosition();
}
public void linearPower(Gamepad gp){ //dynamically will set a "max" - stage one, two or three, or above all stages
if (gp.y)
currentMax = stageThree;
else if(gp.b)
currentMax = stageTwo;
else if (gp.a)
currentMax = stageOne;
else{
currentMax = MAX;
}
// if (getSlideEncoder() > currentMax || getSlideEncoder() < currentMin) {
//
// }
//else {
if (gp.left_stick_y == 0) {
// double offsetPow;
// if (getSlideEncoder() < lastPos) {
// offsetPow = lastPos - getSlideEncoder() * 0.0015;
// } else if (getSlideEncoder() > lastPos) {
// offsetPow = lastPos - getSlideEncoder() * 0.0005;
// } else {
// offsetPow = 0;
// }
linearSlide.setPower(0);
}
else {
if(getSlideEncoder() >= currentMax && gp.left_stick_y < 0)
linearSlide.setPower(0);
else if (isMin() && gp.left_stick_y > 0)
linearSlide.setPower(0);
else
linearSlide.setPower(gp.left_stick_y);
}
// }
lastPos = getSlideEncoder();
}
public void carouselPower(Gamepad gp) { //Blue - Left Trigger || Red - Right Trigger
if(gp.right_trigger > 0)
carousel.setPower(-(gp.right_trigger)/1);
else if(gp.left_trigger > 0)
carousel.setPower((gp.left_trigger)/1);
else
carousel.setPower(zero);
}
}