Line Follower Code



//

import josx.platform.rcx.Motor;
import josx.platform.rcx.Sensor;

/**
 * LineFollower.java
 * Program for RCX that simply follows a black line on a white board.
 * Robot must start in CENTER_ONLY, a state where the center sensor is the only one on the black line.
 *
 * @author CS371 2002
 * @version 1.0
 * @since 1.0
 */
public class LineFollower extends JavaRCX {
  public LineFollower (){
  }
    
  
  /**
   * Runs forever taking appropriate motor actions for each state it is in.
   * While loops within some statements attempt to fine tune the robot so it is back on the line in the CENTER_ONLY state.
   * @param args a String[] value, command line arguments, none in this case
   */
  public static void main(String[] args){
    initialize();
    while (true) {
      if(state == NONE)
	lost();
      else if(state == LEFT_ONLY){
	while(state != CENTER_ONLY){
	  Motor.C.backward();
	  Motor.A.backward();
	}
      }
      else if(state == CENTER_ONLY){
	Motor.A.forward();
	Motor.C.backward();
      }
      else if(state == RIGHT_ONLY){
	while(state != CENTER_ONLY){
	  Motor.A.forward();
	  Motor.C.forward();
	}
      }
      else if(state == LEFT_CENTER){
	//90 degree turn
	while(state != CENTER_ONLY){
	  Motor.A.backward();
	  Motor.C.backward();
	}
      }
      else if(state == LEFT_RIGHT){
	//give it a little bump forward, this way it does not jitter back and forth between the lost method and this state which will call lost again which will see that all sensors are on which will call lost again ....etc.
	Motor.A.forward();
	Motor.C.backward();
	lost();
      }
      else if(state == CENTER_RIGHT){
	//90 degree turn
	while(state != CENTER_ONLY){
	  Motor.A.forward();
	  Motor.C.forward();
	}
      }
      else if(state == ALL){
	//give it a little bump forward, this way it does not jitter back and forth between the lost method and this state which will call lost again which will see that all sensors are on which will call lost again ....etc.
	Motor.A.forward();
	Motor.C.forward();
	lost();
      }
    }
  }

    
}// LineFollower

//