roll11226

encoderturn11.2_needToWork

Feb 11th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 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.eventloop.opmode.TeleOp;
  6. import com.qualcomm.robotcore.hardware.DcMotor;
  7. import com.qualcomm.robotcore.hardware.DcMotorSimple;
  8.  
  9.  
  10. @Autonomous(name = "test_turn")
  11. public class encoder_turn extends LinearOpMode
  12. {
  13. private HardWare11226 hardware = new HardWare11226();
  14. boolean runonce = true;
  15.  
  16. @Override
  17. public void runOpMode() throws InterruptedException
  18. {
  19.  
  20. hardware.init(hardwareMap);
  21. telemetry.addData("Status", "init");
  22.  
  23.  
  24. hardware.leftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  25. hardware.rightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  26. hardware.leftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  27. hardware.rightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  28.  
  29. waitForStart();
  30.  
  31. hardware.leftMotor.setDirection(DcMotor.Direction.FORWARD); // Set to REVERSE if using AndyMark motors
  32. hardware.rightMotor.setDirection(DcMotor.Direction.REVERSE);
  33.  
  34. while (opModeIsActive())
  35. {
  36. if (runonce == true)
  37. {
  38. turning(90, -0.4, 0.4);
  39. runonce = false;
  40. }
  41. }
  42. }
  43.  
  44. public void turning(double angle, double right_power, double left_power)
  45. {
  46. hardware.leftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  47. hardware.rightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  48.  
  49. int leftTarget = (int) (((120.89 / 31.9024 ) / 360) * angle);
  50. int rightTarget = (int) (((120.89 / 31.9024 ) / 360) * angle);
  51.  
  52. hardware.leftMotor.setTargetPosition(leftTarget);
  53. hardware.rightMotor.setTargetPosition(-rightTarget);
  54.  
  55. hardware.leftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  56. hardware.rightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  57.  
  58. hardware.leftMotor.setPower(left_power);
  59. hardware.rightMotor.setPower(right_power);
  60.  
  61. while (hardware.rightMotor.isBusy())
  62. {
  63. telemetry.addData("Status: ", "isBusy.");
  64. telemetry.addData("left pos", hardware.leftMotor.getCurrentPosition());
  65. telemetry.addData("right pos", hardware.rightMotor.getCurrentPosition());
  66. telemetry.update();
  67. }
  68.  
  69. hardware.leftMotor.setPower(0);
  70. hardware.rightMotor.setPower(0);
  71.  
  72. telemetry.update();
  73.  
  74. hardware.leftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  75. hardware.rightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  76. }
  77. }
Add Comment
Please, Sign In to add comment