001 public class NodeToHemisson {
002 public static String TURN_RIGHT = "3,227"; //turns 90 degrees to the right, with offset
003 public static String TURN_LEFT = "4,227"; //turns 90 degrees to the left, with offset
004 public static String RESET = "5"; //reset after encountering line
005 public static String MOVE_FORWARD = "6"; //moves 1 sec/11.5 cm forwards
006 public static String MOVE_BACKWARD = "7"; //moves 1 sec/11.5 cm backwards
007 public static String LINE_TRESHOLD = "8,F"; //set black line treshold
008 public static String MOVE_HALF_BACKWARD = "9,255"; //move specified time back
009 public static String MOVE_HALF_BACKWARD_OFFSET = "9,200"; //move specified time back
010
011 /**
012 * Actually moves the Hemisson from one node to another
013 * @return the orientation after the move
014 * @param nStart The start Node
015 * @param nGoal The Goal Node
016 * @param nOrientation The initial orientation
017 */
018 public static int run(Node nStart, Node nGoal, int iOrientation){
019 int iDirection = 0;
020 if(nGoal.getY() > nStart.getY()){iDirection = 0;}
021 if(nGoal.getX() > nStart.getX()){iDirection = 1;}
022 if(nGoal.getY() < nStart.getY()){iDirection = 2;}
023 if(nGoal.getX() < nStart.getX()){iDirection = 3;}
024
025 if((iOrientation % 2) != (iDirection % 2)){
026 if(((iDirection - iOrientation) == -3) || ((iDirection - iOrientation) == 1)){
027 moveHemisson(TURN_RIGHT);
028 } else {
029 moveHemisson(TURN_LEFT);
030 }
031 iOrientation = iDirection;
032 }
033
034 if(iOrientation == iDirection){
035 moveHemisson(MOVE_FORWARD);
036 }else{
037 moveHemisson(MOVE_BACKWARD);
038 }
039 return iOrientation;
040 }
041 /**
042 * moveHemisson send the String commands to the class Hemisson and waits.
043 * @return true if a white line was encountered by the Hemisson Robot
044 * @param sMove a string with a move
045 */
046 public static boolean moveHemisson(String sMove){
047 int iSleep;
048
049 if(sMove == TURN_RIGHT || sMove == TURN_LEFT || sMove == MOVE_HALF_BACKWARD || sMove == MOVE_HALF_BACKWARD_OFFSET){
050 iSleep = Integer.parseInt(sMove.substring(2,5));
051 Hemisson.write(sMove.substring(0,1) + "," + Integer.toHexString(iSleep).toUpperCase());
052 } else if(sMove == MOVE_FORWARD || sMove == MOVE_BACKWARD){
053 iSleep = 1000;
054 Hemisson.write(sMove + ",FF");
055 } else {
056 iSleep = 0;
057 Hemisson.write(sMove);
058 }
059
060 String input ="";
061
062 long lStart = System.currentTimeMillis();
063 while((System.currentTimeMillis() - lStart) < iSleep/10){
064 input += Hemisson.read();
065 }
066 if(input.indexOf('F') != -1){
067 System.out.println("Encountered line");
068 return true;
069 } else {
070 System.out.println("Free");
071 return false;
072 }
073
074 }
075 }