Advertisement
Guest User

Robot.java

a guest
Jul 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.43 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. // TODO: move the position data here
  50. // private int xPosGlobal;
  51. // ...
  52.  
  53.  
  54. public Robot(HardwareMap hardwareMap, Telemetry telemetry, LinearOpMode linearOpMode){
  55. this.telemetry = telemetry;
  56. this.hardwareMap = hardwareMap;
  57. this.linearOpMode = linearOpMode;
  58.  
  59. //config names need to match configs on the phone
  60. //Map drive motors
  61. fLeft = hardwareMap.dcMotor.get("fLeft");
  62. fRight = hardwareMap.dcMotor.get("fRight");
  63. bLeft = hardwareMap.dcMotor.get("bLeft");
  64. bRight = hardwareMap.dcMotor.get("bRight");
  65.  
  66. //Set direction of drive motors
  67. fLeft.setDirection(DcMotor.Direction.FORWARD);
  68. fRight.setDirection(DcMotor.Direction.REVERSE);
  69. bLeft.setDirection(DcMotor.Direction.FORWARD);
  70. bRight.setDirection(DcMotor.Direction.REVERSE);
  71. }
  72.  
  73. public void setDrivePower(double power){
  74. fLeft.setPower(power);
  75. fRight.setPower(power);
  76. bLeft.setPower(power);
  77. bRight.setPower(power);
  78. }
  79.  
  80. public void intializeIMU(){
  81. BNO055IMU.Parameters parameters = new BNO055IMU.Parameters();
  82. parameters.angleUnit = BNO055IMU.AngleUnit.DEGREES;
  83. parameters.accelUnit = BNO055IMU.AccelUnit.METERS_PERSEC_PERSEC;
  84. parameters.calibrationDataFile = "BNO055IMUCalibration.json"; // see the calibration sample opmode
  85. parameters.loggingEnabled = true;
  86. parameters.loggingTag = "IMU";
  87. parameters.accelerationIntegrationAlgorithm = new JustLoggingAccelerationIntegrator();
  88.  
  89. imu = hardwareMap.get(BNO055IMU.class, "imu");
  90. imu.initialize(parameters);
  91. // TODO: I doubt you want to call startAccelerationIntegration()
  92. // the information it produces is useless and it secretly reads from another thread, potentially slowing your loops
  93. imu.startAccelerationIntegration(new Position(), new Velocity(), 1000);
  94.  
  95. angles = imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.YXZ, AngleUnit.DEGREES);
  96. }
  97.  
  98. public void resetEncoders(){
  99. fLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  100. fRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  101. bLeft.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  102. bRight.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  103. }
  104.  
  105. public void changeRunModeToUsingEncoder(){
  106. fLeft.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  107. fRight.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  108. bLeft.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  109. bRight.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  110. }
  111.  
  112. public void setMotorMode(DcMotor.RunMode runMode){
  113. fLeft.setMode(runMode);
  114. fRight.setMode(runMode);
  115. bLeft.setMode(runMode);
  116. bRight.setMode(runMode);
  117. }
  118.  
  119. public double[] getPosUsingIncrements() {
  120. // TODO: you can remove this; the odometry equations don't have a time dependence
  121. long startTime = SystemClock.elapsedRealtime();
  122.  
  123. double[] positions = new double[3];
  124. final double radius = 2;
  125. final double encoderPerRevolution = 537.6;
  126. final double l = 7;
  127. final double w = 6.5;
  128.  
  129. // TODO: again, move these to the robot scope
  130. double xPosGlobal = 0;
  131. double yPosGlobal = 0;
  132. double angleGlobal = 0;
  133.  
  134. // find robot position
  135. double fl = 2 * Math.PI * fLeft.getCurrentPosition() / encoderPerRevolution; //radians each motor has travelled
  136. double fr = 2 * Math.PI * fRight.getCurrentPosition() / encoderPerRevolution;
  137. double bl = 2 * Math.PI * bLeft.getCurrentPosition() / encoderPerRevolution;
  138. double br = 2 * Math.PI * bRight.getCurrentPosition() / encoderPerRevolution;
  139.  
  140. double xPosRobot = radius/4 * (fl + bl + br + fr);
  141. double yPosRobot = radius/4 * (-fl + bl - br + fr);
  142. double angleRobot = radius/4 *(-fl/(l+w) - bl/(l+w) + br/(l+w) + fr/(l+w));
  143. // TODO: since you use this angle for further computations, I wouldn't convert to degrees
  144. // Java requires radians for Math.cos(), Math.sin(), etc.
  145. angleRobot = angleRobot * 180/(2*Math.PI);
  146.  
  147. //converting to global frame
  148. // TODO: remove this lopp
  149. // you want this code to run exactly once per loop and the op mode already does that
  150. while(linearOpMode.opModeIsActive()){
  151.  
  152. long elapsedTime = SystemClock.elapsedRealtime() - startTime;
  153.  
  154. //these variables have random names, used to make typing xPosGlobal and yPosGlobal easier
  155. // TODO: because fl, fr, bl, and br are all *deltas*, xPosRobot, yPosRobot, angleRobot are already deltas
  156. // therefore you don't need to multiply by elapsedTime (you don't need elapsedTime at all!)
  157. double angleDeltaRobot = angleRobot * elapsedTime;
  158. double xDeltaRobot = xPosRobot * elapsedTime;
  159. double yDeltaRobot = yPosRobot * elapsedTime;
  160.  
  161. //finding global position
  162. // TODO: I don't really have the bandwidth to check this; I recommend splitting the two matrix multiplies into two sets of computations
  163. // I intentionally split the matrices for this exact purpose
  164. 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;
  165. 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;
  166. angleGlobal += angleDeltaRobot;
  167.  
  168. // TODO: minor point but you can just do
  169. // return new double[] { xPosGlobal, yPosGlobal, angleGlobal };
  170. // or better yet make this method void and add getters for the global position
  171. //inserting into return array
  172. positions[0] += xPosGlobal;
  173. positions[1] += yPosGlobal;
  174. positions[2] += angleGlobal;
  175.  
  176. // TODO: you shouldn't reset encoders in a loop as it wastes time
  177. // Instead just store the last positions yourself and compute the deltas manually
  178. resetEncoders();
  179. // TODO: there's really no reason to sleep with REV hardware since the calls are blocking
  180. // Sleeping makes your loops slower and hurts performance
  181. linearOpMode.sleep(10);
  182. }
  183. return positions;
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement