Advertisement
AsianInvasion

CurrentAutonCode

Feb 17th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.68 KB | None | 0 0
  1. package org.firstinspires.ftc.teamcode;
  2.  
  3. import com.qualcomm.robotcore.eventloop.opmode.OpMode;
  4. import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
  5. import java.util.List;
  6. import org.firstinspires.ftc.robotcore.external.ClassFactory;
  7. import org.firstinspires.ftc.robotcore.external.navigation.AxesOrder;
  8. import org.firstinspires.ftc.robotcore.external.navigation.AxesReference;
  9. import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer;
  10. import org.firstinspires.ftc.robotcore.external.navigation.VuforiaLocalizer.CameraDirection;
  11. import org.firstinspires.ftc.robotcore.external.tfod.TFObjectDetector;
  12. import org.firstinspires.ftc.robotcore.external.tfod.Recognition;
  13. import com.qualcomm.robotcore.hardware.Servo;
  14. import com.qualcomm.robotcore.hardware.DcMotor;
  15. import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
  16. import com.qualcomm.hardware.bosch.BNO055IMU;
  17. // use "import com.qualcomm.robotcore.hardware.Bot;" later, for robot.gyro.position or whatever but
  18. // not now
  19. // calibrate robot when it is on the ground
  20.  
  21.  
  22. //fellow comrades, please note that the encoder ticks below are just for reference, since they
  23. //must be calculated later on
  24. //this is for crater
  25.  
  26. @Autonomous(name = "Julia is the Best78999012")
  27.  
  28. //@Disabled
  29. public class TensorFlowIterative extends OpMode {
  30.  
  31. //Declare OpMode members
  32. private DcMotor upperLeftDrive = null;
  33. private DcMotor lowerLeftDrive = null;
  34. private DcMotor upperRightDrive = null;
  35. private DcMotor lowerRightDrive = null;
  36. private DcMotor hook = null;
  37. private Servo teamMarker = null;
  38. private DcMotor intakeArm = null;
  39. private BNO055IMU imu;
  40. private AngleUnit unit = AngleUnit.DEGREES;
  41.  
  42.  
  43. //these are example ratios and circumferences for the drive train,
  44. //change accordingly, btw for 2:1 it is 2, 80 tooth to 40 tooth
  45. //for gyro, i am assuming that the head of the robot is at 0 initially, and goes clockwise
  46.  
  47. private double DriveGearRatio = 1 / 2;
  48. private double DriveCircumference = (1.97 * 2) * Math.PI;
  49.  
  50. //not for the actual motor, but for the final gear
  51. private int ticksPerRotation = (int) (DriveGearRatio * 1120);
  52.  
  53. private static final String TFOD_MODEL_ASSET = "RoverRuckus.tflite";
  54. private static final String LABEL_GOLD_MINERAL = "Gold Mineral";
  55. private static final String LABEL_SILVER_MINERAL = "Silver Mineral";
  56. private static final String VUFORIA_KEY = "AbZUuPf/////AAAAGUmS0Chan00iu7rnRhzu63+JgDtPo889M6dNtjvv+WKxiMJ8w2DgSJdM2/zEI+a759I7DlPj++D2Ryr5sEHAg4k1bGKdo3BKtkSeh8hCy78w0SIwoOACschF/ImuyP/V259ytjiFtEF6TX4teE8zYpQZiVkCQy0CmHI9Ymoa7NEvFEqfb3S4P6SicguAtQ2NSLJUX+Fdn49SEJKvpSyhwyjbrinJbak7GWqBHcp7fGh7TNFcfPFMacXg28XxlvVpQaVNgkvuqolN7wkTiR9ZMg6Fnm0zN4Xjr5lRtDHeE51Y0bZoBUbyLWSA+ts3SyDjDPPUU7GMI+Ed/ifb0csVpM12aOiNr8d+HsfF2Frnzrj2";
  57. private VuforiaLocalizer vuforia;
  58. private TFObjectDetector tfod;
  59. private String mineralPosition = "";
  60. private int step = 1;
  61. private int nextstep = 0;
  62. private int targetPosition = 0;
  63. private int position = 1;
  64. private boolean fl;
  65. private boolean fr;
  66. private boolean dl;
  67. private boolean dr;
  68.  
  69.  
  70. @Override
  71. public void init() {
  72. // The TFObjectDetector uses the camera frames from the VuforiaLocalizer, so we create that
  73. // first.
  74.  
  75. telemetry.addData("Status", "Initialized");
  76. telemetry.update();
  77.  
  78. upperLeftDrive = hardwareMap.get(DcMotor.class, "front left");
  79. upperRightDrive = hardwareMap.get(DcMotor.class, "front right");
  80. lowerLeftDrive = hardwareMap.get(DcMotor.class, "back left");
  81. lowerRightDrive = hardwareMap.get(DcMotor.class, "back right");
  82. teamMarker = hardwareMap.get(Servo.class, "team marker");
  83. hook = hardwareMap.get(DcMotor.class, "hook");
  84. intakeArm = hardwareMap.get(DcMotor.class, "intake arm");
  85. imu = hardwareMap.get(BNO055IMU.class, "imu");
  86.  
  87. //initialization
  88. upperLeftDrive.setDirection(DcMotor.Direction.FORWARD);
  89. upperRightDrive.setDirection(DcMotor.Direction.REVERSE);
  90. lowerLeftDrive.setDirection(DcMotor.Direction.FORWARD);
  91. lowerRightDrive.setDirection(DcMotor.Direction.REVERSE);
  92.  
  93. }
  94.  
  95. @Override
  96. public void init_loop() {
  97. }
  98.  
  99. @Override
  100. public void start() {
  101. initVuforia();
  102. }
  103.  
  104. //if opMode is active
  105.  
  106. //assumes there's a bug in the code because there is no communication between the robot and the phones
  107. //state machine, upon finishing a state, one can go to another state
  108. //similar to a flow chart, its linear
  109. //make one variable in case 0, but in each case, make it different things, different ways to refer to the same object
  110. //do hook and vuforia
  111. //break makes it exit the switch statement
  112. //make a variable that is 8400
  113. @Override
  114. public void loop() {
  115.  
  116. switch(step) {
  117. case -1:
  118. fl = upperLeftDrive.getCurrentPosition() >= position - 20;
  119. fr = upperRightDrive.getCurrentPosition() >= position - 20;
  120. dl = lowerLeftDrive.getCurrentPosition() >= position - 20;
  121. dr = lowerRightDrive.getCurrentPosition() >= position - 20;
  122.  
  123. if (fl && fr && dl && dr) {
  124. //update to see if they are still under the target position
  125. step = nextstep;
  126. }
  127. break;
  128.  
  129. case 0:
  130. if (hook.getCurrentPosition() >= 8400 - 20) {
  131. step = nextstep;
  132. }
  133. break;
  134. case 1:
  135. DrivePosition(8400);
  136. nextstep = 2;
  137. step = 0;
  138. break;
  139. case 2:
  140. //here, position is 0
  141. position = DriveTrain(20, "backwards");
  142. nextstep = 3;
  143. step = -1;
  144. break;
  145. case 3:
  146. break;
  147. }
  148.  
  149. telemetry.addLine("" + hook.getCurrentPosition());
  150. telemetry.addLine(upperLeftDrive.getCurrentPosition() + "");
  151. telemetry.addLine(upperRightDrive.getCurrentPosition() + "");
  152. telemetry.addLine(lowerLeftDrive.getCurrentPosition() + "");
  153. telemetry.addLine(lowerRightDrive.getCurrentPosition() + "");
  154. telemetry.addLine(step + "");
  155. telemetry.addLine(position + "");
  156. telemetry.update();
  157.  
  158.  
  159.  
  160. /*
  161.  
  162. //move the hook
  163. DrivePosition(8400);
  164. if (hook.getCurrentPosition() <= 8400 - 20) {
  165. telemetry.addLine(hook.getCurrentPosition() + "");
  166. telemetry.update();
  167. } else if (hook.getCurrentPosition() >= 8400 - 20) {
  168. hook.setPower(0);
  169. } else {
  170. hook.setPower(0);
  171. }
  172.  
  173. //drive off
  174. DriveTrain(5, "backwards");
  175.  
  176. try {
  177. Thread.sleep(10000);
  178. }
  179. catch (Exception e) {
  180. telemetry.addLine("Error, weeb");
  181. telemetry.update();
  182. }
  183.  
  184. long startTime = System.currentTimeMillis(); //log when you started to turn
  185.  
  186. //point the phone to the minerals
  187. while (true) {
  188. gyroCorrect(270, 5, getGyroRotation(unit), .1, .4);
  189.  
  190. long currentTime = System.currentTimeMillis(); // get current time
  191. if (currentTime - startTime > 5000) { // if more than 5 seconds has passed, stop turning and procede
  192. break; //exit loop
  193. }
  194. }
  195.  
  196. //if tfod can be initiated
  197. if (ClassFactory.getInstance().canCreateTFObjectDetector()) {
  198. initTfod();
  199. } else {
  200. telemetry.addData("Sorry!", "This device is not compatible with TFOD");
  201. }
  202.  
  203. telemetry.update();
  204.  
  205. // Activate Tensor Flow Object Detection.
  206. if (tfod != null) {
  207. tfod.activate();
  208.  
  209. //allocate time for tfod to work
  210. try {
  211. Thread.sleep(1000);
  212. }
  213. catch (Exception e) {
  214. telemetry.addLine("Sorry, error with the code.");
  215. }
  216.  
  217.  
  218. // getUpdatedRecognitions() will return null if no new information is available since
  219. // the last time that call was made.
  220. List<Recognition> updatedRecognitions = tfod.getUpdatedRecognitions();
  221. if (updatedRecognitions != null) {
  222. telemetry.addData("# Object Detected", updatedRecognitions.size());
  223. if (updatedRecognitions != null) {
  224. for (int i = 0; i < updatedRecognitions.size(); i++) {
  225. if (updatedRecognitions.get(i).getWidth() * updatedRecognitions.get(i).getHeight() < 10000 ||
  226. updatedRecognitions.get(i).getWidth() - updatedRecognitions.get(i).getHeight() > 50) {
  227. //If the difference between height and width is larger than 50 it is unlikely to be a mineral
  228. updatedRecognitions.remove(i); // remove the recognition
  229. i--;
  230. }
  231. }
  232.  
  233. //there should be three minerals left
  234. if (updatedRecognitions.size() == 3) {
  235.  
  236. //these initializations are just to check that, later on, the values
  237. //are different and thus minerals have been recognized
  238. int goldMineralX = -1;
  239. int silverMineral1X = -1;
  240. int silverMineral2X = -1;
  241.  
  242. //a for each loop to run through each recognition to check for
  243. //the gold mineral
  244.  
  245. for (Recognition recognition : updatedRecognitions) {
  246.  
  247. //get the label of the recognition according to the neural
  248. //network
  249.  
  250. //save the x coordinate of each identification to subsequent
  251. //mineral variable
  252. if (recognition.getLabel().equals(LABEL_GOLD_MINERAL)) {
  253. goldMineralX = (int) recognition.getLeft();
  254.  
  255. } else if (silverMineral1X == -1) {
  256. silverMineral1X = (int) recognition.getLeft();
  257.  
  258. } else {
  259. silverMineral2X = (int) recognition.getLeft();
  260. }
  261. }
  262.  
  263. //if all three minerals were identified
  264. if (goldMineralX != -1 && silverMineral1X != -1 && silverMineral2X != -1) {
  265. if (goldMineralX < silverMineral1X && goldMineralX < silverMineral2X) {
  266. telemetry.addData("Gold Mineral Position", "Left");
  267. mineralPosition = "left";
  268.  
  269. } else if (goldMineralX > silverMineral1X && goldMineralX > silverMineral2X) {
  270. telemetry.addData("Gold Mineral Position", "Right");
  271. mineralPosition = "right";
  272.  
  273. } else {
  274. telemetry.addData("Gold Mineral Position", "Center");
  275. mineralPosition = "center";
  276.  
  277. }
  278.  
  279. }
  280.  
  281. }
  282.  
  283. //strafe to go closer to the minerals
  284. strafeleft(24);
  285.  
  286. //depending on each mineral location,
  287.  
  288. if (mineralPosition.equals("left")) { //if the gold mineral is all the way on the left (from the perspective
  289. //of the camera)
  290.  
  291. //go backwards towards the leftmost mineral
  292. DriveTrain(30, "backwards");
  293.  
  294. //strafe to hit the mineral
  295. strafeleft(12);
  296.  
  297. //strafe back out
  298. straferight(12);
  299.  
  300. } else if (mineralPosition.equals("right")) {
  301.  
  302. //go backwards towards the rightmost mineral
  303. DriveTrain(30, "forwards");
  304.  
  305. //strafe to hit the mineral
  306. strafeleft(12);
  307.  
  308. //strafe back out
  309. straferight(12);
  310.  
  311. //go back to the left to get ready for the weird turn
  312. DriveTrain(60, "backwards");
  313.  
  314. } else { //most likely, the mineral will be in the center if the above
  315. //if statements fail , but in case tfod doesn't
  316. //detect any minerals, this gives us a fail-safe
  317.  
  318. //strafe to hit the mineral
  319. strafeleft(12);
  320.  
  321. //strafe back out
  322. straferight(12);
  323.  
  324. DriveTrain(30, "backwards");
  325. }
  326.  
  327.  
  328. //do the weird turn
  329. weirdTurn(24);
  330.  
  331. //strafe closer to hit the wall
  332. strafeleft(28);
  333.  
  334. //go towards the depot square
  335. DriveTrain(36, "backwards");
  336.  
  337. //turns towards the depot square
  338. startTime = System.currentTimeMillis(); //log when you started to turn
  339.  
  340. while (true) {
  341. gyroCorrect(-90, 5, getGyroRotation(unit), .1, .4);
  342. //correct to the angle of 90
  343. long currentTime = System.currentTimeMillis(); // get current time
  344. if (currentTime - startTime > 5000) { // if more than 5 seconds has passed (more time than should be needed for a turn), stop turning and procede
  345. break; //exit loop
  346. }
  347. }
  348.  
  349. //deploy the teamMarker
  350. teamMarker.setPosition(0);
  351.  
  352. //strafe towards the crater
  353. straferight(36);
  354.  
  355. //turn towards the crater
  356. startTime = System.currentTimeMillis(); //log when you started to turn
  357.  
  358. //point the phone to the minerals
  359. while (true) {
  360. gyroCorrect(90, 5, getGyroRotation(unit), .1, .4);
  361. //correct to the angle of 90
  362. long currentTime = System.currentTimeMillis(); // get current time
  363. if (currentTime - startTime > 5000) { // if more than 5 seconds has passed (more time than should be needed for a turn), stop turning and procede
  364. break; //exit loop
  365. }
  366. }
  367.  
  368. //move the arm to the crater
  369. DrivePositionArm(-360);
  370. telemetry.update();
  371. }
  372. }
  373. }
  374.  
  375. */
  376. }
  377.  
  378. @Override
  379. public void stop() {
  380. if (tfod != null) {
  381. tfod.shutdown();
  382. }
  383. telemetry.addLine("Long live the russian regime! (aka Sasha)");
  384. telemetry.update();
  385. }
  386.  
  387. private void initVuforia() {
  388. //Configure Vuforia by creating a Parameter object, and passing it to the Vuforia engine.
  389. //config the vuforia with parameters and variables
  390.  
  391. VuforiaLocalizer.Parameters parameters = new VuforiaLocalizer.Parameters();
  392.  
  393. parameters.vuforiaLicenseKey = VUFORIA_KEY;
  394. parameters.cameraDirection = CameraDirection.FRONT;
  395.  
  396. // Instantiate the Vuforia engine
  397. vuforia = ClassFactory.getInstance().createVuforia(parameters);
  398.  
  399. // Loading trackables is not necessary for the Tensor Flow Object Detection engine.
  400. }
  401.  
  402. /**
  403. * Initialize the Tensor Flow Object Detection engine.
  404. */
  405. private void initTfod() {
  406. //stores an id for where to later draw the image, a window per se
  407. int tfodMonitorViewId = hardwareMap.appContext.getResources().getIdentifier(
  408. "tfodMonitorViewId", "id", hardwareMap.appContext.getPackageName());
  409.  
  410. TFObjectDetector.Parameters tfodParameters = new TFObjectDetector.Parameters(tfodMonitorViewId);
  411.  
  412. //implements the parameters, and passes the image to tfod
  413. tfod = ClassFactory.getInstance().createTFObjectDetector(tfodParameters, vuforia);
  414.  
  415. /* professor Wen states that this is a neural network, so each parameter is linked
  416. to a specific identification system and we are simply giving names to those final
  417. identifications to reference them later on */
  418. tfod.loadModelFromAsset(TFOD_MODEL_ASSET, LABEL_GOLD_MINERAL, LABEL_SILVER_MINERAL);
  419. }
  420.  
  421. public void DrivePosition(int position) {
  422. //apparently using negative or positive power here doesn't matter
  423. hook.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  424.  
  425. hook.setTargetPosition(position);
  426.  
  427. hook.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  428.  
  429. hook.setPower(1);
  430. }
  431.  
  432. public void DrivePositionArm(int position) {
  433. intakeArm.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  434.  
  435. intakeArm.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  436.  
  437. intakeArm.setTargetPosition(position);
  438.  
  439. intakeArm.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  440.  
  441. intakeArm.setPower(1);
  442. }
  443.  
  444. //for forwards, power = -1, and for backwards, power = 1
  445. public int DriveTrain(double distance, String direction) {
  446. //example gear ratio, 2:1
  447. //example circumference
  448. //distance is in inches
  449.  
  450. double circumferenceTraveled = distance / DriveCircumference;
  451.  
  452. int position = (int) (ticksPerRotation * circumferenceTraveled);
  453.  
  454. //if it is forwards or backwards, change sign of position
  455. if (direction.equalsIgnoreCase("forwards")) {
  456. position = -position;
  457. }
  458. else if (direction.equalsIgnoreCase("backwards")) {
  459. //position is equal to itself
  460. }
  461.  
  462. double power = powerScaleDistance(distance);
  463.  
  464. upperLeftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  465. upperLeftDrive.setTargetPosition(position);
  466. upperLeftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  467.  
  468. lowerLeftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  469. lowerLeftDrive.setTargetPosition(position);
  470. lowerLeftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  471.  
  472. upperRightDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  473. upperRightDrive.setTargetPosition(position);
  474. upperRightDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  475.  
  476. lowerRightDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  477. lowerRightDrive.setTargetPosition(position);
  478. lowerRightDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  479.  
  480. upperRightDrive.setPower(power);
  481. lowerLeftDrive.setPower(power);
  482. upperRightDrive.setPower(power);
  483. lowerRightDrive.setPower(power);
  484.  
  485. return position;
  486. }
  487.  
  488. //power scale according to distance
  489. public double powerScaleDistance(double distance) {
  490. double DrivePower = 0;
  491. if (distance <= 24) {
  492. DrivePower = 0.5;
  493. }
  494. else if (distance > 24) {
  495. DrivePower = 1;
  496. }
  497. return DrivePower;
  498. }
  499.  
  500. public void strafeleft(double distance) {
  501. //guess and check, then check formulas later on
  502. //assuming a similar relationship from distance to ticks in the drive train, probably going to
  503. //be much less
  504.  
  505. //how much the wheel actually needs to turn
  506. double circumferenceTraveled = distance / DriveCircumference;
  507.  
  508. //here position must be positive
  509. int position = (int) (ticksPerRotation * circumferenceTraveled);
  510.  
  511. double power = powerScaleDistance(distance);
  512.  
  513. upperLeftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  514. upperLeftDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  515. upperLeftDrive.setTargetPosition(position);
  516. upperLeftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  517. upperLeftDrive.setPower(powerScaleDistance(power));
  518.  
  519. upperRightDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  520. upperRightDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  521. upperRightDrive.setTargetPosition(-position);
  522. upperRightDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  523. upperRightDrive.setPower(powerScaleDistance(power));
  524.  
  525. lowerLeftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  526. lowerLeftDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  527. lowerLeftDrive.setTargetPosition(-position);
  528. lowerLeftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  529. lowerLeftDrive.setPower(powerScaleDistance(power));
  530.  
  531. lowerRightDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  532. lowerRightDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  533. lowerRightDrive.setTargetPosition(position);
  534. lowerRightDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  535. lowerRightDrive.setPower(powerScaleDistance(power));
  536. }
  537.  
  538. public void straferight(double distance) {
  539. //guess and check, then check formulas later on
  540. //assuming a similar relationship from distance to ticks in the drive train, probably going to
  541. //be much less
  542.  
  543. double circumferenceTraveled = distance / DriveCircumference;
  544.  
  545. int position = (int) (ticksPerRotation * circumferenceTraveled);
  546.  
  547. double power = powerScaleDistance(distance);
  548.  
  549. upperLeftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  550. upperLeftDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  551. upperLeftDrive.setTargetPosition(-position);
  552. upperLeftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  553. upperLeftDrive.setPower(powerScaleDistance(power));
  554.  
  555. upperRightDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  556. upperRightDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  557. upperRightDrive.setTargetPosition(position);
  558. upperRightDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  559. upperRightDrive.setPower(powerScaleDistance(power));
  560.  
  561. lowerLeftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  562. lowerLeftDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  563. lowerLeftDrive.setTargetPosition(position);
  564. lowerLeftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  565. lowerLeftDrive.setPower(powerScaleDistance(power));
  566.  
  567. lowerRightDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  568. lowerRightDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  569. lowerRightDrive.setTargetPosition(-position);
  570. lowerRightDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  571. lowerRightDrive.setPower(powerScaleDistance(power));
  572. }
  573.  
  574. public void gyroCorrect(double gyroTarget, double gyroRange, double gyroActual, double minSpeed,
  575. double addSpeed) {
  576.  
  577. while ((gyroTarget - getGyroRotation(unit) + 360.0) % 360.0 < 0.5) {
  578.  
  579. //this makes it an actual angle between 0 and 360 anyways, so it doesn't matter if the target
  580. //is weird for the gyro or actually between 0 and 360
  581. double delta = (gyroTarget - gyroActual + 360.0) % 360.0; // in case it is negative
  582.  
  583. if (delta > 180.0) {
  584. delta -= 360.0; // delta becomes between -180 and 180
  585. //because the range is from 0-> 180 and -180-> 0 instead of 0-> 360
  586. }
  587.  
  588. if (Math.abs(delta) > gyroRange) {
  589. double gyroMod = delta / 45.0; // if delta is less than 45 and bigger than -45, this will make a scale from
  590. // -1 to 1
  591.  
  592. if (Math.abs(gyroMod) > 1.0) {
  593. gyroMod = Math.signum(gyroMod); //makes gyroMod -1 or 1 if error is more than 45
  594. // or less than -45 degrees
  595. }
  596.  
  597. //if the error is more than 180, then the power is positive, and it turns to the left
  598. //if the error is less than 180, the power in the turn in negative, and it turns to the
  599. //right
  600. //if the error is larger, faster speed
  601. this.turn(minSpeed * Math.signum(gyroMod) + addSpeed * gyroMod);
  602. } else {
  603. turn(0.0);
  604. }
  605. }
  606. }
  607.  
  608. public void turn(double power) { //this is opposite to the varsity because our gear train makes
  609. //forwards negative and backwards positive
  610. upperLeftDrive.setPower(power);
  611. upperRightDrive.setPower(-power);
  612. lowerLeftDrive.setPower(power);
  613. lowerRightDrive.setPower(-power);
  614. }
  615.  
  616. //gets current angle position
  617. public float getGyroRotation(AngleUnit unit) {
  618. return imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.ZYX, unit).firstAngle;
  619. }
  620.  
  621. //this is a weird turn because only the left half of the motors move
  622. public void weirdTurn(double distance) {
  623. //example gear ratio, 2:1
  624. //example circumference
  625. //distance is in inches
  626.  
  627. double circumferenceTraveled = distance / DriveCircumference;
  628.  
  629. int position = (int) (ticksPerRotation * circumferenceTraveled);
  630.  
  631. //if it is forwards or backwards, change sign of position
  632. double power = powerScaleDistance(distance);
  633.  
  634. //position is positive because it will make the motors go backwards
  635. upperLeftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  636. upperLeftDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  637. upperLeftDrive.setTargetPosition(position);
  638. upperLeftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  639. upperLeftDrive.setPower(powerScaleDistance(power));
  640.  
  641. lowerLeftDrive.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  642. lowerLeftDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  643. lowerLeftDrive.setTargetPosition(position);
  644. lowerLeftDrive.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  645. lowerLeftDrive.setPower(power);
  646. }
  647. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement