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.
The magenta SwarmBots shown here are our first effort at making robots swarm. A SwarmBot looks at the total light intensity around it. If the total brightness if too large, the SwarmBot moves away from the light by turning right if the light on its left front and side eyes is brighter than the light on its right front and side eyes, and turning left otherwise. If the total light is too small, the SwarmBot reverses this rule and moves toward the light instead.
For the curious, here is the definition of the SwarmBot class:
/**
* SwarmBots move toward light if they are in the dark, and vice-versa.
* They therefore like moderate total light.
* If they hit something, they back up and turn.
*/
public class SwarmBot extends ICRobot implements Runnable {
public SwarmBot(float x, float y, float theta, RobotsApplet app){
super(x,y,theta,Color.magenta, app);
}
void main() {
float lf, ls, rf, rs, r;
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)){
motor(L_MOTOR, -10);
motor(R_MOTOR, -10);
msleep(500);
motor(L_MOTOR, 10);
msleep(random(1000));
}else{
lf = analog(LF_EYE);
ls = analog(LS_EYE);
rf = analog(RF_EYE);
rs = analog(RS_EYE);
r = analog(R_EYE);
if (lf + ls + rf + rs + r < 0.006){ // Too dark. Go to light.
motor(L_MOTOR, 40 + round(300 * sqrt(rf+rs)));
motor(R_MOTOR, 40 + round(300 * sqrt(lf+ls)));
}else{ // Too light. Go to dark.
motor(R_MOTOR, 40 + round(300 * sqrt(rf+rs)));
motor(L_MOTOR, 40 + round(300 * sqrt(lf+ls)));
}
}
defer();
}
}
}