Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.77 KB | None | 0 0
  1. /* Copyright (c) 2017 FIRST. All rights reserved.
  2. *
  3. * Redistribution and use in source and binary forms, with or without modification,
  4. * are permitted (subject to the limitations in the disclaimer below) provided that
  5. * the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice, this list
  8. * of conditions and the following disclaimer.
  9. *
  10. * Redistributions in binary form must reproduce the above copyright notice, this
  11. * list of conditions and the following disclaimer in the documentation and/or
  12. * other materials provided with the distribution.
  13. *
  14. * Neither the name of FIRST nor the names of its contributors may be used to endorse or
  15. * promote products derived from this software without specific prior written permission.
  16. *
  17. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
  18. * LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  20. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29.  
  30. package org.firstinspires.ftc.teamcode;
  31.  
  32. import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
  33. import com.qualcomm.robotcore.eventloop.opmode.Disabled;
  34. import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
  35. import com.qualcomm.robotcore.hardware.DcMotor;
  36. import com.qualcomm.robotcore.hardware.DcMotorSimple;
  37. import com.qualcomm.robotcore.util.ElapsedTime;
  38.  
  39. /**
  40. * This file illustrates the concept of driving a path based on encoder counts.
  41. * It uses the common Pushbot hardware class to define the drive on the robot.
  42. * The code is structured as a LinearOpMode
  43. *
  44. * The code REQUIRES that you DO have encoders on the wheels,
  45. * otherwise you would use: PushbotAutoDriveByTime;
  46. *
  47. * This code ALSO requires that the drive Motors have been configured such that a positive
  48. * power command moves them forwards, and causes the encoders to count UP.
  49. *
  50. * The desired path in this example is:
  51. * - Drive forward for 48 inches
  52. * - Spin right for 12 Inches
  53. * - Drive Backwards for 24 inches
  54. * - Stop and close the claw.
  55. *
  56. * The code is written using a method called: encoderDrive(speed, leftInches, rightInches, timeoutS)
  57. * that performs the actual movement.
  58. * This methods assumes that each movement is relative to the last stopping place.
  59. * There are other ways to perform encoder based moves, but this method is probably the simplest.
  60. * This code uses the RUN_TO_POSITION mode to enable the Motor controllers to generate the run profile
  61. *
  62. * Use Android Studios to Copy this Class, and Paste it into your team's code folder with a new name.
  63. * Remove or comment out the @Disabled line to add this opmode to the Driver Station OpMode list
  64. */
  65.  
  66. @Autonomous(name="AutoEncoder", group="Pushbot")
  67. //@Disabled
  68. public class TryEncoders extends LinearOpMode {
  69.  
  70. /* Declare OpMode members. */
  71. private ElapsedTime runtime = new ElapsedTime();
  72.  
  73. static final double COUNTS_PER_MOTOR_REV = 1120 ; // eg: TETRIX Motor Encoder
  74. static final double DRIVE_GEAR_REDUCTION = 1.7 ; // This is < 1.0 if geared UP
  75. static final double WHEEL_DIAMETER_INCHES = 4.0 ; // For figuring circumference
  76. static final double COUNTS_PER_INCH = (COUNTS_PER_MOTOR_REV * DRIVE_GEAR_REDUCTION) /
  77. (WHEEL_DIAMETER_INCHES * 3.1415);
  78. static final double DRIVE_SPEED = 0.6;
  79. static final double TURN_SPEED = 0.5;
  80.  
  81. private DcMotor leftfront;
  82. private DcMotor rightfront;
  83.  
  84.  
  85. @Override
  86. public void runOpMode() {
  87.  
  88. leftfront = hardwareMap.dcMotor.get("left_front");
  89. rightfront = hardwareMap.dcMotor.get("right_front");
  90.  
  91. leftfront.setDirection(DcMotorSimple.Direction.REVERSE);
  92.  
  93. telemetry.addData("Status", "Resetting Encoders"); //
  94. telemetry.update();
  95.  
  96. leftfront.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  97. rightfront.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  98.  
  99. leftfront.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  100. rightfront.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  101.  
  102. // Send telemetry message to indicate successful Encoder reset
  103. telemetry.addData("Path0", "Starting at %7d :%7d",
  104. leftfront.getCurrentPosition(),
  105. rightfront.getCurrentPosition());
  106. telemetry.update();
  107.  
  108. waitForStart();
  109.  
  110. encoderDrive(DRIVE_SPEED, 30, 30, 5.0); // S1: Forward 47 Inches with 5 Sec timeout
  111.  
  112. sleep(1000); // pause for servos to move
  113.  
  114. telemetry.addData("Path", "Complete");
  115. telemetry.update();
  116. }
  117.  
  118.  
  119. public void encoderDrive(double speed,
  120. double leftInches, double rightInches,
  121. double timeoutS) {
  122. int newLeftTarget;
  123. int newRightTarget;
  124.  
  125. // Ensure that the opmode is still active
  126. if (opModeIsActive()) {
  127.  
  128. // Determine new target position, and pass to motor controller
  129. newLeftTarget = leftfront.getCurrentPosition() + (int)(leftInches * COUNTS_PER_INCH);
  130. newRightTarget = rightfront.getCurrentPosition() + (int)(rightInches * COUNTS_PER_INCH);
  131. leftfront.setTargetPosition(newLeftTarget);
  132. rightfront.setTargetPosition(newRightTarget);
  133.  
  134. // Turn On RUN_TO_POSITION
  135. leftfront.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  136. rightfront.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  137.  
  138. // reset the timeout time and start motion.
  139. runtime.reset();
  140. leftfront.setPower(Math.abs(speed));
  141. rightfront.setPower(Math.abs(speed));
  142.  
  143. // keep looping while we are still active, and there is time left, and both motors are running.
  144. // Note: We use (isBusy() && isBusy()) in the loop test, which means that when EITHER motor hits
  145. // its target position, the motion will stop. This is "safer" in the event that the robot will
  146. // always end the motion as soon as possible.
  147. // However, if you require that BOTH motors have finished their moves before the robot continues
  148. // onto the next step, use (isBusy() || isBusy()) in the loop test.
  149. while (opModeIsActive() &&
  150. (runtime.seconds() < timeoutS) &&
  151. (leftfront.isBusy() && rightfront.isBusy())) {
  152.  
  153. // Display it for the driver.
  154. telemetry.addData("Path1", "Running to %7d :%7d", newLeftTarget, newRightTarget);
  155. telemetry.addData("Path2", "Running at %7d :%7d",
  156. leftfront.getCurrentPosition(),
  157. rightfront.getCurrentPosition());
  158. telemetry.update();
  159. }
  160.  
  161. // Stop all motion;
  162. leftfront.setPower(0);
  163. rightfront.setPower(0);
  164.  
  165. // Turn off RUN_TO_POSITION
  166. leftfront.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  167. rightfront.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  168.  
  169. // sleep(250); // optional pause after each move
  170. }
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement