示例#1
0
  public static void main(String[] args) throws Exception {
    new RaceMain();

    // Race
    Settings.PILOT.setTravelSpeed(Settings.PILOT.getMaxTravelSpeed() * 0.70);
    Settings.PILOT.setRotateSpeed(Settings.PILOT.getMaxRotateSpeed() / 4);
    Motor.A.setSpeed(Motor.A.getMaxSpeed() / 5);
    new SensorHeadCalibrate();
    Settings.motorAAngle = Settings.SENSOR_RIGHT;
    Settings.atStart = false;

    new LightCalibrate();

    Behavior b1 = new RaceFollowWall(13);
    Behavior b2 = new Race();
    Behavior b4 = new ReadCodes();
    Behavior b3 = new RaceDrive();
    Behavior b5 = new SensorHeadPosition();
    Behavior b6 = new MotorAStall();
    Behavior[] bArray = {b1, b2, b4, b3, b5, b6};

    CustomArbitrator arbitrator = new CustomArbitrator(bArray);
    Thread t = new Thread(arbitrator);
    t.start();
  }
 /*
  * This method makes the robot drive FRONT or BACK depending on the direction it is given.
  */
 public void drive(Direction direction) {
   if (direction == Direction.FRONT) {
     Motor.A.forward();
     Motor.B.forward();
   } else if (direction == Direction.BACK) {
     Motor.A.backward();
     Motor.B.backward();
   }
 }
示例#3
0
  /**
   * Stops the robot when the ultrasonic sensor detects a certain distance.
   *
   * @param stopDistance as long as the current distance is less than this stop distance, the robot
   *     will keep moving. Once the current distance is larger than the stop distance, the function
   *     returns.
   * @return the current distance upon stopping.
   */
  private boolean stopBeforeEdge() {
    int currentDistance = 0;
    int lightLevelReading;
    int distanceToObject;
    boolean edgeIsSafe = false;

    do {
      lightLevelReading = lightSensorDown.readValue();

      Motor.A.rotate(centerToEdgeDistance, true);
      Motor.C.rotate(centerToEdgeDistance, true);
    } while ((lightLevelReading > BLACK_WHITE_THRESHOLD)
        && (Motor.A.isMoving() && Motor.C.isMoving()));

    pilot.stop();

    lightLevelReading = lightSensorDown.readValue();

    /*
    distanceToObject = ultrasonicSensor.getDistance();

    if(distanceToObject < 20)
    {
    	edgeIsSafe = true;
    	Motor.A.setSpeed(1000);
    	Motor.C.setSpeed(1000);
    	pilot.setRotateSpeed(1000);
    	pilot.travel(280);
    	Delay.msDelay(10000);
    	return(edgeIsSafe);
    }

    if(lightLevelReading < BLACK_WHITE_THRESHOLD)
    {
    	System.out.println("Unsafe edge");
    	edgeIsSafe = false;
    	return(edgeIsSafe);
    }
    else if (lightLevelReading > BLACK_WHITE_THRESHOLD)
    {
    	System.out.println("Safe edge");
    	edgeIsSafe = true;
    	safeEdgeCount++;
    	return(edgeIsSafe);
    }

    */

    return (edgeIsSafe);
  }
示例#4
0
  /** Run. */
  public void run() {
    Motor.A.setSpeed(90);
    Motor.C.setSpeed(90);

    while (true) {
      moveForward();

      waitForReading();
      Motor.A.backward();
      Motor.C.backward();
      Delay.msDelay(1000);
      rotation(TURN_90_DEGREES, TURN_LEFT);
    }
  }
  /**
   * Main Entry Point. Creates the behaviors and starts the Arbitrator.
   *
   * @param args
   */
  public static void main(String[] args) {
    // Wall sensing behavior
    Behavior b1 = new WallBehavior(SensorPort.S3);
    // Move forward behavior
    Behavior b2 = new ForwardBehavior();
    // Left Light sensor sensing behavior
    Behavior b3 = new LeftLightBehavior(SensorPort.S2, SensorPort.S1);
    // Right Light sensor sensing behavior
    Behavior b4 = new RightLightBehavior(SensorPort.S2, SensorPort.S1);
    // Both Light sensors sensing behavior
    Behavior b5 = new BothLightsBehavior(SensorPort.S2, SensorPort.S1);
    // Win Behavior
    Behavior b6 = new WinBehavior(SensorPort.S2, SensorPort.S1);

    // Put the behaviors into the array based on priority
    Behavior[] behaviorArray = {b2, b1, b3, b4, b5, b6};

    // Create the arbitrator
    Arbitrator arby = new Arbitrator(behaviorArray);

    // Set the motor speeds
    Motor.A.setSpeed(800);
    Motor.C.setSpeed(800);

    // Start
    arby.start();
  }
  /*
   * This method makes the robot turn 90 deg to the given direction
   */
  public void turnNXT(Direction direction) {
    if (direction == Direction.RIGHT) {
      Motor.A.rotate(237);

    } else if (direction == Direction.LEFT) {
      Motor.B.rotate(237);
    }
  }
