Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.firstinspires.ftc.teamcode;
- import android.media.MediaPlayer;
- import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
- import com.qualcomm.robotcore.hardware.ColorSensor;
- import com.qualcomm.robotcore.hardware.DcMotor;
- import com.qualcomm.robotcore.hardware.I2cAddr;
- import com.qualcomm.robotcore.hardware.OpticalDistanceSensor;
- import com.qualcomm.robotcore.hardware.Servo;
- /**
- * Created by Andrew on 1/17/2017.
- */
- public abstract class InitCode extends LinearOpMode {
- //Line Sensor
- final int LLMIN = 20;
- final int LRMIN = 7;
- double leftLinePowerBCK = -0.2;
- double RightLinePowerBCK = -0.2;
- double leftLinePowerFWD = 0.15;
- double RightLinePowerFWD = 0.15;
- MediaPlayer Vader;
- DcMotor motor1; //left wheel
- DcMotor motor2; //right wheel
- DcMotor motor3; //shooting motor
- ColorSensor color1; //left color sensor
- ColorSensor color2; //right color sensor
- ColorSensor line1; //left line sensor
- ColorSensor line2; //right line sensor
- OpticalDistanceSensor optical1;
- Servo servo1; //left button pusher
- Servo servo2; //right button pusher
- public void initSound()
- {
- Vader = MediaPlayer.create(hardwareMap.appContext, R.raw.bottheme);
- }
- public void initMotors()
- {
- motor1 = hardwareMap.dcMotor.get("motor1");
- motor2 = hardwareMap.dcMotor.get("motor2");
- motor3 = hardwareMap.dcMotor.get("motor3");
- motor1.setDirection(DcMotor.Direction.FORWARD); // remember to change directions for team color
- motor2.setDirection(DcMotor.Direction.REVERSE);
- motor3.setDirection(DcMotor.Direction.FORWARD);
- motor1.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- motor2.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- motor3.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
- }
- public void initSensors()
- {
- color1 = hardwareMap.colorSensor.get("color1");
- color2 = hardwareMap.colorSensor.get("color2");
- optical1 = hardwareMap.opticalDistanceSensor.get("optical1");
- color1.setI2cAddress(I2cAddr.create8bit(0x6c));
- color2.setI2cAddress(I2cAddr.create8bit(0x7c));
- line1 = hardwareMap.colorSensor.get("line1"); //left line sensor
- line2 = hardwareMap.colorSensor.get("line2"); //right line sensor
- line1.setI2cAddress(I2cAddr.create8bit(0x4c));
- line2.setI2cAddress(I2cAddr.create8bit(0x5c));
- }
- public void initServos()
- {
- servo1 = hardwareMap.servo.get("servo1");
- servo2 = hardwareMap.servo.get("servo2");
- servo1.setPosition(1);
- servo2.setPosition(0);
- }
- public void initAll()
- {
- initMotors();
- initServos();
- initSensors();
- initSound();
- }
- }
- Above is our init class. I have a methods class and the end product (one for each side). I'll be posting methods and Red Side below.
- package org.firstinspires.ftc.teamcode;
- import android.media.MediaPlayer;
- import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
- import com.qualcomm.robotcore.hardware.ColorSensor;
- import com.qualcomm.robotcore.hardware.DcMotor;
- import com.qualcomm.robotcore.hardware.I2cAddr;
- import com.qualcomm.robotcore.hardware.OpticalDistanceSensor;
- import com.qualcomm.robotcore.hardware.Servo;
- /**
- * Created by Cole on 12/27/2016.
- */
- public abstract class BaseAutonomous extends InitCode {
- //21 pos: 1 degree
- //280/pi pos : 1 inch
- //RED CNST BELOW
- private boolean dank = false;
- //1st turn
- public final int M1T1 = 0;
- public final int M2T1 = degreesToPositions(43);
- public final double PWRT1 = 0.5;
- //2nd turn
- public final int M1T2 = 0;
- public final int M2T2 = degreesToPositions(-35);
- public final double PWRT2 = -0.5;
- //2.5th turn
- public final int M1T25 = degreesToPositions(5);
- public final int M2T25 = 0;
- public final double PWRT25 = 0.5;
- //3rd turn
- public final int M1T3 = degreesToPositions(-45);
- public final int M2T3 = 0;
- public final double PWRT3 = -.25;
- //4th turn
- public final int M1T4 = degreesToPositions(37);
- public final int M2T4 = 0;
- public final double PWRT4 = 0.25;
- //1st move
- public final int PM1 = inchesToPositions(60);
- public final double PWRM1 = 0.5;
- //linefollow increment
- public int LMCNST1 = inchesToPositions(-2);
- public int LMCNST2 = inchesToPositions(-2);
- public double LPWR = -0.2;
- public double LPWR2 = 0.2;
- //2nd move
- public final int PM2 = (int) inchesToPositions(-10);
- public final double PWRM2 = 0.2;
- //3rd move
- public final int PM3 = inchesToPositions(6);
- public final double PWRM3 = 0.2;
- //4th move
- public final int PM4 = inchesToPositions(54);
- public final double PWRM4 = 0.25;
- //5th move
- public final int PM5 = inchesToPositions(15);
- public final double PWRM5 = 0.5;
- //6th move
- public final int PM6 = inchesToPositions(-75);
- public final double PWRM6 = -0.7;
- //Beacon
- public final int PBM = 37;
- public final double PWRBM = 1.0;
- //BLUE CNST BELOW
- //Move 1
- public final int MOVE1B = inchesToPositions(18);
- public final double MOVE1SPD = 0.5;
- //Move 2
- public final int MOVE2B = inchesToPositions(-60);
- public final double MOVE2SPD = -0.5;
- //Move 3
- public final int MOVE3B = inchesToPositions(-12);
- public final double MOVE3SPD = -0.2;
- //Move 4
- public final int MOVE4B = inchesToPositions(15);
- public final double MOVE4SPD = 0.5;
- //Turn 1
- public final int TURN1B = degreesToPositions(-160);
- public final int TURN1B2 = 0;
- public final double TURN1SPD = -0.5;
- //Turn 2
- public final int TURN2B = 0;
- public final int TURN2B2 = degreesToPositions(47);
- public final double TURN2SPD = 0.5;
- //Turn 3
- public final int TURN3B = 0;
- public final int TURN3B2 = degreesToPositions(-90);
- public final double TURN3SPD = -0.5;
- //Turn 4
- public final int TURN4B = degreesToPositions(-23);
- public final int TURN4B2 = degreesToPositions(22);
- public final double TURN4SPD = 0.5;
- public void move(int addPos, double power) {
- int currentPos1 = motor1.getCurrentPosition();
- int currentPos2 = motor2.getCurrentPosition();
- motor1.setTargetPosition(currentPos1 + addPos);
- motor2.setTargetPosition(currentPos2 + addPos);
- motor1.setPower(power);
- motor2.setPower(power);
- while (opModeIsActive() && motor1.isBusy() && motor2.isBusy());
- motor1.setPower(0);
- motor2.setPower(0);
- }
- public void shoot(double power) {
- final int POS_INCREMENT = 1000;
- int currentPos3 = motor3.getCurrentPosition();
- motor3.setTargetPosition(currentPos3 + POS_INCREMENT);
- motor3.setPower(power);
- while (opModeIsActive() && motor3.isBusy());
- motor3.setPower(0);
- }
- public void turn(int pos1, int pos2, double power) {
- int currentPos1 = motor1.getCurrentPosition();
- int currentPos2 = motor2.getCurrentPosition();
- motor1.setTargetPosition(pos1 + currentPos1);
- motor2.setTargetPosition(pos2 + currentPos2);
- motor1.setPower(power);
- motor2.setPower(power);
- while (motor1.isBusy() || motor2.isBusy() && opModeIsActive());
- motor1.setPower(0);
- motor2.setPower(0);
- }
- private boolean seesLineLeft() {
- return ((line1.alpha() > LLMIN));
- }
- private boolean seesLineRight() {
- return ((line2.alpha() > LRMIN));
- }
- public void pushBeaconRed() throws InterruptedException{
- if(color1.red() > color2.red()|| color1.blue() < color2.blue()) {
- servo2.setPosition(1);
- sleep(500);
- servo2.setPosition(0);
- sleep(500);
- servo2.setPosition(1);
- }
- else if (color1.red() < color2.red() || color2.blue() < color1.blue()) {
- servo1.setPosition(0);
- sleep(500);
- servo1.setPosition(1);
- sleep(500);
- servo1.setPosition(0);
- }
- sleep(500);
- servo1.setPosition(1);
- servo2.setPosition(0);
- }
- public void pushBeaconBlue() throws InterruptedException{
- if(color1.red() < color2.red()|| color1.blue() > color2.blue()) {
- servo2.setPosition(1);
- sleep(500);
- servo2.setPosition(0);
- sleep(500);
- servo2.setPosition(1);
- }
- else if (color1.red() > color2.red() || color2.blue() > color1.blue()) {
- servo1.setPosition(0);
- sleep(500);
- servo1.setPosition(1);
- sleep(500);
- servo1.setPosition(0);
- }
- sleep(500);
- servo1.setPosition(1);
- servo2.setPosition(0);
- }
- public void MLG360quickscopeNOSCOPEbillythekidmtndewDORITOSBasti0nMaIn(boolean dank) {
- if(!dank) {
- motor1.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
- motor2.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
- motor1.setPower(0.75);
- motor2.setPower(-0.75);
- }
- }
- private int degreesToPositions(int degrees){
- return degrees*21;
- }
- private int inchesToPositions(double inches) {
- return (int) Math.round(inches*280/Math.PI);
- }
- public boolean seesLineBCK()
- {
- motor1.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
- motor2.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
- motor1.setPower(leftLinePowerBCK);
- motor2.setPower(RightLinePowerBCK);
- if (motor1.getPower() == 0 && motor2.getPower() == 0) //only if both see the line
- {
- motor1.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- motor2.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- motor1.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- motor2.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- leftLinePowerBCK = -0.2;
- RightLinePowerBCK = -0.2;
- return true;
- }
- else if (seesLineLeft()) //runs left sensor function
- {
- leftLinePowerBCK = 0;
- motor1.setPower(0);
- return false;
- }
- else if (seesLineRight()) //runs right sensor function
- {
- RightLinePowerBCK = 0;
- motor2.setPower(0);
- return false;
- }
- return false;
- }
- public boolean seesLineFWD()
- {
- motor1.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- motor2.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
- motor1.setPower(leftLinePowerFWD);
- motor2.setPower(RightLinePowerFWD);
- if (motor1.getPower() == 0 && motor2.getPower() == 0) //only if both see the line
- {
- motor1.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- motor2.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- motor1.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- motor2.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- return true;
- }
- else if (seesLineLeft()) //runs left sensor function
- {
- leftLinePowerFWD = 0;
- motor1.setPower(0);
- return false;
- }
- else if (seesLineRight()) //runs right sensor function
- {
- RightLinePowerFWD = 0;
- motor2.setPower(0);
- return false;
- }
- return false;
- }
- public void resetEncoders()
- {
- motor1.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- motor2.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
- }
- }
- Below is our red side autonomous code.
- package org.firstinspires.ftc.teamcode;
- import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
- import com.qualcomm.robotcore.hardware.DcMotor;
- /**
- * Created by Andrew on 1/17/2017.
- */
- @Autonomous
- public class RedAutonomous extends BaseAutonomous {
- @Override
- public void runOpMode() throws InterruptedException {
- initAll();
- waitForStart();
- Vader.start();
- motor3.setPower(0.8); //Shooting motor
- sleep(1600);
- motor3.setPower(0); //Stop shooting
- optical1.enableLed(true);
- turn(M1T1, M2T1, PWRT1); //45 deg turn
- move(PM1, PWRM1); //runs to the wall
- move(PM3, PWRM3);
- while(optical1.getLightDetected() < 0.06 && opModeIsActive()) {
- motor1.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
- motor2.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
- motor1.setPower(0.2);
- motor2.setPower(0.2);
- }
- motor1.setPower(0);
- motor2.setPower(0);
- motor1.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- motor2.setMode(DcMotor.RunMode.RUN_TO_POSITION);
- turn(M1T2, M2T2, PWRT2); //turns parallel to the wall
- resetEncoders();
- while(!seesLineBCK() && opModeIsActive()); // runs until it detects a line
- pushBeaconRed(); //hits beacons
- move(PM4,PWRM4);
- turn(M1T25, M2T25, PWRT25);
- resetEncoders();
- while(!seesLineBCK() && opModeIsActive());
- pushBeaconRed(); //hits beacons
- move(PM5, PWRM5);
- turn(M1T3,M2T3,PWRT3); //turns towards cap ball
- //turn(M1T4,M2T4, PWRT4);
- move(PM6,PWRM6); //runs into cap ball, knocks off the ball and parks
- Vader.stop();
- }
- }
- Everything does what it's supposed to, but it's just that seesline function that is strange, because other parts of our code have the motors running with no issue at that power.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement