public static void main(String... args) throws InterruptedException {
    init();
    long lastVictimTime = 0;
    while (true) {
      RawColor color = colors.getSensorColor(robot.colorSensor);
      if ((color == colors.silver || color == colors.green)
          && System.currentTimeMillis() - lastVictimTime > 2500) {
        synchronized (robot.pilot) {
          robot.pilot.stop();
          robot.showVictimFound();
          lastVictimTime = System.currentTimeMillis();
          robot.pilot.travel(5);
          findline();
        }
      } else if (color == colors.white || color == colors.black) {
        synchronized (robot.pilot) {
          int[] sensors = robot.lineSensor.getSensors();
          if (sensors != null) {

            int leftspeed = calcLeftSpeed(sensors);
            int rightspeed = calcRightSpeed(sensors);
            robot.pilot.setLeftSpeed(leftspeed);
            robot.pilot.setRightSpeed(rightspeed);
          }
        }
      }
      Thread.sleep(50);
    }
  }
Example #2
0
  /** Main entry to the program. */
  public static void main(String[] args) {
    RemoteControl remote = new RemoteControl();

    RemoteDevice racecar = Bluetooth.getKnownDevice("Batmobile");

    if (racecar == null) {
      System.out.println("No Such device existed");
      System.exit(1);
    }
    BTConnection racecarConnection = Bluetooth.connect(racecar);

    if (racecarConnection == null) {
      System.out.println("Connection Failed");
      System.exit(1);
    } else {
      System.out.println("Connected to Racecar");
    }

    DataOutputStream dos = racecarConnection.openDataOutputStream();
    try {
      System.out.println("Sending controller signal");
      dos.writeInt(CONTROLLER_DEVICE);
      dos.flush();
      System.out.println("Sent Controller signal");
    } catch (Exception ex) {
      // Do nothing
    }
    remote.setRacecarConnection(racecarConnection);
    remote.setRacecarOutputStream(dos);

    // Waiting for flag to set us off here
    System.out.println("Waiting for Flag connection");
    NXTConnection flagConnection = Bluetooth.waitForConnection();
    System.out.println("Connected to flag");
    DataInputStream dis = flagConnection.openDataInputStream();
    try {
      int check = dis.readInt();
      if (check == FLAG_SIGNAL) {
        System.out.println("Recived flag signal");
        dos.writeInt(FLAG_SIGNAL);
        dos.flush();
        System.out.println("sent flag signal to racecar");
        dis.close();
        remote.run();
      } else {
        System.out.println("Did not recieve flag connection");
      }

    } catch (Exception e) {

    }
  }
Example #3
0
  public static void main(String[] args) {
    /*
     * Wait for startup button press
     * Button.ID_LEFT = BangBang Type
     * Button.ID_RIGHT = P Type
     */

    int option = 0;
    Printer.printMainMenu();
    while (option == 0)
      // option = Button.waitForPress();
      option = Button.waitForAnyPress();
    // depends on version

    // Setup controller objects
    BangBangController bangbang =
        new BangBangController(bandCenter, bandWidth, motorLow, motorHigh);
    PController p = new PController(bandCenter, bandWidth);

    // Setup ultrasonic sensor
    UltrasonicSensor usSensor = new UltrasonicSensor(usPort);

    // Setup Printer
    Printer printer = null;

    // Setup Ultrasonic Poller
    UltrasonicPoller usPoller = null;

    switch (option) {
      case Button.ID_LEFT:
        usPoller = new UltrasonicPoller(usSensor, bangbang);
        printer = new Printer(option, bangbang);
        break;
      case Button.ID_RIGHT:
        usPoller = new UltrasonicPoller(usSensor, p);
        printer = new Printer(option, p);
        break;
      default:
        System.out.println("Error - invalid button");
        System.exit(-1);
        break;
    }

    usPoller.start();
    printer.start();

    // Wait for another button press to exit
    // Button.waitForPress();
    Button.waitForAnyPress();
    // depends on version
    System.exit(0);
  }
  static void init() throws InterruptedException {
    Sound.beep();
    try {
      File f = new File("CalibrationData.dat");

      FileInputStream is = new FileInputStream(f);
      colors = RescueColors.readObject(is);
      is.close();
      LCD.drawString("File read", 0, 0);
      LCD.drawString("sucessfully", 0, 0);
    } catch (IOException e) {
      LCD.drawString("Error reading", 0, 0);
      LCD.drawString("file", 0, 1);
      Thread.sleep(1000);
      System.exit(-1);
    }
    colors.printToLCD();
    robot = new RescueRobot(colors);
    Button.ENTER.waitForPressAndRelease();

    logger = new MovementLogger(robot);
    Thread t = new Thread(logger);
    t.setDaemon(true);
    t.start();
  }
  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);
  }