package de.nordakademie.cerca;
import lejos.navigation.ZPilot;
import lejos.nxt.Button;
import lejos.nxt.LightSensor;
import lejos.nxt.Motor;
import lejos.nxt.SensorPort;
import de.nordakademie.lejos.statemachine.AbstractState;
import de.nordakademie.lejos.statemachine.AbstractTransition;
import de.nordakademie.lejos.statemachine.EndState;
import de.nordakademie.lejos.statemachine.IState;
import de.nordakademie.lejos.statemachine.ITransition;
import de.nordakademie.lejos.statemachine.LCDChangeListener;
import de.nordakademie.lejos.statemachine.StateChangeListener;
import de.nordakademie.lejos.statemachine.Statemachine;
public class LineFollower extends Statemachine {
public LineFollower() {
this(null);
}
public LineFollower(Statemachine parent) {
super(parent);
driveOnLine.setTransitions(new ITransition[]{bright, exit});
searchLine.setTransitions(new ITransition[]{dark, exit});
setStartTransitions(new ITransition[]{new ITransition() {
final public boolean guard() {
return true;
}
final public IState getTarget() {
return driveOnLine;
}
final public int getDelay() {
return 0;
}
}
});
}
/**
* Variables of the statemachine
*
**/
private ZPilot pilot = new ZPilot(5.6f, 11.6f, Motor.A, Motor.C);
private LightSensor light = new LightSensor(SensorPort.S1, true);
/**
* Internal states of the statemachine including exit, excluding initial states
*
**/
public IState driveOnLine = new AbstractState(this) {
public void doMethod() throws InterruptedException {
pilot.forward();
}
public void entryMethod() {
pilot.setSpeed(300);
}
final public String getName() {
return "DriveOnLine";
}
};
public IState endState = new EndState(this);
public IState searchLine = new AbstractState(this) {
public void doMethod() throws InterruptedException {
int sweep = 10;
while (true) {
pilot.rotate(sweep);
sweep *= -2;
}
}
public void entryMethod() {
pilot.setSpeed(200);
}
final public String getName() {
return "SearchLine";
}
};
/**
* Transtions of the statemachine including transitions from start state
*
**/
public ITransition dark = new AbstractTransition(driveOnLine) {
final public boolean guard() {
return light.readValue() < 39;
}
final public static String NAME = "Dark";
};
public ITransition bright = new AbstractTransition(searchLine) {
final public boolean guard() {
return light.readValue() > 42;
}
final public static String NAME = "Bright";
};
public ITransition exit = new AbstractTransition(endState) {
final public boolean guard() {
return Button.ESCAPE.isPressed();
}
final public static String NAME = "Exit";
};
/**
* Set he Listeners thru the complete statemachine hierarchy
*
**/
public void setListener(StateChangeListener[] listeners) {
super.setListener(listeners);
}
/**
* Main method to make the statemachine startable from outside
*
**/
public static void main(String[] args) {
LineFollower lineFollower = new LineFollower();
StateChangeListener[] allListeners = new StateChangeListener[]{new LCDChangeListener()};
lineFollower.setListener(allListeners);
try {
lineFollower.doMethod();
} catch (InterruptedException i) {
}
for (StateChangeListener l : allListeners) {
l.stop();
}
}
final public String getName() {
return "LineFollower";
}
}