Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 KB | None | 0 0
  1. import edu.cmu.ri.createlab.terk.robot.finch.Finch;
  2. public class Assignment2Real {
  3.     static Finch f = new Finch() ;
  4.     static int minimumrequirment = 50; //the min requirment for light to be used
  5.     static int left = (int) leftSensorValue();
  6.     static int right = (int) rightSensorValue();
  7.  
  8.     public static void main (String args[]) throws InterruptedException  {
  9.         System.out.println(leftSensorValue());
  10.         System.out.println(rightSensorValue());
  11.        
  12.         run();
  13.    
  14.        
  15.  
  16.     }
  17.  
  18.     public static void run() throws InterruptedException {
  19.        
  20.         if (rightSensorValue() < minimumrequirment && leftSensorValue() < minimumrequirment && !f.isBeakUp() ) {
  21.             noLightFound();
  22.            
  23.         }
  24.         if (rightSensorValue() > minimumrequirment && leftSensorValue() > minimumrequirment && !f.isBeakUp()) {
  25.             brightnessLevel();
  26.             moveWithLight();
  27.                
  28.         }
  29.        
  30.         else if (f.isBeakUp()) {
  31.             System.out.println("finch is beak up");
  32.             f.quit();
  33.         }
  34.        
  35.     }
  36.     public static double rightSensorValue() {
  37.         return f.getRightLightSensor();
  38.     }
  39.     //These 2 methods allow the light sensor to change values. Int wouldn't update so I chose method as it always gave me the changed values.
  40.     public static double leftSensorValue() {
  41.         return f.getLeftLightSensor();
  42.     }
  43.  
  44.     public static void startUp() throws InterruptedException{
  45.         if(leftSensorValue()<minimumrequirment && rightSensorValue()<minimumrequirment) {
  46.             f.setLED(200,150,0); //change to yellow
  47.             f.setWheelVelocities(50,50,3000); // moving at a slow speed for 4 seconds  correct in debug
  48.         }
  49.     }
  50.  
  51.     public static void moveWithLight() throws InterruptedException{
  52.         if(leftSensorValue() > minimumrequirment &&  rightSensorValue() > minimumrequirment && leftSensorValue() > rightSensorValue() && !f.isBeakUp()) {
  53.             f.setWheelVelocities(90,210);
  54.             System.out.println("turning left");
  55.         }
  56.         if(leftSensorValue() > minimumrequirment &&  rightSensorValue() > minimumrequirment && rightSensorValue() > leftSensorValue() && !f.isBeakUp()) {
  57.             f.setWheelVelocities(210,90);
  58.             System.out.println("turning right");
  59.         }
  60.         if (leftSensorValue() < minimumrequirment &&  rightSensorValue() < minimumrequirment && !f.isBeakUp()){
  61.             run();
  62.             return; // add return as a failsafe method.
  63.         }
  64.         if (f.isBeakUp()) {
  65.             run();
  66.             return;
  67.         }
  68.         moveWithLight();
  69.     }
  70.  
  71.     private static void noLightFound() throws InterruptedException {  
  72.         if(leftSensorValue()<minimumrequirment && rightSensorValue()<minimumrequirment && !f.isBeakUp()) {
  73.             f.setLED(200,150,0); //change to yellow
  74.             f.setWheelVelocities(50,50,3000); // moving at a slow speed for 4 seconds  correct in debug
  75.         }
  76.         // This keeps on moving finch 90 degrees at a time to find the light
  77.         if (rightSensorValue() < minimumrequirment && leftSensorValue() < minimumrequirment && !f.isBeakUp()) {
  78.             Thread.sleep(500);//stop 1/2 second
  79.             f.setWheelVelocities(0,100,1350); //turn  Left 90 degrees
  80.             if (rightSensorValue() > minimumrequirment && leftSensorValue() > minimumrequirment && !f.isBeakUp()) {
  81.                 System.out.println("Looking for Light");
  82.                 run();
  83.                 return;
  84.             }
  85.         }
  86.         if (f.isBeakUp()) {
  87.             run();
  88.             return;
  89.         }
  90.         noLightFound(); //recursive method
  91.     }
  92.  
  93.     public static void brightnessLevel() throws InterruptedException{ //making finch beak change Brightness
  94.         if(leftSensorValue() > (minimumrequirment + 1) && leftSensorValue() <=99 && rightSensorValue() > (minimumrequirment +1) && rightSensorValue() <=99 && !f.isBeakUp() );{ //if finch sensors are between these variables, then the brightness changes correct with debug
  95.             f.buzz(500, 2000);
  96.             f.setLED(20,0,0);
  97.         }
  98.         if(leftSensorValue() >= 100 && leftSensorValue() <=170 && rightSensorValue() >= 100 && rightSensorValue() <=170 && !f.isBeakUp()) {
  99.             f.buzz(500, 2000);
  100.             f.setLED(100,0,0);
  101.         }
  102.  
  103.         if(leftSensorValue() >= 171 && leftSensorValue() <=255 && rightSensorValue() >= 171 && rightSensorValue() <=255 && !f.isBeakUp()) {
  104.             f.setLED(255,0,0);
  105.         }
  106.        
  107.         if (f.isBeakUp()) {
  108.             run();
  109.             return;
  110.            
  111.         }
  112.     }
  113.    
  114.    
  115.  
  116.    
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement