Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.34 KB | None | 0 0
  1. /*
  2. Copyright (c) 2016 Robert Atkinson
  3.  
  4. All rights reserved.
  5.  
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted (subject to the limitations in the disclaimer below) provided that
  8. the following conditions are met:
  9.  
  10. Redistributions of source code must retain the above copyright notice, this list
  11. of conditions and the following disclaimer.
  12.  
  13. Redistributions in binary form must reproduce the above copyright notice, this
  14. list of conditions and the following disclaimer in the documentation and/or
  15. other materials provided with the distribution.
  16.  
  17. Neither the name of Robert Atkinson nor the names of his contributors may be used to
  18. endorse or promote products derived from this software without specific prior
  19. written permission.
  20.  
  21. NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
  22. LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  24. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESSFOR A PARTICULAR PURPOSE
  25. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  26. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  30. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  31. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. package org.firstinspires.ftc.teamcode;
  34.  
  35. import com.qualcomm.hardware.adafruit.BNO055IMU;
  36. import com.qualcomm.hardware.adafruit.JustLoggingAccelerationIntegrator;
  37. import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
  38. import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
  39. import com.qualcomm.robotcore.hardware.DcMotor;
  40. import com.qualcomm.robotcore.hardware.DcMotorSimple;
  41. import com.qualcomm.robotcore.util.ElapsedTime;
  42. import com.qualcomm.robotcore.util.Range;
  43.  
  44. import org.firstinspires.ftc.robotcontroller.external.samples.HardwareTank;
  45. import org.firstinspires.ftc.robotcore.external.Func;
  46. import org.firstinspires.ftc.robotcore.external.navigation.Acceleration;
  47. import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
  48. import org.firstinspires.ftc.robotcore.external.navigation.AxesOrder;
  49. import org.firstinspires.ftc.robotcore.external.navigation.AxesReference;
  50. import org.firstinspires.ftc.robotcore.external.navigation.Orientation;
  51.  
  52. import java.util.Locale;
  53.  
  54. import static android.R.attr.angle;
  55.  
  56. /**
  57. * This file illustrates the concept of driving a path based on time.
  58. * It uses the common Pushbot hardware class to define the drive on the robot.
  59. * The code is structured as a LinearOpMode
  60. *
  61. * The code assumes that you do NOT have encoders on the wheels,
  62. * otherwise you would use: PushbotAutoDriveByEncoder;
  63. *
  64. * The desired path in this example is:
  65. * - Drive forward for 3 seconds
  66. * - Spin right for 1.3 seconds
  67. * - Drive Backwards for 1 Second
  68. * - Stop and close the claw.
  69. *
  70. * The code is written in a simple form with no optimizations.
  71. * However, there are several ways that this type of sequence could be streamlined,
  72. *
  73. * Use Android Studios to Copy this Class, and Paste it into your team's code folder with a new name.
  74. * Remove or comment out the @Disabled line to add this opmode to the Driver Station OpMode list
  75. */
  76. @Autonomous(name="Tank: AutotomousTurning", group="Tank")
  77. public class TankAutoGyro extends LinearOpMode {
  78.  
  79. /* Declare OpMode members. */
  80. HardwareTank robot = new HardwareTank();
  81. private ElapsedTime runtime = new ElapsedTime();
  82.  
  83. // The IMU sensor object
  84. BNO055IMU imu;
  85.  
  86. // State used for updating telemetry
  87. Orientation angles;
  88. Acceleration gravity;
  89.  
  90. boolean firstAngle = true;
  91. boolean secondAngle = true;
  92.  
  93. public int distance(double dis)
  94. {
  95. return (int)(dis*robot.ticksPerInch);
  96. }
  97.  
  98. public enum DIRECTION {
  99. FORWARD (+0.25), REVERSE (.25), Clockwise (.1), Counter_Clockwise(-.1);
  100. public final double value;
  101. DIRECTION (double value) {this.value = value;}
  102. }
  103. public enum Direction {
  104. FORWARD (+1.0), REVERSE (+1.0), Clockwise (1.0), Counter_Clockwise(-1.0);
  105. public final double value;
  106. Direction (double value) {this.value = value;}
  107. }
  108.  
  109.  
  110. //0 = forward, 2 = reverse
  111. public void drive(DIRECTION direction, int ticks)
  112. {
  113. robot.leftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  114. robot.rightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  115.  
  116. robot.leftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  117. robot.rightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  118.  
  119. robot.leftMotor.setTargetPosition(ticks);
  120. robot.rightMotor.setTargetPosition(ticks);
  121. double timeTemp = runtime.seconds()+10;
  122. switch (direction)
  123. {
  124. case FORWARD:
  125. robot.leftMotor.setPower(DIRECTION.FORWARD.value);
  126. robot.rightMotor.setPower(DIRECTION.FORWARD.value);
  127. while(robot.leftMotor.isBusy() && robot.rightMotor.isBusy())
  128. {
  129.  
  130. }
  131. robot.leftMotor.setPower(0);
  132. robot.rightMotor.setPower(0);
  133.  
  134. robot.leftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  135. robot.rightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  136. break;
  137. case REVERSE:
  138. robot.leftMotor.setTargetPosition(-ticks);
  139. robot.rightMotor.setTargetPosition(-ticks);
  140.  
  141. robot.leftMotor.setPower(DIRECTION.REVERSE.value);
  142. robot.rightMotor.setPower(DIRECTION.REVERSE.value);
  143. while(robot.leftMotor.isBusy() && robot.rightMotor.isBusy() && opModeIsActive())
  144. {
  145. telemetry.addData("motorLeft Pos", robot.leftMotor.getCurrentPosition());
  146. telemetry.update();
  147. }
  148.  
  149. robot.leftMotor.setPower(0);
  150. robot.rightMotor.setPower(0);
  151.  
  152. robot.leftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  153. robot.rightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  154. robot.leftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  155. robot.rightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  156. break;
  157. }
  158. }
  159. public double adjustAngle(double angle)
  160. {
  161. while (angle > 180) angle -= 360;
  162. while (angle <= -180) angle += 360;
  163. return angle;
  164. }
  165.  
  166. public void driveTrain(double leftPow, double rightPow)
  167. {
  168. robot.leftMotor.setPower(leftPow);
  169. robot.rightMotor.setPower(rightPow);
  170. }
  171.  
  172.  
  173. public void turnP(double degrees, Direction direction, double timeout, double speed, double kp) {
  174. robot.leftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  175. robot.rightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  176. double targetAngle = adjustAngle(getHeading() + direction.value * degrees);
  177. double error;
  178. double power;
  179.  
  180. do {
  181. error = adjustAngle(targetAngle - getHeading());
  182. power = kp * error;
  183. power = Range.clip(power, -speed, +speed);
  184. driveTrain(-power, power);
  185. idle();
  186. } while (opModeIsActive() && error > 0.5);
  187. driveTrain(0,0);
  188. }
  189. public void turnDrive(double angle)
  190. {
  191. robot.leftMotor.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
  192. robot.rightMotor.setMode(DcMotor.RunMode.RUN_WITHOUT_ENCODER);
  193. DIRECTION direction = DIRECTION.Clockwise;
  194.  
  195. double desiredAngle = getHeading() + angle;
  196.  
  197. double error;
  198. desiredAngle = adjustAngle(desiredAngle);
  199. if (desiredAngle < 0)
  200. {
  201. direction = DIRECTION.Clockwise;
  202. }
  203. else if (desiredAngle > 0)
  204. {
  205. direction = DIRECTION.Counter_Clockwise;
  206. }
  207. telemetry.addData("desiredAngle", desiredAngle);
  208. switch (direction)
  209. {
  210. case Clockwise:
  211. robot.leftDrivePower = DIRECTION.Clockwise.value;
  212. robot.rightDrivePower = -robot.leftDrivePower;
  213.  
  214. do {
  215. error = adjustAngle(desiredAngle - getHeading());
  216.  
  217. }while (opModeIsActive() && error > .5);
  218.  
  219. while (opModeIsActive() && getHeading() < desiredAngle)
  220. {
  221. telemetry.update();
  222. robot.leftMotor.setPower(robot.leftDrivePower);
  223. robot.rightMotor.setPower(robot.rightDrivePower);
  224. }
  225. robot.rightMotor.setPower(0);
  226. robot.leftMotor.setPower(0);
  227. break;
  228. case Counter_Clockwise:
  229. robot.rightDrivePower = DIRECTION.Counter_Clockwise.value;
  230. robot.leftDrivePower = robot.rightDrivePower;
  231.  
  232. while (opModeIsActive() && getHeading() > desiredAngle)
  233. {
  234. telemetry.update();
  235. robot.leftMotor.setPower(robot.leftDrivePower);
  236. robot.rightMotor.setPower(robot.rightDrivePower);
  237. }
  238.  
  239.  
  240.  
  241.  
  242. }
  243. }
  244.  
  245. public boolean turningDriveBoolean(double power, int angle, float angleDesired)
  246. {
  247. robot.leftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  248. robot.rightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  249. boolean temp = true;
  250.  
  251. if (angle < 0)
  252. {
  253. robot.leftDrivePower = power;
  254. robot.rightDrivePower = -power;
  255.  
  256. if (AngleUnit.DEGREES.fromUnit(angles.angleUnit, angles.firstAngle) < angleDesired)
  257. {
  258. robot.leftDrivePower = 0;
  259. robot.rightDrivePower = 0;
  260.  
  261. temp = false;
  262.  
  263. }
  264. robot.leftMotor.setPower(robot.leftDrivePower);
  265. robot.rightMotor.setPower(robot.rightDrivePower);
  266. }
  267. else if (angle > 0)
  268. {
  269. robot.leftDrivePower = -power;
  270. robot.rightDrivePower = power;
  271.  
  272. if (AngleUnit.DEGREES.fromUnit(angles.angleUnit,angles.firstAngle) > angleDesired)
  273. {
  274. robot.leftDrivePower = 0;
  275. robot.rightDrivePower =0;
  276. temp = false;
  277. }
  278. robot.leftMotor.setPower(robot.leftDrivePower);
  279. robot.rightMotor.setPower(robot.rightDrivePower);
  280. }
  281. return temp;
  282. }
  283.  
  284.  
  285.  
  286. public double getHeading() {return angles.firstAngle;}
  287.  
  288. public void calibrateIMU()
  289. {
  290. BNO055IMU.Parameters parameters = new BNO055IMU.Parameters();
  291. parameters.angleUnit = BNO055IMU.AngleUnit.DEGREES;
  292. parameters.accelUnit = BNO055IMU.AccelUnit.METERS_PERSEC_PERSEC;
  293. parameters.calibrationDataFile = "AdafruitIMUCalibration.json"; // see the calibration sample opmode
  294. parameters.loggingEnabled = true;
  295. parameters.loggingTag = "IMU";
  296. parameters.accelerationIntegrationAlgorithm = new JustLoggingAccelerationIntegrator();
  297. this.imu.initialize(parameters);
  298. }
  299. @Override
  300. public void runOpMode() {
  301.  
  302. /*
  303. * Initialize the drive system variables.
  304. * The init() method of the hardware class does all the work here
  305. */
  306. robot.init(hardwareMap);
  307. robot.leftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  308. robot.rightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  309.  
  310. robot.leftMotor.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
  311. robot.rightMotor.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
  312.  
  313. // Send telemetry message to signify robot waiting;
  314. telemetry.addData("Status", "Ready to run");
  315. telemetry.addData("Alliance Colour", "Red or Blue");
  316. telemetry.update();
  317.  
  318.  
  319.  
  320. imu = hardwareMap.get(BNO055IMU.class, "imu");
  321. calibrateIMU();
  322.  
  323. double tempTime = 0;
  324. // Wait for the game to start (driver presses PLAY)
  325. waitForStart();
  326. angles = imu.getAngularOrientation().toAxesReference(AxesReference.INTRINSIC).toAxesOrder(AxesOrder.ZYX);
  327. // Step through each leg of the path, ensuring that the Auto mode has not been stopped along the way
  328.  
  329. //Step 1: drive forward one foam Pad
  330. robot.leftMotor.setPower(0);
  331. robot.rightMotor.setPower(0);
  332. runtime.reset();
  333. composeTelemetry();
  334.  
  335.  
  336.  
  337. //telemetry.addData("Angle",angleDesired);
  338. robot.leftDrivePower = -.1;
  339. robot.rightDrivePower = .1;
  340.  
  341. float angleDesired = 0;
  342. float angleDesired2 = 0;
  343.  
  344.  
  345. boolean driveForward1 = true;
  346. boolean flywheels = false;
  347. boolean turnDrive1 = false;
  348. boolean driveForward2 = false;
  349. boolean turnDrive2 = false;
  350.  
  351. //used for the flyWheels
  352. double desiredTime = 0;
  353.  
  354. while (opModeIsActive()) {
  355. //sleep(2000);
  356. //sleep(1000);
  357.  
  358. //first conditional is relating to the inital movement
  359.  
  360.  
  361. if (driveForward1)
  362. {
  363. drive(DIRECTION.FORWARD, distance(22));
  364.  
  365. flywheels = true;
  366. driveForward1=false;
  367.  
  368. }
  369. else if (flywheels)
  370. {
  371. if (desiredTime == 0)
  372. {
  373. desiredTime = runtime.seconds() + 10;
  374. }
  375. robot.flyWheelMotor1.setPower(robot.defaultFlyPower);
  376. robot.flyWheelMotor2.setPower(robot.defaultFlyPower);
  377.  
  378. if (runtime.seconds() > desiredTime -2)
  379. {
  380. robot.spin2Motor.setPower(.4);
  381. }
  382. if (runtime.seconds() >= desiredTime)
  383. {
  384. robot.flyWheelMotor1.setPower(0);
  385. robot.flyWheelMotor2.setPower(0);
  386.  
  387. robot.spin2Motor.setPower(0);
  388.  
  389. turnDrive1 = true;
  390. flywheels = false;
  391. }
  392. }
  393. else if (turnDrive1)
  394. {
  395. if (angleDesired == 0)
  396. {
  397. angleDesired = AngleUnit.DEGREES.fromUnit(angles.angleUnit,angles.firstAngle)+45;
  398. }
  399. turnDrive1 = turningDriveBoolean(.1, 45, angleDesired);
  400. driveForward2 = !turnDrive2;
  401. }
  402.  
  403. /*
  404. if (runtime.seconds() < 5) {
  405. drive(2, .25, distance(22));
  406. }
  407. //second conditional is relating to the firing of the particles
  408. else if (runtime.seconds() < 12)
  409. {
  410. if (runtime.seconds() > 7)
  411. {
  412. robot.flyWheelMotor1.setPower(robot.defaultFlyPower);
  413. robot.flyWheelMotor2.setPower(robot.defaultFlyPower);
  414. }
  415. else if (runtime.seconds() < 11)
  416. {
  417. robot.spin2Motor.setPower(.4);
  418. }
  419. else if (runtime.seconds() >= 11)
  420. {
  421. robot.spin2Motor.setPower(0);
  422. robot.flyWheelMotor1.setPower(0);
  423. robot.flyWheelMotor2.setPower(0);
  424. }
  425.  
  426. }
  427. else if (runtime.seconds() < 15)
  428. {
  429. turningDrive(.1, 45);
  430. }
  431. else if (runtime.seconds() < 18)
  432. {
  433. drive(2, .25, distance(22));
  434. }
  435. else if (runtime.seconds() < 21)
  436. {
  437. turningDrive(.1, 45);
  438. }
  439.  
  440.  
  441.  
  442.  
  443. */
  444. //turningDrive(.1, 90);
  445. //telemetry.addData("Angle", angleDesired);
  446. /*
  447. if (!firstMove)
  448. {
  449. drive(2, .25, distance(22));
  450. flyWheel = false;
  451. firstMove = true;
  452.  
  453. }
  454. else if (!flyWheel)
  455. {
  456. if (tempTime == 0)
  457. {
  458. tempTime = runtime.seconds() + 8;
  459. }
  460.  
  461. if (runtime.seconds() < tempTime)
  462. {
  463. robot.flyWheelMotor1.setPower(0.7);
  464. robot.flyWheelMotor2.setPower(0.7);
  465.  
  466. if (runtime.seconds() > tempTime + 3)
  467. {
  468. robot.spin2Motor.setPower(.4);
  469. }
  470. }
  471. robot.spin1Motor.setPower(0);
  472. robot.spin2Motor.setPower(0);
  473. robot.flyWheelMotor1.setPower(0);
  474. robot.flyWheelMotor2.setPower(0);
  475. firstTurn = false;
  476. flyWheel = true;
  477. }
  478. else if (!firstTurn) {
  479. if (AngleUnit.DEGREES.fromUnit(angles.angleUnit, angles.firstAngle) < angleDesired) {
  480. robot.leftDrivePower = 0;
  481. robot.rightDrivePower = 0;
  482. secondMove = false;
  483. firstTurn = true;
  484. }
  485.  
  486. robot.rightMotor.setPower(robot.rightDrivePower);
  487. robot.leftMotor.setPower(robot.leftDrivePower);
  488. }
  489. */
  490. /*
  491. else if (!secondMove)
  492. {
  493. drive(2, .25, distance(22));
  494. secondTurn =false;
  495. secondMove = true;
  496. }
  497. else if (!secondTurn)
  498. {
  499. robot.leftDrivePower = .1;
  500. robot.rightDrivePower = -.1;
  501. if (AngleUnit.DEGREES.fromUnit(angles.angleUnit, angles.firstAngle) > angleDesired2) {
  502. robot.leftDrivePower = 0;
  503. robot.rightDrivePower = 0;
  504. secondTurn = true;
  505. }
  506. robot.rightMotor.setPower(robot.rightDrivePower);
  507. robot.leftMotor.setPower(robot.leftDrivePower);
  508.  
  509. }
  510. */
  511. telemetry.update();
  512. }
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521. /*
  522. drive(2, .25, distance(22));
  523. sleep(1000);
  524.  
  525.  
  526. double tempTime = runtime.seconds() + 8;
  527. while (runtime.seconds() < tempTime) {
  528. if (runtime.seconds() < tempTime - 3) {
  529.  
  530. robot.flyWheelMotor1.setPower(.7);
  531. robot.flyWheelMotor2.setPower(.7);
  532. }
  533.  
  534.  
  535.  
  536. //robot.spin1Motor.setPower(.8);
  537. robot.spin2Motor.setPower(.4);
  538. }
  539. robot.spin1Motor.setPower(0);
  540. robot.spin2Motor.setPower(0);
  541. robot.flyWheelMotor1.setPower(0);
  542. robot.flyWheelMotor2.setPower(0);
  543.  
  544. turningDrive(-.5, distance(100));
  545. */
  546. //runtime.reset();
  547. //sleep(2000);
  548. //drive(2, 1, distance(35));
  549.  
  550.  
  551. // Step 4: Stop and close the claw
  552.  
  553.  
  554.  
  555. //sleep(1000);
  556. }
  557. void composeTelemetry() {
  558.  
  559. // At the beginning of each telemetry update, grab a bunch of data
  560. // from the IMU that we will then display in separate lines.
  561. telemetry.addAction(new Runnable() { @Override public void run()
  562. {
  563. // Acquiring the angles is relatively expensive; we don't want
  564. // to do that in each of the three items that need that info, as that's
  565. // three times the necessary expense.
  566. angles = imu.getAngularOrientation().toAxesReference(AxesReference.INTRINSIC).toAxesOrder(AxesOrder.ZYX);
  567. gravity = imu.getGravity();
  568. }
  569. });
  570.  
  571. telemetry.addLine()
  572. .addData("status", new Func<String>() {
  573. @Override public String value() {
  574. return imu.getSystemStatus().toShortString();
  575. }
  576. })
  577. .addData("calib", new Func<String>() {
  578. @Override public String value() {
  579. return imu.getCalibrationStatus().toString();
  580. }
  581. });
  582.  
  583. telemetry.addLine()
  584. .addData("heading", new Func<String>() {
  585. @Override public String value() {
  586. return formatAngle(angles.angleUnit, angles.firstAngle);
  587. }
  588. })
  589. .addData("roll", new Func<String>() {
  590. @Override public String value() {
  591. return formatAngle(angles.angleUnit, angles.secondAngle);
  592. }
  593. })
  594. .addData("pitch", new Func<String>() {
  595. @Override public String value() {
  596. return formatAngle(angles.angleUnit, angles.thirdAngle);
  597. }
  598. });
  599.  
  600. telemetry.addLine()
  601. .addData("grvty", new Func<String>() {
  602. @Override public String value() {
  603. return gravity.toString();
  604. }
  605. })
  606. .addData("mag", new Func<String>() {
  607. @Override public String value() {
  608. return String.format(Locale.getDefault(), "%.3f",
  609. Math.sqrt(gravity.xAccel*gravity.xAccel
  610. + gravity.yAccel*gravity.yAccel
  611. + gravity.zAccel*gravity.zAccel));
  612. }
  613. });
  614. }
  615.  
  616. String formatAngle(AngleUnit angleUnit, double angle) {
  617. return formatDegrees(AngleUnit.DEGREES.fromUnit(angleUnit, angle));
  618. }
  619.  
  620. String formatDegrees(double degrees){
  621. return String.format(Locale.getDefault(), "%.1f", AngleUnit.DEGREES.normalize(degrees));
  622. }
  623.  
  624. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement