Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. package org.firstinspires.ftc.teamcode;
  2.  
  3. import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
  4. import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
  5. import com.qualcomm.robotcore.hardware.DcMotor;
  6. import com.qualcomm.robotcore.util.ElapsedTime;
  7.  
  8. import org.firstinspires.ftc.robotcontroller.external.samples.HardwarePushbot;
  9.  
  10.  
  11. @Autonomous (name = "autoTest" , group = "pushbot")
  12. public class Drive extends LinearOpMode
  13. {
  14. /* Declare OpMode members. */
  15. HardWareBushBot robot = new HardWareBushBot(); // Use a Pushbot's hardware
  16. private ElapsedTime runtime = new ElapsedTime();
  17.  
  18. static final double COUNTS_PER_MOTOR_REV = 1440 ; // eg: TETRIX Motor Encoder
  19. static final double DRIVE_GEAR_REDUCTION = 2.0 ; // This is < 1.0 if geared UP
  20. static final double WHEEL_DIAMETER_INCHES = 4.0 ; // For figuring circumference
  21. static final double COUNTS_PER_INCH = (COUNTS_PER_MOTOR_REV * DRIVE_GEAR_REDUCTION) /
  22. (WHEEL_DIAMETER_INCHES * 3.1415);
  23.  
  24.  
  25. public void encoderDrive(double speed,
  26. double leftInches, double rightInches,
  27. double timeoutS) {
  28. int newLeftTarget;
  29. int newRightTarget;
  30.  
  31. // Ensure that the opmode is still active
  32. if (opModeIsActive()) {
  33.  
  34. // Determine new target position, and pass to motor controller
  35. newLeftTarget = robot.BDL.getCurrentPosition() + (int)(leftInches * COUNTS_PER_INCH);
  36. newRightTarget = robot.BDR.getCurrentPosition() + (int)(rightInches * COUNTS_PER_INCH);
  37. robot.BDL.setTargetPosition(newLeftTarget);
  38. robot.BDR.setTargetPosition(newRightTarget);
  39.  
  40. // Turn On RUN_TO_POSITION
  41. robot.BDL.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  42. robot.BDR.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  43.  
  44. // reset the timeout time and start motion.
  45. runtime.reset();
  46. robot.BDL.setPower(Math.abs(speed));
  47. robot.BDR.setPower(Math.abs(speed));
  48.  
  49. // keep looping while we are still active, and there is time left, and both motors are running.
  50. // Note: We use (isBusy() && isBusy()) in the loop test, which means that when EITHER motor hits
  51. // its target position, the motion will stop. This is "safer" in the event that the robot will
  52. // always end the motion as soon as possible.
  53. // However, if you require that BOTH motors have finished their moves before the robot continues
  54. // onto the next step, use (isBusy() || isBusy()) in the loop test.
  55. while (opModeIsActive() &&
  56. (runtime.seconds() < timeoutS) &&
  57. (robot.BDL.isBusy() && robot.BDR.isBusy())) {
  58.  
  59. // Display it for the driver.
  60. telemetry.addData("Path1", "Running to %7d :%7d", newLeftTarget, newRightTarget);
  61. telemetry.addData("Path2", "Running at %7d :%7d",
  62. robot.BDL.getCurrentPosition(),
  63. robot.BDR.getCurrentPosition());
  64. telemetry.update();
  65. }
  66.  
  67. // Stop all motion;
  68. robot.BDL.setPower(0);
  69. robot.BDR.setPower(0);
  70.  
  71. // Turn off RUN_TO_POSITION
  72. robot.BDL.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  73. robot.BDR.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  74.  
  75. // sleep(250); // optional pause after each move
  76. }
  77. }
  78. public void runOpMode()
  79. {
  80. robot.init(hardwareMap);
  81. robot.BDR.setDirection(DcMotor.Direction.REVERSE);
  82. telemetry.addData("Status", "Resetting Encoders"); //
  83. telemetry.update();
  84.  
  85. robot.BDL.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  86. robot.BDR.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  87.  
  88. robot.BDL.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  89. robot.BDR.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  90.  
  91. // Send telemetry message to indicate successful Encoder reset
  92. telemetry.addData("Path0", "Starting at %7d :%7d",
  93. robot.BDL.getCurrentPosition(),
  94. robot.BDR.getCurrentPosition());
  95. telemetry.update();
  96.  
  97. // Wait for the game to start (driver presses PLAY)
  98. waitForStart();
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement