import lejos.nxt.*;
import lejos.nxt.Button;
import lejos.nxt.LCD;
import lejos.nxt.MotorPort;
import lejos.nxt.NXTMotor;
import lejos.nxt.SensorPort;
import lejos.nxt.addon.NXTLineLeader;
import lejos.util.Delay;
import lejos.nxt.addon.ColorHTSensor;
import lejos.nxt.addon.*;
import java.io.*;
import lejos.nxt.comm.*;
import lejos.util.Delay;
import lejos.nxt.comm.*;
class LineLeader{
static NXTLineLeader liner = new NXTLineLeader(SensorPort.S1);
static UltrasonicSensor us = new UltrasonicSensor(SensorPort.S2);
static RFIDSensor rfs = new RFIDSensor(SensorPort.S3);
static UltrasonicSensor us2 = new UltrasonicSensor(SensorPort.S4);
static NXTMotor MotorA = new NXTMotor(MotorPort.A);
static NXTMotor MotorB = new NXTMotor(MotorPort.B);
static NXTMotor MotorC = new NXTMotor(MotorPort.C);
static int b,afactories1,afactories2,afactories3;
static int re;
public static void main(String arg[]){
Button.ESCAPE.addButtonListener(new ButtonListener(){
public void buttonPressed(Button b){System.exit(1);}
public void buttonReleased(Button b) {}
});
LineLeader ll = new LineLeader();
ll.Calibration();
LCD.drawString("Waiting...", 0, 0);
NXTConnection connection = Bluetooth.waitForConnection();
DataInputStream dataIn = connection.openDataInputStream();
DataOutputStream dataOut =connection.openDataOutputStream();
LCD.drawString("OK...", 0, 0);
try{
while(true){
b = dataIn.read();
afactories1 = dataIn.read();
afactories2 = dataIn.read();
afactories3 = dataIn.read();
if(b==1){
ll.followLine(afactories1,afactories2,afactories3,dataIn,re);
LCD.drawString("ok", 0, 1);
}
}
}catch(IOException e) {System.exit(1);}
}
public void Calibration() {
liner.wakeUp();
LCD.drawString("Calibration: ", 0, 0);
LCD.drawString("Place LineLeader", 0, 1);
LCD.drawString("over bright area ", 0, 2);
LCD.drawString("Press Enter", 0, 3);
Button.ENTER.waitForPressAndRelease();
LCD.drawString("**** wait ******", 0, 3);
liner.calibrate(NXTLineLeader.LineColor.WHITE);
LCD.clear();
LCD.drawString("Calibration: ", 0, 0);
LCD.drawString("Place LineLeader", 0, 4);
LCD.drawString("over black area ", 0, 5);
LCD.drawString("Press Enter", 0, 6);
Button.ENTER.waitForPressAndRelease();
LCD.drawString("**** wait ******", 0, 7);
liner.calibrate(NXTLineLeader.LineColor.BLACK);
LCD.clear();
LCD.drawString("Calibration done", 0, 4);
Button.ENTER.waitForPressAndRelease();
LCD.clear();
}
public void followLine(int afactories1,int afactories2,int afactories3,DataInputStream dataIn,int re ){
int sound,sound2;
int save=0;
int total=0;
int save2=0;
int save1=0;
int steering;
int a=0;
int b=0;
int c=0;
int d=0;
int e=0;
int llResult;
long rfid1,rfid2,rfid3,rfid4,rfid5,rfid6,rfid7,rfid8,rfid9,
rfid10,rfid11,rfid12,rfid13,rfid14,rfid15,rfid16,rfid17,rfid18,rfid19,rfid20;
String r1="462887518288";
String r2="620140363856";
String r3="952060739664";
String r4="376166023248";
String r5="672418168912";
String r6="1048630460496";
String r7="511893831760";
String r8="577291354192";
String r9="748687392848";
String r10="471292837968";
String r15="534442344528";
String r16="200055586896";
rfid1=Long.parseLong(r1);
rfid2=Long.parseLong(r2);
rfid3=Long.parseLong(r3);
rfid4=Long.parseLong(r4);
rfid5=Long.parseLong(r5);
rfid6=Long.parseLong(r6);
rfid7=Long.parseLong(r7);
rfid8=Long.parseLong(r8);
rfid9=Long.parseLong(r9);
rfid10=Long.parseLong(r10);
rfid15=Long.parseLong(r15);
rfid16=Long.parseLong(r16);
long id;
while (!Button.ESCAPE.isDown()){
id = rfs.readTransponderAsLong(true);
llResult = liner.getResult();
sound = us.getDistance();
sound2 = us2.getDistance();
LCD.clear();
LCD.drawInt(liner.getResult(), 0, 0);
LCD.drawString(Integer.toBinaryString(llResult), 0, 1);
LCD.drawInt(a, 0, 1);
LCD.drawInt(b, 0, 1);
if(id==rfid1||id==rfid2){
a++;
}
if(id==rfid3||id==rfid4){
b++;
}
if(id==rfid5||id==rfid6){
c++;
}
if(id==rfid7||id==rfid8){
e++;
}
if(sound<=18){
while(sound<30){
sound = us.getDistance();
Motor.A.setSpeed(0);
Motor.B.setSpeed(0);
Motor.A.forward();
Motor.B.forward();
}
}
else if(id==rfid1||id==rfid2){
for(int i=0;i<afactories2;i++){
Motor.A.setSpeed(0);
Motor.B.setSpeed(0);
Motor.C.setSpeed(400);
Motor.C.forward();
Delay.msDelay(900);
Motor.A.setSpeed(0);
Motor.B.setSpeed(0);
Motor.C.setSpeed(0);
Delay.msDelay(500);
}
Motor.A.setSpeed(300);
Motor.B.setSpeed(300);
Motor.A.forward();
Motor.B.forward();
Delay.msDelay(500);
}
else if(id==rfid3||id==rfid4){
for(int i=0;i<afactories3;i++){
Motor.A.setSpeed(0);
Motor.B.setSpeed(0);
Motor.C.setSpeed(400);
Motor.C.forward();
Delay.msDelay(900);
Motor.A.setSpeed(0);
Motor.B.setSpeed(0);
Motor.C.setSpeed(0);
Delay.msDelay(500);
}
Motor.A.setSpeed(300);
Motor.B.setSpeed(300);
Motor.A.forward();
Motor.B.forward();
Delay.msDelay(500);
}
else if(id==rfid5||id==rfid6){
for(int i=0;i<afactories1;i++){
Motor.A.setSpeed(0);
Motor.B.setSpeed(0);
Motor.C.setSpeed(400);
Motor.C.forward();
Delay.msDelay(900);
Motor.A.setSpeed(0);
Motor.B.setSpeed(0);
Motor.C.setSpeed(0);
Delay.msDelay(500);
}
Motor.A.setSpeed(300);
Motor.B.setSpeed(300);
Motor.A.forward();
Motor.B.forward();
Delay.msDelay(500);
}
else if(id==rfid9||id==rfid10){
Motor.A.setSpeed(400);
Motor.B.setSpeed(0);
Motor.A.forward();
Motor.B.forward();
Delay.msDelay(850);
a=0;
b=0;
c=0;
d=0;
e=0;
}
else if(id==rfid15||id==rfid16){
Motor.A.setSpeed(400);
Motor.B.setSpeed(400);
Motor.A.forward();
Motor.B.forward();
Delay.msDelay(300);
break;
}
llResult = liner.getResult();
if(llResult==24){
Motor.A.setSpeed(300);
Motor.B.setSpeed(300);
Motor.A.forward();
Motor.B.forward();
}
else if(llResult==16){
Motor.A.setSpeed(300);
Motor.B.setSpeed(270);
Motor.A.forward();
Motor.B.forward();
}
else if(llResult==8){
Motor.A.setSpeed(270);
Motor.B.setSpeed(300);
Motor.A.forward();
Motor.B.forward();
}
else if(llResult==48)
Motor.A.setSpeed(300);
Motor.B.setSpeed(240);
Motor.A.forward();
Motor.B.forward();
}
else if(llResult==12){
Motor.A.setSpeed(240);
Motor.B.setSpeed(300);
Motor.A.forward();
Motor.B.forward();
}
else if(llResult==32){
Motor.A.setSpeed(300);
Motor.B.setSpeed(210);
Motor.A.forward();
Motor.B.forward();
}
else if(llResult==4){
Motor.A.setSpeed(210);
Motor.B.setSpeed(300);
Motor.A.forward();
Motor.B.forward();
}
else if(llResult==96){
Motor.A.setSpeed(300);
Motor.B.setSpeed(180);
Motor.A.forward();
Motor.B.forward();
}
else if(llResult==6){
Motor.A.setSpeed(180);
Motor.B.setSpeed(300);
Motor.A.forward();
Motor.B.forward();
}
else if(llResult==64){
Motor.A.setSpeed(300);
Motor.B.setSpeed(150);
Motor.A.forward();
Motor.B.forward();
}
else if(llResult==2){
Motor.A.setSpeed(150);
Motor.B.setSpeed(300);
Motor.A.forward();
Motor.B.forward();
}
else if(llResult==192){
Motor.A.setSpeed(300);
Motor.B.setSpeed(120);
Motor.A.forward();
Motor.B.forward();
}
else if(llResult==3){
Motor.A.setSpeed(120);
Motor.B.setSpeed(300);
Motor.A.forward();
Motor.B.forward();
}
else if(llResult==128){
Motor.A.setSpeed(300);
Motor.B.setSpeed(90);
Motor.A.forward();
Motor.B.forward();
}
else if(llResult==1){
Motor.A.setSpeed(90);
Motor.B.setSpeed(300);
Motor.A.forward();
Motor.B.forward();
}
else if(llResult>0&&llResult<16){
Motor.A.setSpeed(90);
Motor.B.setSpeed(300);
Motor.A.forward();
Motor.B.forward();
}
else if(llResult>=16&&llResult<256){
Motor.A.setSpeed(300);
Motor.B.setSpeed(90);
Motor.A.forward();
Motor.B.forward();
}
}
Motor.A.setSpeed(0);
Motor.B.setSpeed(0);
Motor.A.forward();
Motor.B.forward();
}
}