Advertisement
Sonikku1980

[JAVA] CIT-128 Project 8 NXT Line Follower

Apr 20th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.61 KB | None | 0 0
  1. /*
  2.     Author: Robert A. Petersen and Esteban Pina
  3.     Date: 20 April 2015
  4.     Assignment: Project 8
  5.     Description: Line Follower Robot
  6. */
  7.  
  8. import lejos.nxt.*;
  9. import lejos.robotics.navigation.DifferentialPilot;
  10. import lejos.util.Stopwatch;
  11.  
  12. public class Project8  implements ButtonListener //make sure the classname matches the filename (case-sensitive)
  13. {
  14.     private DifferentialPilot bot;  //field declaration for a Pilot object
  15.     private LightSensor light;  //field declaration for a LightSensor object
  16.  
  17.     //create a constant for the light threshold
  18.     private final int THRESHOLD = 45
  19.     ;
  20.     //the code in the main method will not change (except for the classname)
  21.     public static void main(String[] args)
  22.     {
  23.         Project8 myProgram = new Project8(); //matches classname (case-sensitive)
  24.         Button.ESCAPE.addButtonListener(myProgram);
  25.         Button.LEFT.addButtonListener(myProgram);  //register this object as a listener
  26.         myProgram.run();
  27.     }//main()
  28.  
  29.     //the constructor generally instantiates field objects and
  30.     //sets other initial state information
  31.     public Project8()   //matches classname (case-sensitive)
  32.     {
  33.         //instantiate a DifferentialPilot object named "bot"
  34.         bot = new DifferentialPilot(56, 108, Motor.B, Motor.C);
  35.  
  36.         // place light sensor on Port 3
  37.         light = new LightSensor(SensorPort.S3);
  38.  
  39.     }//constructor
  40.  
  41.     //your code goes in this method
  42.     public void run()
  43.     {
  44.  
  45.         // Create a Stopwatch that monitors elapsed time in milliseconds
  46.         Stopwatch sw = new Stopwatch();
  47.         long time_in_milliseconds = 180000L;
  48.  
  49.         // Create your variables here
  50.         int right = 7;
  51.         int left = -7;
  52.         int rotation;
  53.         //sets bot speeds
  54.         bot.setTravelSpeed(75);
  55.         bot.setRotateSpeed(75);
  56.         // Determine a good threshold value first to set the threshold constant
  57.         // this may be commented out after the threshold has been set.
  58.         // checkLight();
  59.  
  60.         // Find the line, then follow the line until ESCAPE is pressed,
  61.         // use light.readValue() to read the sensor
  62.         while ( sw.elapsed() < 70000 )
  63.         {
  64.             if (light.readValue() > THRESHOLD )
  65.             {
  66.                 // If you are here, the light value is above
  67.                 // the threshold, now do something based on
  68.                 // that fact ...
  69.                 rotation = right;
  70.                 bot.rotate(rotation);
  71.                 if(light.readValue() > THRESHOLD)
  72.                 {
  73.                     rotation = left * 2;
  74.                     bot.rotate(rotation);
  75.                     if (light.readValue() > THRESHOLD)
  76.                     {
  77.                         rotation = right * 4;
  78.                         bot.rotate(rotation);
  79.                         if(light.readValue() > THRESHOLD)
  80.                         {
  81.                             rotation = left * 8;
  82.                             bot.rotate(rotation);
  83.                             if(light.readValue() > THRESHOLD)
  84.                             {
  85.                                 rotation = right * 16;
  86.                                 bot.rotate(rotation);
  87.                                 if (light.readValue() > THRESHOLD)
  88.                                 {
  89.                                     rotation = left * 16;
  90.                                     bot.rotate(rotation);
  91.                                     if (light.readValue() > THRESHOLD)
  92.                                     {
  93.                                         rotation = right * 5;
  94.                                         bot.rotate(rotation);
  95.                                         if (light.readValue() > THRESHOLD)
  96.                                         {
  97.                                             findLine();
  98.                                         }
  99.                                     }
  100.                                 }
  101.                             }
  102.                         }          
  103.                     }
  104.                 }          
  105.             }
  106.             else
  107.             {
  108.                 if (light.readValue() < THRESHOLD)
  109.                 {
  110.                     bot.forward(); // Move Forward
  111.                 }
  112.  
  113.             } // if
  114.  
  115.         } // while
  116.  
  117.     } //run()
  118.  
  119.     //displays values for light and dark until LEFT is pressed
  120.    
  121.     public void findLine()
  122.     {
  123.         //sets bot speeds
  124.         bot.setTravelSpeed(75);
  125.         bot.setRotateSpeed(75);
  126.         //Looks for the the line
  127.         boolean lineFound;
  128.         if (light.readValue() > THRESHOLD)
  129.         {
  130.             lineFound = false;
  131.         }
  132.         else
  133.         {
  134.             lineFound = true;
  135.         }
  136.         while (lineFound == false)
  137.         {
  138.             bot.forward();
  139.             if (light.readValue() < THRESHOLD)
  140.             {
  141.                 lineFound = true;
  142.             }
  143.         }
  144.    
  145.     }
  146.  
  147.     public void checkLight()
  148.     {
  149.         // Use the light sensor as a reflection sensor
  150.         light.setFloodlight(true);
  151.         LCD.drawString("Light %: ", 0, 0);
  152.  
  153.         // Show light percent until LEFT is pressed
  154.         while (! Button.LEFT.isPressed())
  155.         {
  156.             LCD.drawInt(light.readValue(), 3, 9, 0);
  157.             LCD.refresh();
  158.         }
  159.     }
  160.  
  161.     //the following method writes a string to the screen. Useful for debugging.
  162.     public void displayString(String msg)
  163.     {
  164.         LCD.clear();
  165.         LCD.drawString(msg, 0, 0);  //writes the msg to the screen
  166.         LCD.refresh();
  167.     }//displayString()
  168.  
  169.     //the following method writes an int (whole number) to the screen. Useful for debugging.
  170.     public void displayInt(int num)
  171.     {
  172.         LCD.clear();
  173.         LCD.drawInt(num, 4, 0, 0);  //writes the num to the screen
  174.         LCD.refresh();
  175.     } //displayInt
  176.  
  177.     //the following method allows the bot to do what it was doing, but suspends execution of the
  178.     //next statement. When this method returns, the program continues executing where it left off.
  179.     public void pause(int milli)
  180.     {
  181.         try
  182.         {
  183.             Thread.sleep(milli);
  184.         }//try
  185.         catch(InterruptedException e)
  186.         {
  187.         }//catch
  188.     }//sleep()
  189.  
  190.     // this method is required when a class is a ButtonListener
  191.     // it responds to the action of the button being pressed
  192.     public void buttonPressed(Button b)
  193.     {
  194.         if (b.equals(Button.ESCAPE))
  195.         {
  196.             displayString("ESCAPE pressed");
  197.             bot.stop();
  198.         }
  199.         else return;
  200.  
  201.     }
  202.  
  203.     // this is also required, but we aren't using it, so it is empty
  204.     public void buttonReleased(Button b) {}
  205.  
  206. }//class Project8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement