예제 #1
0
  public int getFastMeasurement() {
    sensor.ping();
    for (int i = 0; i < ECHOS; i++) m_iMeasured[i] = -1;
    sensor.getDistances(m_iMeasured);

    //		int count = ECHOS,
    //			sum = 0;
    //		for(int i = 0; i < ECHOS; i++)
    //			if(m_iMeasured[i] == -1)
    //				count--;
    //			else
    //				sum += m_iMeasured[i];
    //
    //		int avg = sum / count;
    //
    //		LCD.clear();
    //		LCD.drawString("measurements:", 0, 0);
    //		LCD.drawString(m_iMeasured[0] + " " + m_iMeasured[1] + " " + m_iMeasured[2] + " " +
    // m_iMeasured[3], 0, 1);
    //		LCD.drawString(m_iMeasured[4] + " " + m_iMeasured[5] + " " + m_iMeasured[6] + " " +
    // m_iMeasured[7], 0, 2);
    //		LCD.drawInt(avg, 0, 3);
    //		Button.waitForAnyPress();

    return m_iMeasured[0];
  }
예제 #2
0
  /**
   * Reads data from the ultrasonic sensor and filters it to prevent false 255 values
   *
   * @return The filtered data
   */
  private int getFilteredData() {
    int distance;

    // do a ping
    us.ping();

    // wait for the ping to complete
    try {
      Thread.sleep(50);
    } catch (InterruptedException e) {
    }

    // there will be a delay here
    distance = us.getDistance();

    if (minDistance > distance) {
      minDistance = distance;
    }

    if (distance > 55) {
      distance = 55;
    }

    if (distance > 255) {
      distance = 255;
    }

    return distance;
  }
 @Override
 public void run() {
   int[] is = new int[8];
   us.setMode(UltrasonicSensor.MODE_PING);
   while (true) {
     int i = us.ping();
     if (i < 0) {
       continue;
     }
     i = us.getDistances(is);
     if (i < 0) {
       continue;
     }
   }
 }
예제 #4
0
  public DistanceMeasure() {
    ultrasonicSensor = new UltrasonicSensor(SensorPort.S1);

    while (true) {
      System.out.println("Distance: " + ultrasonicSensor.getDistance());
    }
  }
예제 #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) {
      }
    }
  }
예제 #6
0
  /**
   * The default constructor
   *
   * @param odo The robot's odometer
   * @param us The robot's left ultrasonic sensor
   */
  public USLocalizer(Odometer odo, UltrasonicSensor us1, UltrasonicSensor us2, Transmission t) {
    this.odo = odo;
    this.us = us1;
    this.nav = new Navigation(odo, us1, us2, t);

    // switch off the ultrasonic sensor
    us.off();
  }
예제 #7
0
 /** The robot moves along the Wall and turns to the right, if the wall is too far away. */
 @Override
 public void action() {
   suppressed = false;
   Settings.motorAAngle = Settings.SENSOR_RIGHT;
   while (!suppressed
       && !((Settings.TOUCH_L.isPressed() || Settings.TOUCH_R.isPressed()))
       && Settings.LIGHT.getLightValue() < 80) {
     if (sonic.getDistance() > (distanceToWall + 10)) {
       pilot.arc(-15, -90, true);
     } else if (sonic.getDistance() <= distanceToWall) {
       pilot.arc(40, 20, true);
     } else if (sonic.getDistance() > distanceToWall) {
       pilot.arc(-60, -20, true);
     }
   }
   if (Settings.LIGHT.getLightValue() >= 80) {
     pilot.stop();
     System.out.println("LINE Detected");
     Settings.afterSlider = true;
     Settings.motorAAngle = Settings.SENSOR_FRONT;
     Settings.PILOT.setTravelSpeed(Settings.PILOT.getMaxTravelSpeed() * 0.15);
   }
 }
예제 #8
0
  public static void Sonarflag() {
    // execute all actions of this state
    LCD.drawString("Sonarflag", 0, 3);
    Sound.beep();
    btThread.write(sonar.getDistance()); // sends the data from the sonar

    // leg de huidige tijd vast voor alle transitions met een timeoutcondition
    long starttime = System.currentTimeMillis();

    // when done, wait for a trigger for a transition
    boolean transitionTaken = false;
    while (!transitionTaken) {
      if ((true)) {
        current = State.WATCH;
        transitionTaken = true;
      }
    }
  }
예제 #9
0
  public static void Watch() {
    // execute all actions of this state
    LCD.drawString("Watch", 0, 3);

    // leg de huidige tijd vast voor alle transitions met een timeoutcondition
    long starttime = System.currentTimeMillis();

    // when done, wait for a trigger for a transition
    boolean transitionTaken = false;
    while (!transitionTaken) {
      if ((sonar.getDistance() <= 10)) {
        current = State.SONARFLAG;
        transitionTaken = true;
      } else if ((lejos.robotics.Color.RED == colorsens.getColorID())
          || (lejos.robotics.Color.BLUE == colorsens.getColorID())
          || (lejos.robotics.Color.GREEN == colorsens.getColorID())) {
        current = State.COLORFLAG;
        transitionTaken = true;
      }
    }
  }
예제 #10
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);
  }
예제 #11
0
 public boolean takeControl() {
   int distance = sonar.getDistance();
   return (distance > floorDistance);
 }
예제 #12
0
 public void action() {
   LCD.clear();
   LCD.drawInt(sonar.getDistance(), 0, 0);
   LCD.refresh();
 }
예제 #13
0
 public UltrasonicControl(SensorPort port) {
   sensor = new UltrasonicSensor(port);
   sensor.ping();
 }