-
Notifications
You must be signed in to change notification settings - Fork 1
/
numberGenerator
267 lines (226 loc) · 10.6 KB
/
numberGenerator
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
/* 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.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.util.ElapsedTime;
import java.util.Random;
/**
* This file illustrates the concept of driving a path based on encoder counts.
* It uses the common Pushbot hardware class to define the drive on the robot.
* The code is structured as a LinearOpMode
*
* The code REQUIRES that you DO have encoders on the wheels,
* otherwise you would use: PushbotAutoDriveByTime;
*
* This code ALSO requires that the drive Motors have been configured such that a positive
* power command moves them forwards, and causes the encoders to count UP.
*
* The desired path in this example is:
* - Drive forward for 48 inches
* - Spin right for 12 Inches
* - Drive Backwards for 24 inches
* - Stop and close the claw.
*
* The code is written using a method called: encoderDrive(speed, leftInches, rightInches, timeoutS)
* that performs the actual movement.
* This methods assumes that each movement is relative to the last stopping place.
* There are other ways to perform encoder based moves, but this method is probably the simplest.
* This code uses the RUN_TO_POSITION mode to enable the Motor controllers to generate the run profile
*
* Use Android Studios to Copy this Class, and Paste it into your team's code folder with a new name.
* Remove or comment out the @Disabled line to add this opmode to the Driver Station OpMode list
*/
@Autonomous(name="Pushbot: Auto Drive By Encoder4", group="Pushbot")
public class RandomNumberGeneratorCode extends LinearOpMode {
/* Declare OpMode members. */
//HardwarePushbot robot = new HardwarePushbot(); // Use a Pushbot's hardware
private ElapsedTime runtime = new ElapsedTime();
static final double COUNTS_PER_MOTOR_REV = 1440; // eg: TETRIX Motor Encoder
static final double DRIVE_GEAR_REDUCTION = 2.0; // This is < 1.0 if geared UP
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;
final int min = 1;
final int max = 3;
final int random = new Random().nextInt(max - min);
private DcMotor leftFront;
private DcMotor rightFront;
private DcMotor leftBack;
private DcMotor rightBack;
@Override
public void runOpMode() {
/*
* Initialize the drive system variables.
* The init() method of the hardware class does all the work here
*/
//robot.init(hardwareMap);
// Send telemetry message to signify robot waiting;
telemetry.addData("Status", "Resetting Encoders"); //
telemetry.update();
leftFront = hardwareMap.dcMotor.get("leftFront");
rightFront = hardwareMap.dcMotor.get("rightFront");
leftBack = hardwareMap.dcMotor.get("leftBack");
rightBack = hardwareMap.dcMotor.get("rightBack");
// Send telemetry message to indicate successful Encoder reset
telemetry.addData("Path0", "Starting at %7d :%7d");
// 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)
//Generate Random Number and Store it in a variable
final int random = new Random().nextInt(3) + 1;
if (new Random().nextInt(3)+1 == 1){
encoderDrive(0.8, 0.8,0.45, 1.5); // S1: Forward 47 Inches with 5 Sec timeout
// Stop all motion;
leftFront.setPower(0);
leftBack.setPower(0);
rightFront.setPower(0);
rightBack.setPower(0);
}
else if (new Random().nextInt(3)+1 == 2){
encoderDrive2(0.8, 0.8,0.45, 2); // S1: Forward 47 Inches with 5 Sec timeout
// Stop all motion;
leftFront.setPower(0);
leftBack.setPower(0);
rightFront.setPower(0);
rightBack.setPower(0);
}
else if (new Random().nextInt(3)+1 == 3){
encoderDrive3(0.8, 0.8,0.45, 3.0); // S1: Forward 47 Inches with 5 Sec timeout
// Stop all motion;
leftFront.setPower(0);
leftBack.setPower(0);
rightFront.setPower(0);
rightBack.setPower(0);
}
//call function
//else if r == 2
//call another function
//else if r ==3
//3rd function
//encoderDrive3(0.8, 0.8,0.45, 3.0); // S1: Forward 47 Inches with 5 Sec timeout
//encoderDrive(TURN_SPEED, 12, -12, 4.0); // S2: Turn Right 12 Inches with 4 Sec timeout
//encoderDrive(DRIVE_SPEED, -24, -24, 4.0); // S3: Reverse 24 Inches with 4 Sec timeout
/*robot.leftClaw.setPosition(1.0); // S4: Stop and close the claw
robot.rightClaw.setPosition(0.0);
sleep(1000); // pause for servos to move*/
telemetry.addData("Path", "Complete");
telemetry.update();
}
/*
* 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 encoderDrive3(double speed1, double speed2, double timeoutSh, double timeSt) {
// Ensure that the opmode is still active
if (opModeIsActive()) {
// reset the timeout time and start motion.
runtime.reset();
//Robot Goes straight for timeSt
while (opModeIsActive() && (runtime.seconds() < timeSt)) {
leftFront.setPower(speed2);
leftBack.setPower(-speed2);
rightFront.setPower(speed1);
rightBack.setPower(-speed1);
}
//reset runtime
runtime.reset();
//Robot Shuffle for timeoutSh
while (opModeIsActive() && (runtime.seconds() < timeoutSh)) {
leftFront.setPower(speed2);
leftBack.setPower(speed2);
rightFront.setPower(-speed1);
rightBack.setPower(-speed1);
telemetry.addData("Path2", "Running at %.3f ms %.3f", runtime.seconds(), speed1);
telemetry.update();
}
// sleep(250); // optional pause after each move
}
}
public void encoderDrive(double speed1, double speed2, double timeoutSh, double timeSt) {
// Ensure that the opmode is still active
if (opModeIsActive()) {
// reset the timeout time and start motion.
runtime.reset();
//Robot Goes straight for timeSt
while (opModeIsActive() && (runtime.seconds() < timeSt)) {
leftFront.setPower(speed2);
leftBack.setPower(-speed2);
rightFront.setPower(speed1);
rightBack.setPower(-speed1);
}
//reset runtime
runtime.reset();
//Robot Shuffle for timeoutSh
while (opModeIsActive() && (runtime.seconds() < timeoutSh)) {
leftFront.setPower(speed2);
leftBack.setPower(speed2);
rightFront.setPower(-speed1);
rightBack.setPower(-speed1);
telemetry.addData("Path2", "Running at %.3f ms %.3f", runtime.seconds(), speed1);
telemetry.update();
}
// sleep(250); // optional pause after each move
}
}
public void encoderDrive2(double speed1, double speed2, double timeoutSh, double timeSt) {
// Ensure that the opmode is still active
if (opModeIsActive()) {
// reset the timeout time and start motion.
runtime.reset();
//Robot Goes straight for timeSt
while (opModeIsActive() && (runtime.seconds() < timeSt)) {
leftFront.setPower(speed2);
leftBack.setPower(-speed2);
rightFront.setPower(speed1);
rightBack.setPower(-speed1);
}
//reset runtime
runtime.reset();
//Robot Shuffle for timeoutSh
while (opModeIsActive() && (runtime.seconds() < timeoutSh)) {
leftFront.setPower(-speed2);
leftBack.setPower(-speed2);
rightFront.setPower(speed1);
rightBack.setPower(speed1);
telemetry.addData("Path2", "Running at %.3f ms %.3f", runtime.seconds(), speed1);
telemetry.update();
}
// sleep(250); // optional pause after each move
}
}
}