Ejercicios
1. HUIR DE LA LUZ (ROBOT ESPÍA O CUCARACHA ARTIFICIAL)
El robot se encuentra inicialmente en un cuarto oscuro. Utiliza su sensor infrarrojo para detectar cualquier posible haz de luz y huir de ella. Es decir, irse por otro camino.
import lejos.nxt.*;
import lejos.robotics.subsumption.*;
import lejos.robotics.navigation.*;
public class Light {
public static void main (String[] aArg)
throws Exception
{
final Pilot pilot = new TachoPilot(5.6f,16.0f,Motor.A, Motor.B, false);
final LightSensor light = new LightSensor(SensorPort.S2);
pilot.setTurnSpeed(300);//100
Behavior DriveForward = new Behavior()
{
public boolean takeControl() {return light.readValue() <= 40;}
public void suppress() {
pilot.stop();
}
public void action() {
pilot.forward();
while(light.readValue() <= 40) Thread.yield();
}
};
Behavior OffLine = new Behavior()
{
private boolean suppress = false;
public boolean takeControl() {return light.readValue() > 40;}
public void suppress() {
suppress = true;
}
public void action() {
int sweep = 5;//5, 10
while (!suppress) {
pilot.rotate(sweep,true);
while (!suppress && pilot.isMoving()) Thread.yield();
sweep *= -2.5;//5
}
pilot.stop();
suppress = false;
}
};
Behavior[] bArray = {OffLine, DriveForward};
LCD.drawString("HUYE DE LA LUZ!!", 0, 1);
Button.waitForPress();
(new Arbitrator(bArray)).start();
}
}
2. UBICAR Y ENCONTRAR UNA FUENTE DE SONIDO
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 java.util.*;
import lejos.nxt.*;
public class Sonido
{
public static void main(String[] args)
{
SoundSensor sonido = new SoundSensor(SensorPort.S2);
boolean flag = false;
Motor.B.setSpeed(300);
Motor.C.setSpeed(300);
int c;
for(;;)
{
c=sonido.readValue();
System.out.println(c);
if(c>70)
{
Motor.B.forward();
Motor.C.forward();
}
else
{
Motor.B.forward();
Motor.C.backward();
}
}
}
}
3. RODEAR UN OBJETO
El robot está programado para que utilizando el sensor de toque pueda encontrar y rodear un objeto siguiendo su contorno. El objeto puede tener cualquier forma (rectangular como una caja o cilíndrica como un bote, el objeto será suficientemente pesado como para que el robot no lo mueva, dentro de límites razonables.
import lejos.nxt.*;
/**
* Simple test of the Lego UltraSonic Sensor.
*
* @author Lawrie Griffiths
*
*/
public class rodear{
public static void main(String[] args) throws Exception {
boolean flag = false;
UltrasonicSensor sonic = new UltrasonicSensor(SensorPort.S1);
TouchSensor ts = new TouchSensor(SensorPort.S4);
while(!Button.ESCAPE.isPressed()) {
Motor.B.forward();
Motor.C.forward();
LCD.clear();
LCD.drawInt(sonic.getDistance(), 0, 3);
LCD.refresh();
Thread.sleep(50);
if(ts.isPressed() || sonic.getDistance() < 20)
{
flag = true;
Motor.B.stop();
Motor.C.stop();
Motor.B.resetTachoCount();
Motor.C.resetTachoCount();
Motor.B.forward();
Motor.C.backward();
System.out.println("ciclo 1");
int count = 0;
while(count < 160 )
{
count = Motor.B.getTachoCount();
}
Motor.B.forward();
Motor.C.forward();
Thread.sleep(1000);
Motor.B.stop();
Motor.C.stop();
Motor.C.resetTachoCount();
Motor.B.resetTachoCount();
Motor.C.forward();
Motor.B.backward();
count = 0;
while(count < 160 )
{
count = Motor.C.getTachoCount();
}
Motor.B.stop();
Motor.C.stop();
}else if(flag == true)
{
Motor.B.forward();
Motor.C.forward();
Thread.sleep(1500);
Motor.B.stop();
Motor.C.stop();
Motor.C.resetTachoCount();
Motor.B.resetTachoCount();
Motor.C.forward();
Motor.B.backward();
int count = 0;
while(count < 180 )
{
count = Motor.C.getTachoCount();
}
Motor.B.stop();
Motor.C.stop();
Thread.sleep(1000);
}
}
}
}
4. SALIR DE UN LABERINTO
El robot está programado para que utilizando el sensor ultrasónico pueda localizar obstáculos y girar para evitarlos. El robot también se debe programar para que utilizando estas habilidades (localizar obstáculos y girar) pueda salir de un laberinto de ángulos rectos.
5. SEGUIR UNA LÍNEA NEGRA
Programar el robot para que utilizando el sensor de luz pueda encontrar y seguir una trayectoria marcada con una línea negra pintada sobre una superficie plana. La trayectoria puede ser recta, con giros en varios ángulos, zig-zag, etc.
Para este ejercicio utilizamos los puertos A y C para las llantas y el sensor de luz al frente y apuntando hacia abajo en el puerto 1.
El robot sigue avanzando mientras detecte la línea (mientras regrese el valor menor igual a 40)
import lejos.nxt.*;
import lejos.robotics.subsumption.*;
import lejos.robotics.navigation.*;
public class LineaNegra {
public static void main (String[] aArg)
throws Exception
{
// Change last parameter of Pilot to specify on which
// direction you want to be "forward" for your vehicle.
// The wheel and axle dimension parameters should be
// set for your robot, but are not critical.
final Pilot pilot = new TachoPilot(5.6f,16.0f,Motor.A, Motor.C, false);
final LightSensor light = new LightSensor(SensorPort.S1);
pilot.setTurnSpeed(180);
/**
* this behavior wants to take control when the light sensor sees the line
*/
Behavior DriveForward = new Behavior()
{
public boolean takeControl() {return light.readValue() <= 40;}
public void suppress() {
pilot.stop();
}
public void action() {
pilot.forward();
while(light.readValue() <= 40) Thread.yield(); //action complete when not on line
}
};
Behavior OffLine = new Behavior()
{
private boolean suppress = false;
public boolean takeControl() {return light.readValue() > 40;}
public void suppress() {
suppress = true;
}
public void action() {
int sweep = 10;
while (!suppress) {
pilot.rotate(sweep,true);
while (!suppress && pilot.isMoving()) Thread.yield();
sweep *= -2;
}
pilot.stop();
suppress = false;
}
};
Behavior[] bArray = {OffLine, DriveForward};
LCD.drawString("Line ", 0, 1);
Button.waitForPress();
(new Arbitrator(bArray)).start();
}
}