Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. import lejos.hardware.Button;
  2. import lejos.hardware.LED;
  3. import lejos.hardware.lcd.LCD;
  4. import lejos.hardware.motor.Motor;
  5. import lejos.hardware.motor.NXTRegulatedMotor;
  6. import lejos.hardware.port.SensorPort;
  7. import lejos.hardware.sensor.EV3ColorSensor;
  8. import lejos.hardware.sensor.EV3UltrasonicSensor;
  9. import lejos.hardware.sensor.MindsensorsDistanceSensor;
  10. import lejos.hardware.sensor.SumoEyesSensor;
  11. import lejos.robotics.SampleProvider;
  12.  
  13. public class WallSpoolFInder {
  14.  
  15. private static final byte SUMO_NOTHING = 1;
  16. private static final byte SUMO_LEFT = 2;
  17. private static final byte SUMO_RIGHT = 3;
  18. private static final byte SUMO_CENTER = 4;
  19.  
  20. public static NXTRegulatedMotor leftMotor = Motor.D;
  21. public static NXTRegulatedMotor rightMotor = Motor.B;
  22. public static NXTRegulatedMotor doorMotor = Motor.C;
  23. public static EV3UltrasonicSensor sonic = new EV3UltrasonicSensor(SensorPort.S1);
  24. public static MindsensorsDistanceSensor iRSensor = new MindsensorsDistanceSensor(SensorPort.S4);
  25. public static SumoEyesSensor eyes = new SumoEyesSensor(SensorPort.S2);
  26.  
  27. public static EV3ColorSensor colorSensor = new EV3ColorSensor(SensorPort.S3);
  28. public static SampleProvider colorSampler = colorSensor.getRGBMode();
  29.  
  30. public static float[] readings = new float[4];
  31.  
  32. public static float[] sonarReadings = new float[sonic.getDistanceMode()
  33. .sampleSize()];
  34. public static float[] iRreadings = new float[1];
  35. static float[] colourReadings = new float[4];
  36.  
  37. static int blackColourCount = 0;
  38. static int frontSensorDistance = 20;
  39. static int sideSensorDistance = 13;
  40. static int loopTimes = 4;
  41.  
  42. public static void main(String[] args) throws Exception {
  43. eyes.setLongRange(true);
  44.  
  45. byte sumoReading = getReading(eyes.getValue());
  46.  
  47. doorMotor.rotateTo(75);
  48. Button.waitForAnyPress();
  49.  
  50. leftMotor.setSpeed(300 * 1);
  51. rightMotor.setSpeed(300 * 1);
  52.  
  53. startMovePot();
  54. leftMotor.stop(true);
  55. rightMotor.stop(true);
  56. //doorMotor.rotateTo(75);
  57. rightMotor.forward();
  58. leftMotor.backward();
  59.  
  60. Thread.sleep(750);
  61.  
  62. leftMotor.stop(true);
  63. rightMotor.stop(true);
  64.  
  65. // Button.waitForAnyPress();
  66.  
  67. leftMotor.forward();
  68. rightMotor.forward();
  69. boolean flip = false;
  70.  
  71.  
  72. while (!Button.ESCAPE.isDown()) {
  73.  
  74. colorSampler.fetchSample(colourReadings, 0);
  75. int r = (int) (colourReadings[0] * 1000);
  76. int g = (int) (colourReadings[1] * 1000);
  77. int b = (int) (colourReadings[2] * 1000);
  78.  
  79. if((r < 45 && g < 45 && b < 45) && flip == false){
  80. flip = true;
  81. loopTimes--;
  82. LCD.drawString("Loop Value: " + loopTimes + " ", 0, 6);
  83. LCD.drawString("FLIP: " + flip + " ", 0, 7);
  84.  
  85. Button.LEDPattern(3);
  86. }
  87. // else if(r < 10 && g < 10 && b < 10){
  88. // flip = true;
  89. // LCD.drawString("FLIP: " + flip + " ", 0, 7);
  90. // Button.LEDPattern(0);
  91. // }
  92. else if((r>45 || g>45 || b>45) && flip == true){
  93. flip = false;
  94. LCD.drawString("FLIP: " + flip + " ", 0, 7);
  95. Button.LEDPattern(0);
  96. }
  97.  
  98.  
  99.  
  100. sonic.getDistanceMode().fetchSample(sonarReadings, 0);
  101. float d = (int) (sonarReadings[0] * 1000) / 10f;
  102.  
  103. int value = eyes.getValue();
  104. LCD.drawString("Raw Value: " + value + " ", 0, 1);
  105. byte reading = getReading(value);
  106.  
  107.  
  108.  
  109. iRSensor.fetchSample(iRreadings, 0);
  110. float di = iRreadings[0] * 10;
  111.  
  112. // BACK END SEES SOMETHING
  113.  
  114. if(loopTimes <=0){
  115. Thread.sleep(1000);
  116. leftMotor.stop(true);
  117. rightMotor.stop(true);
  118. return;
  119. }else
  120. if ((reading == SUMO_RIGHT ||reading == SUMO_CENTER || reading == SUMO_RIGHT)){
  121. //LCD.drawString("CENTER ", 0, 1);
  122. Thread.sleep(700);
  123. leftMotor.stop(true);
  124. rightMotor.stop(true);
  125. doorMotor.rotateTo(0);
  126. leftMotor.backward();
  127. rightMotor.backward();
  128. Thread.sleep(2200);
  129. rightMotor.forward();
  130. leftMotor.backward();
  131. Thread.sleep(1100);
  132. leftMotor.stop(true);
  133. rightMotor.stop(true);
  134. doorMotor.rotateTo(70);
  135. leftMotor.forward();
  136. rightMotor.forward();
  137. Thread.sleep(800);
  138. }else
  139. if (d < 10) {
  140. leftMotor.stop(true);
  141. rightMotor.stop(true);
  142. rightMotor.forward();
  143. leftMotor.backward();
  144.  
  145. Thread.sleep(750);
  146. rightMotor.forward();
  147. leftMotor.forward();
  148. }else
  149.  
  150. if (di > sideSensorDistance + 4) {
  151. leftMotor.setSpeed(300);
  152. rightMotor.setSpeed(175);
  153. Thread.sleep(400);
  154. leftMotor.setSpeed(300);
  155. rightMotor.setSpeed(300);
  156. }else if (di < sideSensorDistance) {
  157. rightMotor.setSpeed(300);
  158. leftMotor.setSpeed(175);
  159. Thread.sleep(400);
  160. leftMotor.setSpeed(300);
  161. rightMotor.setSpeed(300);
  162. }
  163. }
  164. }
  165.  
  166. private static byte getReading(int value) {
  167. if (value > 1000)
  168. return SUMO_NOTHING;
  169. if (value > 600)
  170. return SUMO_LEFT;
  171. if (value > 400)
  172. return SUMO_RIGHT;
  173. return SUMO_CENTER;
  174. }
  175.  
  176. private static void startMovePot() throws InterruptedException {
  177.  
  178. boolean b = true;
  179. while (b) {
  180. leftMotor.forward();
  181. rightMotor.forward();
  182.  
  183. sonic.getDistanceMode().fetchSample(sonarReadings, 0);
  184. float d = (int) (sonarReadings[0] * 1000) / 10f;
  185. LCD.drawString("front D " + d + " ", 0, 0);
  186.  
  187. if (d < 10) {
  188. leftMotor.stop(true);
  189. rightMotor.stop(true);
  190. b = false;
  191. LCD.drawString("BREAK ", 0, 2);
  192. return;
  193. }
  194.  
  195. iRSensor.fetchSample(iRreadings, 0);
  196. float di = iRreadings[0] * 10;
  197.  
  198. if (di > sideSensorDistance + 4) {
  199. leftMotor.setSpeed(300);
  200. rightMotor.setSpeed(175);
  201. Thread.sleep(400);
  202. leftMotor.setSpeed(300);
  203. rightMotor.setSpeed(300);
  204. }
  205. if (di < sideSensorDistance) {
  206. rightMotor.setSpeed(300);
  207. leftMotor.setSpeed(175);
  208. Thread.sleep(400);
  209. leftMotor.setSpeed(300);
  210. rightMotor.setSpeed(300);
  211. }
  212.  
  213. }
  214.  
  215. }
  216.  
  217.  
  218. private static void findBlackSheet() throws InterruptedException {
  219. colorSampler.fetchSample(colourReadings, 0);
  220. int r = (int) (colourReadings[0] * 1000);
  221. int g = (int) (colourReadings[1] * 1000);
  222. int b = (int) (colourReadings[2] * 1000);
  223.  
  224. boolean flip = false;
  225.  
  226.  
  227. if((r < 10 && g < 10 && b < 10) && flip == false){
  228. flip = true;
  229. //loopTimes--;
  230. Button.LEDPattern(3);
  231. }
  232. else if(r < 10 && g < 10 && b < 10){
  233. flip = true;
  234. }
  235. else{
  236. flip = false;
  237. }
  238.  
  239. }
  240.  
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement