-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonAprilTag.java
More file actions
91 lines (74 loc) · 2.97 KB
/
CommonAprilTag.java
File metadata and controls
91 lines (74 loc) · 2.97 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
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.hardware.HardwareMap;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import org.firstinspires.ftc.robotcore.external.hardware.camera.controls.ExposureControl;
import java.util.concurrent.TimeUnit;
import org.firstinspires.ftc.robotcore.external.hardware.camera.controls.GainControl;
import org.firstinspires.ftc.robotcore.external.hardware.camera.WebcamName;
import org.firstinspires.ftc.vision.VisionPortal;
import org.firstinspires.ftc.vision.apriltag.AprilTagProcessor;
import org.firstinspires.ftc.vision.apriltag.AprilTagDetection;
import java.util.List;
public class CommonAprilTag {
private HardwareMap hardwareMap = null;
private LinearOpMode opMode = null;
private AprilTagProcessor aprilTagProcessor;
private VisionPortal visionPortal;
public void initialize (LinearOpMode opMode, HardwareMap hardwareMap)
{
this.hardwareMap = hardwareMap;
this.opMode = opMode;
// Create the AprilTag processor
aprilTagProcessor = AprilTagProcessor.easyCreateWithDefaults();
visionPortal = VisionPortal.easyCreateWithDefaults(
hardwareMap.get(WebcamName.class, "Webcam 1"), aprilTagProcessor);
}
public void waitForCameraReady ()
{
while (!opMode.isStopRequested() && (visionPortal.getCameraState() != VisionPortal.CameraState.STREAMING))
{
opMode.sleep(20);
}
}
/* Manually set the camera gain and exposure.
Can only be called AFTER calling waitForCameraReady();
Returns true if controls are set.
*/
public boolean setManualExposure(int exposureMS, int gain)
{
// Ensure Vision Portal has been setup.
if (visionPortal == null)
{
return false;
}
if (visionPortal.getCameraState() != VisionPortal.CameraState.STREAMING)
{
return false;
}
// Set exposure. Make sure we are in Manual Mode for these values to take effect.
ExposureControl exposureControl = visionPortal.getCameraControl(ExposureControl.class);
if (exposureControl.getMode() != ExposureControl.Mode.Manual) {
exposureControl.setMode(ExposureControl.Mode.Manual);
opMode.sleep(50);
}
exposureControl.setExposure((long)exposureMS, TimeUnit.MILLISECONDS);
opMode.sleep(20);
// Set Gain.
GainControl gainControl = visionPortal.getCameraControl(GainControl.class);
gainControl.setGain(gain);
opMode.sleep(20);
return (true);
}
// This function returns the first ID found. Make sure the camera can only
// see 1 AprilTag at this time.
public int getId ()
{
int id = -1;
List<AprilTagDetection> currentDetections = aprilTagProcessor.getDetections();
if (currentDetections.size() > 0)
{
id = currentDetections.get(0).id;
}
return id;
}
}