Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.33 KB | None | 0 0
  1. package LezCW;
  2. import lejos.hardware.motor.Motor;
  3. import lejos.hardware.port.SensorPort;
  4. import lejos.hardware.sensor.EV3ColorSensor;
  5. import lejos.hardware.sensor.EV3GyroSensor;
  6. import lejos.hardware.sensor.EV3TouchSensor;
  7. import lejos.hardware.sensor.EV3UltrasonicSensor;
  8. import lejos.robotics.SampleProvider;
  9. import lejos.utility.Delay;
  10.  
  11. public class LEZCOMEHOME {
  12.  
  13. // Constructors for sensors.
  14. private static EV3UltrasonicSensor us1 = new
  15. EV3UltrasonicSensor(SensorPort.S1);
  16.  
  17.  
  18. private static EV3TouchSensor touch2 = new
  19. EV3TouchSensor(SensorPort.S2);
  20.  
  21. private static EV3TouchSensor touch3 = new
  22. EV3TouchSensor(SensorPort.S3);
  23.  
  24. /* private static EV3GyroSensor gyroSensor = new
  25. EV3GyroSensor(SensorPort.S2);
  26. private static EV3ColorSensor color1 = new
  27. EV3ColorSensor(SensorPort.S4);
  28. */
  29.  
  30. /*
  31. * Basic testing.
  32. */
  33. public static void main(String[] args) throws InterruptedException {
  34. Motor.A.setSpeed(300);
  35. Motor.B.setSpeed(300);
  36. us1.enable();
  37. while (true){
  38. // while(getSonicDist() > 0.35) {
  39. while(isTouched() == 0) {
  40. Motor.A.forward();
  41. Motor.B.forward();
  42. }
  43. Motor.A.stop();
  44. Motor.B.stop();
  45. turn(getDirection());
  46. }
  47. }
  48.  
  49. // for (int i = 0; i < 100; i++) {
  50. //
  51. // Delay.msDelay(1000);
  52. //// float sonicDist = getSonicDist();
  53. //// if (sonicDist > 0.2) {
  54. //// Motor.A.forward();
  55. //// Motor.B.forward();
  56. //// Delay.msDelay(300);
  57. //// Motor.A.stop();
  58. //// Motor.B.stop();
  59. //// } else {
  60. //// Motor.A.backward();
  61. //// Motor.B.backward();
  62. //// Delay.msDelay(500);
  63. //// Motor.A.forward();
  64. //// Motor.B.backward();
  65. //// Delay.msDelay(350);
  66. //// Motor.A.stop();
  67. //// Motor.B.stop();
  68. //// }
  69. // }
  70. // us1.disable();
  71.  
  72. /*
  73. * Returns the distance currently read by the ultrasonic sensor and prints value to
  74. * EV3 screen.
  75. */
  76. public static float getSonicDist(){
  77. // Measures distance to object in front of sensor and stores in sp.
  78. final SampleProvider sp = us1.getDistanceMode();
  79. float distanceValue = 0;
  80. // Creates a float array with size equal to the number of elements in the sensor sample.
  81. float [] sample = new float[sp.sampleSize()];
  82. // Fetches the sample from sp with offset 0 and stores in sample array.
  83. sp.fetchSample(sample, 0);
  84. // Gets first index of sample array and stores in distanceValue.
  85. distanceValue = sample[0];
  86. System.out.println("Ultrasonic distance: " + distanceValue);
  87. return distanceValue;
  88. }
  89.  
  90. /* Calculates the diretion to travel
  91. *
  92. */
  93. public static char getDirection(){
  94. char direction = 's';
  95. int[] rotationAngle = {45, -90};// rotate right then left
  96. float[] directionDistance = {0,0};
  97. for(int i = 0; i <2; i++){
  98. Motor.C.rotate(rotationAngle[i]);
  99. directionDistance[i] = getSonicDist();
  100. }
  101. if(directionDistance[0]>directionDistance[1]){
  102. direction = 'r'; // if left wall is closer turn right
  103. }else{
  104. direction = 'l';
  105. }
  106. Motor.C.rotate(45);
  107. return direction;
  108. }
  109.  
  110. public static void turn(char dir){
  111. switch(dir){
  112. case 'r':
  113. Motor.A.backward();
  114. Motor.B.backward();
  115. Delay.msDelay(100);
  116. Motor.A.forward();
  117. Motor.B.backward();
  118. Delay.msDelay(250); //determines turn size
  119. Motor.A.stop();
  120. Motor.B.stop();
  121. break;
  122. case'l':
  123. Motor.A.backward();
  124. Motor.B.backward();
  125. Delay.msDelay(100);
  126. Motor.B.forward();
  127. Motor.A.backward();
  128. Delay.msDelay(250); //determines turn size
  129. Motor.A.stop();
  130. Motor.B.stop();
  131. break;
  132. default:
  133. break;
  134. }
  135. }
  136.  
  137. /*
  138. * Returns a 1 if touch sensor is pressed and a 0 if not. GB - THIS IS A GUESS, MAY BE OPPOSITE.
  139. */
  140. public static int isTouched() {
  141. final SampleProvider sp2 = touch2.getTouchMode();
  142. final SampleProvider sp3 = touch3.getTouchMode();
  143. int touchValue2 = 0;
  144. int touchValue3 = 0;
  145. float [] sample2 = new float[sp2.sampleSize()];
  146. float [] sample3 = new float[sp3.sampleSize()];
  147. sp2.fetchSample(sample2, 0);
  148. sp3.fetchSample(sample3, 0);
  149. touchValue2 = (int)sample2[0];
  150. touchValue3 = (int)sample3[0];
  151. System.out.println("Touch 2: " + touchValue2);
  152. System.out.println("Touch 3: " + touchValue3);
  153. if (touchValue2 == 1 || touchValue3 == 1) {
  154. return 1;
  155. } else {
  156. return 0;
  157. }
  158. }
  159. }
  160. /*
  161. * Returns the angle currently read by the gyro sensor and prints value to
  162. * EV3 screen.
  163. */
  164. // public static int getGyroAngle() {
  165. // // Measures the orientation of the sensor in respect to its start orientation.
  166. // final SampleProvider sp = gyroSensor.getAngleMode();
  167. // int angleValue = 0;
  168. // float [] sample = new float[sp.sampleSize()];
  169. // sp.fetchSample(sample, 0);
  170. // angleValue = (int)sample[0];
  171. // System.out.println("Gyro angle: " + angleValue);
  172. // return angleValue;
  173. // }
  174. //
  175. // /*
  176. // * Returns a 1 if touch sensor is pressed and a 0 if not. GB - THIS IS A GUESS, MAY BE OPPOSITE.
  177. // */
  178. // public static int isTouched() {
  179. // final SampleProvider sp = touch1.getTouchMode();
  180. // int touchValue = 0;
  181. // float [] sample = new float[sp.sampleSize()];
  182. // sp.fetchSample(sample, 0);
  183. // touchValue = (int)sample[0];
  184. // System.out.println("Touch: " + touchValue);
  185. // return touchValue;
  186. // }
  187. //
  188. // /*
  189. // * Returns RBG detected of colour sensor. GB - NOT SURE ABOUT THE VALUES IN SAMPLE[]
  190. // */
  191. // public static float[] getRGB() {
  192. // SampleProvider sp = color1.getRGBMode();
  193. // float[] sample = new float[sp.sampleSize()];
  194. // sp.fetchSample(sample, 0);
  195. // System.out.println("R: " + (int)sample[0] + " G: " + (int)sample[1] + " B: " + (int)sample[2]);
  196. // return sample;
  197. // }
  198. //
  199. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement