Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.firstinspires.ftc.teamcode;
- package org.firstinspires.ftc.robotcontroller.external.samples;
- import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
- import com.qualcomm.robotcore.hardware.ColorSensor;
- import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
- import com.qualcomm.robotcore.hardware.Gyroscope;
- import com.qualcomm.robotcore.hardware.Blinker;
- import com.qualcomm.robotcore.hardware.Servo;
- import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
- import com.qualcomm.robotcore.eventloop.opmode.Disabled;
- import com.qualcomm.robotcore.hardware.DcMotor;
- import com.qualcomm.robotcore.hardware.DcMotorSimple;
- import com.qualcomm.robotcore.util.ElapsedTime;
- import com.qualcomm.robotcore.hardware.I2cAddr;
- import com.qualcomm.robotcore.hardware.I2cDeviceSynch;
- import java.util.List;
- 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.TFObjectDetector;
- import org.firstinspires.ftc.robotcore.external.tfod.Recognition;
- @Autonomous
- public class Autonomus 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 = "AY+P56D/////AAABmSI9S7qmYEBRiexvSqpmyeJ7+EL9HqT1F38xiBmq5OyL+bgPAKCEY+Gq0DrzmKRaQ+C6j9Dq8OChZdw0oyIebsHi9f/UFLq8zTwrMmWqKUnfVlC5d7SWprkeXiWdg/0AX3eLMf15EYCkBlxIHOrjaSOzUotlkkUlJCt6btbNokBKQjrtug7s5W1FygD+4UQsYclavq/1dne9bGwE6LqJiFoAsFMiAiV3XJhYeS4V0MWNBT8E495/0tCwpowb45hyGo19lA/pDQdTfUwuHeDs1ojiu9UcRsNGMi/I0CkVz4MGzfFCS0gf+WmnWWt28Ua0BzgT6HqOZsC57W9qnVlFzs4Spdt0OdL5v9jt8uQYboqN";
- private VuforiaLocalizer vuforia;
- private TFObjectDetector tfod;
- private ColorSensor senzor_culoare;
- private Gyroscope imu_1;
- private DcMotor mSS;
- private DcMotor mSF;
- private DcMotor mDS;
- private DcMotor mDF;
- private DcMotor brat;
- private DcMotor carlig;
- private DcMotor lant;
- private Blinker expansion_Hub_1;
- private Blinker expansion_Hub_2;
- private Servo cutie;
- private Servo l;
- private Servo r;
- private Servo art;
- @Override
- public void runOpMode() {
- imu_1 = hardwareMap.get(Gyroscope.class, "imu 1");
- mSS = hardwareMap.get(DcMotor.class, "MSS");
- mSF = hardwareMap.get(DcMotor.class, "MSF");
- mDS = hardwareMap.get(DcMotor.class, "MDS");
- mDF = hardwareMap.get(DcMotor.class, "MDF");
- brat = hardwareMap.get(DcMotor.class, "brat");
- carlig = hardwareMap.get(DcMotor.class, "carlig");
- lant = hardwareMap.get(DcMotor.class, "lant");
- expansion_Hub_1 = hardwareMap.get(Blinker.class, "Expansion Hub 1");
- expansion_Hub_2 = hardwareMap.get(Blinker.class, "Expansion Hub 2");
- cutie = hardwareMap.get(Servo.class, "cutie");
- l = hardwareMap.get(Servo.class, "l");
- r = hardwareMap.get(Servo.class, "r");
- art = hardwareMap.get(Servo.class, "art");
- senzor_culoare=hardwareMap.get(ColorSensor.class,"senzor_culoare");
- initVuforia();
- /**
- * 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;
- vuforia = ClassFactory.getInstance().createVuforia(parameters);
- }
- if (ClassFactory.getInstance().canCreateTFObjectDetector()) {
- initTfod();
- }
- else {
- telemetry.addData("Sorry!", "This device is not compatible with TFOD");
- }
- tfodParameters.minimumConfidence = 0.75;
- /**
- * 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);
- }
- telemetry.addData("Status", "Initialized");
- telemetry.update();
- // Wait for the game to start (driver presses PLAY)
- waitForStart();
- int stare=1;
- /*
- 1-Start;
- 2-coborare
- 3-misca la stang
- 4- Mergi la bile
- 5- Verifica bila din stanga
- 6- mergi la dreapta
- 7- rotire stanga
- 8- atins cub
- 9- rotire dreapta
- */
- double anterior=0;
- double Start_Time=System.currentTimeMillis();
- float timp_de_coborare=8000;
- float timp_miscat_stanga=500;
- float timp_miscat_fata=500;
- float timp_miscat_dreapta=500;
- float timp_rotire_stanga=200;
- float timp_atins_cub=1000;
- float timp_rotire_dreapta=200;
- double viteza=0.5;
- double vit_rot=0.25;
- int activat;
- if (tfod != null) {
- tfod.activate();
- }
- switch(stare){
- case 1:stare=2;break;
- case 2:{
- //Coborarea in sine
- if(System.currentTimeMillis()-Start_Time<anterior+timp_de_coborare){
- carlig.setPower(-1);
- telemetry.addData("Status",Start_Time);
- }
- else{
- anterior+=timp_de_coborare;
- carlig.setPower(0);
- stare=3;
- }
- }break;
- case 3:{
- if(System.currentTimeMillis()-Start_Time<anterior+timp_miscat_stanga){
- //iesi din carlig
- mDF.setPower(-viteza);
- mDS.setPower(viteza);
- mSF.setPower(-viteza);
- mSS.setPower(viteza);
- }
- else{
- anterior+=timp_miscat_stanga;
- //stop
- mDF.setPower(0);
- mDS.setPower(0);
- mSF.setPower(0);
- mSS.setPower(0);
- stare=4;
- }
- }break;
- case 4:{
- //mergi in fata
- if(System.currentTimeMillis()-Start_Time<anterior+timp_miscat_fata){
- mDF.setPower(viteza);
- mDS.setPower(viteza);
- mSF.setPower(-viteza);
- mSS.setPower(-viteza);
- }
- else{
- anterior+=timp_miscat_fata;
- //stop
- mDF.setPower(0);
- mDS.setPower(0);
- mSF.setPower(0);
- mSS.setPower(0);
- stare=5;
- }
- }break;
- case 5:{
- if(System.currentTimeMillis()-Start_Time<anterior+timp_miscat_dreapta){
- //du te n centru
- mDF.setPower(viteza);
- mDS.setPower(-viteza);
- mSF.setPower(viteza);
- mSS.setPower(-viteza);
- }
- else{
- anterior+=timp_miscat_dreapta;
- //stop
- mDF.setPower(0);
- mDS.setPower(0);
- mSF.setPower(0);
- mSS.setPower(0);
- stare=6;
- }
- }break;
- case 6:{
- //verifica pozitia cubului
- activat=1;
- while (activat!=0) {
- 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() == 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) {
- activat == 0;
- if (goldMineralX < silverMineral1X && goldMineralX < silverMineral2X) {
- telemetry.addData("Gold Mineral Position", "Left");
- stare = 7;
- } else if (goldMineralX > silverMineral1X && goldMineralX > silverMineral2X) {
- telemetry.addData("Gold Mineral Position", "Right");
- stare = 9;
- } else {
- telemetry.addData("Gold Mineral Position", "Center");
- stare = 8;
- }
- }
- }
- telemetry.update();
- }
- }
- }
- }break;
- case 7:{
- if(System.currentTimeMillis()-Start_Time<anterior+timp_rotire_stanga){
- //roteste te spre stanga
- mDF.setPower(-vit_rot);
- mDS.setPower(-vit_rot);
- mSF.setPower(-vit_rot);
- mSS.setPower(-vit_rot);
- }
- else{
- anterior+=timp_rotire_stanga;
- //stop
- mDF.setPower(0);
- mDS.setPower(0);
- mSF.setPower(0);
- mSS.setPower(0);
- stare=8;
- }
- }break;
- case 8:{
- if(System.currentTimeMillis()-Start_Time<anterior+timp_atins_cub){
- //atinge cubul
- mDF.setPower(viteza);
- mDS.setPower(viteza);
- mSF.setPower(-viteza);
- mSS.setPower(-viteza);
- }
- else{
- anterior+=timp_atins_cubul;
- //stop
- mDF.setPower(0);
- mDS.setPower(0);
- mSF.setPower(0);
- mSS.setPower(0);
- }
- }break;
- case 9:{
- if(System.currentTimeMillis()-Start_Time<anterior+timp_rotire_dreapta){
- //roteste te spre dreapta
- mDF.setPower(vit_rot);
- mDS.setPower(vit_rot);
- mSF.setPower(vit_rot);
- mSS.setPower(vit_rot);
- }
- else{
- anterior+=timp_rotire_dreapta;
- //stop
- mDF.setPower(0);
- mDS.setPower(0);
- mSF.setPower(0);
- mSS.setPower(0);
- stare=8;
- }
- }break;
- }
- telemetry.update();
- }
- if (tfod != null){
- tfod.shutdown();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement