-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreepyMovement2.java
224 lines (162 loc) · 6.65 KB
/
CreepyMovement2.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
package org.firstinspires.ftc.teamcode;
//imports
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.util.ElapsedTime;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.Blinker;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.Gyroscope;
import com.arcrobotics.ftclib.geometry.Translation2d;
import com.arcrobotics.ftclib.geometry.Pose2d;
import com.arcrobotics.ftclib.kinematics.wpilibkinematics.MecanumDriveKinematics;
import com.arcrobotics.ftclib.kinematics.wpilibkinematics.MecanumDriveOdometry;
import com.arcrobotics.ftclib.kinematics.wpilibkinematics.MecanumDriveWheelSpeeds;
import com.arcrobotics.ftclib.hardware.RevIMU;
import com.arcrobotics.ftclib.geometry.Rotation2d;
import com.arcrobotics.ftclib.hardware.motors.Motor;
import com.arcrobotics.ftclib.drivebase.MecanumDrive;
import com.arcrobotics.ftclib.purepursuit.waypoints.StartWaypoint;
import com.arcrobotics.ftclib.purepursuit.waypoints.GeneralWaypoint;
import com.arcrobotics.ftclib.purepursuit.waypoints.EndWaypoint;
import com.arcrobotics.ftclib.purepursuit.Path;
import com.arcrobotics.ftclib.purepursuit.Waypoint;
import org.openftc.apriltag.AprilTagDetection;
import org.openftc.easyopencv.OpenCvCamera;
import org.openftc.easyopencv.OpenCvCameraFactory;
import org.openftc.easyopencv.OpenCvCameraRotation;
import org.openftc.easyopencv.OpenCvInternalCamera;
import java.util.ArrayList;
@Autonomous
public class CreepyMovement2 extends LinearOpMode {
// Declare the global variable
MecanumDrive driveBase;
OpenCvCamera camera;
RevIMU imu;
AprilTagDetectionPipeline aprilTagDetectionPipeline;
static final double FEET_PER_METER = 3.28084;
// Lens intrinsics
// UNITS ARE PIXELS
// NOTE: this calibration is for the C920 webcam at 800x448.
// You will need to do your own calibration for other configurations!
double fx = 578.272;
double fy = 578.272;
double cx = 402.145;
double cy = 221.506;
// UNITS ARE METERS
double tagsize = 0.166;
@Override
public void runOpMode() throws InterruptedException {
//setup time
ElapsedTime time = new ElapsedTime();
//setup revIMU
imu = new RevIMU(hardwareMap);
imu.init();
int cameraMonitorViewId = hardwareMap.appContext.getResources().getIdentifier("cameraMonitorViewId", "id", hardwareMap.appContext.getPackageName());
camera = OpenCvCameraFactory.getInstance().createInternalCamera(OpenCvInternalCamera.CameraDirection.BACK, cameraMonitorViewId);
aprilTagDetectionPipeline = new AprilTagDetectionPipeline(tagsize, fx, fy, cx, cy);
camera.setPipeline(aprilTagDetectionPipeline);
camera.openCameraDeviceAsync(new OpenCvCamera.AsyncCameraOpenListener()
{
@Override
public void onOpened()
{
camera.startStreaming(864,480, OpenCvCameraRotation.UPRIGHT);
}
@Override
public void onError(int errorCode)
{
}
});
//setup motors
Motor frontLeftMotor = new Motor (hardwareMap, "frontLeft");
Motor frontRightMotor = new Motor (hardwareMap, "frontRight");
Motor backLeftMotor = new Motor (hardwareMap, "backLeft");
Motor backRightMotor = new Motor (hardwareMap, "backRight");
// Set the drivebase before using it
driveBase = new MecanumDrive (frontLeftMotor, frontRightMotor, backLeftMotor, backRightMotor);
//declare variables
double strafeSpeed;
double fowardSpeed;
double rotateSpeed;
double heading;
//wait for the game to start
waitForStart();
boolean foundTag = false;
int coneNumber = 0;
while (opModeIsActive() && !foundTag) {
ArrayList<AprilTagDetection> currentDetections = aprilTagDetectionPipeline.getLatestDetections();
if(currentDetections.size() != 0) {
coneNumber = currentDetections.get(0).id;
foundTag = true;
}
}
switch(coneNumber) {
case 1:
driveFoward(1.4);
rotateLeft(85);
driveFoward(1.25);
rotateRight(0);
break;
case 2:
driveFoward(1.25);
break;
case 3:
driveFoward(1.4);
rotateRight(85);
driveFoward(1.25);
rotateLeft(0);
break;
default:
break;
}
//stop the robot
requestOpModeStop();
}
//setup methods
public void driveFoward(double time) {
double strafeSpeed = 0;
double fowardSpeed = 1;
double rotateSpeed = 0;
double heading = 0;
drive(strafeSpeed ,fowardSpeed, rotateSpeed, heading, time);
}
public void strafeLeft(double time) {
double strafeSpeed = 1;
double fowardSpeed = .75;
double rotateSpeed = 0;
double heading = 0;
drive(strafeSpeed ,fowardSpeed, rotateSpeed, heading, time);
}
public void strafeRight(double time) {
double strafeSpeed = -1;
double fowardSpeed = .7;
double rotateSpeed = 0;
double heading = 0;
drive(strafeSpeed ,fowardSpeed, rotateSpeed, heading, time);
}
public void rotateRight(double degrees) {
double strafeSpeed = 0;
double fowardSpeed = 0;
double rotateSpeed = -0.75;
double heading = 0;
while(-imu.getHeading() <= degrees && opModeIsActive()) {
driveBase.driveFieldCentric(strafeSpeed, fowardSpeed, rotateSpeed,heading, false);
}
}
public void rotateLeft(double degrees) {
double strafeSpeed = 0;
double fowardSpeed = 0;
double rotateSpeed = 0.75;
double heading = 0;
while(imu.getHeading() <= degrees && opModeIsActive()) {
driveBase.driveFieldCentric(strafeSpeed, fowardSpeed, rotateSpeed,heading, false);
}
}
public void drive(double strafeSpeed, double fowardSpeed, double rotateSpeed, double heading, double time ) {
double startTime = getRuntime();
while(getRuntime() - startTime < time && opModeIsActive()) {
// this. is only needed if a parameter has the same name
driveBase.driveFieldCentric(strafeSpeed, fowardSpeed, rotateSpeed,heading, false);
}
}
}