Ejemplo n.º 1
0
 void printOnVision(Point a, Color color) {
   int tmpx = a.x;
   int tmpy = a.y;
   vision.addLine(
       new Point(tmpx - 10, tmpy - 10), new Point(tmpx + 10, tmpy + 10), color.getRGB());
   vision.addLine(
       new Point(tmpx + 10, tmpy - 10), new Point(tmpx - 10, tmpy + 10), color.getRGB());
 }
Ejemplo n.º 2
0
  /** Instantiate objects and start the planning thread */
  public Main2() {
    try {
      if (usingSimulator) simulator = Simulator.startSimulator();
      else vision = Viewer.startVision();
    } catch (Exception ex) {

      if (usingSimulator) System.out.println("Error starting Simulator");
      else System.out.println("Error starting Vision");

      System.out.println(ex.toString());
      ex.printStackTrace();
    }
    nxt = new Robot2(usingSimulator);
    start();
  }
Ejemplo n.º 3
0
  /** Get the most recent information from vision */
  public void getPitchInfo() {

    ObjectInfo pitchInfo;

    if (usingSimulator) pitchInfo = simulator.getObjectInfos();
    else pitchInfo = vision.getObjectInfos();

    // get ball info
    ball.setCoors(pitchInfo.getBallCoors());

    // get info on robots
    if (weAreBlueTeam) {
      nxt.updateRobotDetails(pitchInfo.getBlueBot());
      opponentRobot = pitchInfo.getYellowBot();
    } else {
      nxt.updateRobotDetails(pitchInfo.getYellowBot());
      opponentRobot = pitchInfo.getBlueBot();
    }
  }
Ejemplo n.º 4
0
  private void theLoop() {
    while (true) {
      // pause if gui asks to pause
      while (!robotMoving) {
        nxt.stop();
        Tools.rest(100);
      }
      while (blockingMode && robotMoving) {
        // this is the penalty mode that returns to normal game
        defend();
        Tools.rest(50);
      }

      // get rid of debug lines
      vision.dropAllLines();

      // update coordinates
      getPitchInfo();

      // if robot is by the wall, try to rotate
      // 97-379 seem to be upper and lower limits for robots y value
      int tmpy = ImageProcessor.barrelCorrected(nxt.getCoors()).y;
      if (tmpy < MINYPOS || tmpy > MAXYPOS) {
        System.out.println("im too close to wall");
        boolean d = false;
        int upordown = 0;
        if (tmpy < MINYPOS) {
          // d = ball.getCoors().x > nxt.getCoors().x;
          d = !shootingLeft;
          // which way should it be facing
          upordown = nxt.getAngle() > 0 && nxt.getAngle() < Math.PI ? 1 : -1;
          // by which side is it stuck
        }
        if (tmpy > MAXYPOS) {
          // d = ball.getCoors().x < nxt.getCoors().x;
          d = shootingLeft;
          // which way should it be facing
          upordown = nxt.getAngle() > 0 && nxt.getAngle() < Math.PI ? -1 : 1;
          // by which side is it stuck
        }

        if (d) Main2.nxt.adjustWheelSpeeds(0, upordown * 600);
        else Main2.nxt.adjustWheelSpeeds(upordown * 600, 0);
        Tools.rest(600);
        Main2.nxt.stop();

        continue;
      }

      // target point is the shooting position
      // returns ball coors if there is no shooting position
      Point target = findPositionToShootFrom(ball.getCoors());

      // if the other robot is less than 300 away, go for the ball
      if (ball.getCoors().distance(opponentRobot.getCoors()) < 300) target = ball.getCoors();

      // distance between the robot and the ball
      int dist = (int) Tools.getDistanceBetweenPoint(nxt.getCoors(), ball.getCoors());

      // if we are close to the ball AND it is not behind us, go for the
      // ball
      if (shootingLeft && (ball.getCoors().x - 30) < nxt.getCoors().x && dist < 70) {
        target = ball.getCoors();
      }
      if (!shootingLeft && (ball.getCoors().x + 30) > nxt.getCoors().x && dist < 70) {
        target = ball.getCoors();
      }

      // if we are close to the ball: slow down
      // gear = dist < 100 ? (dist < 70 ? 4 : 7) : 12;

      // if has ball and can shoot -> shoot
      if (hasBall()) {
        // if pointing to the other half -> shoot (also, don't kick
        // 10cm~30px from the wall)
        // 10cm = by how much does the kicker stick our when it kicks
        if (isPointingTheOtherWay()
            && !(tmpy < MINYPOS + 30 && nxt.getAngle() > Math.PI && nxt.getAngle() < 2 * Math.PI)
            && !(tmpy > MAXYPOS - 30 && nxt.getAngle() > 0 && nxt.getAngle() < Math.PI)) {
          nxt.kick();

          nxt.adjustWheelSpeeds(600, 600);

          Tools.rest(50);
          continue;
        }

        System.out.println("hasBall");
        if (isClearToShoot(ball.getCoors())) {
          System.out.println("isClearToShoot");
          nxt.adjustWheelSpeeds(600, 600);
          nxt.kick();
          Tools.rest(50);
          continue;
        } else {
          // if has ball but cannot shoot: just go for goal
          target = ball.getCoors(); // goalPoints[0];
        }
      }

      // print the possible goals.
      for (int i = 0; i < goalPoints.length; i++) {
        printOnVisionFix(goalPoints[i], Color.red);
      }

      // mark blue possible shoothig position
      printOnVisionFix(currentGoal, Color.blue);

      // if close enough, rotate.
      {
        if (dist < 50 && !ball.getCoors().equals(target)) {
          double a = getAngleToFacePoint(currentGoal);
          if (Math.toDegrees(a) > 10) {
            long l = System.currentTimeMillis();
            if (l - lr > 1000) {
              nxt.rotateBy(a);
              lr = l;
            }
            Tools.rest(50);
            continue;
          }
        }
      }

      // Get a path from the path planner, fiddle with it to get the correct waypoint, move, iterate
      ArrayList<Point> path =
          PathSearch.getPath(
              new Point(ball.getCoors().x, 480 - ball.getCoors().y),
              new Point(nxt.getCoors().x, 480 - nxt.getCoors().y),
              (int) Math.toDegrees(nxt.getAngle()),
              new Point(opponentRobot.getCoors().x, 480 - opponentRobot.getCoors().y));
      for (int j = 0; j < path.size(); j++) {
        printOnVision(path.get(j), Color.red);
      }
      if (path.size() > 2) target = path.remove(2);
      else if (path.size() > 1) target = path.remove(1);
      else target = path.remove(0);

      target.y = 480 - target.y;

      printOnVisionFix(target, Color.red);

      moveTo(target);

      Tools.rest(50);
    }
  }