Advertisement
Guest User

asdfasdfasdf

a guest
Jul 17th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.73 KB | None | 0 0
  1. package org.firstinspires.ftc.teamcode.Skystone;
  2.  
  3.  
  4. import android.os.SystemClock;
  5.  
  6. import com.qualcomm.hardware.bosch.BNO055IMU;
  7. import com.qualcomm.hardware.bosch.JustLoggingAccelerationIntegrator;
  8. import com.qualcomm.hardware.rev.Rev2mDistanceSensor;
  9. import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
  10. import com.qualcomm.robotcore.hardware.DcMotor;
  11. import com.qualcomm.robotcore.hardware.HardwareMap;
  12. import com.qualcomm.robotcore.hardware.Servo;
  13. import com.qualcomm.robotcore.util.Range;
  14.  
  15. import org.firstinspires.ftc.robotcore.external.Telemetry;
  16. import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
  17. import org.firstinspires.ftc.robotcore.external.navigation.AxesOrder;
  18. import org.firstinspires.ftc.robotcore.external.navigation.AxesReference;
  19. import org.firstinspires.ftc.robotcore.external.navigation.DistanceUnit;
  20. import org.firstinspires.ftc.robotcore.external.navigation.Orientation;
  21. import org.firstinspires.ftc.robotcore.external.navigation.Position;
  22. import org.firstinspires.ftc.robotcore.external.navigation.Velocity;
  23. import java.util.Timer;
  24.  
  25. import java.util.Timer;
  26.  
  27. import static org.firstinspires.ftc.robotcore.external.navigation.DistanceUnit.CM;
  28. import static org.firstinspires.ftc.robotcore.external.navigation.DistanceUnit.MM;
  29.  
  30. public class Robot {
  31.     //Drive Motors
  32.     public DcMotor fLeft;
  33.     public DcMotor fRight;
  34.     public DcMotor bLeft;
  35.     public DcMotor bRight;
  36.  
  37.     //imu
  38.     private BNO055IMU imu;
  39.     private Orientation angles;
  40.     private Position position;
  41.  
  42.     //Inherited classes from Op Mode
  43.     private Telemetry telemetry;
  44.     private HardwareMap hardwareMap;
  45.     private LinearOpMode linearOpMode;
  46.  
  47.     //PID (concept only)
  48.  
  49.  
  50.  
  51.     public Robot(HardwareMap hardwareMap, Telemetry telemetry, LinearOpMode linearOpMode){
  52.         this.telemetry = telemetry;
  53.         this.hardwareMap = hardwareMap;
  54.         this.linearOpMode = linearOpMode;
  55.  
  56.         //config names need to match configs on the phone
  57.         //Map drive motors
  58.         fLeft = hardwareMap.dcMotor.get("fLeft");
  59.         fRight = hardwareMap.dcMotor.get("fRight");
  60.         bLeft = hardwareMap.dcMotor.get("bLeft");
  61.         bRight = hardwareMap.dcMotor.get("bRight");
  62.  
  63.         //Set direction of drive motors
  64.         fLeft.setDirection(DcMotor.Direction.FORWARD);
  65.         fRight.setDirection(DcMotor.Direction.REVERSE);
  66.         bLeft.setDirection(DcMotor.Direction.FORWARD);
  67.         bRight.setDirection(DcMotor.Direction.REVERSE);
  68.     }
  69.  
  70.     public void setDrivePower(double power){
  71.         fLeft.setPower(power);
  72.         fRight.setPower(power);
  73.         bLeft.setPower(power);
  74.         bRight.setPower(power);
  75.     }
  76.  
  77.     public void intializeIMU(){
  78.         BNO055IMU.Parameters parameters = new BNO055IMU.Parameters();
  79.         parameters.angleUnit           = BNO055IMU.AngleUnit.DEGREES;
  80.         parameters.accelUnit           = BNO055IMU.AccelUnit.METERS_PERSEC_PERSEC;
  81.         parameters.calibrationDataFile = "BNO055IMUCalibration.json"; // see the calibration sample opmode
  82.         parameters.loggingEnabled      = true;
  83.         parameters.loggingTag          = "IMU";
  84.         parameters.accelerationIntegrationAlgorithm = new JustLoggingAccelerationIntegrator();
  85.  
  86.         imu = hardwareMap.get(BNO055IMU.class, "imu");
  87.         imu.initialize(parameters);
  88.         imu.startAccelerationIntegration(new Position(), new Velocity(), 1000);
  89.  
  90.         angles = imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.YXZ, AngleUnit.DEGREES);
  91.     }
  92.  
  93.     public void resetEncoders(){
  94.         fLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  95.         fRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  96.         bLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  97.         bRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  98.     }
  99.  
  100.     public void changeRunModeToUsingEncoder(){
  101.         fLeft.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  102.         fRight.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  103.         bLeft.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  104.         bRight.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  105.     }
  106.  
  107.     public void setMotorMode(DcMotor.RunMode runMode){
  108.         fLeft.setMode(runMode);
  109.         fRight.setMode(runMode);
  110.         bLeft.setMode(runMode);
  111.         bRight.setMode(runMode);
  112.     }
  113.    
  114.     public double[] getPosUsingIncrements() {
  115.         long startTime = SystemClock.elapsedRealtime();
  116.  
  117.         double[] positions = new double[3];
  118.         final double radius = 2;
  119.         final double encoderPerRevolution = 537.6;
  120.         final double l = 7;
  121.         final double w = 6.5;
  122.  
  123.         double xPosGlobal = 0;
  124.         double yPosGlobal = 0;
  125.         double angleGlobal = 0;
  126.  
  127.         // find robot position
  128.         double fl = 2 * Math.PI * fLeft.getCurrentPosition() / encoderPerRevolution; //radians each motor has travelled
  129.         double fr = 2 * Math.PI * fRight.getCurrentPosition() / encoderPerRevolution;
  130.         double bl = 2 * Math.PI * bLeft.getCurrentPosition() / encoderPerRevolution;
  131.         double br = 2 * Math.PI * bRight.getCurrentPosition() / encoderPerRevolution;
  132.  
  133.         double xPosRobot = radius/4 * (fl + bl + br + fr);
  134.         double yPosRobot = radius/4 * (-fl + bl - br + fr);
  135.         double angleRobot = radius/4 *(-fl/(l+w) - bl/(l+w) + br/(l+w) + fr/(l+w));
  136.         angleRobot = angleRobot * 180/(2*Math.PI);
  137.  
  138.         //converting to global frame
  139.         while(linearOpMode.opModeIsActive()){
  140.  
  141.             long elapsedTime = SystemClock.elapsedRealtime() - startTime;
  142.  
  143.             //these variables have random names, used to make typing xPosGlobal and yPosGlobal easier
  144.             double angleDeltaRobot = angleRobot * elapsedTime;
  145.             double xDeltaRobot = xPosRobot * elapsedTime;
  146.             double yDeltaRobot = yPosRobot * elapsedTime;
  147.  
  148.             //finding global position
  149.             xPosGlobal += (Math.cos(angleGlobal) * Math.sin(angleDeltaRobot) - (Math.cos(angleDeltaRobot) - 1) * Math.sin(angleGlobal)) * xDeltaRobot / angleDeltaRobot + (Math.cos(angleGlobal) * (Math.cos(angleDeltaRobot) - 1) - Math.sin(angleGlobal) * Math.sin(angleDeltaRobot)) * yDeltaRobot / angleDeltaRobot;
  150.             yPosGlobal += ((Math.cos(angleDeltaRobot) - 1) * Math.sin(angleGlobal) + (Math.cos(angleGlobal)) * Math.sin(angleDeltaRobot)) * yDeltaRobot / angleDeltaRobot + (Math.cos(angleGlobal) * (Math.cos(angleDeltaRobot) - 1) + Math.sin(angleGlobal) * Math.sin(angleDeltaRobot)) * xDeltaRobot / angleDeltaRobot;
  151.             angleGlobal += angleDeltaRobot;
  152.  
  153.             //inserting into return array
  154.             positions[0] += xPosGlobal;
  155.             positions[1] += yPosGlobal;
  156.             positions[2] += angleGlobal;
  157.  
  158.             resetEncoders();
  159.             linearOpMode.sleep(10);
  160.         }
  161.         return positions;
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement