Beispiel #1
0
  // a method for every state
  public static void Btinit() {
    // execute all actions of this state
    LCD.drawString("Btinit", 0, 3);
    LCD.drawString("Waiting...", 0, 0);
    LCD.refresh();

    BTConnection btc = Bluetooth.waitForConnection();

    LCD.clear();
    LCD.drawString("Connected", 0, 0);
    LCD.refresh();

    DataInputStream dis = btc.openDataInputStream();
    DataOutputStream dos = btc.openDataOutputStream();

    // BTfunctionality btThread = new BTfunctionality("SlaveReader",dis,dos);
    btThread = new BTfunctionality("SlaveReader", dis, dos);
    btThread.start();

    // 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;
      }
    }
  }
  public static void main(String[] args) {
    LCD.drawString(appName, 0, 0);
    LCD.drawString("#################", 0, 2);
    LCD.drawString("#################", 0, 6);

    msc = new MSC(SensorPort.S1);
    // Set to initial angle
    msc.servo1.setAngle(90);

    int angle = 0;
    int pulse = 0;
    int NXTServoBattery = 0;

    while (!Button.ESCAPE.isPressed()) {
      NXTServoBattery = msc.getBattery();

      if (Button.LEFT.isPressed()) {
        angle = 0;
        msc.servo1.setAngle(angle);
      }

      if (Button.ENTER.isPressed()) {
        angle = 90;
        msc.servo1.setAngle(angle);
      }

      if (Button.RIGHT.isPressed()) {
        angle = 180;
        msc.servo1.setAngle(angle);
      }

      clearRows();
      LCD.drawString("Battery: " + NXTServoBattery, 0, 3);
      LCD.drawString("Pulse:   " + msc.servo1.getPulse(), 0, 4);
      LCD.drawString("Angle:   " + msc.servo1.getAngle(), 0, 5);
      LCD.refresh();
    }

    // Set to initial angle
    msc.servo1.setAngle(90);

    LCD.drawString("Test finished", 0, 7);
    LCD.refresh();
    try {
      Thread.sleep(1000);
    } catch (Exception e) {
    }
    credits(3);
    System.exit(0);
  }
Beispiel #3
0
 public synchronized void run() {
   if ((line >= 0) && (line <= 7)) {
     LCD.drawString("                ", 0, getLine());
     LCD.drawString(getAString(), 0, getLine());
     LCD.refresh();
   }
 }
  public void start(iStopCondition stopCond, boolean forward) {
    final int power = 300;
    int sensorVal, powerDiff, whiteDiff = 0;

    pidController = new PIDController(sensor.blackWhiteThreshold, 0);
    pidController.setPIDParam(PIDController.PID_KP, 1.8f);
    pidController.setPIDParam(PIDController.PID_KD, 8f);

    // LCD.clear();
    LCD.drawString("Light: ", 0, 1);

    LCD.drawString("pDiff: ", 0, 2);

    while (!stopCond.stopLoop()) {
      // whiteDiff = (sensorRef.wasWhite() ?
      // sensorRef.getChachedLightValue() - whiteRef : whiteDiff);
      sensorVal = sensor.light() - whiteDiff / 2;

      powerDiff = pidController.doPID(sensorVal);
      if (forward) {
        pilot.forward(power + powerDiff, power - powerDiff);
      } else {
        pilot.backward(power - powerDiff, power + powerDiff);
      }

      LCD.drawInt(sensorVal, 4, 10, 1);
      LCD.drawInt(powerDiff, 4, 10, 2);
      LCD.refresh();

      Thread.yield();
    }
    pilot.stop();
    LCD.clear();
  }
Beispiel #5
0
 public static void main(String[] args) throws Exception {
   RCXTemperatureSensor temperature = new RCXTemperatureSensor(SensorPort.S1);
   boolean s = true;
   while (!Button.ESCAPE.isDown()) {
     LCD.clear();
     if (s) LCD.drawString("C " + temperature.getCelcius(), 0, 0);
     else LCD.drawString("F " + temperature.getFahrenheit(), 0, 0);
     s = !s;
     LCD.refresh();
     Thread.sleep(2000);
   }
 }
 /**
  * Final Message
  *
  * @param seconds
  */
 private static void credits(int seconds) {
   LCD.clear();
   LCD.drawString("LEGO Mindstorms", 0, 1);
   LCD.drawString("NXT Robots  ", 0, 2);
   LCD.drawString("run better with", 0, 3);
   LCD.drawString("Java leJOS", 0, 4);
   LCD.drawString("www.lejos.org", 0, 6);
   LCD.refresh();
   try {
     Thread.sleep(seconds * 1000);
   } catch (Exception e) {
   }
 }