示例#7
0
 public static void main(String[] arguments) {
   Motor.A.setSpeed(200);
   Motor.B.setSpeed(200);
   Motor.A.forward();
   Motor.B.forward();
   final LightSensor sensor = new LightSensor(SensorPort.S1);
   final Object blocker = new Object();
   while (true) {
     if (sensor.readNormalizedValue() >= 512) {
       LCD.bitBlt(
           null,
           LCD.SCREEN_WIDTH,
           LCD.SCREEN_HEIGHT,
           0,
           0,
           0,
           0,
           LCD.SCREEN_WIDTH,
           LCD.SCREEN_HEIGHT,
           LCD.ROP_CLEAR);
     } else {
       LCD.bitBlt(
           null,
           LCD.SCREEN_WIDTH,
           LCD.SCREEN_HEIGHT,
           0,
           0,
           0,
           0,
           LCD.SCREEN_WIDTH,
           LCD.SCREEN_HEIGHT,
           LCD.ROP_SET);
     }
     synchronized (blocker) {
       try {
         blocker.wait(100);
       } catch (InterruptedException e) {
       }
     }
   }
 }
示例#8
0
  public static void main(String[] args) throws InterruptedException {
    LightSensor left = new LightSensor(SensorPort.S3);
    LightSensor right = new LightSensor(SensorPort.S4);

    while (true) {
      System.out.println("left: " + left.getLightValue());
      System.out.println("right: " + right.getLightValue());

      if (left.getLightValue() < 45 && right.getLightValue() < 45) {
        Motor.A.stop();

        Thread.sleep(1245);

      } else if (left.getLightValue() < 45) {
        Motor.B.stop();
      } else if (right.getLightValue() < 45) {
        Motor.A.stop();
      } else {
        Motor.A.backward();
        Motor.B.backward();
      }
    }
  }
示例#9
0
  private int waitForReading() {
    int lightLevelReading;
    int distanceToObject;

    do {
      try {
        Thread.sleep(100);
      } catch (InterruptedException ex) {
      }

      lightLevelReading = lightSensorDown.readValue();
    } while (lightLevelReading > BLACK_WHITE_THRESHOLD);

    distanceToObject = ultrasonicSensor.getDistance();

    if (distanceToObject < 20) {
      Motor.A.setSpeed(1000);
      Motor.C.setSpeed(1000);
      pilot.travel(300);
      Delay.msDelay(10000);
    }

    return (lightLevelReading);
  }
  // Decode msg method
  public void decodeMSG(Byte msg) throws IOException {
    // Motor Movement Functions
    //        int currentSpeedA = Motor.A.getSpeed();
    //        int currentSpeedB = Motor.B.getSpeed();

    if (msg == MOVE_FOWARD) {
      Motor.A.backward();
      Motor.B.backward();
    } else if (msg == MOVE_FOWARD_LEFT) {
      Motor.A.backward();
      Motor.B.setSpeed(motorSpeed / 2);
      Motor.B.backward();

    } else if (msg == MOVE_FOWARD_RIGHT) {
      Motor.A.setSpeed(motorSpeed / 2);
      Motor.A.backward();
      Motor.B.backward();

    } else if (msg == ROTATE_LEFT) {
      Motor.A.backward();
      Motor.B.forward();
    } else if (msg == ROTATE_RIGHT) {
      Motor.A.forward();
      Motor.B.backward();
    } else if (msg == MOVE_BACKWARD) {
      Motor.A.forward();
      Motor.B.forward();
    } else if (msg == STOP) {
      Motor.A.setSpeed(motorSpeed);
      Motor.B.setSpeed(motorSpeed);
      Motor.A.stop();
      Motor.B.stop();
    }

    // Motor set Speed Function
    if (msg == SET_MOTOR_SPD) {

      Byte rMsg = 40;

      rMsg = dis.readByte();

      int spd = 9 * rMsg;
      motorSpeed = spd;

      Motor.A.setSpeed(spd);
      Motor.B.setSpeed(spd);
    }

    if (msg == CUSTOM_MOVE) {
      int motorB = dis.read();
      int motorA = dis.read();
      Motor.A.setSpeed(motorA * 9);
      Motor.B.setSpeed(motorB * 9);
      Motor.A.backward();
      Motor.B.backward();
    }

    // Connection close msg
    if (msg == CLOSE_COMM) {
      closeBTConnection();
    }
  }
示例#11
0
 private void randomTurn() {
   int rnd = (int) Math.round(Math.random() * (double) 4);
   Motor.A.rotate(turnValue[rnd]);
 }
示例#12
0
 /*
  * This method makes the robot stop.
  */
 public void stop() {
   Motor.A.stop();
   Motor.B.stop();
 }
示例#13
0
 private void moveForward() {
   Motor.A.forward();
   Motor.C.forward();
 }