-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathW_WebcamControls_WhiteBalance.java
132 lines (90 loc) · 5.37 KB
/
W_WebcamControls_WhiteBalance.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
/*
This example OpMode allows direct gamepad control of white balance temperature,
if supported. It's a companion to the FTC wiki tutorial on Webcam Controls.
Put the Driver Station Layout in Landscape mode for this telemetry.
Add your own Vuforia key, where shown below.
Questions, comments and corrections to westsiderobotics@verizon.net
from v01 11/12/21
*/
package org.firstinspires.ftc.teamcode;
import org.firstinspires.ftc.robotcore.external.hardware.camera.controls.WhiteBalanceControl;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import org.firstinspires.ftc.robotcore.external.ClassFactory;
import org.firstinspires.ftc.robotcore.external.hardware.camera.WebcamName;
import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer;
@TeleOp(name="Webcam Controls - White Balance v01", group ="Webcam Controls")
public class W_WebcamControls_WhiteBalance_v01 extends LinearOpMode {
private static final String VUFORIA_KEY =
//" INSERT YOUR VUFORIA KEY HERE ";
// Class Members
private VuforiaLocalizer vuforia = null;
private WebcamName webcamName = null;
WhiteBalanceControl myWBControl; // declare White Balance Control object
int minWhiteBalanceTemp; // temperature in degrees Kelvin (K)
int maxWhiteBalanceTemp;
int curWhiteBalanceTemp;
int tempIncrement = 100; // for manual gamepad adjustment
boolean wasTemperatureSet; // did the set() operation succeed?
boolean wasWhiteBalanceModeSet; // did the setMode() operation succeed?
boolean useTempLimits = true;
@Override public void runOpMode() {
telemetry.setMsTransmissionInterval(50);
// Connect to the webcam, using exact name per robot Configuration.
webcamName = hardwareMap.get(WebcamName.class, "Webcam 1");
/*
* Configure Vuforia by creating a Parameter object, and passing it to the Vuforia engine.
* We pass Vuforia the handle to a camera preview resource (on the RC screen).
*/
int cameraMonitorViewId = hardwareMap.appContext.getResources().getIdentifier("cameraMonitorViewId", "id", hardwareMap.appContext.getPackageName());
VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters(cameraMonitorViewId);
parameters.vuforiaLicenseKey = VUFORIA_KEY;
// We also indicate which camera we wish to use.
parameters.cameraName = webcamName;
// Set up the Vuforia engine
vuforia = ClassFactory.getInstance().createVuforia(parameters);
// Assign the white balance control object, to use its methods.
myWBControl = vuforia.getCamera().getControl(WhiteBalanceControl.class);
// display current white balance mode
telemetry.addLine("\nTouch Start arrow to control white balance temperature.");
telemetry.addLine("\nRecommended: put Driver Station Layout in Landscape.");
telemetry.addData("\nCurrent white balance mode", myWBControl.getMode());
telemetry.update();
waitForStart();
// set variable to current actual temperature, if supported
curWhiteBalanceTemp = myWBControl.getWhiteBalanceTemperature();
// get webcam temperature limits, if provided
minWhiteBalanceTemp = myWBControl.getMinWhiteBalanceTemperature();
maxWhiteBalanceTemp = myWBControl.getMaxWhiteBalanceTemperature();
// Set white balance mode to Manual, for direct control.
// A non-default setting may persist in the camera, until changed again.
wasWhiteBalanceModeSet = myWBControl.setMode(WhiteBalanceControl.Mode.MANUAL);
while (opModeIsActive()) {
// manually adjust the color temperature variable
if (gamepad1.x) { // increase with blue X (cooler)
curWhiteBalanceTemp += tempIncrement;
} else if (gamepad1.b) { // decrease with red B (warmer)
curWhiteBalanceTemp -= tempIncrement;
}
// ensure inputs are within webcam limits, if provided
if (useTempLimits) {
curWhiteBalanceTemp = Math.max(curWhiteBalanceTemp, minWhiteBalanceTemp);
curWhiteBalanceTemp = Math.min(curWhiteBalanceTemp, maxWhiteBalanceTemp);
}
// update the color temperature setting
wasTemperatureSet = myWBControl.setWhiteBalanceTemperature(curWhiteBalanceTemp);
// display live feedback while user observes preview image
telemetry.addLine("Adjust temperature with blue X (cooler) & red B (warmer)");
telemetry.addData("\nWhite Balance Temperature",
"Min: %d, Max: %d, Actual: %d",
minWhiteBalanceTemp, maxWhiteBalanceTemp,
myWBControl.getWhiteBalanceTemperature());
telemetry.addData("\nProgrammed temperature", "%d", curWhiteBalanceTemp);
telemetry.addData("Temperature set OK?", wasTemperatureSet);
telemetry.addData("\nCurrent white balance mode", myWBControl.getMode());
telemetry.addData("White balance mode set OK?", wasWhiteBalanceModeSet);
telemetry.update();
sleep(100);
} // end main while() loop
} // end OpMode
} // end class