AsianInvasion

CurrentAuton

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