Instruction |
Bytecode |
PING
|
0x10
|
SETMOTORPOWER
|
0x13
|
CALLSUB
|
0x17
|
PLAYSOUND
|
0x51
|
SETMOTOR
|
0x21
|
Mindstorms programs
-
Programs can be downloaded to the Mindstorms using the IR link
-
Single instructions can also be downloaded and run immediately,
again by the IR link
Mindstorms proxy
A proxy for the Mindstorms robot is built using the following components
-
Native code serial port DLL
-
Java CommAPI
-
Proxy on top of this
RCXPort
Dario Laverde has defined a proxy class
package rcx;
public class RCXPort {
public RCXPort(String port);
public void addRCXListener(RCXListener rl);
public boolean open();
public void close();
public boolean isOpen();
public OutputStream getOutputStream();
public InputStream getInputStream();
public synchronized boolean write(byte[] bArray);
public String getLastError();
}
Example use of RCXPort
import rcx.*;
public class TestRCX implements RCXListener {
static final String PORT_NAME = "/dev/ttyS0"; // Linux
public TestRCX() {
RCXPort port = new RCXPort(PORT_NAME);
port.addRCXListener(this);
byte[] byteArray;
// send ping message, reply should be e7 or ef
byteArray = RCXOpcode.parseString("10"); // Alive
port.write(byteArray);
// beep twice
byteArray = RCXOpcode.parseString("51 01"); // Play sound
port.write(byteArray);
// turn motor A on (forwards)
byteArray = RCXOpcode.parseString("e1 81"); // Set motor direction
port.write(byteArray);
byteArray = RCXOpcode.parseString("21 81"); // Set motor on
port.write(byteArray);
try {
Thread.currentThread().sleep(1000);
} catch(Exception e) {
}
// turn motor A off
byteArray = RCXOpcode.parseString("21 41"); // Set motor off
port.write(byteArray);
// turn motor A on (backwards)
byteArray = RCXOpcode.parseString("e1 41"); // Set motor direction
port.write(byteArray);
byteArray = RCXOpcode.parseString("21 81"); // Set motor on
port.write(byteArray);
try {
Thread.currentThread().sleep(1000);
} catch(Exception e) {
}
// turn motor A off
byteArray = RCXOpcode.parseString("21 41"); // Set motor off
port.write(byteArray);
}
/**
* listener method for messages from the RCX
*/
public void receivedMessage(byte[] message) {
if (message == null) {
return;
}
StringBuffer sbuffer = new StringBuffer();
for(int n = 0; n < message.length; n++) {
int newbyte = (int) message[n];
if (newbyte < 0) {
newbyte += 256;
}
sbuffer.append(Integer.toHexString(newbyte) + " ");
}
System.out.println("response: " + sbuffer.toString());
}
/**
* listener method for error messages from the RCX
*/
public void receivedError(String error) {
System.err.println("Error: " + error);
}
public static void main(String[] args) {
new TestRCX();
}
} // TestRCX
Jan Newmarch (http://jan.newmarch.name)
jan@newmarch.name
Last modified: Mon Mar 31 04:29:33 EST 2003
Copyright ©Jan Newmarch
Copyright © Jan Newmarch, Monash University, 2007
This work is licensed under a
Creative Commons License
The moral right of Jan Newmarch to be identified as the author of this page has been asserted.