Example #1
0
 /**
  * Front is clear method
  *
  * @return True if front is clear
  */
 private boolean frontIsClear() {
   //
   // System.out.print("Front Light Value: ");
   System.out.print("F: ");
   System.out.println(myFrontSensor.getLightValue());
   return myFrontSensor.getLightValue() > lightThreshold;
 }
  public static void main(String[] aArg) throws Exception {
    String left = "Turn left ";
    String right = "Turn right";

    LightSensor light = new LightSensor(SensorPort.S3);
    final int blackWhiteThreshold = 45;

    DataLogger dl = new DataLogger("Memory10MS_C.txt");
    int lightValue;

    Runtime rt = Runtime.getRuntime();

    final int forward = 1;
    final int stop = 3;
    final int flt = 4;
    final int power = 80;

    // Use the light sensor as a reflection sensor
    light.setFloodlight(true);
    LCD.drawString("Light %: ", 0, 0);

    // Show light percent until LEFT is pressed
    LCD.drawString("Press LEFT", 0, 2);
    LCD.drawString("to start", 0, 3);
    while (!Button.LEFT.isDown()) {
      LCD.drawInt(light.readValue(), 3, 9, 0);
    }

    // Follow line until ESCAPE is pressed
    LCD.drawString("Press ESCAPE", 0, 2);
    LCD.drawString("to stop ", 0, 3);

    dl.start();
    while (!Button.ESCAPE.isDown()) {

      lightValue = light.readValue();
      if (lightValue > blackWhiteThreshold) {
        // On white, turn right
        LCD.drawString(right, 0, 1);
        MotorPort.B.controlMotor(0, stop);
        MotorPort.C.controlMotor(power, forward);
      } else {
        // On black, turn left
        LCD.drawString(left, 0, 1);
        MotorPort.B.controlMotor(power, forward);
        MotorPort.C.controlMotor(0, stop);
      }
      LCD.drawInt(lightValue, 3, 9, 0);
      dl.writeSample(rt.freeMemory());
      Thread.sleep(10);
    }

    // Stop car gently with free wheel drive
    MotorPort.B.controlMotor(0, flt);
    MotorPort.C.controlMotor(0, flt);
    LCD.clear();
    dl.close();
    LCD.drawString("Program stopped", 0, 0);
    Thread.sleep(1000);
  }
Example #3
0
 /**
  * Right is clear method
  *
  * @return True if right is clear
  */
 private boolean rightIsClear() {
   //
   // System.out.print("Right Light Value: ");
   System.out.print("R: ");
   System.out.println(myRightSensor.getLightValue());
   return myRightSensor.getLightValue() > lightThreshold;
 }
Example #4
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);
  }
Example #5
0
  public static void main(String[] args) {
    LightSensor light = new LightSensor(SensorPort.S1);
    SoundSensor sound = new SoundSensor(SensorPort.S2);
    TouchSensor touch = new TouchSensor(SensorPort.S3);
    UltrasonicSensor sonic = new UltrasonicSensor(SensorPort.S4);

    while (sound.readValue() < 90) {
      System.out.println("light = " + light.readValue());
      System.out.println("sound = " + sound.readValue());
      System.out.println("touch = " + touch.isPressed());
      System.out.println("distance = " + sonic.getDistance());
      try {
        Thread.sleep(1000);
      } catch (InterruptedException ie) {
      }
    }
  }
Example #6
0
  /**
   * Calibrates the bright light level
   *
   * @return the calibrated light value
   */
  public int clibrateDark() {

    int value;

    Logger.getInstance().system("Place light sensor on a dark area then press enter");
    Button.ENTER.waitForPressAndRelease();
    value = light.readValue();

    return value;
  }
Example #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) {
       }
     }
   }
 }
Example #8
0
  /** Calibration utility method, not sure if we need it */
  private void doCalibration() {
    // Prep sensor
    System.out.println("Port 1: Front Sensor");
    System.out.println("Port 2: Right Sensor");
    System.out.println("Port 3: Compass\n");

    // Calibrate front
    // System.out.println("Calibrate High Front");
    // Button.waitForPress();
    // myFrontSensor.calibrateHigh();
    System.out.print("Value: ");
    System.out.println(myFrontSensor.getLightValue());

    // Calibrate right
    // System.out.println("Calibrate High Right");
    // Button.waitForPress();
    // myRightSensor.calibrateHigh();
    System.out.print("Value: ");
    System.out.println(myRightSensor.getLightValue());
    Button.waitForPress();
  }
 /**
  * If an abyss is detected, the robot will rotate 10 degrees, until the light sensor sees normal
  * ground again.
  */
 @Override
 public void action() {
   System.out.println("Abys");
   suppressed = false;
   while (lightSensor.getLightValue() < threshold && !suppressed) {
     pilot.rotate(10, true);
     if (!pilot.isMoving()) {
       Settings.whipAndBridgeCounter++;
     }
     Delay.msDelay(10);
   }
   while (pilot.isMoving() && !suppressed) {
     Thread.yield();
   }
   pilot.stop();
 }
