-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDriver.java
More file actions
210 lines (184 loc) · 9.09 KB
/
Copy pathDriver.java
File metadata and controls
210 lines (184 loc) · 9.09 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
/*
Copyright 2017
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS
IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.firstinspires.ftc.teamcode;
import org.firstinspires.ftc.teamcode.StateMachine;
import org.firstinspires.ftc.robotcore.external.navigation.Position;
import org.firstinspires.ftc.teamcode.StateMachine.State;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.GyroSensor;
import com.qualcomm.robotcore.hardware.DigitalChannel;
import com.qualcomm.robotcore.hardware.DistanceSensor;
import com.qualcomm.robotcore.hardware.ColorSensor;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
import com.qualcomm.robotcore.util.ElapsedTime;
@TeleOp
public class Driver extends LinearOpMode {
/**
* Calibrates gyro.
*/
public class CalibrateGyro implements StateMachine.State {
@Override
public void start() {
arm_gyro.calibrate();
body_gyro.calibrate();
}
@Override
public State update() {
if(arm_gyro.isCalibrating() || body_gyro.isCalibrating()){
return this;
} else {
return drive;
}
}
}
/**
* Controlls all the stuff.
*/
public class Drive implements StateMachine.State {
@Override
public void start() {
ball_arm.setPosition(0);
left_thumb.setPosition(0);
right_thumb.setPosition(1);
}
@Override
public State update() {
//sets speed values using ternary operator to enable the precision modes
speedDivisor = gamepad2.b?6:1.3;
thumbSpeed = gamepad2.a?0.005:0.03;
/*
//Go to first height
if(gamepad2.dpad_down){
if(arm_gyro.getHeading() < 10 - 5 || arm_gyro.getHeading() > 10 + 5){
arm_lift.setPower(Math.signum(arm_gyro.getHeading() - 10) * 0.25);
} else {
arm_lift.setPower(0);
}
}
//Go to second height
if(gamepad2.dpad_left){
if(arm_gyro.getHeading() < 50 - 5 || arm_gyro.getHeading() > 50 + 5){
arm_lift.setPower(Math.signum(arm_gyro.getHeading() - 50) * 0.25);
} else {
arm_lift.setPower(0);
}
}
//Go to third height
if(gamepad2.dpad_right){
if(arm_gyro.getHeading() < 75 - 5 || arm_gyro.getHeading() > 75 + 5){
arm_lift.setPower(Math.signum(arm_gyro.getHeading() - 75) * 0.25);
} else {
arm_lift.setPower(0);
}
}
//Go to fourth height
if(gamepad2.dpad_up){
if(arm_gyro.getHeading() < 95 - 5 || arm_gyro.getHeading() > 95 + 5){
arm_lift.setPower(Math.signum(arm_gyro.getHeading() - 95) * 0.25);
} else {
arm_lift.setPower(0);
}
}
*/
//opens and closes thumbs with buttons
if(gamepad1.right_trigger < 0.5){
if(gamepad1.right_bumper){
right_thumb.setPosition(right_thumb.getPosition() + thumbSpeed);
left_thumb.setPosition(left_thumb.getPosition() - thumbSpeed);
}
} else if (gamepad1.right_trigger > 0.5){
right_thumb.setPosition(right_thumb.getPosition() - thumbSpeed);
left_thumb.setPosition(left_thumb.getPosition() + thumbSpeed);
}
/*
//sets arm power if the second driver is not using one of the automatic heights
if(!gamepad2.dpad_down && !gamepad2.dpad_up && !gamepad2.dpad_left && !gamepad2.dpad_right){
if(gamepad1.right_stick_y < 0 && arm_gyro.getHeading() < 3){
arm_lift.setPower(0);
} else {
arm_lift.setPower(gamepad1.right_stick_y / speedDivisor);
}
}
*/
arm_lift.setPower(gamepad1.right_stick_y * 0.5 / speedDivisor);
//movement based on left stick
back_left.setPower(gamepad1.left_stick_y / speedDivisor - gamepad1.left_stick_x / speedDivisor);
front_left.setPower(gamepad1.left_stick_y / speedDivisor - gamepad1.left_stick_x / speedDivisor);
back_right.setPower(-(gamepad1.left_stick_y / speedDivisor + gamepad1.left_stick_x / speedDivisor));
front_right.setPower(-(gamepad1.left_stick_y / speedDivisor + gamepad1.left_stick_x / speedDivisor));
telemetry.addData("(B) Motor Precision Mode: ", gamepad2.b?"ON":"OFF");
telemetry.addData("(A) Thumb Precision Mode: ", gamepad2.a?"ON":"OFF");
telemetry.addData("(^) FOURTH Arm Height: ", gamepad2.dpad_up?"ON":"OFF");
telemetry.addData("(>) THIRD Arm Height: ", gamepad2.dpad_right?"ON":"OFF");
telemetry.addData("(<) SECOND Arm Height: ", gamepad2.dpad_left?"ON":"OFF");
telemetry.addData("(⌄) FIRST Arm Height: ", gamepad2.dpad_down?"ON":"OFF");
telemetry.update();
return this;
}
}
@Override
public void runOpMode() {
arm_lift = hardwareMap.get(DcMotor.class, "arm_lift");
front_right = hardwareMap.get(DcMotor.class, "front_right");
front_left = hardwareMap.get(DcMotor.class, "front_left");
back_right = hardwareMap.get(DcMotor.class, "back_right");
back_left = hardwareMap.get(DcMotor.class, "back_left");
color_prox = hardwareMap.get(ColorSensor.class, "color_prox");
right_thumb = hardwareMap.get(Servo.class, "right_thumb");
left_thumb = hardwareMap.get(Servo.class, "left_thumb");
ball_arm = hardwareMap.get(Servo.class, "ball_arm");
arm_gyro = hardwareMap.get(GyroSensor.class, "arm_gyro");
body_gyro = hardwareMap.get(GyroSensor.class, "body_gyro");
calibrateGyro = new CalibrateGyro();
drive = new Drive();
machine = new StateMachine(calibrateGyro);
front_left.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
front_right.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
back_left.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
back_right.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
arm_lift.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
arm_lift.setDirection(DcMotor.Direction.REVERSE);
front_left.setDirection(DcMotor.Direction.REVERSE);
front_right.setDirection(DcMotor.Direction.REVERSE);
back_left.setDirection(DcMotor.Direction.REVERSE);
back_right.setDirection(DcMotor.Direction.REVERSE);
waitForStart();
while (opModeIsActive()) {
machine.update();
}
}
private DcMotor arm_lift;
private DcMotor front_left;
private DcMotor front_right;
private DcMotor back_left;
private DcMotor back_right;
private ColorSensor color_prox;
private Servo right_thumb;
private Servo left_thumb;
private Servo ball_arm;
private GyroSensor arm_gyro;
private GyroSensor body_gyro;
private double thumbSpeed;
private double speedDivisor;
private CalibrateGyro calibrateGyro;
private Drive drive;
private StateMachine machine;
}