Example #1
0
 @Override
 public void mouseReleased(MouseEvent arg0) {
   if (!cmdStartStop)
     synchronized (this) {
       DE.incNxtDyn();
       DE.setNxtCmd(DataExchange.RELEASE_CMD);
     }
 }
Example #2
0
  /* BlueKap main application */
  public static void main(String[] args) throws InterruptedException {

    DE = new DataExchange();
    PC_BTS = new BtConnSend(DE);
    frame = null;
    lbBkg = lbKAPP = null;
    isNormalDrive = true;
    cmdStartStop = cmdCalibrate = false;

    // start BT-Send thread
    PC_BTS.setPriority(Thread.MAX_PRIORITY);
    PC_BTS.start();

    while (!DE.isBtConn()) {
      // wait for BT connection to establish
      // with the NXT brick (running BT_PC_rx)
    }

    // launch GUI:
    EventQueue.invokeLater(
        new Runnable() {
          public void run() {
            try {
              frame = new TKAPPFrame();
              frame.setVisible(true);
            } catch (Exception e) {
              e.printStackTrace();
            }
            ;
          }
        });
  }
Example #3
0
  /** KAPPFrame GUI event handling */
  @Override
  public void mouseDragged(MouseEvent e) {
    // calculate new icon position
    xLab += (int) (e.getX() - wLab / 2);
    yLab += (int) (e.getY() - hLab / 2);

    // icon should not fall off from or partially hidden within the frame
    if (xLab < 4 * xBrd) xLab = 4 * xBrd;
    if (xLab + wLab > wFrm - 6 * xBrd) xLab = wFrm - 6 * xBrd - wLab;
    if (yLab < 2 * yBrd) yLab = 2 * yBrd;
    if (yLab + hLab > hFrm - 8 * yBrd) yLab = hFrm - 8 * yBrd - hLab;

    // calculate OpCode command based on new icon position
    int xCmd = xLab - xRef;
    int yCmd = yRef - yLab;

    // encode new OpCode to be sent over BT to the NXT application
    try {
      // if not in "STOP" or if stopped due to "CALIBRATION", send the OpCode
      if (!cmdStartStop || cmdCalibrate)
        DE.encodeTurnAngleAndSpeed(xCmd, yCmd, cmdCalibrate, isNormalDrive);
    } catch (Exception err) {
      System.err.println("GUI::EX-1:: IO error trying to buffer your CMD!");
      err.printStackTrace();
    }

    // redraw kAPP icon in new position
    lbKAPP.setBounds(xLab, yLab, wLab, hLab);
    lbKAPP.repaint(xLab, yLab, wLab, hLab);
  }
  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);
  }
Example #5
0
  @Override
  public void mouseClicked(MouseEvent arg0) {
    // LEFT BUTTON
    // toggle between normal and reverse drive mode
    if (arg0.getButton() == MouseEvent.BUTTON1 && arg0.getClickCount() == 2) {
      // reverse drive direction selected
      isNormalDrive = !isNormalDrive;

      // apply reverse compass map if reverse drive is selected
      if (isNormalDrive) lbBkg.setIcon(new ImageIcon("images/kAPPframe.png"));
      else lbBkg.setIcon(new ImageIcon("images/kAPPframeRev.png"));

      // recalculate KAPP icon position within the toggled compass map
      xLab += (int) (arg0.getX() - wLab / 2); /* current */
      yLab += (int) (arg0.getY() - hLab / 2);
      int xCmd = -(xLab - xRef);
      int yCmd = -(yRef - yLab);
      xLab = xCmd + xRef; /* new */
      yLab = yRef - yCmd;

      // redraw kAPP icon
      lbKAPP.setBounds(xLab, yLab, wLab, hLab);
      lbKAPP.repaint(xLab, yLab, wLab, hLab);

      // drag the mouse pointer on top of the KAPP icon
      xFrm = frame.getX();
      yFrm = frame.getY();
      int newMouseX = xLab + xBrd + (int) (wLab / 2) + xFrm;
      int newMouseY = yLab + 4 * yBrd + (int) (hLab / 2) + yFrm;
      try {
        Robot robot = new Robot();
        robot.mouseMove(newMouseX, newMouseY);
      } catch (AWTException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
    }

    // WHEEL BUTTON
    // increase/decrease wheel speed (in calibration only)
    if (arg0.getButton() == MouseEvent.BUTTON2 && cmdCalibrate) {
      DE.encodeSpecialCommands(DataExchange.SPDLEV_CMD);
      DE.incSpeedLevel();
      lbKAPP.setToolTipText("SPEED LEVEL: " + DE.getSpeedLevel());
    }

    // RIGHT BUTTON
    // send START/STOP special commands
    if (arg0.getButton() == MouseEvent.BUTTON3) {
      if (arg0.getClickCount() >= 2) {
        // toggle STOP/START command:
        // true => STOP_CMD; false => START_CMD
        cmdStartStop = !cmdStartStop;

        if (cmdStartStop) {
          DE.encodeSpecialCommands(DataExchange.STOP_CMD);
          lbKAPP.setToolTipText("*STOP*");
          frame.setIconImage(Toolkit.getDefaultToolkit().getImage("images/kAPPstop.png"));

        } else {
          DE.encodeSpecialCommands(DataExchange.START_CMD);
          cmdCalibrate = false;
          lbKAPP.setToolTipText("*GO*");
          frame.setIconImage(Toolkit.getDefaultToolkit().getImage("images/kAPPgo.png"));
        }
      }

      // send CALIBRATE special command
      if (cmdStartStop && (arg0.getClickCount() == 1)) {
        // if the Stop command is issued, perform command Motors calibration
        cmdCalibrate = true;
        DE.encodeSpecialCommands(DataExchange.CAL_CMD);
        lbKAPP.setToolTipText("*CALIBRATION*");
        frame.setIconImage(Toolkit.getDefaultToolkit().getImage("images/kAPPcal.png"));
      }
    }
  }