1. Have a look at the examples included with lejos. Specifically SocketTest.java/SocketServer.java .
2. I have a loop on my nxt that sends status info every 2sec and the pc interprets them and updates a swing gui. I send data as strings.
Take a look at the Navigator class it allows the robot to use X and Y to move around. Simplifies drawing on a GUI.
My NXT communication class (some code needs to be commented out due to that i don't post all classes used)
- Code: Select all
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.Socket;
import lejos.nxt.Sound;
import lejos.nxt.comm.BTConnection;
import lejos.nxt.comm.Bluetooth;
//NXT.waita(200) = try{Thread.sleep(200) catch(Exception e){}
public class Communication extends Thread{
private DataInputStream ins;
private DataOutputStream outs;
private BTConnection btc = null;
private Socket sock = null;
private Object writeLock = new Object();
private Object readLock = new Object();
Communication(){
setDaemon(true);
}
public void run(){
while(true){
while(true){ //The break below stops this loop so that the other code gets skipped.
try {
btc = Bluetooth.waitForConnection();
sock = new Socket("localhost", 8081, btc);
} catch (IOException e) {
System.out.println("Failed to connect to server");
//Debug sounds
Sound.playNote(Sound.PIANO, 500, 500);
NXT.waita(200);
Sound.playNote(Sound.PIANO, 500, 500);
break;
}
try {
ins = sock.getDataInputStream();
outs = sock.getDataOutputStream();
} catch (IOException e1) {
System.out.println("IO Exception while getting IO streams");
Sound.playNote(Sound.PIANO, 1000, 500);
NXT.waita(200);
Sound.playNote(Sound.PIANO, 1000, 500);
}
while(true){
try {
String s = readLine();
if (s.equals("bye")){
NXT.close();
}else if (s.equals("status")){
send("_MA" + Motor.A.getTachoCount());
send("_MB" + Motor.B.getTachoCount());
send("_MC" + Motor.C.getTachoCount());
send("_UL" + UltraSound.getDistance());
}else if (s.equals("reset")){
close();
System.exit(0);
}
} catch(EOFException e){
send("EOFException while reading");
break;
} catch(IOException e){
send("IO Exception while reading");
break;
}
}
close();
}
}
}
public void close(){
try {
ins.close();
outs.close();
} catch (IOException e) {
System.out.println("IO Exception while closing");
Sound.playNote(Sound.PIANO, 1500, 500);
NXT.waita(200);
Sound.playNote(Sound.PIANO, 1500, 500);
}
outs=null;
sock.close();
}
private String readLine() throws IOException{
StringBuffer sb = new StringBuffer();
synchronized(readLock){
while(true) {
char c = ins.readChar();
if (c == '\n') break;
sb.append(c);
}
return sb.toString();
}
}
//Other threads can call this method and the synchronized ensures that the message remains intact.
public void send(String s){
if(outs!=null){ //avoids null pointer exception
synchronized(writeLock){
try {
outs.writeChars(s + '\n');
outs.flush();
} catch (IOException e) {
System.out.println("IO Exception while sending");
Sound.playNote(Sound.PIANO, 2000, 500);
NXT.waita(200);
Sound.playNote(Sound.PIANO, 2000, 500);
}
}
}
}
}
PC code
- Code: Select all
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
/**
*I exuvo used the example code and then modified it to my needs.
*Comments below may not be correct anymore.
*----------------------------------------------
* This PC sample works with the leJOS NXJ sample, SocketTest.
*
* It shows you how to connect a program running on the NXT to a TCP/IP server
* on your PC, intranet on the Internet. The host program uses the standard Java
* SocketServer class, and the NXT program uses the NXTSocket class.
*
* To run the sample, first start SocketTest on the NXT and it will wait for
* a connection to the Socket Proxy.
*
* Then run this program and it will display a small GUI window. Change the port
* it listens on if 8081 is used on your PC. If you do this you will need to change
* SocketTest as well.
*
* Then run the Socket Proxy (nxjsocketproxy command) optionally specifying the name and
* or address of the NXT you want to connect to. This will cause Socket Proxy to
* connect to SocketTest on the NXT and SocketTest to connect to this socket server.
*
* You can then type words in the GUI and see the responses come back from the NXT (
* on the standard output stream).
*
* Type "bye" to cause SocketTest on the NXT to stop.
*
*
* @author Ranulf Green and Lawrie Griffiths and exuvo
*
*/
public class NXTConsole {
DataOutputStream outToSocket = null;
DataInputStream inFromSocket = null;
JLabel MA = new JLabel("Motor A Rotation: ");
JLabel MB = new JLabel("Motor B Rotation: ");
JLabel MC = new JLabel("Motor C Rotation: ");
JLabel Ultra = new JLabel("Ultra Distance: ");
JLabel LL = new JLabel("Light Left: ");
JLabel LR = new JLabel("Light Right: ");
public NXTConsole(){
JFrame frame = new JFrame();
JPanel panel = new JPanel(new FlowLayout());
final JTextField input = new JTextField(10);
JButton send = new JButton("Send");
panel.add(input);
panel.add(send);
panel.add(MA);
panel.add(MB);
panel.add(MC);
panel.add(Ultra);
panel.add(LL);
panel.add(LR);
send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String s = input.getText();
System.out.println("Sending: \"" + s + "\"");
s+='\n';
try {
outToSocket.writeChars(s);
outToSocket.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
});
WindowListener listener = new WindowAdapter() {
public void windowClosing(WindowEvent w) {
System.exit(0);
}
};
frame.addWindowListener(listener);
frame.add(panel);
frame.pack();
frame.setVisible(true);
new Listen();
new Reader();
}
private class Listen extends Thread{
Listen(){
start();
}
public void run(){
try {
ServerSocket s = new ServerSocket(8081);
while(true){
Socket sock = s.accept();
System.out.println("Socket Server Connected");
outToSocket = new DataOutputStream(sock.getOutputStream());
inFromSocket = new DataInputStream(sock.getInputStream());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class Reader extends Thread{
Reader(){
start();
}
public void run(){
// Read line
while(true){
while(inFromSocket!=null){
try{
StringBuffer sb = new StringBuffer();
char c;
while(true){
c = inFromSocket.readChar();
if (c == '\n') break;
sb.append(c);
}
String s = sb.toString();
if(s.startsWith("_MA")){
MA.setText("Motor A Rotation: " + s.substring(3));
}else if(s.startsWith("_MB")){
MB.setText("Motor B Rotation: " + s.substring(3));
}else if(s.startsWith("_MC")){
MC.setText("Motor C Rotation: " + s.substring(3));
}else if(s.startsWith("_UL")){
Ultra.setText("Ultra Distance: " + s.substring(3));
}else if(s.startsWith("_LL")){
LL.setText("Light Left: " + s.substring(3));
}else if(s.startsWith("_LR")){
LR.setText("Light Right: " + s.substring(3));
}
//System.out.println("Read: " + s);
} catch (IOException e) {
System.err.println("IOException while reading");
try {
sleep(100);
} catch (InterruptedException e1) {
}
} catch (NullPointerException e){
try {
sleep(100);
} catch (InterruptedException e1) {
}
//System.err.println("NullPointerException while reading");
}
}
}
}
}
public static void main(String[] args) {
new NXTConsole();
}
}
3. Check if the motors are still rotating using Motor.A.getTachoCount()
Easiest way is to have a second Thread that is counting the motors rotations (while(Motor.A.isMoving()) and if it gets too low its stalled.