Advertisement
Guest User

Vuforia

a guest
Nov 16th, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.84 KB | None | 0 0
  1. package org.firstinspires.ftc.teamcode.CRVuforia;
  2.  
  3. import com.qualcomm.robotcore.eventloop.opmode.Disabled;
  4. import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
  5. import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
  6.  
  7. import org.firstinspires.ftc.robotcore.external.ClassFactory;
  8. import org.firstinspires.ftc.robotcore.external.hardware.camera.WebcamName;
  9. import org.firstinspires.ftc.robotcore.external.matrices.OpenGLMatrix;
  10. import org.firstinspires.ftc.robotcore.external.matrices.VectorF;
  11. import org.firstinspires.ftc.robotcore.external.navigation.Orientation;
  12. import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer;
  13. import org.firstinspires.ftc.robotcore.external.navigation.VuforiaTrackable;
  14. import org.firstinspires.ftc.robotcore.external.navigation.VuforiaTrackableDefaultListener;
  15. import org.firstinspires.ftc.robotcore.external.navigation.VuforiaTrackables;
  16.  
  17. import com.qualcomm.robotcore.util.Range;
  18. import com.qualcomm.robotcore.util.ElapsedTime;
  19.  
  20. import static com.qualcomm.robotcore.util.ElapsedTime.Resolution.MILLISECONDS;
  21.  
  22.  
  23. @TeleOp(name="Vuforia Pos Test", group ="Concept")
  24. //@Disabled
  25. public class NewVuforiaTestTest extends LinearOpMode {
  26.  
  27.     // IMPORTANT: If you are using a USB WebCam, you must select CAMERA_CHOICE = BACK; and PHONE_IS_PORTRAIT = false;
  28.     private static final String VUFORIA_KEY =
  29.             "AeCl8dv/////AAABmU0vhtooEkbCoy9D8hM/5Yh8AhufXZT4nSVVD16Vjh1o/rLFmVyKVPNW3S/lXY0UWmDBpSNPS5yMk6lZoMFhTMoq9BMbmXHJ9KU+uKvC+GVp5cuEo18HvMpLMPPNmIVoXgOv9CqDfnRCOLSCblZf5cRF+E/LNqkZU7dEnEe/rrDq76FjVXruSdMBmUefIhu853VEpgvJPJTNopNjE0yU5TJ3+Uprgldx7fdy//VPG8PfXcaxLj4EJOzEKwJuCNdPS43bio37xbTbnLTzbKmfTqCI6BJpPaK5fXCk7o5xdVewJJbZCA8DDuNX6KUTT//OJ1UEWnMSYw5H1BrWMkytK5Syws7gdsCpYUshsQX7VP51";
  30.  
  31.     // Since ImageTarget trackables use mm to specifiy their dimensions, we must use mm for all the physical dimension.
  32.     // We will define some constants and conversions here
  33.  
  34.  
  35.  
  36.     // Class Members
  37.     private VuforiaLocalizer vuforia = null;
  38.     private ElapsedTime timer = new ElapsedTime(ElapsedTime.Resolution.MILLISECONDS);
  39.  
  40.     /**
  41.      * This is the webcam we are to use. As with other hardware devices such as motors and
  42.      * servos, this device is identified using the robot configuration tool in the FTC application.
  43.      */
  44.     WebcamName webcamName = null;
  45.  
  46.  
  47.  
  48.     @Override public void runOpMode() {
  49.         webcamName = hardwareMap.get(WebcamName.class, "Webcam 1");
  50.  
  51.         /*
  52.          * Configure Vuforia by creating a Parameter object, and passing it to the Vuforia engine.
  53.          * We can pass Vuforia the handle to a camera preview resource (on the RC phone);
  54.          * If no camera monitor is desired, use the parameter-less constructor instead (commented out below).
  55.          */
  56.         int cameraMonitorViewId = hardwareMap.appContext.getResources().getIdentifier("cameraMonitorViewId", "id", hardwareMap.appContext.getPackageName());
  57.         VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters(cameraMonitorViewId);
  58.  
  59.         // VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters();
  60.  
  61.         parameters.vuforiaLicenseKey = VUFORIA_KEY;
  62.  
  63.         /*
  64.          * We also indicate which camera on the RC we wish to use.
  65.          */
  66.         parameters.cameraName = webcamName;
  67.  
  68.         //  Instantiate the Vuforia engine
  69.         vuforia = ClassFactory.getInstance().createVuforia(parameters);
  70.  
  71.         // Load the data sets for the trackable objects. These particular data
  72.         // sets are stored in the 'assets' part of our application.
  73.         VuforiaTrackables targetsSkyStone = this.vuforia.loadTrackablesFromAsset("Skystone");
  74.         VuforiaTrackable stoneTarget = targetsSkyStone.get(0);
  75.         stoneTarget.setName("Stone Target");
  76.  
  77.  
  78.         // WARNING:
  79.         // In this sample, we do not wait for PLAY to be pressed.  Target Tracking is started immediately when INIT is pressed.
  80.         // This sequence is used to enable the new remote DS Camera Preview feature to be used with this sample.
  81.         // CONSEQUENTLY do not put any driving commands in this loop.
  82.         // To restore the normal opmode structure, just un-comment the following line:
  83.  
  84.         waitForStart();
  85.  
  86.         // Note: To use the remote camera preview:
  87.         // AFTER you hit Init on the Driver Station, use the "options menu" to select "Camera Stream"
  88.         // Tap the preview window to receive a fresh image.
  89.  
  90.         targetsSkyStone.activate();
  91.         while (!isStopRequested()) {
  92.             if(timer.milliseconds() == 3) continue;
  93.             boolean stoneVisible = false;
  94.  
  95.             // check all the trackable targets to see which one (if any) is visible.
  96.             if (((VuforiaTrackableDefaultListener) stoneTarget.getListener()).isVisible()) {
  97.                 telemetry.addData("Visible Target", stoneTarget.getName());
  98.                 OpenGLMatrix location = ((VuforiaTrackableDefaultListener) stoneTarget.getListener()).getVuforiaCameraFromTarget();
  99.                 if (location != null) {
  100.                     // express position (translation) of robot in inches.
  101.                     VectorF translation = location.getTranslation();
  102.                     float closestX = Range.clip(translation.get(0), -20f, 20f);
  103.                     if (closestX == -20) telemetry.addData("Skystone Target:", "Center");
  104.                     if (closestX == 20) telemetry.addData("Skystone Target:", "Right");
  105.  
  106.                     telemetry.addData("Pos (in)", "{X, Y, Z} = %.1f, %.1f, %.1f",
  107.                             translation.get(0), translation.get(1), translation.get(2));
  108.                 }
  109.             } else {
  110.                 telemetry.addData("Visible Target", "none");
  111.             }
  112.             telemetry.update();
  113.             timer.reset();
  114.         }
  115.  
  116.         // Disable Tracking when we are done;
  117.         targetsSkyStone.deactivate();
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement