Search in sources :

Example 6 with VuforiaTrackables

use of org.firstinspires.ftc.robotcore.external.navigation.VuforiaTrackables in project Relic_Main by TeamOverdrive.

the class ConceptVuMarkIdentification method runOpMode.

@Override
public void runOpMode() {
    /*
         * To start up Vuforia, tell it the view that we wish to use for camera monitor (on the RC phone);
         * If no camera monitor is desired, use the parameterless constructor instead (commented out below).
         */
    int cameraMonitorViewId = hardwareMap.appContext.getResources().getIdentifier("cameraMonitorViewId", "id", hardwareMap.appContext.getPackageName());
    VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters(cameraMonitorViewId);
    // OR...  Do Not Activate the Camera Monitor View, to save power
    // VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters();
    /*
         * IMPORTANT: You need to obtain your own license key to use Vuforia. The string below with which
         * 'parameters.vuforiaLicenseKey' is initialized is for illustration only, and will not function.
         * A Vuforia 'Development' license key, can be obtained free of charge from the Vuforia developer
         * web site at https://developer.vuforia.com/license-manager.
         *
         * Vuforia license keys are always 380 characters long, and look as if they contain mostly
         * random data. As an example, here is a example of a fragment of a valid key:
         *      ... yIgIzTqZ4mWjk9wd3cZO9T1axEqzuhxoGlfOOI2dRzKS4T0hQ8kT ...
         * Once you've obtained a license key, copy the string from the Vuforia web site
         * and paste it in to your code onthe next line, between the double quotes.
         */
    parameters.vuforiaLicenseKey = "ATsODcD/////AAAAAVw2lR...d45oGpdljdOh5LuFB9nDNfckoxb8COxKSFX";
    /*
         * We also indicate which camera on the RC that we wish to use.
         * Here we chose the back (HiRes) camera (for greater range), but
         * for a competition robot, the front camera might be more convenient.
         */
    parameters.cameraDirection = VuforiaLocalizer.CameraDirection.BACK;
    this.vuforia = ClassFactory.createVuforiaLocalizer(parameters);
    /**
     * Load the data set containing the VuMarks for Relic Recovery. There's only one trackable
     * in this data set: all three of the VuMarks in the game were created from this one template,
     * but differ in their instance id information.
     * @see VuMarkInstanceId
     */
    VuforiaTrackables relicTrackables = this.vuforia.loadTrackablesFromAsset("RelicVuMark");
    VuforiaTrackable relicTemplate = relicTrackables.get(0);
    // can help in debugging; otherwise not necessary
    relicTemplate.setName("relicVuMarkTemplate");
    telemetry.addData(">", "Press Play to start");
    telemetry.update();
    waitForStart();
    relicTrackables.activate();
    while (opModeIsActive()) {
        /**
         * See if any of the instances of {@link relicTemplate} are currently visible.
         * {@link RelicRecoveryVuMark} is an enum which can have the following values:
         * UNKNOWN, LEFT, CENTER, and RIGHT. When a VuMark is visible, something other than
         * UNKNOWN will be returned by {@link RelicRecoveryVuMark#from(VuforiaTrackable)}.
         */
        RelicRecoveryVuMark vuMark = RelicRecoveryVuMark.from(relicTemplate);
        if (vuMark != RelicRecoveryVuMark.UNKNOWN) {
            /* Found an instance of the template. In the actual game, you will probably
                 * loop until this condition occurs, then move on to act accordingly depending
                 * on which VuMark was visible. */
            telemetry.addData("VuMark", "%s visible", vuMark);
            /* For fun, we also exhibit the navigational pose. In the Relic Recovery game,
                 * it is perhaps unlikely that you will actually need to act on this pose information, but
                 * we illustrate it nevertheless, for completeness. */
            OpenGLMatrix pose = ((VuforiaTrackableDefaultListener) relicTemplate.getListener()).getPose();
            telemetry.addData("Pose", format(pose));
            /* We further illustrate how to decompose the pose into useful rotational and
                 * translational components */
            if (pose != null) {
                VectorF trans = pose.getTranslation();
                Orientation rot = Orientation.getOrientation(pose, AxesReference.EXTRINSIC, AxesOrder.XYZ, AngleUnit.DEGREES);
                // Extract the X, Y, and Z components of the offset of the target relative to the robot
                double tX = trans.get(0);
                double tY = trans.get(1);
                double tZ = trans.get(2);
                // Extract the rotational components of the target relative to the robot
                double rX = rot.firstAngle;
                double rY = rot.secondAngle;
                double rZ = rot.thirdAngle;
            }
        } else {
            telemetry.addData("VuMark", "not visible");
        }
        telemetry.update();
    }
}
Also used : OpenGLMatrix(org.firstinspires.ftc.robotcore.external.matrices.OpenGLMatrix) VuforiaTrackableDefaultListener(org.firstinspires.ftc.robotcore.external.navigation.VuforiaTrackableDefaultListener) VuforiaTrackables(org.firstinspires.ftc.robotcore.external.navigation.VuforiaTrackables) VuforiaTrackable(org.firstinspires.ftc.robotcore.external.navigation.VuforiaTrackable) VectorF(org.firstinspires.ftc.robotcore.external.matrices.VectorF) Orientation(org.firstinspires.ftc.robotcore.external.navigation.Orientation) RelicRecoveryVuMark(org.firstinspires.ftc.robotcore.external.navigation.RelicRecoveryVuMark) VuforiaLocalizer(org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer)

Aggregations

OpenGLMatrix (org.firstinspires.ftc.robotcore.external.matrices.OpenGLMatrix)6 VuforiaLocalizer (org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer)6 VuforiaTrackable (org.firstinspires.ftc.robotcore.external.navigation.VuforiaTrackable)6 VuforiaTrackableDefaultListener (org.firstinspires.ftc.robotcore.external.navigation.VuforiaTrackableDefaultListener)6 VuforiaTrackables (org.firstinspires.ftc.robotcore.external.navigation.VuforiaTrackables)6 ArrayList (java.util.ArrayList)3 VectorF (org.firstinspires.ftc.robotcore.external.matrices.VectorF)3 Orientation (org.firstinspires.ftc.robotcore.external.navigation.Orientation)3 RelicRecoveryVuMark (org.firstinspires.ftc.robotcore.external.navigation.RelicRecoveryVuMark)3