Example #10
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();
      }
    }
  }
Example #11
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);
  }
Example #12
0
 /**
  * Checks for a bright object in front to indicate that we are at the target
  *
  * @return True if we are at target
  */
 private boolean atTarget() {
   return myFrontSensor.getLightValue() > endThreshold;
 }
Example #13
0
  public static void main(String[] args) throws Exception {
    RemoteNXT nxt = null;
    int power = 0;
    int mode = 1;
    int motor = 0;
    String motorString = "Motor:";
    String modeString = "Mode:";
    String powerString = "Power:";
    String batteryString = "Battery:";
    String lightString = "Light:";
    String tachoString = "Tacho:";

    // Get the type of communications to be used
    String[] connectionStrings = new String[] {"Bluetooth", "RS485"};
    TextMenu connectionMenu = new TextMenu(connectionStrings, 1, "Connection");
    NXTCommConnector[] connectors = {Bluetooth.getConnector(), RS485.getConnector()};

    int connectionType = connectionMenu.select();

    // Now connect
    try {
      LCD.clear();
      LCD.drawString("Connecting...", 0, 0);
      nxt = new RemoteNXT("NXT", connectors[connectionType]);
      LCD.clear();
      LCD.drawString("Type: " + connectionStrings[connectionType], 0, 0);
      LCD.drawString("Connected", 0, 1);
      Thread.sleep(2000);
    } catch (IOException ioe) {
      LCD.clear();
      LCD.drawString("Conn Failed", 0, 0);
      Thread.sleep(2000);
      System.exit(1);
    }

    LCD.clear();
    RemoteMotor[] motors = {nxt.A, nxt.B, nxt.C};
    LightSensor light = new LightSensor(nxt.S2);
    while (true) {
      // Get data from the remote NXT and display it
      LCD.drawString(motorString, 0, 0);
      LCD.drawInt(motor, 3, 10, 0);
      LCD.drawString(powerString, 0, 1);
      LCD.drawInt(power, 3, 10, 1);
      LCD.drawString(modeString, 0, 2);
      LCD.drawInt(mode, 3, 10, 2);
      LCD.drawString(tachoString, 0, 3);
      LCD.drawInt(motors[motor].getTachoCount(), 6, 7, 3);
      LCD.drawString(batteryString, 0, 4);
      LCD.drawInt(nxt.Battery.getVoltageMilliVolt(), 6, 7, 4);
      LCD.drawString(lightString, 0, 5);
      LCD.drawInt(light.readValue(), 6, 7, 5);
      LCD.drawString(nxt.getBrickName(), 0, 6);
      LCD.drawString(nxt.getFirmwareVersion(), 0, 7);
      LCD.drawString(nxt.getProtocolVersion(), 4, 7);
      LCD.drawInt(nxt.getFlashMemory(), 6, 8, 7);

      // Do we have a button press?
      int key = Button.readButtons();
      if (key != 0) {
        // New command, work out what to do.
        if (key == 1) { // ENTER
          power += 20;
          if (power > 100) power = 0;
        } else if (key == 2) { // LEFT
          mode++;
          if (mode > 4) mode = 1;
        } else if (key == 4) { // RIGHT
          motor++;
          if (motor > 2) motor = 0;
        } else if (key == 8) { // ESCAPE
          LCD.clear();
          LCD.drawString("Closing...", 0, 0);
          for (int i = 0; i < motors.length; i++) motors[i].flt();
          nxt.close();
          Thread.sleep(2000);
          System.exit(0);
        }

        LCD.clear();
        LCD.drawString("Setting power", 0, 0);
        motors[motor].setPower(power);
        LCD.drawString("Moving motor", 0, 1);
        if (mode == 1) motors[motor].forward();
        else if (mode == 2) motors[motor].backward();
        else if (mode == 3) motors[motor].flt();
        else if (mode == 4) motors[motor].stop();
        // Wait for the button to be released...
        while (Button.readButtons() != 0) Thread.yield();
        LCD.clear();
      }
    }
  }
Example #14
0
 /**
  * This behavior will take control if the current normalized light value is lower than a given
  * threshold.
  */
 @Override
 public boolean takeControl() {
   return (!Settings.beforeWhip && !Settings.afterWhip && lightSensor.getLightValue() < threshold);
 }
Example #15
0
 public static void getValue() {
   l = sens2.getLightValue();
   r = sens.getLightValue();
 }