AsianInvasion

CurrentAuton

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