Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.firstinspires.ftc.teamcode.CRVuforia;
- import com.qualcomm.robotcore.eventloop.opmode.Disabled;
- 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.matrices.OpenGLMatrix;
- import org.firstinspires.ftc.robotcore.external.matrices.VectorF;
- import org.firstinspires.ftc.robotcore.external.navigation.Orientation;
- import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer;
- import org.firstinspires.ftc.robotcore.external.navigation.VuforiaTrackable;
- import org.firstinspires.ftc.robotcore.external.navigation.VuforiaTrackableDefaultListener;
- import org.firstinspires.ftc.robotcore.external.navigation.VuforiaTrackables;
- import com.qualcomm.robotcore.util.Range;
- import com.qualcomm.robotcore.util.ElapsedTime;
- import static com.qualcomm.robotcore.util.ElapsedTime.Resolution.MILLISECONDS;
- @TeleOp(name="Vuforia Pos Test", group ="Concept")
- //@Disabled
- public class NewVuforiaTestTest extends LinearOpMode {
- // IMPORTANT: If you are using a USB WebCam, you must select CAMERA_CHOICE = BACK; and PHONE_IS_PORTRAIT = false;
- private static final String VUFORIA_KEY =
- "AeCl8dv/////AAABmU0vhtooEkbCoy9D8hM/5Yh8AhufXZT4nSVVD16Vjh1o/rLFmVyKVPNW3S/lXY0UWmDBpSNPS5yMk6lZoMFhTMoq9BMbmXHJ9KU+uKvC+GVp5cuEo18HvMpLMPPNmIVoXgOv9CqDfnRCOLSCblZf5cRF+E/LNqkZU7dEnEe/rrDq76FjVXruSdMBmUefIhu853VEpgvJPJTNopNjE0yU5TJ3+Uprgldx7fdy//VPG8PfXcaxLj4EJOzEKwJuCNdPS43bio37xbTbnLTzbKmfTqCI6BJpPaK5fXCk7o5xdVewJJbZCA8DDuNX6KUTT//OJ1UEWnMSYw5H1BrWMkytK5Syws7gdsCpYUshsQX7VP51";
- // Since ImageTarget trackables use mm to specifiy their dimensions, we must use mm for all the physical dimension.
- // We will define some constants and conversions here
- // Class Members
- private VuforiaLocalizer vuforia = null;
- private ElapsedTime timer = new ElapsedTime(ElapsedTime.Resolution.MILLISECONDS);
- /**
- * This is the webcam we are to use. As with other hardware devices such as motors and
- * servos, this device is identified using the robot configuration tool in the FTC application.
- */
- WebcamName webcamName = null;
- @Override public void runOpMode() {
- webcamName = hardwareMap.get(WebcamName.class, "Webcam 1");
- /*
- * Configure Vuforia by creating a Parameter object, and passing it to the Vuforia engine.
- * We can pass Vuforia the handle to a camera preview resource (on the RC phone);
- * If no camera monitor is desired, use the parameter-less constructor instead (commented out below).
- */
- int cameraMonitorViewId = hardwareMap.appContext.getResources().getIdentifier("cameraMonitorViewId", "id", hardwareMap.appContext.getPackageName());
- VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters(cameraMonitorViewId);
- // VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters();
- parameters.vuforiaLicenseKey = VUFORIA_KEY;
- /*
- * We also indicate which camera on the RC we wish to use.
- */
- parameters.cameraName = webcamName;
- // Instantiate the Vuforia engine
- vuforia = ClassFactory.getInstance().createVuforia(parameters);
- // Load the data sets for the trackable objects. These particular data
- // sets are stored in the 'assets' part of our application.
- VuforiaTrackables targetsSkyStone = this.vuforia.loadTrackablesFromAsset("Skystone");
- VuforiaTrackable stoneTarget = targetsSkyStone.get(0);
- stoneTarget.setName("Stone Target");
- // WARNING:
- // In this sample, we do not wait for PLAY to be pressed. Target Tracking is started immediately when INIT is pressed.
- // This sequence is used to enable the new remote DS Camera Preview feature to be used with this sample.
- // CONSEQUENTLY do not put any driving commands in this loop.
- // To restore the normal opmode structure, just un-comment the following line:
- waitForStart();
- // Note: To use the remote camera preview:
- // AFTER you hit Init on the Driver Station, use the "options menu" to select "Camera Stream"
- // Tap the preview window to receive a fresh image.
- targetsSkyStone.activate();
- while (!isStopRequested()) {
- if(timer.milliseconds() == 3) continue;
- boolean stoneVisible = false;
- // check all the trackable targets to see which one (if any) is visible.
- if (((VuforiaTrackableDefaultListener) stoneTarget.getListener()).isVisible()) {
- telemetry.addData("Visible Target", stoneTarget.getName());
- OpenGLMatrix location = ((VuforiaTrackableDefaultListener) stoneTarget.getListener()).getVuforiaCameraFromTarget();
- if (location != null) {
- // express position (translation) of robot in inches.
- VectorF translation = location.getTranslation();
- float closestX = Range.clip(translation.get(0), -20f, 20f);
- if (closestX == -20) telemetry.addData("Skystone Target:", "Center");
- if (closestX == 20) telemetry.addData("Skystone Target:", "Right");
- telemetry.addData("Pos (in)", "{X, Y, Z} = %.1f, %.1f, %.1f",
- translation.get(0), translation.get(1), translation.get(2));
- }
- } else {
- telemetry.addData("Visible Target", "none");
- }
- telemetry.update();
- timer.reset();
- }
- // Disable Tracking when we are done;
- targetsSkyStone.deactivate();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement