⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 brucelee.cc

📁 该文件是包含了机器人足球比赛中的整个系统的代码
💻 CC
📖 第 1 页 / 共 3 页
字号:
*/

  int cC = currentTrick->Continue();
  char* cMsg = currentTrick->GetErrorMsg(cC);

  keeperRoaming = false;
  double ballDistance = 20000.0;
  if (vo_ball_ != NULL) {
    ballDistance = ABS(vo_ball_->distance_);
      // enemy goal coords
  double currx = wo_self_->x_;
  double curry = wo_self_->y_;
  double currheading = wo_self_->heading_;
  double x_ = 0;
  double y_ = 215;
  if (curry>210) curry=210;
  double theta = (atan2(y_-curry, x_-currx) - (PI / 2.0)) - currheading;

  // heading to goal based on WM.
  double goalHeading = RAD_TO_DEG(theta);

  // we can see opposition goal ! Use vision instead of WM
  if (vo_oppositionGoal_ != NULL) {
    goalHeading = RAD_TO_DEG(vo_oppositionGoal_->heading_);
    if (goalHeadingLeft_ < DEG_TO_RAD(10) && goalHeadingRight_ > DEG_TO_RAD(-10)) {
      goalHeading = 0.0;
    } else {
      double temp = RAD_TO_DEG(goalHeadingLeft_)-10.0;
      if (ABS(temp) < ABS(goalHeading)) {
        goalHeading = temp;
      } 
      temp = RAD_TO_DEG(goalHeadingRight_)+10.0;
      if (ABS(temp) < ABS(goalHeading)) {
        goalHeading = temp;
      } 
    }
   
  // we can see our own goal ! Use vision instead of WM.
  } else if (vo_ownGoal_ != NULL) {
    goalHeading = 180.0 - RAD_TO_DEG(vo_ownGoal_->heading_);
    //cout << "ownGoal->heading = " << RAD_TO_DEG(vo_ownGoal_->heading_) << ", goalHeading: " << goalHeading << endl << flush;
  }

  while (goalHeading > 180.0) goalHeading -= 360.0;
  while (goalHeading <= -180.0) goalHeading += 360.0;
  
  ballDistance+=(ABS(goalHeading)/180.0*ballDistance);

#ifdef ERS_7
    if ((MICRO_TO_DEG(sensorValues_[S_HEAD_TILT1])+MICRO_TO_DEG(sensorValues_[S_HEAD_TILT2]) < -25.0) && (ABS(MICRO_TO_DEG(sensorValues_[S_HEAD_PAN])) < 20.0)) {
      ballDistance = 1.25;
    }
#endif
#ifdef ERS_210
    if ((MICRO_TO_DEG(sensorValues_[S_HEAD_TILT]) < -25.0) && (ABS(MICRO_TO_DEG(sensorValues_[S_HEAD_PAN])) < 20.0)) {
      ballDistance = 1.25;
    }
#endif

    // NOTE! keeper roaming is always OFF here. so we don't REALLY need to call the NonEnlarged version ...
    // keeper cheats in bruce lee distance negotiation stuff.
    if (myPosition == BL_GOALKEEPER && IsBallWithinNonEnlargedChaseBounds()) {
      ballDistance = 1.5;
    } else if (myPosition == BL_GOALKEEPER && (!IsBallWithinNonEnlargedChaseBounds())) {
      // if no one else has chased for over 4 seconds and we're close to the ball, enable keeperRoaming !
      // this essentially just enlarges the keeper's chase bounds.

      if (frame_-timeOfLastChase > 100 && ballDistance < 120.0) {
        keeperRoaming = true;
      } else {
        ballDistance = 20000.0;
      }
    }
  }
  //doing a chase and not interruptible AND we're too fucking close (head tilt measure), FUCK OFF AWAY!!
  if (strcmp(currentTrick->GetName(),"AdvancedChaseWithKick")==0 && strcmp(cMsg,"INTERRUPTIBLE")!=0) {
#ifdef ERS_7
    if ((MICRO_TO_DEG(sensorValues_[S_HEAD_TILT1])+MICRO_TO_DEG(sensorValues_[S_HEAD_TILT2]) < -18.0) && (ABS(MICRO_TO_DEG(sensorValues_[S_HEAD_PAN])) < 20.0)) {
      ballDistance = 1.25;
    }
#endif
  }

  // this babble is so we don't let other chase when we're doing turnwithballuntilgoal
  Trick* cT = currentTrick;
  while (cT != NULL) {
    if (cT->GetCurrentTrick() != NULL) {
      cT = cT->GetCurrentTrick();
    } else break;
  }
  if (cT != NULL) {
    if (strcmp(cT->GetName(),"TurnWithBallUntilGoal")==0 || strcmp(cT->GetName(),"WaitForKick") == 0 || strcmp(cT->GetName(),"Kick") == 0 ) {
      ballDistance = 1.0;
    }                                                                                                             
    if (strcmp(currentTrick->GetName(),"AdvancedChaseWithKick")==0 && ballDistance > 600 && strcmp(cT->GetName(),"PostChaseSearchForBall")!=0) ballDistance = lastBallDistance;

  }

  // if kicking, make sure others DO NOT CHASE !!!!
  // note that postchasesearchforball does a getup, but this DOES NOT COUNT AS SHIT
  if (lcq_.Size() >= 0 && strcmp(cT->GetName(),"PostChaseSearchForBall")!=0) {
    LocomotionCommand currentLocomotionCommand = lcq_.GetCurrentLocomotionCommand();
    int motionType = currentLocomotionCommand.motionType;
    if ((motionType > 4 && motionType < 50) || (motionType > 100) || (motionType == LocomotionCommand::TP_SINGLEWALKWITHOUTFRONT) || (motionType == 99)) {
      if (motionType != 6) {
        ballDistance = 1.0;
      }
    }
  }

  
  lastBallDistance = ballDistance;

