Advertisement
willieshi232

Untitled

Dec 7th, 2018
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. package org.firstinspires.ftc.teamcode;
  2.  
  3. import com.qualcomm.robotcore.eventloop.opmode.Disabled;
  4. import com.qualcomm.robotcore.eventloop.opmode.OpMode;
  5. import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
  6. import com.qualcomm.robotcore.hardware.DcMotor;
  7. import com.qualcomm.robotcore.util.ElapsedTime;
  8. import com.qualcomm.robotcore.util.Range;
  9.  
  10.  
  11. @TeleOp(name="MecanumTeleop", group="MecanumBot")
  12. //@Disabled
  13.  
  14. public class MecanumTeleop extends OpMode {
  15.  
  16. MecanumHardware robot = new MecanumHardware();
  17. ElapsedTime runtime = new ElapsedTime();
  18.  
  19. static final double COUNTS_PER_MOTOR_REV = 1120; // eg: TETRIX Motor Encoder
  20. static final double DRIVE_GEAR_REDUCTION = 1.0; // This is < 1.0 if geared UP
  21. static final double WHEEL_DIAMETER_INCHES = 4.0; // For figuring circumference
  22. static final double COUNTS_PER_INCH = (COUNTS_PER_MOTOR_REV * DRIVE_GEAR_REDUCTION) / (WHEEL_DIAMETER_INCHES * Math.PI);
  23. static final double DRIVE_SPEED = 0.6;
  24. static final double TURN_SPEED = 0.5;
  25.  
  26. private float pLim = 1f; //power multiplier that acts as a limit
  27. private float drive = .6f;
  28. private float tHold = .1f; //lowest threshold for it to register
  29. private float pSlow = .2f;
  30. private float pSlowSlow = .1f;
  31.  
  32. //boolean intakeFor = false;
  33. boolean intakeOld = true;
  34. boolean intakeNew = false;
  35. boolean PowerOn=true;
  36. int armFlipRef = 0;//TODO: FIND VALUE
  37. int count = 0;
  38.  
  39. @Override
  40. public void init() {
  41. /* Initialize the hardware variables.
  42. * The init() method of the hardware class does all the work here
  43. */
  44. robot.init(hardwareMap);
  45. //robot.intake.setPower(0);
  46. robot.armFlip.setPower(0);
  47. }
  48.  
  49. @Override
  50. public void loop() {
  51.  
  52. //horizontal arm movement
  53. if (gamepad1.dpad_right) {
  54. robot.armEx.setPower(-pLim);
  55. }
  56. else if (gamepad1.dpad_left) {
  57. robot.armEx.setPower(pLim);
  58. }
  59. else {
  60. robot.armEx.setPower(0);
  61. }
  62.  
  63. /*if(gamepad1.x) {
  64. if(intakeOn) {
  65. robot.intake.setPower(1);
  66. intakeOn = false;
  67. }
  68. else {
  69. robot.intake.setPower(0);
  70. intakeOn = true;
  71. }
  72. }*/
  73.  
  74. if(gamepad1.y) {
  75. robot.bucket.setPosition(.6);//flat position
  76. //TODO: CHECK VALUE
  77. }
  78. if(gamepad1.a) {
  79. robot.bucket.setPosition(.45);//cube position
  80. //TODO: CHECK VALUE
  81. }
  82. if(gamepad1.b) {
  83. robot.bucket.setPosition(.25);
  84. }
  85. //if(gamepad1.x) {
  86. intakeNew= gamepad1.x;
  87. if(intakeNew != intakeOld)
  88. {
  89. PowerOn= !PowerOn;
  90. }
  91. if(PowerOn)
  92. {
  93. robot.intake.setPower(0.6);
  94. }
  95. else
  96. {
  97. robot.intake.setPower(0);
  98. }
  99. intakeOld = intakeNew;
  100.  
  101.  
  102. /*else if(robot.armFlip.getCurrentPosition() >= armFlipRef && robot.bucket.getPosition() != .5) {
  103. robot.bucket.setPosition(.5);//hold postion
  104. //TODO: CHECK VALUE
  105. }
  106. else if(robot.bucket.getPosition() != 1) {
  107. robot.bucket.setPosition(1);//flat postion
  108. //TODO: CHECK VALUE
  109. }*/
  110.  
  111. //elevator controls
  112. if (gamepad1.dpad_up) {
  113. robot.elevator.setPower(-1);
  114. }
  115. else if (gamepad1.dpad_down) {
  116. robot.elevator.setPower(1);
  117. }
  118. else {
  119. robot.elevator.setPower(0);
  120. }
  121.  
  122.  
  123. //arm flip controls
  124. /*if(gamepad1.a) {
  125. encoderMove(robot.armFlip, 11, 20, 0, pLim);
  126. }
  127. if(gamepad1.y) {
  128. encoderMove(robot.armFlip, -10, 20, robot.armFlip.getCurrentPosition(), -pLim);
  129. }*/
  130.  
  131. //manual movement of the arm flip- may replace encoder movement
  132. if (gamepad1.left_bumper) {
  133. //runtime.reset();
  134. //while(runtime.seconds() < 2) {
  135. //robot.armFlip.setPower(-pSlow);
  136. //mecanumMove();`
  137. //}
  138. robot.armFlip.setPower(-pSlowSlow);
  139.  
  140. }
  141. else if (gamepad1.right_bumper) {
  142. //runtime.reset();
  143. //while(runtime.seconds() < 2) {
  144. robot.armFlip.setPower(pSlowSlow);
  145.  
  146. //}
  147. //robot.armFlip.setPower(0);
  148. //mecanumMove();
  149. }
  150. else if (gamepad1.left_trigger > tHold)
  151. {
  152. robot.armFlip.setPower(-pLim);
  153. }
  154. else if (gamepad1.right_trigger > tHold) {
  155. robot.armFlip.setPower(pLim);
  156. }
  157. else {
  158. robot.armFlip.setPower(0);
  159. }
  160. mecanumMove();
  161. }
  162.  
  163. public void mecanumMove() {
  164. //variables
  165. double r = Math.hypot(gamepad1.left_stick_x, gamepad1.left_stick_y);
  166. double robotAngle = Math.atan2(gamepad1.left_stick_y, gamepad1.left_stick_x) - Math.PI / 4;
  167. double rightX = gamepad1.right_stick_x;
  168. final double v1 = r * Math.cos(robotAngle) + rightX;
  169. final double v2 = r * Math.sin(robotAngle) - rightX;
  170. final double v3 = r * Math.sin(robotAngle) + rightX;
  171. final double v4 = r * Math.cos(robotAngle) - rightX;
  172.  
  173. robot.fLMotor.setPower(drive * v1);
  174. robot.fRMotor.setPower(drive * v2);
  175. robot.bLMotor.setPower(drive * v3);
  176. robot.bRMotor.setPower(drive * v4);
  177.  
  178.  
  179. /*public void encoderMove(DcMotor motor, double inches, double timeoutS, int ref, float power) {
  180. // Determine new target position, and pass to motor controller
  181. int target = ref + (int) (inches * COUNTS_PER_INCH);
  182. motor.setTargetPosition(target);
  183.  
  184. // Turn On RUN_TO_POSITION
  185. motor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  186.  
  187. // reset the timeout time and start motion.
  188. runtime.reset();
  189. motor.setPower(power);
  190.  
  191. // keep looping while we are still active, and there is time left, and both motors are running.
  192. // Note: We use (isBusy() && isBusy()) in the loop test, which means that when EITHER motor hits
  193. // its target position, the motion will stop. This is "safer" in the event that the robot will
  194. // always end the motion as soon as possible.
  195. // However, if you require that BOTH motors have finished their moves before the robot continues
  196. // onto the next step, use (isBusy() || isBusy()) in the loop test.
  197. while ((runtime.seconds() <= timeoutS) && robot.armFlip.isBusy()) {
  198.  
  199. // Display it for the driver.
  200. telemetry.addData("Path1", "Running to %7d", target);
  201. telemetry.addData("Path2", "Running at %7d", motor.getCurrentPosition());
  202. telemetry.update();
  203. }
  204.  
  205. // Stop all motion;
  206. motor.setPower(0);
  207.  
  208. // Turn off RUN_TO_POSITION
  209. motor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  210. }*/
  211. }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement