Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.firstinspires.ftc.teamcode.Autonomous;
- import com.qualcomm.hardware.bosch.BNO055IMU;
- import com.qualcomm.hardware.bosch.JustLoggingAccelerationIntegrator;
- import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
- import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
- import com.qualcomm.robotcore.hardware.DcMotor;
- import com.qualcomm.robotcore.hardware.Servo;
- import com.qualcomm.robotcore.util.ElapsedTime;
- import com.qualcomm.robotcore.util.Range;
- import org.firstinspires.ftc.robotcore.external.ClassFactory;
- import org.firstinspires.ftc.robotcore.external.hardware.camera.WebcamName;
- 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.VuforiaLocalizer;
- import org.firstinspires.ftc.robotcore.external.tfod.Recognition;
- import org.firstinspires.ftc.robotcore.external.tfod.TFObjectDetector;
- import java.util.List;
- @Autonomous(name = "Hades - Facing Depot - Pos 2", group = "Coeus")
- public class Hades extends LinearOpMode {
- private static final String TFOD_MODEL_ASSET = "RoverRuckus.tflite";
- private static final String LABEL_GOLD_MINERAL = "Gold Mineral";
- private static final String LABEL_SILVER_MINERAL = "Silver Mineral";
- private static final String VUFORIA_KEY = "AXSqUwf/////AAABmVi9ifY9YkX7hla6dk//kFRSGKk+yWO9wuL666vmsKegzx+5Io14hzrDHPPSIOUIuBd89kbyiEj6ckbCr0Ak3uWm37MVO6WuHj2iyDGBiFASfDpXymVBKsTiT12M0KxuGVLdxg7JBLeNMq2f7lV/vtdmBf+UOVaruaibuICmy0jdCALIN7Edd3WcSYlY8V6VCxMPqLw4MrRbkPSshdxQ2WVZsDSZJgPqBE43qSmCFHhPwggb4+ZFSe6nn6SureHq5pNgyxeUXCaCsaIDbwHqEL7NrhOT6sqQTrWCsay3dFs6uVPY4YqJEP6+YGq7vYG7aJOC6nro3Z9NnVBhfwZEM07o6imRE0nkNd380XSO46rZ";
- DcMotor ForwardRight;
- DcMotor ForwardLeft;
- DcMotor BackLeft;
- DcMotor BackRight;
- DcMotor EncoderY;
- DcMotor EncoderX;
- DcMotor LeftLift;
- DcMotor RightLift;
- DcMotor CableSpool;
- DcMotor Intake;
- Servo Flap;
- BNO055IMU imu;
- private String Position = "NULL";
- private VuforiaLocalizer vuforia;
- private TFObjectDetector tfod;
- @Override
- public void runOpMode() {
- //Init hardware
- ForwardLeft = hardwareMap.get(DcMotor.class, "ForwardLeft");
- ForwardLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- ForwardLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- ForwardRight = hardwareMap.get(DcMotor.class, "ForwardRight");
- ForwardRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- ForwardRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- BackLeft = hardwareMap.get(DcMotor.class, "BackLeft");
- BackLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- BackLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- BackRight = hardwareMap.get(DcMotor.class, "BackRight");
- BackRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- BackRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- EncoderY = hardwareMap.get(DcMotor.class, "EncoderY");
- EncoderY.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- EncoderY.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- EncoderX = hardwareMap.get(DcMotor.class, "EncoderX");
- EncoderX.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- EncoderX.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- LeftLift = hardwareMap.get(DcMotor.class, "EncoderY");
- LeftLift.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
- LeftLift.setDirection(DcMotor.Direction.REVERSE);
- LeftLift.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
- RightLift = hardwareMap.get(DcMotor.class, "RightLift");
- RightLift.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- RightLift.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- RightLift.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
- CableSpool = hardwareMap.get(DcMotor.class, "CableSpool");
- CableSpool.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- BackRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- CableSpool.setDirection(DcMotor.Direction.REVERSE);
- CableSpool.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
- Intake = hardwareMap.get(DcMotor.class, "EncoderX");
- Intake.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
- Intake.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
- Flap = hardwareMap.get(Servo.class, "Flap");
- ForwardLeft.setDirection(DcMotor.Direction.FORWARD);
- ForwardRight.setDirection(DcMotor.Direction.REVERSE);
- BackLeft.setDirection(DcMotor.Direction.FORWARD);
- BackRight.setDirection(DcMotor.Direction.REVERSE);
- ForwardLeft.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
- ForwardRight.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
- BackLeft.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
- BackRight.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
- telemetry.addData("Status", "Hardware Initialized");
- telemetry.update();
- //Init Vuforia
- initVuforia();
- telemetry.addData("Status", "Vuforia Initialized");
- //Init TFOD
- if (ClassFactory.getInstance().canCreateTFObjectDetector()) {
- initTfod();
- } else {
- telemetry.addData("Sorry!", "This device is not compatible with TFOD");
- telemetry.update();
- }
- telemetry.addData("Status", "TFOD Initialized");
- telemetry.update();
- //Activate TFOD
- if (tfod != null) {
- tfod.activate();
- }
- telemetry.addData("Status", "TFOD Activated. Beginning Recognition");
- telemetry.update();
- //Begin Recognition steps
- while (isStopRequested() != true && !opModeIsActive()) {
- if (tfod != null) {
- List<Recognition> updatedRecognitions = tfod.getUpdatedRecognitions();
- if (updatedRecognitions != null) {
- telemetry.addData("# Object Detected", updatedRecognitions.size());
- if (updatedRecognitions.size() == 3) {
- int goldMineralX = -1;
- int silverMineral1X = -1;
- int silverMineral2X = -1;
- for (Recognition recognition : updatedRecognitions) {
- if (recognition.getLabel().equals(LABEL_GOLD_MINERAL)) {
- goldMineralX = (int) recognition.getLeft();
- } else if (silverMineral1X == -1) {
- silverMineral1X = (int) recognition.getLeft();
- } else {
- silverMineral2X = (int) recognition.getLeft();
- }
- }
- if (goldMineralX != -1 && silverMineral1X != -1 && silverMineral2X != -1) {
- if (goldMineralX < silverMineral1X && goldMineralX < silverMineral2X) {
- Position = "LEFT";
- } else if (goldMineralX > silverMineral1X && goldMineralX > silverMineral2X) {
- Position = "RIGHT";
- } else {
- Position = "CENTER";
- }
- }
- }
- }
- }
- telemetry.addData("Position", Position);
- telemetry.addData("Status", "VARaTiFf DLNN is running");
- telemetry.update();
- }
- telemetry.addData("Status", "No longer running VARaTiFf DLNN");
- telemetry.update();
- /**
- *
- * Initialization stuff above this
- *
- */
- waitForStart();
- tfod.deactivate();
- telemetry.addData("Status", "All systems online, Running");
- ForwardLeft.setPower(.3);
- ForwardRight.setPower(.3);
- BackLeft.setPower(.3);
- BackRight.setPower(.3);
- ForwardLeft.setTargetPosition(-1000);
- while(opModeIsActive()){
- telemetry.addData("Currently Supposed To Run", "Forward Left");
- telemetry.addData("ForwardLeft", ForwardLeft.getCurrentPosition());
- telemetry.addData("ForwardRight", ForwardRight.getCurrentPosition());
- telemetry.addData("BackLeft", BackLeft.getCurrentPosition());
- telemetry.addData("BackRight", BackRight.getCurrentPosition());
- telemetry.update();
- }
- }
- private void initVuforia() {
- /*
- * Configure Vuforia by creating a Parameter object, and passing it to the Vuforia engine.
- */
- VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters();
- parameters.vuforiaLicenseKey = VUFORIA_KEY;
- parameters.cameraName = hardwareMap.get(WebcamName.class, "Webcam 1");
- // Instantiate the Vuforia engine
- vuforia = ClassFactory.getInstance().createVuforia(parameters);
- // Loading trackables is not necessary for the Tensor Flow Object Detection engine.
- }
- private void initTfod() {
- int tfodMonitorViewId = hardwareMap.appContext.getResources().getIdentifier(
- "tfodMonitorViewId", "id", hardwareMap.appContext.getPackageName());
- TFObjectDetector.Parameters tfodParameters = new TFObjectDetector.Parameters(tfodMonitorViewId);
- tfod = ClassFactory.getInstance().createTFObjectDetector(tfodParameters, vuforia);
- tfod.loadModelFromAsset(TFOD_MODEL_ASSET, LABEL_GOLD_MINERAL, LABEL_SILVER_MINERAL);
- }
- void changeMode(int Mode) {
- if (Mode == 1) {
- ForwardLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- BackLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- ForwardRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- BackRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- ForwardLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- BackLeft.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- ForwardRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- BackRight.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- }
- if (Mode == 2) {
- ForwardLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- BackLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- ForwardRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- BackRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- }
- if (Mode == 3) {
- ForwardLeft.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
- BackLeft.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
- ForwardRight.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
- BackRight.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
- }
- if (Mode == 4) {
- ForwardLeft.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
- ForwardRight.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
- BackLeft.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
- BackRight.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
- }
- if (Mode == 5) {
- ForwardLeft.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.FLOAT);
- ForwardRight.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.FLOAT);
- BackLeft.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.FLOAT);
- BackRight.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.FLOAT);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement