Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.DcMotorSimple;
- import com.vuforia.HINT;
- import com.vuforia.Vuforia;
- import org.firstinspires.ftc.robotcore.external.ClassFactory;
- import org.firstinspires.ftc.robotcore.external.matrices.VectorF;
- import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer;
- import org.firstinspires.ftc.robotcore.external.navigation.VuforiaTrackableDefaultListener;
- import org.firstinspires.ftc.robotcore.external.navigation.VuforiaTrackables;
- @Autonomous(name="VuforiaOP_test")
- public class VuforiaOP_Test extends LinearOpMode {
- public void runOpMode() throws InterruptedException {
- HardWare11226 m_robot = new HardWare11226();
- final double CAMERA_FORWARD_DISPLACEMENT = 100; // Camera is 100 mm in front of robot center
- final double WHEEL_CIRCUMFERENCE = 315.993955469;
- final double MOTOR_CPR = 1120;
- // final int CAMERA_VERTICAL_DISPLACEMENT = 200; // Camera is 200 mm above ground
- // final int CAMERA_LEFT_DISPLACEMENT = 0; // Camera is ON the robots center line
- boolean ran_once = false;
- VuforiaLocalizer.Parameters params = new VuforiaLocalizer.Parameters(R.id.cameraMonitorViewId); // Show the camera on the screen of the phone, to save battery you can stop it by removing "R.id.cameraMonitorViewId".
- params.cameraDirection = VuforiaLocalizer.CameraDirection.BACK; // tell which camera is used.
- params.vuforiaLicenseKey = "ASGVUHP/////AAAAGfG8A3nPG0JxkZdKAEiS+7xCi4mxG2B2msUP9NQyzuGr6Py/efOiOgwPc5NQtnY3IgLHHTC0o7UtcDSMVbXG+XJ2UUEE7DzELEFsCcaM94K+awMwovz4QyctBwZYloUG1Itj2rSzByEMP8zMFPfftW474YRrZdnrnirdYu/JtwUaQgcdK2ZrYmvbfBFZlouyPZeVaWt1a559yOhOnEj2lhrfjnL7TYb7m/lth87yFTs4DTswazns1Y5RUB+Em3yCwUQnttIpsRrBRF4b42mysHQWNK3lDyBkrsiFI0BlTbfTYQR3pUp0Us3y+5TivoVBy6n+5IpBwPysU8Da5YLHdmroPcLDZcV58WPR0QtT2ew5"; // THe LicenseKey of Vuforia.
- params.cameraMonitorFeedback = VuforiaLocalizer.Parameters.CameraMonitorFeedback.AXES; // show the axes on the screen.
- VuforiaLocalizer vuforia = ClassFactory.createVuforiaLocalizer(params);
- Vuforia.setHint(HINT.HINT_MAX_SIMULTANEOUS_IMAGE_TARGETS, 4); // Set 4 becaons on the field.
- VuforiaTrackables beacons = vuforia.loadTrackablesFromAsset("FTC_2016-17");
- beacons.get(0).setName("Wheels");
- beacons.get(1).setName("Tools");
- beacons.get(2).setName("Legos");
- beacons.get(3).setName("Gears");
- VuforiaTrackableDefaultListener wheels = (VuforiaTrackableDefaultListener) beacons.get(0).getListener();
- m_robot.init(hardwareMap);
- waitForStart();
- while (opModeIsActive() && !ran_once)
- {
- beacons.activate();
- m_robot.leftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- m_robot.rightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- m_robot.leftMotor.setDirection(DcMotor.Direction.FORWARD);
- m_robot.rightMotor.setDirection(DcMotor.Direction.REVERSE);
- while (wheels.getRawPose() == null)
- {
- m_robot.leftMotor.setPower(0.1);
- m_robot.rightMotor.setPower(0.1);
- }
- m_robot.leftMotor.setPower(0);
- m_robot.rightMotor.setPower(0);
- telemetry.addData("flag 1","made it");
- VectorF angles = anglesFromTarget(wheels);
- VectorF trans = navOffWall(wheels.getPose().getTranslation(), Math.toDegrees(angles.get(0)) - 90, new VectorF(500, 0, 0));
- while(wheels.getPose().getTranslation().get(0)> -50 && wheels.getPose().getTranslation().get(0) < 50)
- {
- if (trans.get(0) > 0)
- {
- m_robot.leftMotor.setPower(0.1);
- m_robot.rightMotor.setPower(-0.1);
- m_robot.collectMotor.setPower(0.1);
- sleep(50);
- m_robot.collectMotor.setPower(0);
- } else
- {
- m_robot.leftMotor.setPower(-0.1);
- m_robot.rightMotor.setPower(0.1);
- m_robot.collectMotor.setPower(0.1);
- sleep(50);
- m_robot.collectMotor.setPower(0);
- }
- trans = navOffWall(wheels.getPose().getTranslation(), Math.toDegrees(angles.get(0)) - 90, new VectorF(500, 0, 0));
- }
- m_robot.leftMotor.setPower(0);
- m_robot.rightMotor.setPower(0);
- /*
- if (trans.get(0) > 0)
- {
- m_robot.leftMotor.setPower(0.3);
- m_robot.rightMotor.setPower(-0.3);
- } else
- {
- m_robot.leftMotor.setPower(-0.3);
- m_robot.rightMotor.setPower(0.3);
- }
- telemetry.addData("flag 2","made it");
- do
- {
- if (wheels.getPose() != null)
- {
- trans = navOffWall(wheels.getPose().getTranslation(), Math.toDegrees(angles.get(0)) -90, new VectorF(500, 0, 0));
- }
- idle();
- }
- while (opModeIsActive() && Math.abs(trans.get(0)) > 30);*/
- m_robot.leftMotor.setPower(0);
- m_robot.rightMotor.setPower(0);
- telemetry.addData("flag 3","made it");
- m_robot.leftMotor.setTargetPosition((int) (m_robot.leftMotor.getCurrentPosition() + ((Math.hypot(trans.get(0), trans.get(2)) + CAMERA_FORWARD_DISPLACEMENT)
- / WHEEL_CIRCUMFERENCE * MOTOR_CPR)));
- m_robot.rightMotor.setTargetPosition((int) (m_robot.rightMotor.getCurrentPosition() + ((Math.hypot(trans.get(0), trans.get(2)) + CAMERA_FORWARD_DISPLACEMENT)
- / WHEEL_CIRCUMFERENCE * MOTOR_CPR)));
- m_robot.leftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- m_robot.rightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- m_robot.leftMotor.setPower(0.3);
- m_robot.rightMotor.setPower(0.3);
- while (m_robot.leftMotor.isBusy() && m_robot.rightMotor.isBusy()) {
- idle();
- }
- m_robot.leftMotor.setPower(0);
- m_robot.rightMotor.setPower(0);
- telemetry.addData("flag 4","made it");
- m_robot.leftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- m_robot.rightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- while ((wheels.getPose() == null || Math.abs(wheels.getPose().getTranslation().get(0)) > 10)) {
- if (wheels.getPose() != null) {
- if (wheels.getPose().getTranslation().get(0) > 0) {
- m_robot.leftMotor.setPower(-0.3);
- m_robot.rightMotor.setPower(0.3);
- }
- else {
- m_robot.leftMotor.setPower(0.3);
- m_robot.rightMotor.setPower(-0.3);
- }
- }
- else {
- m_robot.leftMotor.setPower(0.3);
- m_robot.rightMotor.setPower(-0.3);
- }
- }
- m_robot.leftMotor.setPower(0);
- m_robot.rightMotor.setPower(0);
- ran_once = true;
- telemetry.addData("flag 5","made it");
- }
- }
- private VectorF navOffWall(VectorF p_trans, double p_robotAngle, VectorF p_offWall) { // Set where the robot need to reach
- return new VectorF((float) (p_trans.get(0) - p_offWall.get(0) * Math.sin(Math.toRadians(p_robotAngle)) - p_offWall.get(2) * Math.cos(Math.toRadians(p_robotAngle))), p_trans.get(1), (float) (p_trans.get(2) + p_offWall.get(0) * Math.cos(Math.toRadians(p_robotAngle)) - p_offWall.get(2) * Math.sin(Math.toRadians(p_robotAngle))));
- }
- private VectorF anglesFromTarget(VuforiaTrackableDefaultListener p_image) {
- float[] data = p_image.getRawPose().getData();
- float[][] rotation = {{data[0], data[1]}, {data[4], data[5], data[6]}, {data[8], data[9], data[10]}};
- double thetaX = Math.atan2(rotation[2][1], rotation[2][2]);
- double thetaY = Math.atan2(-rotation[2][0], Math.sqrt(rotation[2][1] * rotation[2][1] + rotation[2][2] * rotation[2][2]));
- double thetaZ = Math.atan2(rotation[1][0], rotation[0][0]);
- return new VectorF((float) thetaX, (float) thetaY, (float) thetaZ);
- }
- }
- //https://github.com/gearsincorg/FTCVuforiaDemo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement