108.

previous chapter table of contents next chapter
  

RCX Programs

At the lowest level, the RCX is controlled by machine-code programs sent via the infrared link. It will respond to these programs by stopping and starting motors, changing speed, and so on. As it completes commands or receives information from sensors, it can send replies back to the host computer. The RCX can handle instructions sent directly or have a program downloaded into firmware and run from there.

Kekoa Proudfoot has produced a list of the opcodes understood by the RCX, and it is available at http://graphics. stanford .edu/~kekoa/rcx/ . Using these and the rcx package from Dario Laverde, we can control the RCX from a computer by standalone programs such as this:

 /**  * TestRCX.java  */ package standalone; 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 
  


A Programmer[ap]s Guide to Jini Technology
A Programmer[ap]s Guide to Jini Technology
ISBN: 1893115801
EAN: N/A
Year: 2000
Pages: 189

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net