roll11226

auto+red

Feb 18th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.19 KB | None | 0 0
  1. package org.firstinspires.ftc.teamcode;
  2.  
  3. import android.graphics.Color;
  4.  
  5. import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
  6. import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
  7. import com.qualcomm.robotcore.hardware.DcMotor;
  8. import com.qualcomm.robotcore.util.ElapsedTime;
  9. import com.qualcomm.robotcore.util.Hardware;
  10.  
  11.  
  12. @Autonomous(name = "ALL", group = "nobody likes you")
  13. public class autonomous_red extends LinearOpMode
  14. {
  15.  
  16. private enum beaconcolor
  17. {
  18. sRed,
  19. sBlue,
  20. sUnknown
  21. }
  22.  
  23. private ElapsedTime time = new ElapsedTime();
  24. private int redtimes = 0;
  25. private int bluetimes = 0;
  26. private beaconcolor m_beaconcolor;
  27. private HardWare11226 m_hardware = new HardWare11226();
  28. boolean runonce;
  29. final double wheel_peremeter = 12.56;
  30.  
  31. @Override
  32. public void runOpMode() throws InterruptedException {
  33.  
  34. m_hardware.init(hardwareMap);
  35.  
  36. runonce = true;
  37.  
  38. m_hardware.leftMotor.setDirection(DcMotor.Direction.FORWARD); // Set to REVERSE if using AndyMark motors
  39. m_hardware.rightMotor.setDirection(DcMotor.Direction.REVERSE);
  40.  
  41. beginf();
  42.  
  43. telemetry.addData("Status", "ready to run.");
  44. telemetry.update();
  45.  
  46. waitForStart();
  47. time.reset();
  48. time.startTime();
  49.  
  50. while (opModeIsActive())
  51. {
  52. telemetry.addData("Status: ", "while.");
  53.  
  54. if (runonce == true)
  55. {
  56. telemetry.addData("Status: ", "if.");
  57. m_hardware.leftMotor.setPower(0.3);
  58. m_hardware.rightMotor.setPower(0.3);
  59. sleep(200);
  60.  
  61. driving(52, 0.8);
  62. turning(-90, 90, -0.4, 0.4);
  63. driving(35, 0.3);
  64. telemetry.addData("done","2");
  65. telemetry.update();
  66. driving(-8, -0.4);
  67. driving(9, 0.3);
  68. telemetry.addData("done","3");
  69. telemetry.update();
  70. driving(-2, -0.2);
  71. beaconDetectRed();
  72. telemetry.addData("done", "4");
  73. telemetry.update();
  74. driving(-32, -0.4);
  75. turning(90, -90, 0.4, -0.4);
  76. telemetry.addData("done","5");
  77. telemetry.update();
  78. driving(46, 0.8);
  79. telemetry.addData("done","6");
  80. telemetry.update();
  81. turning(-90, 90, -0.4, 0.4);
  82. telemetry.addData("Status: ", "done.");
  83. runonce = false;
  84. }
  85.  
  86. telemetry.update();
  87. idle();
  88. }
  89. }
  90.  
  91. public void turning(int L_angle, int R_angle, double left_power, double right_power) {
  92.  
  93.  
  94. int leftTarget = (int) (((46.4955713 / wheel_peremeter) * 3.111) * L_angle);
  95. int rightTarget = (int) (((46.4955713 / wheel_peremeter) * 3.111) * R_angle);
  96.  
  97. m_hardware.leftMotor.setTargetPosition(leftTarget);
  98. m_hardware.rightMotor.setTargetPosition(rightTarget);
  99.  
  100. m_hardware.leftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  101. m_hardware.rightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  102.  
  103. m_hardware.leftMotor.setPower(left_power);
  104. m_hardware.rightMotor.setPower(right_power);
  105.  
  106. while (m_hardware.leftMotor.isBusy() && m_hardware.rightMotor.isBusy()) {
  107. telemetry.addData("Status: ", "isBusy.");
  108. telemetry.addData("left pos", m_hardware.leftMotor.getCurrentPosition());
  109. telemetry.addData("right pos", m_hardware.rightMotor.getCurrentPosition());
  110. telemetry.update();
  111. }
  112.  
  113. Stop();
  114. beginf();
  115. sleep(350);
  116.  
  117. telemetry.update();
  118. }
  119.  
  120. public void driving(double distance, double power)
  121. {
  122.  
  123. int Target = (int) ((distance / wheel_peremeter) * 1120);
  124. m_hardware.leftMotor.setTargetPosition(Target);
  125. m_hardware.rightMotor.setTargetPosition(Target);
  126.  
  127. m_hardware.leftMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  128. m_hardware.rightMotor.setMode(DcMotor.RunMode.RUN_TO_POSITION);
  129.  
  130. m_hardware.leftMotor.setPower(power);
  131. m_hardware.rightMotor.setPower(power);
  132.  
  133. while (m_hardware.rightMotor.isBusy())
  134. {
  135. telemetry.addData("Status: ", "isBusy.");
  136. telemetry.addData("left pos", m_hardware.leftMotor.getCurrentPosition());
  137. telemetry.addData("right pos", m_hardware.rightMotor.getCurrentPosition());
  138.  
  139. }
  140.  
  141. Stop();
  142. beginf();
  143. sleep(350);
  144.  
  145. telemetry.update();
  146. }
  147.  
  148. public void Stop()
  149. {
  150. m_hardware.leftMotor.setPower(0);
  151. m_hardware.rightMotor.setPower(0);
  152. }
  153.  
  154.  
  155. public void beaconDetectRed() throws InterruptedException
  156. {
  157. float hsvValues[] = {0F, 0F, 0F};
  158. Color.RGBToHSV(m_hardware.color_sensor_red.red() * 8, m_hardware.color_sensor_red.green() * 8, m_hardware.color_sensor_red.blue() * 8, hsvValues);
  159. time.reset();
  160. while (time.milliseconds() <= 600) {
  161. if (hsvValues[0] <= 40 || hsvValues[0] >= 340)
  162. {
  163. redtimes++;
  164. telemetry.addData("red :", redtimes);
  165. telemetry.update();
  166. }
  167. else if (hsvValues[0] <= 240 || hsvValues[0] >= 220)
  168. {
  169. bluetimes++;
  170. telemetry.addData("blue :", bluetimes);
  171. telemetry.update();
  172. }
  173. }
  174.  
  175. if (redtimes > bluetimes)
  176. {
  177. m_beaconcolor = beaconcolor.sRed;
  178. telemetry.addData("case red :", redtimes);
  179. telemetry.update();
  180. }
  181. else if (bluetimes > redtimes)
  182. {
  183. m_beaconcolor = beaconcolor.sBlue;
  184. telemetry.addData("case blue", bluetimes);
  185. telemetry.update();
  186. }
  187. else
  188. {
  189. m_beaconcolor = beaconcolor.sUnknown;
  190. }
  191.  
  192.  
  193. telemetry.addData("hue", hsvValues[0]);
  194. telemetry.addData("redtimes", redtimes);
  195. telemetry.addData("bluetimes", bluetimes);
  196.  
  197. telemetry.update();
  198.  
  199. switch (m_beaconcolor)
  200. {
  201. case sBlue:
  202. telemetry.addData("beacon color detected!!!", " blue");
  203. driving(-4, -0.3);
  204. sleep(4500);
  205. driving(5, 0.4);
  206. driving(5, -0.4);
  207. driving(5, 0.4);
  208. driving(5, -0.4);
  209. break;
  210.  
  211. case sRed:
  212. telemetry.addData("beacon color detected!!!", " red");
  213. break;
  214.  
  215. case sUnknown:
  216. telemetry.addData("beacon color NOT detected!!!", " search again for color");
  217. beaconDetectRed();
  218. break;
  219. }
  220. }
  221.  
  222.  
  223. public void beginf()
  224. {
  225.  
  226. m_hardware.leftMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  227. m_hardware.rightMotor.setMode(DcMotor.RunMode.STOP_AND_RESET_ENCODER);
  228.  
  229. m_hardware.leftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  230. m_hardware.rightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
  231.  
  232.  
  233. }
  234. }
Add Comment
Please, Sign In to add comment