001/* This source code is released under the new BSD license, a copy of the 002 * license is in the distribution directory. */ 003 004package mazerob.nxt; 005 006import java.io.DataInputStream; 007import java.io.DataOutputStream; 008import java.io.IOException; 009import java.lang.RuntimeException; 010import lejos.nxt.Button; 011import lejos.nxt.comm.BTConnection; 012import lejos.nxt.comm.Bluetooth; 013import mazerob.conn.CommandCode; 014 015/** 016 * Main NXT application 017 * 018 * @author Pedro I. López 019 * 020 */ 021public class RobotApp { 022 /** 023 * Manages the {@link mazerob.nxt.Robot} remote method invocation 024 * 025 * <p> 026 * <ol> 027 * <li>Stablishes a Bluetooth connection with the PC (see {@link 028 * mazerob.pc.Robot})</li> 029 * <li>Gets a {@link mazerob.nxt.Robot} instance with configuration 030 * received from the PC (see {@link mazerob.pc.Robot})</li> 031 * <li>Enters the main loop where it waits for {@link 032 * mazerob.pc.Robot} to invoke methods from {@link 033 * mazerob.nxt.Robot} </li> 034 * <li>When {@link mazerob.nxt.Robot#scan} is invoked, it sends the {@link 035 * lejos.robotics.RangeReadings} object through the Bluetooth link to the 036 * {@link mazerob.pc.Robot} instance 037 * <li>When {@link mazerob.nxt.Robot#end} is invoked, it closes the 038 * Bluetooth connection and wait for program termination (user must 039 * press red button on NXT)</li> 040 * </ol> 041 * </p> 042 * 043 */ 044 public static void main(String [] args) throws Exception { 045 final String EXIT_MSG = "Press red button to end program"; 046 final String WAITING_MSG = "Waiting for connection..."; 047 final String CONNECTED_MSG = "Connected"; 048 final int WAIT_DRAIN_TIME = 100; 049 DataInputStream dis; 050 DataOutputStream dos; 051 BTConnection btc; 052 CommandCode commandCode; 053 CommandCode[] commandCodeValues = CommandCode.values(); 054 double distance, angle; 055 Robot robot; 056 057 System.out.println(WAITING_MSG); 058 btc = Bluetooth.waitForConnection(); 059 System.out.println(CONNECTED_MSG); 060 dis = btc.openDataInputStream(); 061 dos = btc.openDataOutputStream(); 062 063 robot = new Robot(dis.readDouble(), 064 dis.readDouble(), 065 dis.readBoolean(), 066 dis.readDouble(), 067 dis.readDouble(), 068 dis.readDouble()); 069 070 try { 071 while(true) { 072 commandCode = commandCodeValues[dis.readInt()]; 073 074 switch(commandCode) { 075 case TRANSLATE: 076 distance = dis.readDouble(); 077 robot.translate(distance); 078 break; 079 case TRANSLATE_FORWARD: 080 robot.translateForward(); 081 break; 082 case TRANSLATE_BACKWARD: 083 robot.translateBackward(); 084 break; 085 case ROTATE: 086 angle = dis.readDouble(); 087 robot.rotate(angle); 088 break; 089 case ROTATE_RIGHT: 090 robot.rotateRight(); 091 break; 092 case ROTATE_LEFT: 093 robot.rotateLeft(); 094 break; 095 case SCAN: 096 robot.scan().dumpObject(dos); 097 dos.flush(); 098 break; 099 case END: 100 default: 101 robot.end(); 102 } 103 104 } 105 } 106 catch(IOException e) { 107 System.out.println(e); 108 } 109 catch(RuntimeException e) { 110 System.out.println(EXIT_MSG); 111 } 112 finally { 113 dis.close(); 114 dos.close(); 115 Thread.sleep(WAIT_DRAIN_TIME); // wait for data to drain 116 btc.close(); 117 while (true) if (Button.ENTER.isDown()) break; 118 } 119 120 } 121}