Programar el robot para que emule el comportamiento de un robot espía. Este robot utiliza sus sensores ultrasónicos para moverse en una habitación oscura. Si la luz se enciende quiere decir que alguien entra a la habitación y debe esconderse en un lugar oscuro (debajo de algún mueble en un rincón). OJO. Para fines de esta práctica, el robot debe correr a un rincón oscuro, no solamente protegerse de la luz.
import lejos.nxt.*;import lejos.robotics.subsumption.*;import lejos.robotics.navigation.*;
public class Hide {
public static void main (String[] aArg) throws Exception { TachoPilot pilot = new TachoPilot(5.6f, 16.0f, Motor.B, Motor.C); LightSensor light = new LightSensor(SensorPort.S3); UltrasonicSensor distance =new UltrasonicSensor(SensorPort.S1); pilot.setTurnSpeed(300); Behavior getBackToShadow = new GetBackToShadow(pilot, light); //Behavior goForward = new GoForward(pilot, light); Behavior findAWall = new FindAWall(pilot,distance);
//Behavior [] behaviorArray = {getBackToLine, goForward, findAWall}; Behavior [] behaviorArray = {getBackToShadow, findAWall}; Arbitrator arbitrator = new Arbitrator(behaviorArray); LCD.drawString("Hide", 2, 2); Button.waitForPress(); arbitrator.start(); }}
import lejos.nxt.*;import lejos.robotics.subsumption.*;import lejos.robotics.navigation.*; public class GetBackToShadow implements Behavior, SensorPortListener{ private Pilot pilot; private LightSensor light; private int sweep; private boolean suppress; private boolean inLight;
public GetBackToShadow(Pilot pilot, LightSensor light){ this.pilot = pilot; this.light = light; suppress = false; inLight = false; SensorPort.S3.addSensorPortListener(this); }
public void stateChanged(SensorPort port, int oldValue, int newValue){ if(light.readValue() > 20){ inLight = true; } }
public boolean takeControl(){ if(inLight){ LCD.clear(); LCD.drawString("GetBackToShadow", 2, 2); inLight = false; return true; } else { return false; } }
public void suppress(){ do{ pilot.stop(); }while(light.readValue() < 20); suppress = true; }
public void action(){ while(!suppress){ int prev=light.readValue(); if(prev>20){ pilot.travel(20); if(prev < light.readvalue())< div=""> { pilot.rotate(90); } } while(!suppress && pilot.isMoving()){ Thread.yield(); } } suppress = false; } }light.readvalue())
import lejos.nxt.*;import lejos.robotics.subsumption.*;import lejos.robotics.navigation.*; public class FindAWall implements Behavior, SensorPortListener{ private Pilot pilot; private UltrasonicSensor distance; private int sweep; private boolean suppress; private boolean findWall;
public FindAWall(Pilot pilot, UltrasonicSensor distance){ this.pilot = pilot; this.distance = distance; suppress = false; findWall = false; SensorPort.S3.addSensorPortListener(this); }
public void stateChanged(SensorPort port, int oldValue, int newValue){ if(distance.getDistance() < 20){ findWall = true; } }
public boolean takeControl(){ if(findWall){ LCD.clear(); LCD.drawString("FindAWall", 2, 2); findWall = false; return true; } else { return false; } }
public void suppress(){ suppress = true; }
public void action(){ while(distance.getDistance()<20){ pilot.rotate(90); } suppress=false; } }
No hay comentarios:
Publicar un comentario