Programar el robot para que utilizando el sensor de sonido (micrófono) pueda ubicar y llegar hasta una fuente de sonido que si bien está fija, funciona de manera episódica. La fuente de sonido puede encontrarse en cualquier dirección con respecto de la dirección inicial del robot y dentro de un área de 15 m2
import lejos.nxt.Button;import lejos.nxt.LCD;import lejos.nxt.Motor;import lejos.nxt.SensorPort;import lejos.nxt.SoundSensor;import lejos.robotics.navigation.Pilot;import lejos.robotics.navigation.TachoPilot;
public class SoundFinder {
public static SoundSensor sound; public static Pilot pilot; public static double averange;
public static void main(String[] args) { pilot = new TachoPilot(5.6f, 11.5f, Motor.B, Motor.C); sound = new SoundSensor(SensorPort.S2,true); pilot.setTurnSpeed(180);
Button.waitForPress();
while(true) { int selectedPos=0;
selectedPos=rotateAndFind(); rotate(selectedPos); pilot.travel(20); }
}
public static int rotateAndFind() { double value=5; int pos=0; double temp=0;
for(int i=0; i<8; i++) {
temp=readSensorValue(); print("esc "+temp); if(temp>value) { value=temp; pos=i; } pilot.rotate(45); while(pilot.isMoving()); pilot.stop(); }
pilot.rotate(10); //fix de 180 print("Val max:"+value); return pos;
}
public static void print(String text) { LCD.clear(); LCD.drawString(text, 2, 2);
}
public static void rotate(int val) { print("Acomodando"); pilot.rotate(45*val); pilot.rotate(55); //fix de rotación }
public static double readSensorValue() { double value=0.0; sleep(500); value=sound.readValue(); sleep(500); return value;
}
public static void sleep(int time) { try { Thread.sleep(time); } catch (InterruptedException e) { } }
}
No hay comentarios:
Publicar un comentario