-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlueTruss2
234 lines (192 loc) · 10.7 KB
/
BlueTruss2
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
package org.firstinspires.ftc.teamcode.Auto;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.CRServo;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
import com.qualcomm.robotcore.hardware.Servo;
import com.qualcomm.robotcore.util.ElapsedTime;
import org.checkerframework.checker.units.qual.A;
@Autonomous(name="BlueTruss2", group="Robot")
public class BlueTruss2 extends LinearOpMode {
/* Declare OpMode members. */
private DcMotor fr = null;
private DcMotor fl = null;
private DcMotor bl = null;
private DcMotor br = null;
private ElapsedTime runtime = new ElapsedTime();
// Calculate the COUNTS_PER_INCH for your specific drive train.
// Go to your motor vendor website to determine your motor's COUNTS_PER_MOTOR_REV
// For external drive gearing, set DRIVE_GEAR_REDUCTION as needed.
// For example, use a value of 2.0 for a 12-tooth spur gear driving a 24-tooth spur gear.
// This is gearing DOWN for less speed and more torque.
// For gearing UP, use a gear ratio less than 1.0. Note this will affect the direction of wheel rotation.
static final double COUNTS_PER_MOTOR_REV = 1440 ; // eg: TETRIX Motor Encoder
static final double DRIVE_GEAR_REDUCTION = 1.0 ; // No External Gearing.
static final double WHEEL_DIAMETER_INCHES = 4.0 ; // For figuring circumference
static final double COUNTS_PER_INCH = (COUNTS_PER_MOTOR_REV * DRIVE_GEAR_REDUCTION) /
(WHEEL_DIAMETER_INCHES * 3.1415);
static final double DRIVE_SPEED = 0.6;
static final double TURN_SPEED = 0.5;
@Override
public void runOpMode() {
// Initialize the drive system variables.
fr = hardwareMap.get(DcMotor.class, "fR");
fl = hardwareMap.get(DcMotor.class, "fL");
br = hardwareMap.get(DcMotor.class, "bR");
bl = hardwareMap.get(DcMotor.class, "bL");
// To drive forward, most robots need the motor on one side to be reversed, because the axles point in opposite directions.
// When run, this OpMode should start both motors driving forward. So adjust these two lines based on your first test drive.
// Note: The settings here assume direct drive on left and right wheels. Gear Reduction or 90 Deg drives may require direction flips
fr.setDirection(DcMotor.Direction.FORWARD);
fl.setDirection(DcMotor.Direction.REVERSE);
br.setDirection(DcMotor.Direction.FORWARD);
bl.setDirection(DcMotor.Direction.REVERSE);
fr.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
fl.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
br.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
bl.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
fr.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
fl.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
br.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
bl.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
// Send telemetry message to indicate successful Encoder reset
telemetry.addData("Starting at", "%7d :%7d :%7d :%7d",
fr.getCurrentPosition(),
fl.getCurrentPosition(),
br.getCurrentPosition(),
bl.getCurrentPosition());
telemetry.update();
// Wait for the game to start (driver presses PLAY)
waitForStart();
// Step through each leg of the path,
// Note: Reverse movement is obtained by setting a negative distance (not speed)
encoderDrive(DRIVE_SPEED, 9, 9, 5.0); // S1: Forward 12 Inches with 5 Sec timeout
encoderStrafe(DRIVE_SPEED, 45, 45, 10.0); // S2: Strafe Right 12 Inches with 4 Sec timeout
telemetry.addData("Path", "Complete");
telemetry.update();
sleep(1000); // pause to display final telemetry message.
}
/*
* Method to perform a relative move, based on encoder counts.
* Encoders are not reset as the move is based on the current position.
* Move will stop if any of three conditions occur:
* 1) Move gets to the desired position
* 2) Move runs out of time
* 3) Driver stops the opmode running.
*/
public void encoderDrive(double speed,
double leftInches, double rightInches,
double timeoutS) {
int newflTarget;
int newfrTarget;
int newblTarget;
int newbrTarget;
// Ensure that the opmode is still active
if (opModeIsActive()) {
// Determine new target position, and pass to motor controller
newflTarget = fl.getCurrentPosition() + (int)(leftInches * COUNTS_PER_INCH);
newfrTarget = fr.getCurrentPosition() + (int)(rightInches * COUNTS_PER_INCH);
newblTarget = bl.getCurrentPosition() + (int)(leftInches * COUNTS_PER_INCH);
newbrTarget = br.getCurrentPosition() + (int)(rightInches * COUNTS_PER_INCH);
fl.setTargetPosition(newflTarget);
fr.setTargetPosition(newfrTarget);
bl.setTargetPosition(newblTarget);
br.setTargetPosition(newbrTarget);
// Turn On RUN_TO_POSITION
fr.setMode(DcMotor.RunMode.RUN_TO_POSITION);
fl.setMode(DcMotor.RunMode.RUN_TO_POSITION);
br.setMode(DcMotor.RunMode.RUN_TO_POSITION);
bl.setMode(DcMotor.RunMode.RUN_TO_POSITION);
// reset the timeout time and start motion.
runtime.reset();
fr.setPower(Math.abs(speed));
fl.setPower(Math.abs(speed));
br.setPower(Math.abs(speed));
bl.setPower(Math.abs(speed));
// keep looping while we are still active, and there is time left, and both motors are running.
// Note: We use (isBusy() && isBusy()) in the loop test, which means that when EITHER motor hits
// its target position, the motion will stop. This is "safer" in the event that the robot will
// always end the motion as soon as possible.
// However, if you require that BOTH motors have finished their moves before the robot continues
// onto the next step, use (isBusy() || isBusy()) in the loop test.
while (opModeIsActive() &&
(runtime.seconds() < timeoutS) &&
(fr.isBusy() && br.isBusy() && bl.isBusy() && fl.isBusy())) {
// Display it for the driver.
telemetry.addData("Running to", " %7d :%7d :%7d :%7d", newfrTarget, newflTarget, newbrTarget, newblTarget);
telemetry.addData("Currently at", " at %7d :%7d :%7d :%7d", newfrTarget, newflTarget, newbrTarget, newblTarget,
fr.getCurrentPosition(), fl.getCurrentPosition(), br.getCurrentPosition(), bl.getCurrentPosition());
telemetry.update();
}
// Stop all motion;
fr.setPower(0);
fl.setPower(0);
br.setPower(0);
bl.setPower(0);
// Turn off RUN_TO_POSITION
fr.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
fl.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
br.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
bl.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
sleep(250); // optional pause after each move.
}
}
//strafing encoders
public void encoderStrafe(double speed, double leftInches, double rightInches, double timeoutS){
int newflTarget;
int newfrTarget;
int newblTarget;
int newbrTarget;
if (opModeIsActive()) {
// Determine new target position, and pass to motor controller
// For strafing one side's wheels 'attract' each other while the other side 'repels' each other
//The positive value will make the robot go to the left with current arrangement
newflTarget = fl.getCurrentPosition() - (int)(leftInches * COUNTS_PER_INCH);
newfrTarget = fr.getCurrentPosition() + (int)(rightInches * COUNTS_PER_INCH);
newblTarget = bl.getCurrentPosition() + (int)(leftInches * COUNTS_PER_INCH);
newbrTarget = br.getCurrentPosition() - (int)(rightInches * COUNTS_PER_INCH);
fl.setTargetPosition(newflTarget);
fr.setTargetPosition(newfrTarget);
bl.setTargetPosition(newblTarget);
br.setTargetPosition(newbrTarget);
// Turn On RUN_TO_POSITION
fr.setMode(DcMotor.RunMode.RUN_TO_POSITION);
fl.setMode(DcMotor.RunMode.RUN_TO_POSITION);
br.setMode(DcMotor.RunMode.RUN_TO_POSITION);
bl.setMode(DcMotor.RunMode.RUN_TO_POSITION);
// reset the timeout time and start motion.
runtime.reset();
fr.setPower(Math.abs(speed));
fl.setPower(Math.abs(speed));
br.setPower(Math.abs(speed));
bl.setPower(Math.abs(speed));
// keep looping while we are still active, and there is time left, and both motors are running.
// Note: We use (isBusy() && isBusy()) in the loop test, which means that when EITHER motor hits
// its target position, the motion will stop. This is "safer" in the event that the robot will
// always end the motion as soon as possible.
// However, if you require that BOTH motors have finished their moves before the robot continues
// onto the next step, use (isBusy() || isBusy()) in the loop test.
while (opModeIsActive() &&
(runtime.seconds() < timeoutS) &&
(fr.isBusy() && br.isBusy() && bl.isBusy() && fl.isBusy())) {
// Display it for the driver.
telemetry.addData("Running to", " %7d :%7d :%7d :%7d", newfrTarget, newflTarget, newbrTarget, newblTarget);
telemetry.addData("Currently at", " at %7d :%7d :%7d :%7d", newfrTarget, newflTarget, newbrTarget, newblTarget,
fr.getCurrentPosition(), fl.getCurrentPosition(), br.getCurrentPosition(), bl.getCurrentPosition());
telemetry.update();
}
// Stop all motion;
fr.setPower(0);
fl.setPower(0);
br.setPower(0);
bl.setPower(0);
// Turn off RUN_TO_POSITION
fr.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
fl.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
br.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
bl.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
sleep(250); // optional pause after each move.
}
}
}