Beispiel #7
0
  public static void main(String[] args) throws Exception {
    String connected = "Connected";
    String waiting = "Waiting...";
    String closing = "Closing...";

    while (true) {
      LCD.drawString(waiting, 0, 0);
      LCD.refresh();

      // BTConnection btc = Bluetooth.waitForConnection();
      BTConnection btc = Bluetooth.waitForConnection(0, NXTConnection.RAW);

      LCD.clear();
      LCD.drawString(connected, 0, 0);
      LCD.refresh();

      DataInputStream dis = btc.openDataInputStream();
      DataOutputStream dos = btc.openDataOutputStream();

      for (int i = 0; i < 100; i++) {
        int n = dis.readInt();
        LCD.drawInt(n, 7, 0, 1);
        LCD.refresh();
        dos.writeInt(-n);
        dos.flush();
      }

      dis.close();
      dos.close();
      Thread.sleep(100); // wait for data to drain
      LCD.clear();
      LCD.drawString(closing, 0, 0);
      LCD.refresh();
      btc.close();
      LCD.clear();
    }
  }
Beispiel #8
0
  public static void main(String[] args) throws Exception {
    ColorHTSensor cmps = new ColorHTSensor(SensorPort.S1);
    String color = "Color";
    String r = "R";
    String g = "G";
    String b = "B";

    String[] colorNames = {
      "Red",
      "Green",
      "Blue",
      "Yellow",
      "Magenta",
      "Orange",
      "White",
      "Black",
      "Pink",
      "Gray",
      "Light gray",
      "Dark Gray",
      "Cyan"
    };

    while (!Button.ESCAPE.isDown()) {
      LCD.clear();
      LCD.drawString(cmps.getVendorID(), 0, 0);
      LCD.drawString(cmps.getProductID(), 0, 1);
      LCD.drawString(cmps.getVersion(), 9, 1);
      LCD.drawString(color, 0, 3);
      LCD.drawInt(cmps.getColorID(), 7, 3);
      LCD.drawString(colorNames[cmps.getColorID()], 0, 4);
      LCD.drawString(r, 0, 5);
      LCD.drawInt(cmps.getRGBComponent(Color.RED), 1, 5);
      LCD.drawString(g, 5, 5);
      LCD.drawInt(cmps.getRGBComponent(Color.GREEN), 6, 5);
      LCD.drawString(b, 10, 5);
      LCD.drawInt(cmps.getRGBComponent(Color.BLUE), 11, 5);
      LCD.refresh();
      Thread.sleep(INTERVAL);
    }
  }
  public void run() {

    // infinite task
    while (true) {
      // retrieve decoded opcode info
      synchronized (this) {
        newDyn = DEObj.getNxtDyn();
        newCmd = DEObj.getNxtCmd();
        newPos = DEObj.getNxtPos();
        curPos = centreMotor.getTachoCount();
        DEObj.setCurSteerPos(curPos);
      }
      ;

      // steer to newPos, if conditions apply
      if ((newCmd != DataExchange.EXIT_CMD) && (newCmd != DataExchange.NULL_CMD)) {
        if (oldDyn != newDyn) {
          oldDyn = newDyn;

          switch (newCmd) {
            case DataExchange.STOP_CMD:
              isNxtStop = true;
              centreMotor.stop();
              break;
            case DataExchange.CAL_CMD:
              centreMotor.rotate(getDeltaPos(newPos, curPos));
              LCD.clear(6);
              LCD.drawString("CMD:: CALIBRATE", 0, 6);
              break;
            case DataExchange.START_CMD:
              isNxtStop = false;
              if (isNxtCalib) {
                // calibration is done, the current TachoCount
                // position becomes the reference, i.e. "0 deg".
                isNxtCalib = false;
                centreMotor.resetTachoCount();
                curPos = centreMotor.getTachoCount();
              }
              break;
            case DataExchange.RELEASE_CMD:
              // when user releases the mouse drag, the vehicle attempts to recover a straight
              // drive.
              // It simulates what happens naturally in a real car when the driver gets his hands
              // off
              // the steering upon exiting from a curve.
              centreMotor.rotateTo(0);
              break;
            case DataExchange.FRWD_CMD:
            case DataExchange.BKWD_CMD:
              if (!isNxtStop) {
                centreMotor.rotate(getDeltaPos(newPos, curPos));
              }
              break;
            default:
              break;
          }
          ; // switch case
        } // if (old != new)
        else {
          centreMotor.flt();
        }

        try {
          // thread going to sleep
          ManageNxtSteering.sleep(50);
        } catch (InterruptedException e) {
          e.printStackTrace();
          LCD.drawString("EX-6:", 0, 5);
          LCD.refresh();
        }

      } // if (new != EXIT_CMD)
      else {
        if (newCmd == DataExchange.EXIT_CMD) {
          centreMotor.stop(); /* stop motor */
          break; /* exit while loop */
        }
      }
    } // while loop

    DEObj.setNXTdone(true);
  }
Beispiel #10
0
 public void action() {
   LCD.clear();
   LCD.drawInt(sonar.getDistance(), 0, 0);
   LCD.refresh();
 }
 private static void drawMessage(String message) {
   LCD.clear();
   LCD.drawString(message, 0, 0);
   LCD.refresh();
 }