-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeleop.java
241 lines (205 loc) · 8.3 KB
/
Teleop.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
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
package org.firstinspires.ftc.teamcode;
import static org.firstinspires.ftc.robotcore.external.BlocksOpModeCompanion.gamepad1;
import static org.firstinspires.ftc.robotcore.external.BlocksOpModeCompanion.gamepad2;
import static org.firstinspires.ftc.robotcore.external.BlocksOpModeCompanion.hardwareMap;
import static org.firstinspires.ftc.robotcore.external.BlocksOpModeCompanion.opMode;
import com.acmerobotics.dashboard.config.Config;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.CRServo;
import com.qualcomm.robotcore.hardware.DcMotorEx;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
import com.qualcomm.robotcore.hardware.Gamepad;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.hardware.TouchSensor;
import com.qualcomm.robotcore.hardware.HardwareMap;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.util.ElapsedTime;
@TeleOp(name = "TeleOp")
@Config
/*
Action: Controls: Gamepad ID:
General Movement ---- Left Stick & Right Stick [gamepad 1]
Linear Slide -------- Right Bumper (up) & Left Bumper (down) [gamepad 2]
Intake -------------- Toggled; B (reverse) & X (forward) [gamepad 2]
Roller Height ------- Right Trigger (down) & Left Trigger (up) [gamepad 2]
Outtake ------------- A [gamepad 2]
Drone --------------- DPad Left [gamepad 1]
Hanging ------------- DPad Up and Down [gamepad 2]
*/
public class Teleop extends LinearOpMode {
// Configurations
public static int SLIDE_RESET_TIME = 800;
public static double SLIDE_POW = 0.65;
public static double POW_MULTIPLIER = 0.75;
public static double SLOW_MULTIPLIER = 0.3;
public static double HANG_POW = 0.5;
//RESET used to be .15
public static double RESET = 0.15;
// Mecanum Wheels
public DcMotor leftBack;
public DcMotor leftFront;
public DcMotor rightBack;
public DcMotor rightFront;
// Intake to Outtake
public TouchSensor touch;
public DcMotor linearSlide;
public DcMotor intakeRoller;
public Servo outtake;
//Miscellaneous
public Servo drone;
public Servo liftRelease;
public DcMotor lift;
// Counters
public int countSlow = 0;
public int countIntake = 1;
public int countHang = 0;
public void runOpMode() {
// Initialize Mecanum wheels
leftFront = hardwareMap.get(DcMotor.class, "leftFront");
leftBack = hardwareMap.get(DcMotor.class, "leftBack");
rightBack = hardwareMap.get(DcMotor.class, "rightBack");
rightFront = hardwareMap.get(DcMotor.class, "rightFront");
leftFront.setDirection(DcMotor.Direction.REVERSE);
rightBack.setDirection(DcMotor.Direction.REVERSE);
// Initialize other motors and servos
intakeRoller = hardwareMap.get(DcMotor.class, "intake");
touch = hardwareMap.touchSensor.get("touchSensor");
linearSlide = hardwareMap.get(DcMotor.class, "linearSlide");
outtake = hardwareMap.get(Servo.class, "yury");
drone = hardwareMap.get(Servo.class, "drone");
lift = hardwareMap.get(DcMotor.class, "kent");
liftRelease = hardwareMap.get(Servo.class, "liftRelease");
// Reset initial servo positions
liftRelease.setPosition(1);
drone.setPosition(1);
outtake.setPosition(1);
lift.setPower(0);
waitForStart();
if (isStopRequested()) {
return;
}
double hang_power = 0.1;
while (opModeIsActive()) {
// GAMEPAD 1 ACTIONS *****************************
// Mecanum wheels
move();
// Release drone
if (gamepad1.dpad_left) {
drone.setPosition(0);
}
// GAMEPAD 2 ACTIONS ****************************
// Turn intake on
intakeRoller.setPower(gamepad2.right_trigger);
// Reverse roller direction
if (gamepad2.x) {
intakeRoller.setPower(-1);
}
// Rotate outtake servo
if (gamepad2.a) {
outtake.setPosition(0);
sleep(200);
outtake.setPosition(1);
sleep(200);
}
// Hanging mechanism
if (gamepad2.dpad_up) {
countHang++;
while (gamepad2.dpad_up) {
lift.setPower(HANG_POW);
}
}
if (gamepad2.dpad_down) {
countHang = 0;
lift.setPower(-0.1);
while (gamepad2.dpad_down) {
lift.setPower(-0.3);
}
}
if(gamepad2.dpad_right){
liftRelease.setPosition(.5);
}
if (countHang != 0) { // Keep suspended in air
lift.setPower(0.1); // 0.25 what is minimum power to keep suspended??
}
else {
lift.setPower(0);
}
// Raise the cascading slide
if (gamepad2.right_bumper) {
while (!touch.isPressed()) {
linearSlide.setPower(SLIDE_POW);
move();
if (gamepad2.y) {
break;
}
}
while (touch.isPressed()) {
linearSlide.setPower(-.1);
if (gamepad2.y) {
break;
}
}
linearSlide.setPower(0);
}
// Lower the cascading slide
if (gamepad2.left_bumper) {
while (!touch.isPressed()) {
linearSlide.setPower(-.4);
move();
if (gamepad2.y) {
break;
}
}
}
while (touch.isPressed()) {
linearSlide.setPower(RESET);
if (gamepad2.y) {
break;
}
}
linearSlide.setPower(0);
}
}
private void move() {
double max;
// POV Mode uses left joystick to go forward & strafe, and right joystick to rotate.
double axial = -gamepad1.left_stick_y; // Note: pushing stick forward gives negative value
double lateral = gamepad1.left_stick_x;
double yaw = gamepad1.right_stick_x;
// Combine the joystick requests for each axis-motion to determine each wheel's power.
// Set up a variable for each drive wheel to save the power level for telemetry.
double leftFrontPower = axial + lateral + yaw;
double rightFrontPower = axial - lateral - yaw;
double leftBackPower = axial - lateral + yaw;
double rightBackPower = axial + lateral - yaw;
// Normalize the values so no wheel power exceeds 100%
// This ensures that the robot maintains the desired motion.
max = Math.max(Math.abs(leftFrontPower), Math.abs(rightFrontPower));
max = Math.max(max, Math.abs(leftBackPower));
max = Math.max(max, Math.abs(rightBackPower));
if (max > 1.0) {
leftFrontPower /= max;
rightFrontPower /= max;
leftBackPower /= max;
rightBackPower /= max;
}
// Send calculated power to wheels
if (countSlow % 2 == 0) {
leftFront.setPower(leftFrontPower * POW_MULTIPLIER);
rightFront.setPower(rightFrontPower * POW_MULTIPLIER);
leftBack.setPower(leftBackPower * POW_MULTIPLIER);
rightBack.setPower(rightBackPower * POW_MULTIPLIER);
}
else {
leftFront.setPower(leftFrontPower * SLOW_MULTIPLIER);
rightFront.setPower(rightFrontPower * SLOW_MULTIPLIER);
leftBack.setPower(leftBackPower * SLOW_MULTIPLIER);
rightBack.setPower(rightBackPower * SLOW_MULTIPLIER);
}
if (gamepad1.a) {
countSlow++;
sleep(300);
}
}
}