Hello I am a Programmer from Team 8720 "Insert Your Name Here" from Evergreen Middle School. In our autonomous, we are doing the jewel and vitoria. When we tried to run our code we faced a Null Pointer Exception relating to the hardware map app context. My best guess is that the problem is something related to Vuforia. Help would be greatly appreciated!
Code:
package org.firstinspires.ftc.teamcode.teamcode; import com.qualcomm.robotcore.eventloop.opmode.Autonomous; import com.qualcomm.robotcore.eventloop.opmode.Disabled; import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode; import com.qualcomm.robotcore.hardware.ColorSensor; import com.qualcomm.robotcore.hardware.DcMotor; import com.qualcomm.robotcore.hardware.Servo; import com.qualcomm.robotcore.util.ElapsedTime; import org.firstinspires.ftc.robotcore.external.matrices.OpenGLMatrix; import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer; import org.firstinspires.ftc.robotcontroller.external.samples.ConceptVuforiaNavigation; import org.firstinspires.ftc.robotcore.external.ClassFactory; import org.firstinspires.ftc.robotcore.external.matrices.OpenGLMatrix; import org.firstinspires.ftc.robotcore.external.matrices.VectorF; import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit; import org.firstinspires.ftc.robotcore.external.navigation.AxesOrder; import org.firstinspires.ftc.robotcore.external.navigation.AxesReference; import org.firstinspires.ftc.robotcore.external.navigation.Orientation; import org.firstinspires.ftc.robotcore.external.navigation.RelicRecoveryVuMark; import org.firstinspires.ftc.robotcore.external.navigation.VuMarkInstanceId; 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; @Autonomous(name = "AutonomousBlue1V7",group = "FTC") public class AutonomousBlue1 extends LinearOpMode { DcMotor FrontLeftWheel; DcMotor FrontRightWheel; DcMotor BackLeftWheel; DcMotor BackRightWheel; DcMotor LeftCycleMotor; DcMotor RightCycleMotor; DcMotor RelicMotor; DcMotor DumperMotor; Servo LeftCycleServo; Servo RightCycleServo; Servo LightStick; double MotorSpeed; ColorSensor color_sensor; ColorSensor color_sensor1; public static final String TAG = "Vuforia VuMark Sample"; OpenGLMatrix lastLocation = null; /** * {@link #vuforia} is the variable we will use to store our instance of the Vuforia * localization engine. */ VuforiaLocalizer vuforia; int cameraMonitorViewId = hardwareMap.appContext.getResources().getIdentifier("cameraMonitorViewId", "id", hardwareMap.appContext.getPackageName()); VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters(cameraMonitorViewId); private ElapsedTime runTime = new ElapsedTime(); enum AllianceColor { UNKNOWN, RED, BLUE, } enum Platform { RED1, RED2, BLUE1, BLUE2, } public AllianceColor readJewelColor() { AllianceColor jewel = AllianceColor.UNKNOWN; // Wait for a good reading, but Don't wait more than 1 second runTime.reset(); while (opModeIsActive() && (runTime.time() < 1) && (jewel == AllianceColor.UNKNOWN)) { sleep(20); // Give time for values to settle double red = color_sensor.red(); double blue = color_sensor.blue(); double alpha = color_sensor.alpha(); telemetry.addData("red", red); telemetry.addData("blue",blue); telemetry.addData("alpha",alpha); // Ensure that we have at least some color signal strength if (!((blue == 0) && (red == 0))) { if (blue > red) { jewel = AllianceColor.BLUE; } else { jewel = AllianceColor.RED; } } telemetry.addData("Jewel", jewel); telemetry.update(); } return (jewel); } @Override public void runOpMode() throws InterruptedException { FrontLeftWheel = hardwareMap.dcMotor.get("front_left_wheel"); FrontRightWheel = hardwareMap.dcMotor.get("front_right_wheel"); BackLeftWheel = hardwareMap.dcMotor.get("back_left_wheel"); BackRightWheel = hardwareMap.dcMotor.get("back_right_wheel"); LeftCycleMotor = hardwareMap.dcMotor.get("left_cycle_motor"); RightCycleMotor = hardwareMap.dcMotor.get("right_cycle_motor"); RelicMotor = hardwareMap.dcMotor.get("relic_motor"); DumperMotor = hardwareMap.dcMotor.get("dumper_motor"); LeftCycleServo = hardwareMap.servo.get("left_cycle_servo"); RightCycleServo = hardwareMap.servo.get("right_cycle_servo"); LightStick = hardwareMap.servo.get("light_stick"); MotorSpeed = 1; LeftCycleMotor.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER); RightCycleMotor.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER); color_sensor = color_sensor1; parameters.vuforiaLicenseKey = "ASdeW4n/////AAAAGQ3gx37Bok5bsetrw+NOuwA8Kmy/dauF2oHhHyy3O7LATTKCJEVWGQQzVWtFJ/55ZJ1KNV16pgUPrWLe1Wc0U2NHlzhWnJ3VaXgkeTjxf6vwUDP1TyYKzu3q9Ktyx2G+a5tukSVqWWGBqVCDiHv5U0ZVdRVqNHN/t+7Yitb27kTIfzsbnN5lLFjReHUSt5Utz9KCfBNVqdrNEGN2eP3u2BcV7sOsfSsZW4bc9ukXCkMgbjaevpzjj3LNtLklmHAXCiU+znKM6rs2rz+ALu3O4U5R1oRI+thosXU1gJGsFeV6H/RN17BpEZ61haJnp2i7C6osy6ffgNk8V8b2pg83Og9aQ3L8aFgxUoFZnEdFBfLN"; parameters.cameraDirection = VuforiaLocalizer.CameraDirection.BACK; this.vuforia = ClassFactory.createVuforiaLocalizer(parameters); VuforiaTrackables relicTrackables = this.vuforia.loadTrackablesFromAsset("RelicVuMark"); VuforiaTrackable relicTemplate = relicTrackables.get(0); relicTemplate.setName("relicVuMarkTemplate"); // can help in debugging; otherwise not necessary telemetry.addData(">", "Press Play to start"); telemetry.update(); waitForStart(); relicTrackables.activate(); waitForStart(); while (opModeIsActive()) { 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(); } } String format(OpenGLMatrix transformationMatrix) { return (transformationMatrix != null) ? transformationMatrix.formatAsTransform() : "null"; } }
Comment