forked from FIRST-Tech-Challenge/FtcRobotController
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Encoder_WRed.java
225 lines (172 loc) · 7.62 KB
/
Encoder_WRed.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
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.hardware.Servo;
import com.qualcomm.robotcore.util.ElapsedTime;
@Autonomous(name="Encoder_WRed")
//@Disabled
public class Encoder_WRed extends LinearOpMode{
private ElapsedTime runtime = new ElapsedTime();
static DcMotor leftFront;
static DcMotor rightFront;
static DcMotor leftBack;
static DcMotor rightBack;
DcMotor spinner;
Servo dropper;
Servo flipperLeft;
DcMotor extender;
public double COUNTS_PER_MOTOR_REV;
public double DRIVE_GEAR_REDUCTION;
public double WHEEL_DIAMETER_INCH;
public double COUNTS_PER_INCH;
public double ROBOT_DIAMETER;
public double ROBOT_CIRCUMFERENCE;
double power = 0.5;
@Override
public void runOpMode() {
telemetry.addData("Status", "Initialized");
telemetry.update();
leftFront = hardwareMap.dcMotor.get("leftFront");
leftBack = hardwareMap.dcMotor.get("leftBack");
rightFront = hardwareMap.dcMotor.get("rightFront");
rightBack = hardwareMap.dcMotor.get("rightBack");
spinner = hardwareMap.dcMotor.get("spinner");
dropper = hardwareMap.servo.get("dropper");
extender = hardwareMap.dcMotor.get("extender");
flipperLeft = hardwareMap.servo.get("flipperLeft");
leftFront.setDirection(DcMotor.Direction.REVERSE);
leftBack.setDirection(DcMotor.Direction.REVERSE);
COUNTS_PER_MOTOR_REV = 537.6;
DRIVE_GEAR_REDUCTION = 19.2/1; // This is < 1.0 if geared UP (32 teeth to 16 teeth)
WHEEL_DIAMETER_INCH = 96; // For figuring circumference
COUNTS_PER_INCH = (COUNTS_PER_MOTOR_REV * DRIVE_GEAR_REDUCTION) / (WHEEL_DIAMETER_INCH* 3.1415);
ROBOT_CIRCUMFERENCE = ROBOT_DIAMETER * 3.1415;
waitForStart();
runtime.reset();
//strafe left
encoderStrafeINCH(-34, 0.5);
leftFront.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
rightFront.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
leftBack.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
rightBack.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
//runs backwards
encoderMoveINCH(-29, -29, 0.5);
//dropper starts and ends
extender.setPower(-1);
sleep(1500);
flipperLeft.setPosition(0);
sleep(1500);
//deposit element onto shipping hub
dropper.setPosition(1);
sleep(1500);
//set dropper back into position
dropper.setPosition(0);
sleep(1500);
//runs forward
encoderMoveINCH(20, 20, 0.4);
//turn
encoderMoveINCH(-25, 25, 0.4);
//strafe left
encoderStrafeINCH(-25, 0.4);
leftFront.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
rightFront.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
leftBack.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
rightBack.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
leftFront.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
rightFront.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
leftBack.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
rightBack.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
//runs forward
encoderMoveINCH(70, 70, 0.3);
}
int leftPos = 0;
int rightPos = 0;
public void encoderMoveBasic(double leftTarget, double rightTarget, double power){
leftPos += leftTarget;
rightPos += rightTarget;
leftFront.setTargetPosition(leftPos);
leftBack.setTargetPosition(leftPos);
rightFront.setTargetPosition(rightPos);
rightBack.setTargetPosition(rightPos);
leftFront.setPower(power);
rightFront.setPower(power);
leftBack.setPower(power);
rightBack.setPower(power);
leftFront.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightFront.setMode(DcMotor.RunMode.RUN_TO_POSITION);
leftBack.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightBack.setMode(DcMotor.RunMode.RUN_TO_POSITION);
}
/*public void encoderStrafe (double distance, double power){
int ticks = (int)(distance * 301.59);
leftFront.setTargetPosition(leftFront.getCurrentPosition()-ticks);
leftBack.setTargetPosition(leftBack.getCurrentPosition()+ticks);
rightFront.setTargetPosition(rightFront.getCurrentPosition()+ticks);
rightBack.setTargetPosition(rightBack.getCurrentPosition()-ticks);
leftFront.setPower(-power);
rightFront.setPower(power);
leftBack.setPower(power);
rightBack.setPower(-power);
leftFront.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightFront.setMode(DcMotor.RunMode.RUN_TO_POSITION);
leftBack.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightBack.setMode(DcMotor.RunMode.RUN_TO_POSITION);
}*/
public void encoderStrafeINCH(double inches, double power){
int ticks = (int)(inches*COUNTS_PER_INCH);
leftFront.setTargetPosition(leftFront.getCurrentPosition()-ticks);
leftBack.setTargetPosition(leftBack.getCurrentPosition()+ticks);
rightFront.setTargetPosition(rightFront.getCurrentPosition()+ticks);
rightBack.setTargetPosition(rightBack.getCurrentPosition()-ticks);
leftFront.setPower(power);
rightFront.setPower(power);
leftBack.setPower(power);
rightBack.setPower(power);
leftFront.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightFront.setMode(DcMotor.RunMode.RUN_TO_POSITION);
leftBack.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightBack.setMode(DcMotor.RunMode.RUN_TO_POSITION);
//continue moving
while (opModeIsActive() && ((leftFront.isBusy() && rightFront.isBusy() && leftBack.isBusy() && rightBack.isBusy()))) {
}
// Stop all motion;
leftFront.setPower(0);
rightFront.setPower(0);
leftBack.setPower(0);
rightBack.setPower(0);
}
public void encoderMoveINCH(double leftINCH, double rightINCH, double power){
int newLeftTarget = leftFront.getCurrentPosition()+(int)(leftINCH*COUNTS_PER_INCH);
int newRightTarget = rightFront.getCurrentPosition()+(int)(rightINCH*COUNTS_PER_INCH);
double leftSpeed;
double rightSpeed;
leftFront.setTargetPosition(newLeftTarget);
leftBack.setTargetPosition(newLeftTarget);
rightFront.setTargetPosition(newRightTarget);
rightBack.setTargetPosition(newRightTarget);
if (Math.abs(leftINCH) > Math.abs(rightINCH)) {
leftSpeed = power;
rightSpeed = (power * rightINCH) / leftINCH;
} else {
rightSpeed = power;
leftSpeed = (power * leftINCH) / rightINCH;
}
leftFront.setPower(leftSpeed);
rightFront.setPower(rightSpeed);
leftBack.setPower(leftSpeed);
rightBack.setPower(rightSpeed);
leftFront.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightFront.setMode(DcMotor.RunMode.RUN_TO_POSITION);
leftBack.setMode(DcMotor.RunMode.RUN_TO_POSITION);
rightBack.setMode(DcMotor.RunMode.RUN_TO_POSITION);
//continue movinggg
while (opModeIsActive() && ((leftFront.isBusy() && rightFront.isBusy() && leftBack.isBusy() && rightBack.isBusy()))) {
}
// Stop all motion;
leftFront.setPower(0);
rightFront.setPower(0);
leftBack.setPower(0);
rightBack.setPower(0);
}
}