2 Vibrobots, 2 LightSeekingBots and a Fleet of Randobots

Use

Turn motion on and off with the buttons at the bottom right of the applet (just above this text). The slider controls the speed of the robots. Bothered that at high speeds the robots go right through one another? Then pick a lower speed. When the simulator is stopped, you can drag robots and lights from the palette into the arena. Dragging a light or the middle of a robot moves it. Dragging the end or corner of a robot turns it. Dragging lights or robots out of the arena deletes them. Just drop them someplace in the applet if you’re using Netscape.

Discussion

There are 3 kinds of robots shown here. Blue Vibrobots are the simplest—they just turn both motors together back and forth, back and forth. Red Randobots go in straight lines until they hit something, then they back up and turn by a random amount, and resume their monotonous path. Green LightSeekingBots have the speed of their left motors increasing with the intensity of light at their right front eyes, and the speed of their right motors increasing with the intensity of light at their left front eyes. Think about how they’ll move.

The Details

For the curious, here are the definitions of the Vibrobot and Randobot classes:

/**
 * A Vibrobot alternates going forward and backward for 1 second each.
 */
public class Vibrobot extends ICRobot implements Runnable {

	public Vibrobot(float x, float y, float theta, RobotsApplet app) {
		super(x,y,theta,Color.blue, app);
	}

	void main() {
		while(!stop_button()) { // A real robot should check whether
								// it is being turned off.  In our applet,
								// a thread can be stopped whenever
								// it checks the stop button.
			motor(L_MOTOR, 20); // Forward at speed 20
			motor(R_MOTOR, 20);
			msleep(1000);       // Sleep 1000 ms.
			motor(L_MOTOR, -20);// Back at speed 20.
			motor(R_MOTOR, -20);
			msleep(1000);
		}
	}
}

/**
 * Randobots run slowly until they hit something, then back up, turn a random amount.
 * and go forward again.
 */
public class Randobot extends ICRobot implements Runnable {

	public Randobot(float x, float y, float theta, RobotsApplet app) {
		super(x,y,theta,Color.red, app);
	}

	void main() {
		motor(L_MOTOR, 10); // Go forward slowly.
		motor(R_MOTOR, 10);
		while(!stop_button()) { // A real robot should check whether
								// it is being turned off.  In our applet,
								// a thread can be stopped whenever
								// it checks the stop button.
			if (digital(L_TOUCH) || digital(R_TOUCH)){ // If either sensor touches
				motor(L_MOTOR, -10); // Back up for 1000 ms
				motor(R_MOTOR, -10);
				msleep(1000);
				motor(R_MOTOR, 10); // spin right for a random part of 3000 ms
				msleep(random(3000));
				motor(L_MOTOR, 10); // go forward again
			}
			defer(); // I.e., yield().  Let another Robot have some cycles.
					 // It's important to yield, since Java Virtual Machines are
					 // not guaranteed to timeslice.  If we wrote main() without
					 // the defer(), then on some systems, only one bot would move.
		}
	}
}

Back to Robot Simulation Page.
Back to school.