Advertisement
Pratik03

Untitled

Mar 26th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. import edu.cmu.ri.createlab.terk.robot.finch.Finch;
  4. import java.util.ArrayList;
  5. public class FinchLight {
  6. private static Finch myFinch = null;
  7.  
  8. public static int MaxLightSensor;
  9. static ArrayList<Long>TStart = new ArrayList<Long>();
  10. public static int LeftLightSensor;
  11. public static int RightLightSensor;
  12. public static int MinLightSensor;
  13. public static int noLight = 100;
  14. public static int delay = 5000;
  15. public static int LightCounter;
  16. public static long start;
  17. public static int LightSensorLeft;
  18. public static int LightSensorRight;
  19.  
  20. public static void main(String[] args) throws InterruptedException {
  21.  
  22. long start_time = System.currentTimeMillis();
  23. TStart.add(start_time);
  24.  
  25. myFinch = new Finch();
  26.  
  27. MinLightSensor = getMinLightSensor();
  28. finchstart();
  29.  
  30. }
  31. public static int getRightLightSensor() {
  32. int LeftLightSensor = myFinch.getLeftLightSensor();
  33. return LeftLightSensor;
  34. }
  35.  
  36. public static int getLeftLightSensor() {
  37. int RightLightSensor = myFinch.getRightLightSensor();
  38. return RightLightSensor;
  39. }
  40.  
  41.  
  42.  
  43. public static int getMinLightSensor() {
  44. int LeftLightSensor = myFinch.getLeftLightSensor();
  45. int RightLightSensor = myFinch.getRightLightSensor();
  46. int min;
  47. if (LeftLightSensor < RightLightSensor) {
  48. min = LeftLightSensor;
  49. } else {
  50. min = RightLightSensor;
  51. }
  52.  
  53. return min;
  54.  
  55.  
  56. }
  57. public static int getMaxLightSensor() {
  58. int LeftLightSensor = myFinch.getLeftLightSensor();
  59. int RightLightSensor = myFinch.getRightLightSensor();
  60. int max;
  61. if (LeftLightSensor > RightLightSensor) {
  62. max = LeftLightSensor;
  63. } else {
  64. max = RightLightSensor;
  65. }
  66.  
  67. return max;
  68.  
  69.  
  70. }
  71.  
  72.  
  73. public static void finchstart() throws InterruptedException {
  74. if (myFinch.isFinchLevel()== false) {
  75. myFinch.quit();
  76. }
  77. while (myFinch.isFinchLevel() == false || myFinch.isBeakUp() == false) {
  78. long start = System.currentTimeMillis();
  79. TStart.add(start);
  80. if (myFinch.getLeftLightSensor() < noLight || myFinch.getRightLightSensor() < noLight) {
  81.  
  82. myFinch.setLED(255, 0, 0, 2000);
  83. myFinch.setLED(125, 125, 0, 2000);
  84. myFinch.setLED(125, 0, 125, 3000);
  85. lightsearch();
  86. } else {
  87. lightfound();
  88. }
  89. }
  90. logs();
  91. }
  92.  
  93. public static void lightsearch() throws InterruptedException {
  94. while (myFinch.isFinchLevel() == true || myFinch.isBeakUp() == false) {
  95. long time = System.currentTimeMillis();
  96. myFinch.setLED(0, 255, 0);
  97. while (System.currentTimeMillis() - time < delay) {
  98. myFinch.setWheelVelocities(125, 125);
  99. int LightSensorLeft = myFinch.getLeftLightSensor();
  100. int LightSensorRight = myFinch.getRightLightSensor();
  101. if (LightSensorLeft > noLight | LightSensorRight > noLight) {
  102. LightCounter++;
  103. myFinch.buzz(255,1000);
  104.  
  105. lightfound();
  106. }
  107. }
  108. myFinch.stopWheels();
  109. myFinch.sleep(1000);
  110. myFinch.setLED(0,0,255);
  111. myFinch.setWheelVelocities(255, 0,2000);
  112. myFinch.setWheelVelocities(200, 0, 1000);
  113.  
  114.  
  115. lightsearch();
  116.  
  117. }
  118. logs();
  119. }
  120.  
  121. public static void lightfound() throws InterruptedException {
  122.  
  123. while (myFinch.isFinchLevel() == false || myFinch.isBeakUp() == false) {
  124. MaxLightSensor = getMaxLightSensor();
  125. MinLightSensor = getMinLightSensor();
  126. LeftLightSensor = getLeftLightSensor();
  127. RightLightSensor= getRightLightSensor();
  128. whichside();
  129. if (MinLightSensor < noLight) {
  130. lightsearch();
  131. }
  132.  
  133. }
  134. logs();
  135.  
  136. }
  137.  
  138. public static void whichside() throws InterruptedException {
  139. while (true) {
  140.  
  141. int LightSensorLeft = myFinch.getLeftLightSensor();
  142. int LightSensorRight = myFinch.getRightLightSensor();
  143. int average = (LightSensorLeft + LightSensorRight) / 2;
  144. myFinch.setLED(average, 0, 0);
  145.  
  146. if (myFinch.getLeftLightSensor() > 100 || myFinch.getRightLightSensor() > 100) {
  147.  
  148. if (myFinch.getLeftLightSensor() > myFinch.getRightLightSensor()) {
  149. myFinch.setWheelVelocities(0, 100);
  150. } else {
  151. myFinch.setWheelVelocities(100, 0);
  152. }
  153.  
  154. } else if (myFinch.getLeftLightSensor() < noLight) {
  155. myFinch.buzz(1, 1000);
  156.  
  157.  
  158. lightsearch();
  159. }
  160.  
  161. }
  162.  
  163. }
  164.  
  165. public static void logs() {
  166. myFinch.quit();
  167. long end = System.currentTimeMillis();
  168. long start = TStart.get(0);
  169. long TotalDuration = (end - start) / 1000;
  170. Scanner reader = new Scanner(System.in);
  171. System.out.println("Do you want to see the logs? (1 = Yes, 2 = No): ");
  172. int UserInput = reader.nextInt();
  173. if (UserInput == 1) {
  174. System.out.println("The minimum amount of light sensed is " + MinLightSensor);
  175. System.out.println("The maximum amount of light sensed is " + MaxLightSensor);
  176. System.out.println("The amount of times light was detected " + LightCounter);
  177. System.out.println("The left light sensor value is " + LeftLightSensor);
  178. System.out.println("The right light sensor value is " + RightLightSensor);
  179. System.out.println("The total duration is " +TotalDuration + " seconds");
  180. System.out.println("Sorry to see you go :C");
  181.  
  182. } else if (UserInput == 2) {
  183. System.out.println("Sorry to see you go :C");
  184. System.exit(0);
  185. } else {
  186. System.out.println("Invalid input!");
  187. logs();
  188. }
  189. System.exit(0);
  190. }
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement