Advertisement
Sonikku1980

CIT 128 - NXT Lego Mindstorms Wall Follower Project

Apr 8th, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.05 KB | None | 0 0
  1. /*
  2.  
  3.     Author(s): Robert A. Petersen and Esteban Tina
  4.     Assignment:  Project #7 --- Wall Follower Robot
  5.     Date: April 8, 2015
  6.     Description:  Program NXT Robot to follow wall, if touch sensor is triggered the robot will back up, and continue following wall.
  7. */
  8.  
  9. import lejos.nxt.*;
  10. import lejos.robotics.navigation.DifferentialPilot;
  11. import lejos.util.Stopwatch;
  12.  
  13. public class Project7 implements ButtonListener // Make sure the classname matches the filename (case-sensitive)
  14. {
  15.     private DifferentialPilot bot; // Field declaration for a Pilot object
  16.  
  17.     //the code in the main method will not change (except for the classname)
  18.     public static void main(String[] args)
  19.     {
  20.  
  21.         Project7 myProgram = new Project7(); // Matches classname (case-sensitive)
  22.         Button.ESCAPE.addButtonListener(myProgram); // Register this object as a listener
  23.         myProgram.run();
  24.  
  25.     } // main()
  26.  
  27.     /*
  28.  
  29.         The constructor generally instantiates field objects and
  30.         sets other initial state information
  31.  
  32.     */
  33.     public Project7()   //matches classname (case-sensitive)
  34.     {
  35.  
  36.         bot = new DifferentialPilot(56, 108, Motor.B, Motor.C); // Instantiate a Pilot object named "bot"
  37.  
  38.     } //constructor
  39.  
  40.     /*
  41.  
  42.         Your code goes in this method
  43.  
  44.     */
  45.     public void run()
  46.     {
  47.  
  48.         // Create a TouchSensor that monitors sensor port S1
  49.         TouchSensor touch = new TouchSensor(SensorPort.S1);
  50.  
  51.         // Create a Stopwatch that monitors elapsed time in milliseconds
  52.         Stopwatch sw = new Stopwatch();
  53.        
  54.         /*
  55.             Create your variables here.
  56.             Call pause() to allow time to get your finger off the button.
  57.         */
  58.         pause(3000);
  59.         // Reset the stop watch to 0, before entering the loop
  60.         sw.reset();
  61.  
  62.         /*
  63.             Set the stop watch to run for some amount of time.
  64.  
  65.                 10  seconds = 10000 milliseconds  (10 seconds)
  66.                 30  seconds = 30000 milliseconds  (1/2 minute)
  67.                 60  seconds = 60000 milliseconds  (1 minute)
  68.                 120 seconds = 120000 milliseconds (2 minutes)
  69.                 180 seconds = 180000 milliseconds (3 minutes)
  70.                 etc.
  71.         */
  72.  
  73.         long time_in_milliseconds = 10000L;
  74.  
  75.         while ( sw.elapsed() < 20000 )
  76.         {
  77.             bot.forward (); // Send the robot forward
  78.  
  79.             if (touch.isPressed())
  80.             {
  81.         bot.travel(-100);
  82.                 bot.rotate(-15);
  83.             } // if
  84.  
  85.         } // while
  86.  
  87.     } // run()
  88.  
  89.     /*
  90.  
  91.         The following method writes a string to the screen. Useful for debugging.
  92.  
  93.     */
  94.     public void displayString(String msg)
  95.     {
  96.  
  97.         LCD.clear();
  98.         LCD.drawString(msg, 0, 0); //writes the msg to the screen
  99.         LCD.refresh();
  100.  
  101.     } // displayString()
  102.  
  103.     /*
  104.  
  105.         The following method writes an int (whole number) to the screen.
  106.         Useful for debugging.
  107.  
  108.     */
  109.     public void displayInt(int num)
  110.     {
  111.  
  112.         LCD.clear();
  113.         LCD.drawInt(num, 4, 0, 0); //writes the num to the screen
  114.         LCD.refresh();
  115.  
  116.     } // displayInt()
  117.  
  118.     /*
  119.  
  120.         The following method allows the bot to do what it was doing,
  121.         but suspends execution of the next statement.
  122.  
  123.         When this method returns, the program continues executing
  124.         where it left off.
  125.  
  126.     */
  127.     public void pause(int milli)
  128.     {
  129.  
  130.         try
  131.         {
  132.             Thread.sleep(milli);
  133.         } // try
  134.  
  135.         catch(InterruptedException e)
  136.         {
  137.         } // catch
  138.  
  139.     } // pause()
  140.  
  141.     /*
  142.  
  143.         This method is required when a class is a ButtonListener
  144.         it responds to the action of the button being pressed.
  145.  
  146.     */
  147.     public void buttonPressed(Button b)
  148.     {
  149.         displayString("btn pressed");
  150.         bot.stop();
  151.     }
  152.  
  153.     /*
  154.  
  155.         This is also required, but we aren't using it, so it is empty.
  156.  
  157.     */
  158.     public void buttonReleased(Button b)
  159.     {
  160.  
  161.         // Empty method
  162.  
  163.     } // buttonReleased()
  164.  
  165. } // class Project7
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement