Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.firstinspires.ftc.teamcode;
- import com.vuforia.Vuforia;
- import org.firstinspires.ftc.robotcore.external.tfod.TFObjectDetector;
- import org.firstinspires.ftc.robotcore.external.tfod.TfodBase;
- import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer;
- import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
- import com.qualcomm.robotcore.eventloop.opmode.OpMode;
- import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
- import com.qualcomm.robotcore.hardware.DcMotorSimple;
- import com.qualcomm.robotcore.eventloop.opmode.Disabled;
- import com.qualcomm.robotcore.hardware.DcMotor;
- import com.qualcomm.robotcore.util.ElapsedTime;
- import com.qualcomm.robotcore.util.Range;
- import com.qualcomm.hardware.bosch.BNO055IMU;
- import org.firstinspires.ftc.robotcore.external.navigation.Acceleration;
- 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.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="TFGYROENC1",group= "LinearOpMode")
- public class Autonomocontfor extends LinearOpMode {
- private static final String TFOD_MODEL_ASSET = "Skystone.tflite";
- private static final String LABEL_FIRST_ELEMENT = "Stone";
- private static final String LABEL_SECOND_ELEMENT = "Skystone";
- private BNO055IMU imu;
- private ElapsedTime runtime = new ElapsedTime();
- private DcMotor BackRight;
- private DcMotor BackLeft;
- private DcMotor FrontRight;
- private DcMotor FrontLeft;
- double INCHES = 0;
- double Kp = 0;
- double target;
- boolean zeroTo360;
- double current;
- double imuAngle=0;
- double THRESHOLD = 2;
- private static final String VUFORIA_KEY =
- " AU3Trdb/////AAABmb4EgBXvH0QqqkopAIsu5GpM7KbbJoDF3A52lamSYVnQiJEpPbutvqQ2T88XPN5JrxT0AOrlVdoNz+EYMSjHMt99pLkMEXxrRHpdZKfypK14migPrrocBRMn7pe2YNRkjrIe27hIsGKN7yiFoXphsfdmIz5w7FKh0Lh1aqQPt740Chy/B3WFz/0BHLy7T2CZZN6ADwyTyw0NNBTNFvmz3XkMI1ms0Fr0Lofzmlv7qLWt6T1pwG1RqU3aHb/nW0f2D3FzcQsSxlZvuRCMPkzZ7wKfghgv2YYV3CaDqew5BAo6PViV7bGtC9xAwzHW/bDA8nXw96jBhITb7ARBX8O2jdXhU6Q2Y0T60KMxE01LXEWP ";
- private VuforiaLocalizer vuforia;
- private TFObjectDetector tfod;
- int DISTANCE = (int)Math.floor(INCHES) * (int)Math.floor(COUNTS_PER_INCH);
- static final double PI=3.1416;
- static final double COUNTS_PER_MOTOR_REV = 28 ; // eg: TETRIX Motor Encoder
- static final double DRIVE_GEAR_REDUCTION = 50 ;
- static final double WHEEL_DIAMETER= 4; // This is < 1.0 if geared UP // For figuring circumference
- static final double COUNTS_PER_INCH = (COUNTS_PER_MOTOR_REV * DRIVE_GEAR_REDUCTION) / (PI*WHEEL_DIAMETER);
- double power;
- @Override
- public void runOpMode() {
- Vuforia.init();
- if (ClassFactory.getInstance().canCreateTFObjectDetector())
- {
- initTfod();
- }
- else
- {
- telemetry.addData("Sorry!", "This device is not compatible with TFOD");
- }
- if (tfod != null) {
- tfod.activate();
- }
- BNO055IMU.Parameters imuParameters;
- Orientation angles;
- telemetry.addData("Status", "Initialized");
- telemetry.update();
- imu = hardwareMap.get(BNO055IMU.class, "imu");
- BackLeft = hardwareMap.dcMotor.get("BackLeft");
- BackRight = hardwareMap.dcMotor.get("BackRight");
- FrontLeft = hardwareMap.dcMotor.get("FrontLeft");
- FrontRight = hardwareMap.dcMotor.get("FrontRight");
- BackRight.setDirection(DcMotorSimple.Direction.REVERSE);
- FrontRight.setDirection(DcMotorSimple.Direction.REVERSE);
- // Create new IMU Parameters object.
- imuParameters = new BNO055IMU.Parameters();
- // Use degrees as angle unit.
- imuParameters.angleUnit = BNO055IMU.AngleUnit.DEGREES;
- // Express acceleration as m/s^2.
- // Disable logging.
- imuParameters.loggingEnabled = false;
- // Initialize IMU.
- imu.initialize(imuParameters);
- angles = imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
- waitForStart();
- while (opModeIsActive()&&!!isStopRequested())
- {
- 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());
- // step through the list of recognitions and display boundary info.
- int i = 0;
- for (Recognition recognition : updatedRecognitions) {
- telemetry.addData(String.format("label (%d)", i), recognition.getLabel());
- telemetry.addData(String.format(" left,top (%d)", i), "%.03f , %.03f",
- recognition.getLeft(), recognition.getTop());
- telemetry.addData(String.format(" right,bottom (%d)", i), "%.03f , %.03f",
- recognition.getRight(), recognition.getBottom());
- }
- }
- }
- if (tfod != null) {
- tfod.shutdown();
- }
- // Display orientation info.
- telemetry.addData("rot about Z", angles.firstAngle);
- telemetry.addData("rot about Y", angles.secondAngle);
- telemetry.addData("rot about X", angles.thirdAngle);
- imuAngle = angles.firstAngle;
- while(Math.abs(gyroRotate(25, imu.getAngularOrientation().firstAngle, false, .01)) > THRESHOLD);
- telemetry.update();
- runtime.reset();
- }
- }
- public void DriveForwardDistance (double power, double INCHES){
- int DISTANCE = (int)Math.floor(INCHES) * (int)Math.floor(COUNTS_PER_INCH);
- int prev = FrontLeft.getCurrentPosition() +FrontRight.getCurrentPosition();
- FrontLeft.setPower(power);
- BackLeft.setPower(power);
- FrontRight.setPower(power);
- BackRight.setPower(power);
- while(FrontLeft.getCurrentPosition() + FrontRight.getCurrentPosition() < prev + DISTANCE) ;
- FrontLeft.setPower(0.0);
- BackLeft.setPower(0.0);
- FrontRight.setPower(0.0);
- BackRight.setPower(0.0);
- telemetry.update();
- }
- public void DriveRightDistance (double power, double INCHES){
- int DISTANCE = (int)Math.floor(INCHES) * (int)Math.floor(COUNTS_PER_INCH);
- double DistanceComp = (DISTANCE/(Math.sin(45)));
- int prev = FrontLeft.getCurrentPosition() - FrontRight.getCurrentPosition();
- FrontLeft.setPower(power*.9);
- BackLeft.setPower(-power);
- FrontRight.setPower(-power);
- BackRight.setPower(power*.9);
- while(FrontLeft.getCurrentPosition() - FrontRight.getCurrentPosition() < prev + DistanceComp);
- FrontLeft.setPower(0.0);
- BackLeft.setPower(0.0);
- FrontRight.setPower(0.0);
- BackRight.setPower(0.0);
- telemetry.update();
- }
- public void DriveLeftDistance (double power, double INCHES){
- int DISTANCE = (int)Math.floor(INCHES) * (int)Math.floor(COUNTS_PER_INCH);
- double DistanceComp = (DISTANCE/(Math.sin(45)));
- int prev = FrontRight.getCurrentPosition() - FrontLeft.getCurrentPosition();
- FrontLeft.setPower(-power);
- BackLeft.setPower(power);
- FrontRight.setPower(power);
- BackRight.setPower(-power);
- while(FrontRight.getCurrentPosition() - FrontLeft.getCurrentPosition() < prev + DistanceComp);
- FrontLeft.setPower(0.0);
- BackLeft.setPower(0.0);
- FrontRight.setPower(0.0);
- BackRight.setPower(0.0);
- telemetry.update();
- }
- public void DriveReturnDistance (double power, double INCHES){
- int DISTANCE = (int)Math.floor(INCHES) * (int)Math.floor(COUNTS_PER_INCH);
- int prev = (-FrontLeft.getCurrentPosition() -FrontRight.getCurrentPosition());
- FrontLeft.setPower(-power);
- BackLeft.setPower(-power);
- FrontRight.setPower(-power);
- BackRight.setPower(-power);
- while(-FrontLeft.getCurrentPosition() - FrontRight.getCurrentPosition() < prev + DISTANCE) ;
- FrontLeft.setPower(0.0);
- BackLeft.setPower(0.0);
- FrontRight.setPower(0.0);
- BackRight.setPower(0.0);
- telemetry.update();
- }
- public double gyroRotate(double target, double current, boolean zeroTo360, double Kp){
- if(zeroTo360 && current < 0)
- {
- current+= 360;
- }
- double err = current - target;
- FrontRight.setPower(err*Kp);
- FrontLeft.setPower(-err*Kp);
- BackRight.setPower(err*Kp);
- BackLeft.setPower(-err*Kp);
- return err;
- }
- 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 TensorFlow Object Detection engine.
- }
- private void initTfod() {
- int tfodMonitorViewId = hardwareMap.appContext.getResources().getIdentifier(
- "tfodMonitorViewId", "id", hardwareMap.appContext.getPackageName());
- TFObjectDetector.Parameters tfodParameters = new TFObjectDetector.Parameters(tfodMonitorViewId);
- tfodParameters.minimumConfidence = 0.8;
- tfod = ClassFactory.getInstance().createTFObjectDetector(tfodParameters, vuforia);
- tfod.loadModelFromAsset(TFOD_MODEL_ASSET, LABEL_FIRST_ELEMENT, LABEL_SECOND_ELEMENT);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement