From efecf8ba85d5b20f6a551fb3e90ae11013cef62d Mon Sep 17 00:00:00 2001 From: Natanael Adityasatria Date: Thu, 29 Oct 2015 11:20:33 +0100 Subject: [PATCH] Made the robot connected --- mart/ev3/ex2/nl/ru/des/BTMemory.java | 81 +++++++++++++++-------- mart/ev3/ex2/nl/ru/des/CommBehaviour.java | 30 +++++++++ mart/ev3/ex2/nl/ru/des/Main.java | 12 +++- 3 files changed, 93 insertions(+), 30 deletions(-) create mode 100644 mart/ev3/ex2/nl/ru/des/CommBehaviour.java diff --git a/mart/ev3/ex2/nl/ru/des/BTMemory.java b/mart/ev3/ex2/nl/ru/des/BTMemory.java index 2321396..e47ab0e 100644 --- a/mart/ev3/ex2/nl/ru/des/BTMemory.java +++ b/mart/ev3/ex2/nl/ru/des/BTMemory.java @@ -3,76 +3,103 @@ package nl.ru.des; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintWriter; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; +import lejos.hardware.Sound; +import lejos.hardware.lcd.LCD; import lejos.remote.nxt.BTConnector; import lejos.remote.nxt.NXTConnection; +import lejos.utility.Delay; -public abstract class BTMemory extends Thread{ - private List COLORSTOFIND = Arrays.asList(new String[]{"Blue", "Red", "Yellow"}); +public abstract class BTMemory extends Thread { + private List COLORSTOFIND = Arrays.asList(new String[] { "Blue", "Red", "Yellow" }); private Set colors; protected NXTConnection connection; - protected DataOutputStream dataOutput; + protected OutputStream dataOutput; protected DataInputStream dataInput; - - public BTMemory(){ - colors = new HashSet(); - setup(); + protected static PrintWriter writer; + + public BTMemory() { + colors = new HashSet(); + setup(); } - + public abstract void setup(); - - public void addColor(String c){ + + public void addColor(String c) { colors.add(c); } - - public boolean finished(){ + + public boolean finished() { return colors.equals(new HashSet(COLORSTOFIND)); } - + @Override - public void run(){ + public void run() { try { - while(true){ - if(dataInput.available()>0){ + while (true) { + if (dataInput.available() > 0) { + //String msg = dataInput.readLine(); LCDPrinter.print(Character.toChars(dataInput.read()).toString()); } } - } catch (IOException e) { + } + catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } + + public static void SendMessage(String message){ + writer.println(message); + writer.flush(); + } - public static BTMemory getBTMemory(boolean master, final String rovername){ - if(master){ - return new BTMemory(){ + public static BTMemory getBTMemory(boolean master, final String rovername) { + if (master) { + return new BTMemory() { @Override public void setup() { + //LCD.drawString("Create connection", 0, 0); BTConnector btconnector = new BTConnector(); + //LCD.drawString("BTConnector created", 0, 1); connection = btconnector.connect(rovername, NXTConnection.RAW); + //LCD.drawString("Connection created", 0, 2); dataInput = connection.openDataInputStream(); - dataOutput = connection.openDataOutputStream(); + dataOutput = connection.openOutputStream(); + writer = new PrintWriter(dataOutput); + //LCD.drawString("Input output created", 0, 3); try { - dataOutput.write(77); - } catch (IOException e) { + //dataOutput.write(77); + writer.println("Hellow"); + writer.flush(); + //LCD.clear(); + //LCD.drawString("Message sent", 0, 4); + } catch (Exception e) { e.printStackTrace(); } } }; } else { - return new BTMemory(){ + return new BTMemory() { @Override public void setup() { + //LCD.drawString("Create connection", 0, 0); BTConnector btconnector = new BTConnector(); - btconnector.waitForConnection(60, NXTConnection.RAW); + //LCD.drawString("BTConnector created", 0, 1); + connection = btconnector.waitForConnection(60000, NXTConnection.RAW); + //LCD.drawString("Connection created", 0, 2); dataInput = connection.openDataInputStream(); - dataOutput = connection.openDataOutputStream(); + dataOutput = connection.openOutputStream(); + writer = new PrintWriter(dataOutput); + //LCD.drawString("Input output created", 0, 3); } }; } } -} +} \ No newline at end of file diff --git a/mart/ev3/ex2/nl/ru/des/CommBehaviour.java b/mart/ev3/ex2/nl/ru/des/CommBehaviour.java new file mode 100644 index 0000000..c52e15c --- /dev/null +++ b/mart/ev3/ex2/nl/ru/des/CommBehaviour.java @@ -0,0 +1,30 @@ +package nl.ru.des; + +import lejos.hardware.Sound; +import lejos.hardware.motor.EV3LargeRegulatedMotor; +import lejos.robotics.subsumption.Behavior; + +public class CommBehaviour implements Behavior { + + private String message; + + @Override + public boolean takeControl() { + // TODO Auto-generated method stub + return false; + } + + @Override + public void action() { + // TODO Auto-generated method stub + LCDPrinter.print(message); + } + + @Override + public void suppress() { + // TODO Auto-generated method stub + + } + + +} diff --git a/mart/ev3/ex2/nl/ru/des/Main.java b/mart/ev3/ex2/nl/ru/des/Main.java index d03946c..7c78e31 100644 --- a/mart/ev3/ex2/nl/ru/des/Main.java +++ b/mart/ev3/ex2/nl/ru/des/Main.java @@ -3,6 +3,7 @@ package nl.ru.des; import lejos.hardware.ev3.EV3; import lejos.hardware.ev3.LocalEV3; import lejos.hardware.lcd.Font; +import lejos.hardware.lcd.LCD; import lejos.hardware.motor.EV3LargeRegulatedMotor; import lejos.hardware.port.MotorPort; import lejos.hardware.sensor.EV3ColorSensor; @@ -14,10 +15,15 @@ public class Main { public static void main(String[] args) { EV3 brick = LocalEV3.get(); - - //BTMemory.getBTMemory(true, null); - BTMemory.getBTMemory(false, "Rover1"); LCDPrinter lcdprinter = new LCDPrinter(brick.getGraphicsLCD(), Font.getSmallFont()); + + /* + * Start the slave first. "Socket is 18" is always displayed, we can just ignore this + */ + //BTMemory.getBTMemory(false, "Rover2"); + BTMemory.getBTMemory(true, "Rover1"); + LCDPrinter.print("Robots are connected"); + LCDPrinter.print("Starting up systems"); lcdprinter.start(); -- 2.20.1