import java.util.*; import java.util.List; public class tohemisson extends Vector { public static String TURN_RIGHT = "3,227"; //turns 90 degrees to the right, with offset public static String TURN_LEFT = "4,227"; //turns 90 degrees to the left, with offset public static String RESET = "5"; //reset after encountering line public static String MOVE_FORWARD = "6"; //moves 1 sec/11.5 cm forwards public static String MOVE_BACKWARD = "7"; //moves 1 sec/11.5 cm backwards public static String LINE_TRESHOLD = "8,F"; //set black line treshold public static String MOVE_HALF_BACKWARD = "9,255"; //move specified time back public static String MOVE_HALF_BACKWARD_OFFSET = "9,200"; //move specified time back /** * Actually moves the Hemisson from one node to another * @return the orientation after the move * @param nStart The start Node * @param nGoal The Goal Node * @param nOrientation The initial orientation */ public static int run(AStarNode nStart, AStarNode nGoal, int iOrientation){ int iDirection = 0; if(nGoal.y > nStart.y){iDirection = 0;} if(nGoal.x > nStart.x){iDirection = 1;} if(nGoal.y < nStart.y){iDirection = 2;} if(nGoal.x < nStart.x){iDirection = 3;} if((iOrientation % 2) != (iDirection % 2)){ if(((iDirection - iOrientation) == -3) || ((iDirection - iOrientation) == 1)){ moveHemisson(TURN_RIGHT); } else { moveHemisson(TURN_LEFT); } iOrientation = iDirection; } if(iOrientation == iDirection){ moveHemisson(MOVE_FORWARD); }else{ moveHemisson(MOVE_BACKWARD); } return iOrientation; } /** * moveHemisson send the String commands to the class Hemisson and waits. * @return true if a white line was encountered by the Hemisson Robot * @param sMove a string with a move */ public static boolean moveHemisson(String sMove){ int iSleep; if(sMove == TURN_RIGHT || sMove == TURN_LEFT || sMove == MOVE_HALF_BACKWARD || sMove == MOVE_HALF_BACKWARD_OFFSET){ iSleep = Integer.parseInt(sMove.substring(2,5)); Hemisson.write(sMove.substring(0,1) + "," + Integer.toHexString(iSleep).toUpperCase()); } else if(sMove == MOVE_FORWARD || sMove == MOVE_BACKWARD){ iSleep = 1000; Hemisson.write(sMove + ",FF"); } else { iSleep = 0; Hemisson.write(sMove); } String input =""; long lStart = System.currentTimeMillis(); while((System.currentTimeMillis() - lStart) < iSleep/10){ input += Hemisson.read(); } if(input.indexOf('F') != -1){ System.out.println("Encountered line"); return true; } else { System.out.println("Free"); return false; } } }