AsianInvasion

CurrentAuton

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