Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author(s): Robert A. Petersen and Esteban Tina
- Assignment: Project #7 --- Wall Follower Robot
- Date: April 8, 2015
- Description: Program NXT Robot to follow wall, if touch sensor is triggered the robot will back up, and continue following wall.
- */
- import lejos.nxt.*;
- import lejos.robotics.navigation.DifferentialPilot;
- import lejos.util.Stopwatch;
- public class Project7 implements ButtonListener // Make sure the classname matches the filename (case-sensitive)
- {
- private DifferentialPilot bot; // Field declaration for a Pilot object
- //the code in the main method will not change (except for the classname)
- public static void main(String[] args)
- {
- Project7 myProgram = new Project7(); // Matches classname (case-sensitive)
- Button.ESCAPE.addButtonListener(myProgram); // Register this object as a listener
- myProgram.run();
- } // main()
- /*
- The constructor generally instantiates field objects and
- sets other initial state information
- */
- public Project7() //matches classname (case-sensitive)
- {
- bot = new DifferentialPilot(56, 108, Motor.B, Motor.C); // Instantiate a Pilot object named "bot"
- } //constructor
- /*
- Your code goes in this method
- */
- public void run()
- {
- // Create a TouchSensor that monitors sensor port S1
- TouchSensor touch = new TouchSensor(SensorPort.S1);
- // Create a Stopwatch that monitors elapsed time in milliseconds
- Stopwatch sw = new Stopwatch();
- /*
- Create your variables here.
- Call pause() to allow time to get your finger off the button.
- */
- pause(3000);
- // Reset the stop watch to 0, before entering the loop
- sw.reset();
- /*
- Set the stop watch to run for some amount of time.
- 10 seconds = 10000 milliseconds (10 seconds)
- 30 seconds = 30000 milliseconds (1/2 minute)
- 60 seconds = 60000 milliseconds (1 minute)
- 120 seconds = 120000 milliseconds (2 minutes)
- 180 seconds = 180000 milliseconds (3 minutes)
- etc.
- */
- long time_in_milliseconds = 10000L;
- while ( sw.elapsed() < 20000 )
- {
- bot.forward (); // Send the robot forward
- if (touch.isPressed())
- {
- bot.travel(-100);
- bot.rotate(-15);
- } // if
- } // while
- } // run()
- /*
- The following method writes a string to the screen. Useful for debugging.
- */
- public void displayString(String msg)
- {
- LCD.clear();
- LCD.drawString(msg, 0, 0); //writes the msg to the screen
- LCD.refresh();
- } // displayString()
- /*
- The following method writes an int (whole number) to the screen.
- Useful for debugging.
- */
- public void displayInt(int num)
- {
- LCD.clear();
- LCD.drawInt(num, 4, 0, 0); //writes the num to the screen
- LCD.refresh();
- } // displayInt()
- /*
- The following method allows the bot to do what it was doing,
- but suspends execution of the next statement.
- When this method returns, the program continues executing
- where it left off.
- */
- public void pause(int milli)
- {
- try
- {
- Thread.sleep(milli);
- } // try
- catch(InterruptedException e)
- {
- } // catch
- } // pause()
- /*
- This method is required when a class is a ButtonListener
- it responds to the action of the button being pressed.
- */
- public void buttonPressed(Button b)
- {
- displayString("btn pressed");
- bot.stop();
- }
- /*
- This is also required, but we aren't using it, so it is empty.
- */
- public void buttonReleased(Button b)
- {
- // Empty method
- } // buttonReleased()
- } // class Project7
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement