void AI_main(struct RoboAI *ai, struct blob *blobs, void *state)
{
 // Top-level AI

 fprintf (stderr,"AI_main() called for ai at %p and blob list at %p\n",ai,blobs);

 if (ai->st.state==0)  // Initial set up - find own, ball, and opponent blobs			
 {
  fprintf(stderr,"State is 0, self-id in progress...\n");
  id_bot(ai,blobs);
  if (ai->st.selfID==1)
  {
   ai->st.state=1;
   all_stop();
   clear_motion_flags(ai);
  }
 }
}
Beispiel #2
0
void AI_main(struct RoboAI *ai, struct blob *blobs, void *state)
{
    /*************************************************************************
     You will be working with a state-based AI. You are free to determine
     how many states there will be, what each state will represent, and
     what actions the robot will perform based on the state as well as the
     state transitions.

     You must *FULLY* document your state representation in the report

     Here two states are defined:
     State 0,100,200 - Before robot ID has taken place (this state is the initial
                       state, or is the result of pressing 'r' to reset the AI)
     State 1,101,201 - State after robot ID has taken place. At this point the AI
                       knows where the robot is, as well as where the opponent and
                       ball are (if visible on the playfield)

     Relevant UI keyboard commands:
     'r' - reset the AI. Will set AI state to zero and re-initialize the AI
    data structure.
     't' - Toggle the AI routine (i.e. start/stop calls to AI_main() ).
     'o' - Robot immediate all-stop! - do not allow your NXT to get damaged!

     ** Do not change the behaviour of the robot ID routine **
    **************************************************************************/

    if (ai->st.state == 0 || ai->st.state == 100 || ai->st.state == 200) // Initial set up - find own, ball, and opponent blobs
    {
        // Carry out self id process.
        fprintf(stderr, "Initial state, self-id in progress...\n");
        id_bot(ai, blobs);
        if ((ai->st.state % 100) != 0) // The id_bot() routine will change the AI state to initial state + 1
        {
            // if robot identification is successful.
            if (ai->st.self->cx[0] >= 512) ai->st.side = 1; else ai->st.side = 0;
            all_stop();
            clear_motion_flags(ai);
            fprintf(stderr, "Self-ID complete. Current position: (%f,%f), current heading: [%f, %f], AI state=%d\n", ai->st.self->cx[0], ai->st.self->cy[0], ai->st.self->mx, ai->st.self->my, ai->st.state);
        }
    }
    else
    {
        /****************************************************************************
         TO DO:
         You will need to replace this 'catch-all' code with actual program logic to
         have the robot do its work depending on its current state.
         After id_bot() has successfully completed its work, the state should be
         1 - if the bot is in SOCCER mode
         101 - if the bot is in PENALTY mode
         201 - if the bot is in CHASE mode

         Your AI code needs to handle these states and their associated state
         transitions which will determine the robot's behaviour for each mode.
        *****************************************************************************/
        // track_agents(ai,blobs);        // Currently, does nothing but endlessly track
        // fprintf(stderr,"Just trackin'!\n");    // bot, opponent, and ball.

        switch(ai->st.state){
            case 101:

                kick();
                sleep(2);
                break;
            case 201:
                chase(ai);
                break;
        }
    }

}
Beispiel #3
0
void AI_main(struct RoboAI *ai, struct blob *blobs, void *state)
{
    /*************************************************************************
     This is the main AI loop.

     It is called by the imageCapture code *once* per frame. And it *must not*
     enter a loop or wait for visual events, since no visual refresh will happen
     until this call returns!

     Therefore. Everything you do in here must be based on the states in your
     AI and the actions the robot will perform must be started or stopped
     depending on *state transitions*.

     E.g. If your robot is currently standing still, with state = 03, and
      your AI determines it should start moving forward and transition to
      state 4. Then what you must do is
      - send a command to start forward motion at the desired speed
      - update the robot's state
      - return

     I can not emphasize this enough. Unless this call returns, no image
     processing will occur, no new information will be processed, and your
     bot will be stuck on its last action/state.

     You will be working with a state-based AI. You are free to determine
     how many states there will be, what each state will represent, and
     what actions the robot will perform based on the state as well as the
     state transitions.

     You must *FULLY* document your state representation in the report

     The first two states for each more are already defined:
     State 0,100,200 - Before robot ID has taken place (this state is the initial
               	    state, or is the result of pressing 'r' to reset the AI)
     State 1,101,201 - State after robot ID has taken place. At this point the AI
               	    knows where the robot is, as well as where the opponent and
               	    ball are (if visible on the playfield)

     Relevant UI keyboard commands:
     'r' - reset the AI. Will set AI state to zero and re-initialize the AI
    data structure.
     't' - Toggle the AI routine (i.e. start/stop calls to AI_main() ).
     'o' - Robot immediate all-stop! - do not allow your NXT to get damaged!

     ** Do not change the behaviour of the robot ID routine **
    **************************************************************************/
    double x_distance;
    double y_distance;

    if (ai->st.state==0||ai->st.state==100||ai->st.state==200)  	// Initial set up - find own, ball, and opponent blobs
    {
        // Carry out self id process.
        fprintf(stderr,"Initial state, self-id in progress...\n");
        id_bot(ai,blobs);
        if ((ai->st.state%100)!=0)	// The id_bot() routine will change the AI state to initial state + 1
        {   // if robot identification is successful.
            if (ai->st.self->cx>=512) ai->st.side=1;
            else ai->st.side=0;
            all_stop();
            clear_motion_flags(ai);
            fprintf(stderr,"Self-ID complete. Current position: (%f,%f), current heading: [%f, %f], AI state=%d\n",ai->st.self->cx,ai->st.self->cy,ai->st.self->mx,ai->st.self->my,ai->st.state);
        }
    }
    else
    {
        /****************************************************************************
         TO DO:
         You will need to replace this 'catch-all' code with actual program logic to
         implement your bot's state-based AI.

         After id_bot() has successfully completed its work, the state should be
         1 - if the bot is in SOCCER mode
         101 - if the bot is in PENALTY mode
         201 - if the bot is in CHASE mode

         Your AI code needs to handle these states and their associated state
         transitions which will determine the robot's behaviour for each mode.

         Please note that in this function you should add appropriate functions below
         to handle each state's processing, and the code here should mostly deal with
         state transitions and with calling the appropriate function based on what
         the bot is supposed to be doing.
        *****************************************************************************/
        //fprintf(stderr,"Just trackin'!\n");	// bot, opponent, and ball.
        //track_agents(ai,blobs);		// Currently, does nothing but endlessly track

        if(ai->st.state ==101) {
            x_distance = abs(ai->st.ball->cx - ai->st.self->cx);
            y_distance = abs(ai->st.ball->cy - ai->st.self->cy);
            while(x_distance > 10) {

                drive_speed(50);
                x_distance -= 45;
                all_stop();
                fprintf(stderr,"distance is: %f \n", x_distance);
            }
            ai->st.state += 1;
            fprintf(stderr,"state is %i \n", ai->st.state);
        }
    }

}
Beispiel #4
0
void AI_main(struct RoboAI *ai, struct blob *blobs, void *state)
{
  /*************************************************************************
  This is the main AI loop.

  It is called by the imageCapture code *once* per frame. And it *must not*
  enter a loop or wait for visual events, since no visual refresh will happen
  until this call returns!

  Therefore. Everything you do in here must be based on the states in your
  AI and the actions the robot will perform must be started or stopped 
  depending on *state transitions*. 
  E.g. If your robot is currently standing still, with state = 03, and
   your AI determines it should start moving forward and transition to
   state 4. Then what you must do is 
   - send a command to start forward motion at the desired speed
   - update the robot's state
   - return

  I can not emphasize this enough. Unless this call returns, no image
  processing will occur, no new information will be processed, and your
  bot will be stuck on its last action/state.
  You will be working with a state-based AI. You are free to determine
  how many states there will be, what each state will represent, and
  what actions the robot will perform based on the state as well as the
  state transitions.
  You must *FULLY* document your state representation in the report
  The first two states for each more are already defined:
  State 0,100,200 - Before robot ID has taken place (this state is the initial
                  state, or is the result of pressing 'r' to reset the AI)
  State 1,101,201 - State after robot ID has taken place. At this point the AI
                  knows where the robot is, as well as where the opponent and
                  ball are (if visible on the playfield)
  Relevant UI keyboard commands:
  'r' - reset the AI. Will set AI state to zero and re-initialize the AI
  data structure.
  't' - Toggle the AI routine (i.e. start/stop calls to AI_main() ).
  'o' - Robot immediate all-stop! - do not allow your NXT to get damaged!
  ** Do not change the behaviour of the robot ID routine **
  **************************************************************************/

  if (ai->st.state==0||ai->st.state==100||ai->st.state==200)   // Initial set up - find own, ball, and opponent blobs
  {
    // Carry out self id process.
    fprintf(stderr,"Initial state, self-id in progress...\n");
    id_bot(ai,blobs);
    if ((ai->st.state%100)!=0)  // The id_bot() routine will change the AI state to initial state + 1
    {       // if robot identification is successful.
      if (ai->st.self->cx>=512) ai->st.side=1; else ai->st.side=0;
      all_stop();
      clear_motion_flags(ai);
      fprintf(stderr,"Self-ID complete. Current position: (%f,%f), current heading: [%f, %f], AI state=%d\n",ai->st.self->cx,ai->st.self->cy,ai->st.self->mx,ai->st.self->my,ai->st.state);
    }
  } else {
    track_agents(ai,blobs);  

    // reserve first three states in each mode for forward drive direction determination
    if ((ai->st.state > 0 && ai->st.state < 3) || 
        (ai->st.state > 100 && ai->st.state < 103) ||
        (ai->st.state > 200 && ai->st.state < 203)) {
      clear_motion_flags(ai);
      drive_speed(50);
      ai->st.state++;
    } 
    // otherwise, go to respective state mode and navigate
    else {
      // save up to date blob info to avoid segfaults
      backupReadings(ai);

      // SOCCER MODE
      if (ai->st.state >= 3 && ai->st.state <= 100) {
        switch(ai->st.state) {
          case 3: 
            if (checkOpponent(ai, 1)){
              approachTargetLocation(ai, TO_KICK_POS);
            } else {
              approachTargetLocation(ai, TO_DEFENSE);
            }
            break;
          case 4: 
            // if ball has moved by opponent or earthquake, jump back a state
            if ((ballHistX[0] - ballHistX[1] > 10.0) || (ballHistY[0] - ballHistY[1] > 10.0))
              --ai->st.state;
            approachTargetLocation(ai, TO_BALL); 
            break;
          case 5: kickBall(ai); break;
          default: ai->st.state = 1; break;
        }
      }
      // PENALTY MODE
      else if (ai->st.state >= 103 && ai->st.state <= 200) {
        switch(ai->st.state) {
          case 103: approachTargetLocation(ai, TO_KICK_POS); break;
          case 104: approachTargetLocation(ai, TO_BALL); break;
          case 105: kickBall(ai); break;
          default: ai->st.state = 101; break;
        }
      }
      // CHASE MODE
      else if (ai->st.state >= 203 && ai->st.state <= 300) {
        switch(ai->st.state) {
          case 203: approachTargetLocation(ai, TO_BALL); break;
          case 204: kickBall(ai); break;
          default: ai->st.state = 201; break;
        }
      }
    }
  }
}