//cout << "ct: " << cT->GetName() << ", balldistance: " << ballDistance << endl << flush;

  //executing normally. we may wish to interrupt.
  if (cC >= 1) {
    // we're searching but somebody else has seen the ball .. maybe we don't need to search anymore
    if (strcmp(currentTrick->GetName(),"SearchForBall")==0) {
       // If somebody can see it and its not close to me then i should just look where they say it is
       if (ClosestToBall() < 525.0 && (pow(wo_self_->x_ - wo_ball_->x_, 2)+pow(wo_self_->y_ - wo_ball_->y_, 2) > 900)) {
          currentTrick->Abort();
          delete currentTrick;
          currentTrick = new MovementTricks::MoveToPoint(homeX,homeY,0.0,MovementTricks::MoveToPoint::MTP_DYNAMIC_FACE_BALL);
          currentTrick->Start();
       }
    // we're chasing, but we're allowed to interrupt. Also don't interrupt if we are close to the ball.
    #ifdef ERS_7
    // if we're doing postchasesearchforball w/i advancedchasewithkick, we cannot force it to exit out here.
    } else if ((strcmp(currentTrick->GetName(),"AdvancedChaseWithKick")==0) && strcmp(cMsg,"INTERRUPTIBLE")==0 && strcmp(cT->GetName(),"PostChaseSearchForBall")!=0/*&& MICRO_TO_DEG(sensorValues_[S_HEAD_TILT1])+MICRO_TO_DEG(sensorValues_[S_HEAD_TILT2]) > -15*/) {
    #endif
    #ifdef ERS_210
    } else if ((strcmp(currentTrick->GetName(),"ChaseWithKick")==0) && strcmp(cMsg,"INTERRUPTIBLE")==0 && MICRO_TO_DEG(sensorValues_[S_HEAD_TILT]) > -15) {
    #endif
      // exceeded chase bounds ! back off !!! (note- Chase will terminate if ball is not visible - so this is ok)
      //int whoIs = WhoIsChasing();
      //WhoIsChasing();
      if (IsBallWithinChaseBounds() == false || ClosestToBall() < ballDistance) {
        if (debugOutput)
          cout << "BruceLee: Aborted AdvancedChaseWithKick (chase bound exceeded)." << endl << flush;
        // change to move home
        currentTrick->Abort();
        delete currentTrick;
        currentTrick = new MovementTricks::MoveToPoint(homeX,homeY,0.0,MovementTricks::MoveToPoint::MTP_DYNAMIC_FACE_BALL);
        currentTrick->Start();
        // look around for beacons !
        if (IsBallVisible()) {
          headTrick->Abort();
          delete headTrick;
          headTrick = new HeadTricks::FollowBallWithHeadSticky(10);
        } else {
          headTrick->Abort();
          delete headTrick;
          headTrick = new HeadTricks::HeadPanForBall();
        }
        headTrick->Start();
      }
    // currently moving home or searching.
    } else if (strcmp(currentTrick->GetName(),"MoveToPoint")==0) {
      // ball is within our chase area !
      if (AmIAllowedToChase(ballDistance)) {
        if (debugOutput)
          cout << "BruceLee: Ball within chase area. Aborting SearchForBall/MoveToPoint and chasing." << endl << flush;
        currentTrick->Abort();  
        delete currentTrick;
        // If we are the keeper chase a bit slower so we don't make a mistake
        if (myPosition == BL_GOALKEEPER) currentTrick = new ComplexTricks::AdvancedChaseWithKick(); // Chase a bit slower so we don't make a mistake
        else currentTrick = new ComplexTricks::AdvancedChaseWithKick();
        cC = currentTrick->Start();
        headTrick->Abort();
        delete headTrick;
        headTrick = new BasicTricks::NullHead();
        headTrick->Start();
      } else if (ballPrediction_.ShouldIDive()) {
        // if we're the goalie, or we're facing basically towards opposition goal ...
        if (myPosition == BL_GOALKEEPER || ABS(RAD_TO_DEG(wo_self_->heading_)) < 80) {
            /* int direction = ballPrediction_.DiveDirection();
             if (lcq_.Front().motionType < 4) {  
               if (direction == BallPrediction::BP_LEFT) utils.Kick(LocomotionCommand::TP_GOALIE_LEFT,false);
               else if (direction == BallPrediction::BP_RIGHT) utils.Kick(LocomotionCommand::TP_GOALIE_RIGHT,false);
               else utils.Kick(LocomotionCommand::TP_GOALIE_BLOCK, false);
             } */
            utils.Kick(LocomotionCommand::TP_GOALIE_BLOCK, false);
        }
      } else {
        // not chasing, not blocking. reposition instead.
        double destX = homeX;
        double destY = homeY;

        if (myPosition == BL_GOALKEEPER) {
          double theta = (atan2(wo_ball_->y_-homeY, homeX-wo_ball_->x_) - (PI / 2.0));
          while (theta > PI) theta -= 2*PI;
          while (theta < -PI) theta += 2*PI;
          destX = homeX+homeRadius*sin(theta)*2.4;
          destY = homeY+homeRadius*cos(theta)*0.30;
          if (destY < -210) destY = -210;
          if (destY > -200) destY = -200;

          // if the ball is in the corners bring the circle focus back
          // so the ball wont sneak down the walls
          if (wo_ball_->y_ < - 160 && wo_ball_->x_ > 65) {
            destY = -202;
            destX = 35;            
          }
          else if (wo_ball_->y_ < - 160 && wo_ball_->x_ < -65 ) {
            destY = -202;
            destX = -35;
          }
          // else its far away or in the middle - use normal code 
          else {
            double theta = (atan2(wo_ball_->y_-homeY, homeX-wo_ball_->x_) - (PI / 2.0));
            while (theta > PI) theta -= 2*PI;
            while (theta < -PI) theta += 2*PI;
            destX = homeX+homeRadius*sin(theta)*2.4;
            destY = homeY+homeRadius*cos(theta)*0.30;
            if (destY < -210) destY = -210;
          }
/*
          // if ball is on other half of field and we dont need to move much (5cm)
          // relocalise
          if (wo_ball_->y_ > 0 && ( (destX - wo_self_->x_) * (destX - wo_self_->x_) + (destY - wo_self_->y_) * (destY - wo_self_->y_) < 25 ))
          {
            delete headTrick;
            headTrick = new HeadTricks::HeadPan(85,-85,3,50);   
            headTrick->Start();
            //delete currentTrick;
            //currentTrick = new BasicTricks::NullBody();
            //currentTrick->Start();
          }
*/
        } else {
          if (myPosition == BL_CENTREDEFENDER /*|| myPosition == BL_CENTRESTRIKER (MQ 5/7)*/) {
            destX = (wo_ball_->x_ + homeX)/2.0;
          } else if (myPosition == BL_LEFTSTRIKER || myPosition == BL_RIGHTSTRIKER) {
            /*
            if (homeX > 0) {
              destX = 135+wo_ball_->x_;
            } else {
              destX = wo_ball_->x_-135;
            }
            if (ABS(destX) > ABS(homeX)) destX = homeX;
            */

            destY = (wo_ball_->y_ + homeY)/2.0;
            if (destY<homeY) destY = homeY;
          } 
        }
        if (myPosition != BL_GOALKEEPER) {
          if (destY > chaseUpperY-boundOffset) destY = chaseUpperY-boundOffset;
          if (destY < chaseLowerY+boundOffset) destY = chaseLowerY+boundOffset;
          if (destY < -150) destY = -150;

          if (destX > chaseUpperX-boundOffset) destX = chaseUpperX-boundOffset;
          if (destX < chaseLowerX+boundOffset) destX = chaseLowerX+boundOffset;
        }
        ((MovementTricks::MoveToPoint*)(currentTrick))->ResetHome((int)destX,(int)destY);    
        actualHomeX = destX;
        actualHomeY = destY;

        if (strcmp(headTrick->GetName(),"FollowBallWithHeadSticky")!=0 && IsBallVisible() == false && !(ClosestToBall() < 525.0) /*&& (pow(wo_self_->x_ - actualHomeX, 2)+pow(wo_self_->y_ - actualHomeY, 2) < homeRadius*homeRadius)*/) {
          // we're moving to point, and we can't see the ball.
          // if we're practically home AND no one else is chasing, we'd better look for the ball !!

          currentTrick->Abort();
          delete currentTrick;
          currentTrick = new MovementTricks::SearchForBall(360.0);
          currentTrick->Start();
          delete headTrick;
          headTrick = new BasicTricks::NullHead();
          headTrick->Start();
        }
      }
    }
  // move to point exited. start moving again ?
  } else if (cC < 1) {
    if (AmIAllowedToChase(ballDistance)) {
      if (debugOutput)
        cout << "BruceLee: Ball within chase area, previous trick has ended. Chasing." << endl << flush;
      delete currentTrick;
      // If we are the keeper chase a bit slower so we don't make a mistake
      if (myPosition == BL_GOALKEEPER) currentTrick = new ComplexTricks::AdvancedChaseWithKick(); 
      else currentTrick = new ComplexTricks::AdvancedChaseWithKick();
      cC = currentTrick->Start();

      headTrick->Abort();
      delete headTrick;
      headTrick = new BasicTricks::NullHead();
      headTrick->Start();
    } else {
      // if self is within chase bounds, and we did NOT just search, and the ball is not visible .. spin around !
      // note - don't do this if in sticky search.
      if ( (strcmp(currentTrick->GetName(), "SearchForBall")!=0) && (IsBallVisible() == false) && (IsSelfWithinChaseBounds()) && strcmp(headTrick->GetName(),"FollowBallWithHeadSticky")!=0 && strcmp(headTrick->GetName(),"QuickPan")!=0) {
        if (debugOutput)
          cout << "BruceLee: Searching for ball." << endl << flush;
        delete currentTrick;
/*
        if (lastBallHeading_ > 0) {
          turnRight = false;
        }
        // if we kicked left very recently, we probably should turn that way...
        if (frame_-lastKickedLeft_ < 25) turnRight = false;
        // same deal for right
        if (frame_-lastKickedRight_ < 25) turnRight = true;*/

        currentTrick = new MovementTricks::SearchForBall(360.0);
        currentTrick->Start();
        delete headTrick;
        headTrick = new BasicTricks::NullHead();
        headTrick->Start();
      } else {
        // if either a) we've just searched, b) the ball is visible but out of bounds or c) WE are out of bounds, then go home!
        delete currentTrick;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -