Tic tac toe AL5C

Hi,
I’m a french student (sorry for my english) and I’ve got a project to do at school. I have to make my Lynxmotion arm playing tic tac toe. And I’ve been asking to program it in Java. But I don’t know how to provide Java to the robot. Please can someone help me ?

Thanks,

Gomis

Salut Gomis,

Tu as un projet très intéressant.
Si tu utilise un bras Lynxmotion version SSC-32, tout ce que tu auras a faire c’est de créer ton programme Java et envoyer des commande série au SSC-32.
Une version comme celle-ci par exemple:

Regarde le manuel du SSC-32 pour te donner plus de détails.

Et bien sure, n’hésite pas a poser des questions ici…!
(Je te répond en Francais, mais si tu veux des réponses des autres membre, en Anglais c’est mieux)

Eric Nantel - DiaLFonZo

Quelques autres ressources:

Le protocole du module SSC-32 est décrit ici:

En Java, vous pouvez utiliser la libraire RXTX pour faire la communication série:

Oh super les gars, merci !

Yes it’s a SSC-32 robot I’m am using. I’m going to read and work with the informations you gave me and I’ll keep you informed how the project is doing.

Gomis

Hello again,

To help me on the projet I found a book writen by Scott Pretson called “The Definitive Guide to Building Java Robots”. It is really usefull but I still stuck on a lot of problems.

One I can’t tackle, is on “OutputStream” java method. I’ve got an error “java.lang.NullPointerExecption” and I don’t really get what it means. Can you help me ?

Thanks,

Gomis

NullPointerExecption would mean that a variable you are passing to the method is Null when it should contain an object. If you can post your code, we may be able to quickly review it…

My goal is to send strings to the robot trough serial port. I worked with the Arduino link you gave before and it seem to work. But there is an error that I don’t understand :

Exception in thread “main” java.io.FileNotFoundException: COM3 (Accès refusé)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.(Unknown Source)
at java.io.FileOutputStream.(Unknown Source)
at java.io.PrintStream.(Unknown Source)
at AL5C.Déplacement.main(Déplacement.java:107)

My code:

[code]package AL5C;

import java.io.;
import java.util.Enumeration;
import gnu.io.
;

public class Déplacement {

private static final int TIME_OUT = 2000;
private static final int DATA_RATE = 9600;

public void initialize() {
	CommPortIdentifier portId = null;
	Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

	while (portEnum.hasMoreElements()) {
		CommPortIdentifier currPortId = (CommPortIdentifier) portEnum
				.nextElement();
		for (String portName : PORT_NAMES) {
			if (currPortId.getName().equals(portName)) {
				portId = currPortId;
				break;
			}
		}
	}
	if (portId == null) {
		System.out.println("Could not find COM port.");
		return;
	}

	try {
		serialPort = (SerialPort) portId.open(this.getClass().getName(),
				TIME_OUT);

		serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8,
				SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

	} catch (Exception e) {
		System.err.println(e.toString());
	}
}

public static void main(String] args) throws Exception {
	String cmd = "#0 P1000";
	
	Déplacement main = new Déplacement();
	main.initialize();
	Thread t = new Thread() {
		public void run() {
			try {
				Thread.sleep(1000000);
			} catch (InterruptedException ie) {
			}
		}
	};
	t.start();

	PrintStream out = new PrintStream("COM3");
	out.print(cmd);
	out.close();
	
}

}
[/code]

Instead of creating a new PrintStream object with

new PrintStream("COM3");

You should get the one that’s associated with the serial port:

out = serialPort.getOutputStream();

If you want to receive strings from the robot, you will also want InputStream of the serial port:

ins = serialPort.getInputStream();

I’ve created a PrintStream associated with the port:

OutputStream outStream = serialPort.getOutputStram();

But then if I write something on it:

outStream.write("#4 P1000".getBytes());

Nothing happened on my robot.

In fact I thought that as soon as I was connected with the port and I wrote a string in the outStream, my robot was going to move.
What am I doing wrong ?

(I think it’s the right thing to do because I’m also using LynxTerm and when I wrote something like "#2 P1000 T2000 " it moves).

You don’t have your in your Java code. You want to do this:

outStream.write("#4 P1000\n".getBytes());

The \n does the in Java.

It doesn’t work … Once it’s written in the OutputStream, does it has to move ? Or does I have to do something else ?

[code]package Robot_AL5C;

import java.io.;
import gnu.io.
;

public class GestionnairePortSérie {

private SerialPort serialPort;
private OutputStream outStream;
private InputStream inStream;

/** Serial connection*/
public void connect(String portName) throws IOException {
	try {
		CommPortIdentifier portId = CommPortIdentifier
				.getPortIdentifier(portName);

		serialPort = (SerialPort) portId.open("Application", 5000);

		setSerialPortParameters();

		// i/o opening for the connection.
		outStream = serialPort.getOutputStream();
		inStream = serialPort.getInputStream();

		System.out.println("Connected to " + portName);

	} catch (NoSuchPortException e) {
		throw new IOException(e.getMessage());
	} catch (PortInUseException e) {
		throw new IOException(e.getMessage());
	} catch (IOException e) {
		serialPort.close(); // Closing the port if it fail connecting
		throw e;
	}
}

/** Serial parameters */
private void setSerialPortParameters() throws IOException {
	int baudRate = 9600; 

	try {
		// Serial initialization
		serialPort.setSerialPortParams(baudRate,
				SerialPort.DATABITS_8, 
				SerialPort.STOPBITS_1, 
				SerialPort.PARITY_NONE); 

		serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
	} catch (UnsupportedCommOperationException ex) {
		throw new IOException("Unsupported parameters.");
	}
}

/** Disconnect serial port */
public void disconnect() {
	if (serialPort != null) {
		try {
			// i/o streams closing.
			outStream.close();
			inStream.close();
		} catch (IOException ex) {
		}
		// Closing the port.
		serialPort.close();
		System.out.println("Disconnected");
	}
}

/** Robot movement*/
public void move() throws IOException {
	String cmd = "#4 P1000\n";
	try {
		OutputStream outStream = serialPort.getOutputStream();
		outStream.write(cmd.getBytes());
		System.out.println("Bytes from cmd :" + cmd.getBytes());
		System.out.println("Movement done");
	} catch (IOException ex) {
		System.out.println("Mouvement non effectué");
	}
	
	
}

/** Pause*/
public static void pause(long ms) {
	try {
		Thread.sleep(ms);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
}

/** Main */
public static void main(String] args) {
	GestionnairePortSérie main = new GestionnairePortSérie();
	try {
		main.connect("COM5");
		pause(2000); // Time for the movement to be complete
		main.move();

	} catch (IOException ex) {
		System.err.println(ex.getMessage());
	}

	main.disconnect();

}

}
[/code]

I’ve found the solution ! It’s not \n but \r\n for

outStream.write("#4 P1000\r\n".getBytes());

I’m now working on a Tic Tac Toe java application, i’ll keep you updated.

That’s right, \r\n. Glad to hear everything’s working for you. Looking forward to seeing some updates and videos!

My project is almost finished. I’ve created a Java tic-tac-toe app and when you clic on a square, the robot takes the piece and put it at the right place. Then an AI choose a move, and the robot execute it. Unforunately, some “bugs” are still existing on our AI (sometimes, the player can win) but I’m working on it.

I’m posting a video when i’m done :slight_smile: