User Tools

Site Tools


reu:cs177_lab

CS177 Lab Assignment

Lab Overview

Design a robot program that does the following three tasks:

  1. Runs away when it senses a person standing above it
  2. Be able to turn once it discovers an obstacle
  3. Beep and spin around if it is caught and gets “tagged”.

The lab should be done in a group of three students by expanding the given skeleton code (thanks to Alan Lagree):

/**
 * Lab 11
 *          LEGO NXT Lab
 *
 * Name:    student1's name
 *          student2's name
 *          student3's name
 *
 * Course:  CS17700
 *
 * Section: XXX
 *
 * Date:    mm/dd/2009
 *
 */
 
import lejos.nxt.*; // this is required for all programs that run on the NXT
 
public class legoRobo
{
 
    public static void main(String[] args)
    {
	boolean touchValue = false;
	int distance = 255;
	int curLight = 0;
	int normalLight = 0;
	TouchSensor touch = new TouchSensor(SensorPort.S1);
 
	/* TODO 1.1  
	 * Set up variables for the UltrasonicSensor (SensorPort S4)
	 * and the LightSensor  (port S3).
	 * Also, make a variable called "normalLight" and set
	 * it with the readNormalizedValue() method of the LightSensor
	*/
 
 
	while(true)
	{
		/* TODO 1.2
		 * Using the the LCD class, print a message on the
		 * robot's screen saying "Catch Me!!" on one line
		 * and some form of a smilie face on the next
		*/
 
		/*
                 * Make robot turn if its Ultrasonic sensor senses an object near by
                 * TODO 2.1
		 * On the next line get the distance from the Ultrasonic sensor
                 * UltrasonicSensor provides a getDistance() method that returns
                 * distance in cm or 255 if no object is in range.
		 *
		 * For our purpose we want to check if an object is closer
		 * than 60cm.
                */
 
 
		if(distance < 60)
		{
			turn();	
		}
 
 
		/*
                 * Make robot run away if its light sensor senses someone nearby
                 * The following line gets the value of the light sensor
                */
 
		curLight = light.readNormalizedValue();
		if(curLight < normalLight*(.8))
		{
			runAway();
		}
 
 
		/*
                 * Make robot beep and spin if its touch sensor has been pressed
		 * TODO 3.1
		 * On the next line, test if the touch sensor has been pressed
                */
 
 
		if(touchValue)
		{
			beepAndSpin();
		}
 
		/* If the dark grey button is pressed, we want
		 * the program to end
		 */
 
		if( Button.ESCAPE.isPressed() )
		{
			return;
		}
	}
 
    }
 
    /**
     * runAway: make the robot go a certain distance
     *
     */
    public static void runAway()	
    {
	int count=0;
 
        // Motor has a method resetTachoCount() which resets the tachometer count to 0
        // so that you can get an idea how far it has gone since reset
        // The tachometer counts the degrees the motor has rotated since the last reset
	Motor.A.resetTachoCount();
 
	// First we should make the robot turn 180 degrees.  
	// To do this we must make one wheel turn for a distance of roughly 850
 
	Motor.A.forward();
	while(count < 850)
	{
		count = Motor.A.getTachoCount();
	}
        Motor.A.stop();
 
 
 
	// Now we should move the robot forward.  
	// Notice how we first called the resetTachoCount() function
	// and reset count to 0
	count = 0;
	Motor.A.resetTachoCount();
	Motor.A.forward();
	Motor.C.forward();
 
	while(count < 1024)
	{
		count = Motor.A.getTachoCount();
	}
        Motor.A.stop();
        Motor.C.stop();
	Motor.A.resetTachoCount();
    }
 
    /**
     * turn: make the robot turn a random number of degrees less than 360
     *
     */
    public static void turn()	
    {
	/* TODO 2.2:
	 * Using the Motor class, turn the robot
	 * by moving one wheel forwards and one 
	 * backwards.
	 *
	 * The number of degrees the robot should 
	 * turn each wheel should be determined by the 
	 * Math.random() function multiplied by 850.
	 *
	*/
 
    }
 
    /**
     * beepAndSpin: make the robot beep 4 times and spin for a distance of 1700
     *
     */
    public static void beepAndSpin()	
    {
	/* TODO 3.2:
	 *
	 * Using the Sound class, emit four beeps, 
 
 
	 * then with the Motor class spin the robot
	 * for a distance of 1700.  
	 *
	 * You can use either one or two wheels to spin the robot.
	 *
	*/
 
    }
}
reu/cs177_lab.txt · Last modified: 2009/04/30 15:16 by akhandok