Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Copyright (c) 2018 FIRST. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted (subject to the limitations in the disclaimer below) provided that
- * the following conditions are met:
- *
- * Redistributions of source code must retain the above copyright notice, this list
- * of conditions and the following disclaimer.
- *
- * Redistributions in binary form must reproduce the above copyright notice, this
- * list of conditions and the following disclaimer in the documentation and/or
- * other materials provided with the distribution.
- *
- * Neither the name of FIRST nor the names of its contributors may be used to endorse or
- * promote products derived from this software without specific prior written permission.
- *
- * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
- * LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- package org.firstinspires.ftc.teamcode;
- 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 org.firstinspires.ftc.robotcore.external.ClassFactory;
- import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer;
- import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer.CameraDirection;
- import org.firstinspires.ftc.robotcore.external.tfod.Recognition;
- import org.firstinspires.ftc.robotcore.external.tfod.TFObjectDetector;
- import java.util.List;
- @Autonomous(name = "AutonomiecompletaV3", group = "Concept")
- //@Disabled
- public class AutonomiecompletaV3 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";
- DcMotor leftMotor;
- DcMotor rightMotor;
- DcMotor motorlander;
- DcMotor motorlandersus;
- DcMotor motorbratright,motorbratleft;
- Servo servomarker;
- double tpower=0.5;
- //int pozitie=0;
- private static final String VUFORIA_KEY = "AQavSyD/////AAABmRp+aJs4/0GOvHSzkUoszhAxYckIV6Db8eBIYLJDuXeoiOq0EgfMitokZIH6N2p9ITLl9GNugwEmUlyDo4G+6Oq10VwlFMlSUl+4a1c69ko8Eo215Gdu7TaOIz7Yy53z1VbCoz1TJ7C6L71W74hsmX9Tb9y0XjrRpjvxF8/YWfU0Y+Eil6UTYLqlycZ2MugKt7dK8ZlueYKuJTVEGxAfVIZDWwSDj13WqCVIhONEXN2lfg78k1xxeJR/6aAyCNc2CpQSQHwF2sxp6+7RDBxWwGQKNVNuhlz8HY3sfe9anu5Cu5syyNJlEOuXQKthAlgxWrO6VTswvTLo/gocHvpPbFh2yLs8gOl1exVCVwv9hibU";
- /**
- * {@link #vuforia} is the variable we will use to store our instance of the Vuforia
- * localization engine.
- */
- private VuforiaLocalizer vuforia;
- /**
- * {@link #tfod} is the variable we will use to store our instance of the Tensor Flow Object
- * Detection engine.
- */
- private TFObjectDetector tfod;
- @Override
- public void runOpMode() {
- // The TFObjectDetector uses the camera frames from the VuforiaLocalizer, so we create that
- // first.
- leftMotor=hardwareMap.dcMotor.get("Left_Motor");
- rightMotor=hardwareMap.dcMotor.get("Right_Motor");
- motorlander=hardwareMap.dcMotor.get("motorlander");
- motorlandersus=hardwareMap.dcMotor.get("motorlandersus");
- motorbratright=hardwareMap.dcMotor.get("motorbratright");
- motorbratleft=hardwareMap.dcMotor.get("motorbratleft");
- servomarker=hardwareMap.servo.get("servomarker");
- leftMotor.setDirection(DcMotor.Direction.FORWARD);
- rightMotor.setDirection(DcMotor.Direction.REVERSE);
- motorlandersus.setDirection(DcMotor.Direction.REVERSE);
- motorbratleft.setDirection(DcMotor.Direction.REVERSE);
- servomarker.setPosition(1.0);
- initVuforia();
- if (ClassFactory.getInstance().canCreateTFObjectDetector()) {
- initTfod();
- } else {
- telemetry.addData("Sorry!", "This device is not compatible with TFOD");
- }
- /** Wait for the game to begin */
- telemetry.addData(">", "Press Play to start tracking");
- telemetry.update();
- waitForStart();
- coboaradepelander(1800);
- moveforward(200);
- turnright(1200);
- moveforward(50);
- turnright(450);
- movebackward(10);
- moveforward(120);
- turnleft(350);
- moveforward(110);
- sleep(1000);
- if (opModeIsActive()) {
- /** Activate Tensor Flow Object Detection. */
- if (tfod != null) {
- tfod.activate();
- }
- while (opModeIsActive()) {
- if (tfod != null) {
- // getUpdatedRecognitions() will return null if no new information is available since
- // the last time that call was made.
- List<Recognition> updatedRecognitions = tfod.getUpdatedRecognitions();
- if (updatedRecognitions != null) {
- // telemetry.addData("# Object Detected", updatedRecognitions.size());
- if (updatedRecognitions.size() >= 1) {
- int goldMineralX = -1;
- int silverMineral1X = -1;
- int silverMineral2X = -1;
- int ok=0;
- int c=0;
- for (Recognition recognition : updatedRecognitions) {
- while (ok == 0) {c++;telemetry.addData("# Object Detected", updatedRecognitions.size());
- sleep(500);
- if (recognition.getLabel().equals(LABEL_GOLD_MINERAL)) {
- moveforward(500);
- ok = 1;
- }
- else {
- movebackward(100);
- turnright(300);
- moveforward(100);
- sleep(2000);
- }
- }
- if(c==1) mineralstanga();
- else if(c==2) mineralcentru();
- else if(c==3) mineraldreapta();
- }
- /* if (goldMineralX != -1 && silverMineral1X != -1 && silverMineral2X != -1) {
- if (goldMineralX < silverMineral1X && goldMineralX < silverMineral2X) {
- telemetry.addData("Gold Mineral Position", "Left");
- mineralstanga();
- } else if (goldMineralX > silverMineral1X && goldMineralX > silverMineral2X) {
- telemetry.addData("Gold Mineral Position", "Right");
- mineraldreapta();
- } else {
- telemetry.addData("Gold Mineral Position", "Center");
- mineralcentru();
- }
- }
- */
- }
- telemetry.update();
- }
- }
- }
- }
- if (tfod != null) {
- tfod.shutdown();
- }
- }
- /**
- * Initialize the Vuforia localization engine.
- */
- 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.cameraDirection = CameraDirection.BACK;
- // Instantiate the Vuforia engine
- vuforia = ClassFactory.getInstance().createVuforia(parameters);
- // Loading trackables is not necessary for the Tensor Flow Object Detection engine.
- }
- /**
- * Initialize 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);
- tfodParameters.minimumConfidence=1.0;
- }
- //Functii
- public void movebackward(int time)
- {rightMotor.setPower(1.0);
- leftMotor.setPower(1.0);
- sleep(time);
- rightMotor.setPower(0.0);
- leftMotor.setPower(0.0);
- sleep(500);
- }
- public void moveforward(int time)
- {rightMotor.setPower(-1.0);
- leftMotor.setPower(-1.0);
- sleep(time);
- rightMotor.setPower(0.0);
- leftMotor.setPower(0.0);
- sleep(500);
- }
- public void turnright(int time)
- {leftMotor.setPower(-1.0);
- rightMotor.setPower(1.0);
- sleep(time);
- rightMotor.setPower(0.0);
- leftMotor.setPower(0.0);
- sleep(500);
- }
- public void turnleft(int time)
- {rightMotor.setPower(-1.0);
- leftMotor.setPower(1.0);
- sleep(time);
- rightMotor.setPower(0.0);
- leftMotor.setPower(0.0);
- sleep(500);
- }
- public void coboaradepelander(int time)
- {
- motorlander.setPower(0.5);
- motorlandersus.setPower(0.5);
- sleep(time);
- motorlander.setPower(0.0);
- motorlandersus.setPower(0.0);
- sleep(500);
- }
- public void revenireBratRidicare(int time)
- {
- motorlander.setPower(-0.5);
- motorlandersus.setPower(-0.5);
- sleep(time);
- motorlander.setPower(0.0);
- motorlandersus.setPower(0.0);
- sleep(500);
- }
- public void datjosmarker()
- {servomarker.setPosition(0.0);
- }
- public void coboarabrat(int time)
- {motorbratright.setPower(-1.0);
- motorbratleft.setPower(-1.0);
- sleep(time);
- motorbratright.setPower(0.0);
- motorbratleft.setPower(0.0);
- sleep(500);
- }
- public void mineralstanga()
- {
- moveforward(500);
- turnright(250);
- moveforward(500);
- /* datjosmarker();
- turnleft(2000);
- moveforward(5000);
- coboarabrat(1500);*/
- }
- public void mineralcentru()
- {
- moveforward(1000);
- /*datjosmarker();
- turnleft(400);
- moveforward(1000);
- turnleft(70);
- moveforward(1500);
- coboarabrat(1500);*/
- }
- public void mineraldreapta()
- {
- turnright(250);
- moveforward(500);
- turnleft(250);
- moveforward(500);
- /*datjosmarker();
- turnleft(2000);
- moveforward(5000);
- coboarabrat(1500);*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement