Пример #1
0
static void posDataUpdate(Kart* thisKart){
	thisKart->currentXPosition = 0;
	thisKart->currentYPosition = 0;
	thisKart->currentOrientation = 0;
	uint16_t zoneNumber = 0;
	uint16_t XPosition = 0x0000;
	uint16_t YPosition = 0x0000;
	int16_t orientation = 0x0000;
	XPosition|= getDataByte(thisKart->position_command_index, POS_X_M_INDEX)<<8;
	XPosition|= getDataByte(thisKart->position_command_index, POS_X_L_INDEX);
	
	YPosition|= getDataByte(thisKart->position_command_index, POS_Y_M_INDEX)<<8;
	YPosition|= getDataByte(thisKart->position_command_index, POS_Y_L_INDEX);
	orientation|= getDataByte(thisKart->position_command_index, ORIENT_M_INDEX)<<8;
	orientation|= getDataByte(thisKart->position_command_index, ORIENT_L_INDEX);
	
	thisKart->currentXPosition = XPosition;
	thisKart->currentYPosition = YPosition;
	thisKart->currentOrientation = orientation;
	
	zoneNumber = determineZone(XPosition,YPosition, thisKart);
	
	if(thisKart->kartNumber == our_kart.kartNumber){
		if(zoneNumber != thisKart->zone){
			ES_Event Event;
			Event.EventType = ZoneUpdate;
			Event.EventParam = zoneNumber;
			PostStrategySM(Event);
			PostTurningFSM(Event);
			thisKart->zone = zoneNumber;
		}
	}
}
Пример #2
0
static int determineZone(uint16_t Xpos, uint16_t Ypos, Kart* kart){

	if(Xpos >= ZONE_2_X_MIN && Xpos  < zone_two_x_max && Ypos >= zone_two_y_min  && Ypos < zone_two_y_max){
		//printf("Zone 2\r\n");
		return 2;
	}
	if(Xpos >= zone_three_x_min && Xpos  < zone_three_x_max && Ypos >= zone_three_y_min && Ypos < zone_three_y_max) {
		//printf("Zone 3\r\n");
		return 3;
	}
	if(Xpos >= zone_four_x_min && Xpos  < zone_four_x_max && Ypos >= zone_four_y_min && Ypos < ZONE_4_Y_MAX) {
		
		if(Xpos >= CENTER_OF_OBSTACLE_POINT_X && kart->kartNumber == our_kart.kartNumber &&
				listenToCenterOfObstacle){ //&&getKartAZone()!=5 &&  getKartBZone() !=5){
						ES_Event Event;
						Event.EventType = CenterOfObstacle;
					  PostStrategySM(Event);
						listenToCenterOfObstacle = false;
			}
		//printf("Zone 4\r\n"); 
		return 4;
	}
	if(Xpos >= zone_five_x_min && Xpos  < ZONE_5_X_MAX && Ypos >= zone_five_y_min && Ypos < zone_five_y_max){
			
			if(Ypos <= OBSTACLE_PAST_CROSSOVER_POINT_Y && kart->kartNumber == our_kart.kartNumber &&
				listenToMostlyCrossed){
						ES_Event Event;
						Event.EventType = ObstacleMostlyCrossed;
					  PostStrategySM(Event);
						listenToMostlyCrossed = false;
			}
			//printf("Zone 5\r\n"); 
			return 5;
	}
	
	else if(listenToPostObstacleZoneOne && Ypos <= ZONE_ONE_FROM_ZONE_FIVE_STOP_Y && kart->kartNumber == our_kart.kartNumber){
						ES_Event Event;
						Event.EventType = ZoneOneStop;
					  PostStrategySM(Event);
						listenToPostObstacleZoneOne = false;
			}
	//printf("Zone 1\r\n"); 
	return 1;
}
Пример #3
0
static void updateOurKartStatus(void){
	uint8_t gameStatusByte = getDataByte(GAME_STATUS_COM_INDEX,our_kart.game_status_byte);
	
	//Update Number of Laps
		uint8_t numLapsLeft = maskAndShiftByte (gameStatusByte, NUM_LAPS_LEFT_MASK, NUM_LAPS_LEFT_SHIFT);
		if(our_kart.numLaps != numLapsLeft){
				ES_Event ThisEvent;
				ThisEvent.EventType = LapUpdate;
				ThisEvent.EventParam = numLapsLeft;
				PostStrategySM(ThisEvent);
				our_kart.numLaps = numLapsLeft;
			}
		//Update Obstacle Status only do this until the obstacle has been made
		 if(our_kart.obstacleStatus == 0){
				uint8_t obstacleStatus = maskAndShiftByte(gameStatusByte, OBSTACLE_STATUS_STATUS_MASK, OBSTACLE_STATUS_STATUS_SHIFT);
				if(obstacleStatus!=our_kart.obstacleStatus){
					ES_Event ThisEvent;
					ThisEvent.EventType = ObstacleComplete;
					PostStrategySM(ThisEvent);
					our_kart.obstacleStatus = 1;
				}
			}
		//Update TargetStatus
		if(our_kart.targetStatus ==0){
			uint8_t targetStatus = maskAndShiftByte (gameStatusByte, TARGET_STATUS_MASK, TARGET_STATUS_SHIFT);	
			//printf("\r0x%#08x = Game_Status_Byte,  0x%#08x = targetStatus\n", gameStatusByte, targetStatus);
			if(targetStatus!=our_kart.targetStatus){
				ES_Event ThisEvent;
				ThisEvent.EventType = TargetComplete;
				PostStrategySM(ThisEvent);
				our_kart.targetStatus = 1;
			}
		} 
		//Update Run Status
			ES_Event ThisEvent;
			uint8_t status = maskAndShiftByte (gameStatusByte, RUN_STATUS_MASK, RUN_STATUS_SHIFT);
			if(status!= RunStatus){
				RunStatus = status;
				if(status == 0x01){
					ThisEvent.EventType = FlagDropped;
			    PostStrategySM(ThisEvent);	
					HWREG(GPIO_PORTB_BASE +(GPIO_O_DATA + ALL_BITS))|= (BIT3HI);
				}
				else if(status ==0x02){
					ThisEvent.EventType = CautionFlag;
				
				 	PostStrategySM(ThisEvent);
					PostTurningFSM(ThisEvent);
					HWREG(GPIO_PORTB_BASE +(GPIO_O_DATA + ALL_BITS))&= ~(BIT3HI);
				}
				else if(status == 0x03){
					ThisEvent.EventType = RaceOver;
			
					PostStrategySM(ThisEvent);
					PostTurningFSM(ThisEvent);
					HWREG(GPIO_PORTB_BASE +(GPIO_O_DATA + ALL_BITS))&= ~(BIT3HI);
				}
			}
			
}
Пример #4
0
static void updateOurKartStatus(){
	currentByte = getDataByte(GAME_STATUS_COMMAND_INDEX,(our_kart.kartNumber+GAME_STATUS_BYTE_INDEX));
	
	//Update Number of Laps
		uint8_t numLapsLeft = maskAndShiftByte (currentByte, NUM_LAPS_LEFT_MASK, NUM_LAPS_LEFT_SHIFT);
		if(our_kart.numLaps != numLapsLeft){
				ES_Event ThisEvent;
				ThisEvent.EventType = LAP_UPDATE;
				ThisEvent.EventParam = numToTranslate;
				PostStrategySM(ThisEvent);
				our_kart.numLaps = numLapsLeft;
			}
		//Update Obstacle Status only do this until the obstacle has been made
		 if(our_kart.obstacleStatus == 0){
				uint8_t obstacleStatus = maskAndShiftByte(currentByte, OBSTACLE_STATUS_STATUS_MASK, OBSTACLE_STATUS_STATUS_SHIFT);
				if(obstacleStatus!=our_kart.obstacleStatus){
					ES_Event ThisEvent;
					ThisEvent.EventType = OBSTACLE_COMPLETE;
					PostStrategySM(ThisEvent);
					our_kart.obstacleStatus = 1;
				}
			}
		//Update TargetStatus
		if(our_kart.targetStatus ==0){
			uint8_t targetStatus = maskAndShiftByte (currentByte, TARGET_STATUS_MASK, TARGET_STATUS_SHIFT);	
			if(targetStatus!=our_kart.targetStatus){
				ES_Event ThisEvent;
				ThisEvent.EventType = TARGET_COMPLETE;
				PostStrategySM(ThisEvent);
				our_kart.targetStatus = 1;
			}
		} 
		
		//Update Run Status
			ES_Event ThisEvent;
			uint8_t runStatus = maskAndShiftByte (currentByte, RUN_STATUS_MASK, RUN_STATUS_SHIFT);
			if(runStatus == 0x01){
				ThisEvent.EventType = FLAG_DROPPED;
				PostStrategySM(ThisEvent);
			}
			else if(runStatus ==0x02){
				ThisEvent.EventType = CAUTION_FLAG;
				PostStrategySM(ThisEvent);
			}
			else if(runStatus == 0x03){
				ThisEvent.EventType = RACE_OVER;
				PostStrategySM(ThisEvent);
			}
			
}
Пример #5
0
/**
* Calculates the time between consecutive rising edges on PC4. This is
* the interrupt service routine (ISR).
*/
void BeaconSenseResponse(void) {
	//printf("BSR\r\n"); //****************************************************************************
	uint32_t thisCapture;
	/* Start by clearing the source of the interrupt, the input capture event */
	HWREG(WTIMER0_BASE+TIMER_O_ICR) = TIMER_ICR_CAECINT;
	thisCapture = HWREG(WTIMER0_BASE+TIMER_O_TAR);
	period = thisCapture - lastCapture; // add to periodSum
		/* Update lastCapture to prepare for the next edge */  
	lastCapture = thisCapture;
	
	/* Send a BeaconSensed event */
	
	if(period >=targetPeriodMinus && period <= targetPeriodPlus){
		ES_Event ThisEvent;
	  ThisEvent.EventType = BeaconDetected;
		PostStrategySM(ThisEvent);
		/* Mask the interrupt so that it doesn't call the ISR again */
		maskBeaconInterrupt(true);
	}
	//printf("B\r\n");
}
Пример #6
0
/****************************************************************************
 Function
   Check4Keystroke
 Parameters
   None
 Returns
   bool: true if a new key was detected & posted
 Description
   checks to see if a new key from the keyboard is detected and, if so, 
   retrieves the key and posts an ES_NewKey event to TestHarnessService0
 Notes
   The functions that actually check the serial hardware for characters
   and retrieve them are assumed to be in ES_Port.c
   Since we always retrieve the keystroke when we detect it, thus clearing the
   hardware flag that indicates that a new key is ready this event checker 
   will only generate events on the arrival of new characters, even though we
   do not internally keep track of the last keystroke that we retrieved.
 Author
   J. Edward Carryer, 08/06/13, 13:48
****************************************************************************/
bool Check4Keystroke(void)
{
  if ( IsNewKeyReady() ) // new key waiting?
  {
    ES_Event ThisEvent;
    
    uint8_t keyPressed = GetNewKey();
    // test distribution list functionality by sending the 'L' key out via
    // a distribution list.
		
		//PostToStrategy
		if(keyPressed == 'a')
		{
			ThisEvent.EventType = GET_KEYPRESS_STATUS;
			PostStrategySM(ThisEvent);
		}
		
		if(keyPressed == 'c')
		{
			ThisEvent.EventType = POTENTIAL_POLL_STATION;
			PostStrategySM(ThisEvent);
		}
		else if(keyPressed == 'g')
		{
			ThisEvent.EventType = FOLLOW_LINE;
			PostStrategySM(ThisEvent);
		}
		else if(keyPressed == 's')
		{
			ThisEvent.EventType = STOP_NOW_CHANGE_TO_CAPTURE;
			PostStrategySM(ThisEvent);
		}
		
		else if (keyPressed == 'p')
		{
			//Activate shooter
			ThisEvent.EventType = SHOOT_NOW;
			PostStrategySM(ThisEvent);
		}
		
		/*
		else if(keyPressed == 'h')
		{
			ThisEvent.EventType = EOT_TIMEOUT;
			PostStatusPAC(ThisEvent);
		}
		else if(keyPressed == 'g')
		{
			ThisEvent.EventType = ES_TIMEOUT;
			PostStatusPAC(ThisEvent);
		}*/
		
		/*else if(keyPressed == 'r')
		{
			ThisEvent.EventType = CAPTURE_STATION_NOW;
			ThisEvent.EventParam = 9;
			PostCampaigningSM(ThisEvent);
		}*/
		
    return true;
  }
  return false;
}
/****************************************************************************
 Function
    RunSendBytesStatus

 Parameters
   ES_Event: the event to process

 Returns
   ES_Event: an event to return

 Description
   add your description here
 Notes
   uses nested switch/case to implement the machine.
 Author
   J. Edward Carryer, 2/11/05, 10:45AM
****************************************************************************/
ES_Event RunSendBytesStatus( ES_Event CurrentEvent )
{
   bool MakeTransition = false;/* are we making a state transition? */
   SendBytesState_t NextState = CurrentState;
   ES_Event EntryEventKind = { ES_ENTRY, 0 };// default to normal entry to new state
   ES_Event ReturnEvent = CurrentEvent; // assume we are not consuming event

   switch ( CurrentState )
   {
       case WAITING_SENDBYTES :       // If current state is WAITING
         // Execute During function for WAITING. ES_ENTRY & ES_EXIT are
         // processed here allow the lower level state machines to re-map
         // or consume the event
         CurrentEvent = DuringWaiting(CurrentEvent);
         //process any events
         if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active
         {
            switch (CurrentEvent.EventType)
            {
               case RETURN_STATUS : //If event is RETURN_STATUS
                  // Execute action function for state one : event one
                  NextState = SENDING_SENDBYTES;//Decide what the next state will be
                  // for internal transitions, skip changing MakeTransition
                  MakeTransition = true; //mark that we are taking a transition
                  // if transitioning to a state with history change kind of entry
                  EntryEventKind.EventType = ES_ENTRY;
                  // optionally, consume or re-map this event for the upper
                  // level state machine
                  ReturnEvent.EventType = ES_NO_EVENT;
							 
									//This is a status request
									isStatus = true;
							 
									PostStatusPAC(CurrentEvent);
                  break;
							 
							 case RETURN_REQUEST : //If event is RETURN_STATUS
                  // Execute action function for state one : event one
                  NextState = SENDING_SENDBYTES;//Decide what the next state will be
                  // for internal transitions, skip changing MakeTransition
                  MakeTransition = true; //mark that we are taking a transition
                  // if transitioning to a state with history change kind of entry
                  EntryEventKind.EventType = ES_ENTRY;
                  // optionally, consume or re-map this event for the upper
                  // level state machine
                  ReturnEvent.EventType = ES_NO_EVENT;
							 
							 
									PostStatusPAC(CurrentEvent);
                  break;
							 
							 case RETURN_QUERY : //If event is RETURN_STATUS
                  // Execute action function for state one : event one
                  NextState = SENDING_SENDBYTES;//Decide what the next state will be
                  // for internal transitions, skip changing MakeTransition
                  MakeTransition = true; //mark that we are taking a transition
                  // if transitioning to a state with history change kind of entry
                  EntryEventKind.EventType = ES_ENTRY;
                  // optionally, consume or re-map this event for the upper
                  // level state machine
                  ReturnEvent.EventType = ES_NO_EVENT;
							 
							 
									PostStatusPAC(CurrentEvent);
                  break;
                             case RETURN_PSEUDO_QUERY : //If event is RETURN_STATUS
                  // Execute action function for state one : event one
                  NextState = SENDING_SENDBYTES;//Decide what the next state will be
                  // for internal transitions, skip changing MakeTransition
                  MakeTransition = true; //mark that we are taking a transition
                  // if transitioning to a state with history change kind of entry
                  EntryEventKind.EventType = ES_ENTRY;
                  // optionally, consume or re-map this event for the upper
                  // level state machine
                  ReturnEvent.EventType = ES_NO_EVENT;
							 
                                    isPseudo = true;
									PostStatusPAC(CurrentEvent);
                  break;    

            }
         }
         break;

	   case SENDING_SENDBYTES:       // If current state is SENDING
         // Execute During function for SENDING. ES_ENTRY & ES_EXIT are
         // processed here allow the lower level state machines to re-map
         // or consume the event
         CurrentEvent = DuringSending(CurrentEvent);
					uint8_t freqCode;
         //process any events
         if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active
         {
            switch (CurrentEvent.EventType)
            {
               case RETURN_STATUS : //If event is GET_STATUS
								 
									#ifdef DEBUG_SENDBYTES
									printf("\rSending status\r\n");
									#endif
								 
									for (uint8_t i = 0; i < 5; i++)
									{
										WriteFIFO(getStatus[i]);
										#ifdef DEBUG_SENDBYTES
									  printf("%x\r\n",getStatus[i]);
										#endif
									}

									//Enable the NVIC interrupt for the SSI when starting to transmit (or receive?)
									HWREG(NVIC_BASE+EN0) |= BIT7HI; //Interrupt 7 for SSI0

                  // Execute action function for state one : event one
                  NextState = WAITING4EOT;//Decide what the next state will be
                  // for internal transitions, skip changing MakeTransition
                  MakeTransition = true; //mark that we are taking a transition
                  // if transitioning to a state with history change kind of entry
                  EntryEventKind.EventType = ES_ENTRY;
                  // optionally, consume or re-map this event for the upper
                  // level state machine
                  ReturnEvent.EventType = ES_NO_EVENT;
                  break;
							 
							 case RETURN_REQUEST : //If event is GET_STATUS
								 
									freqCode = (uint8_t)CurrentEvent.EventParam;
									uint8_t myColor = getMyColor();
									uint8_t colorCode = ((myColor << 4) | (myColor << 5));
									getRequest[0] = 0x80 | freqCode | colorCode;
							 
									#ifdef DEBUG_SENDBYTES
									printf("\rCurrent Request\r\n");
									#endif
									for (uint8_t i = 0; i < 5; i++)
									{
										WriteFIFO(getRequest[i]);
										#ifdef DEBUG_SENDBYTES
										printf("%x\r\n",getRequest[i]);
										#endif
									}
									//Enable the NVIC interrupt for the SSI when starting to transmit (or receive?)
									HWREG(NVIC_BASE+EN0) |= BIT7HI; //Interrupt 7 for SSI0
                  // Execute action function for state one : event one
                  NextState = WAITING4EOT;//Decide what the next state will be
                  // for internal transitions, skip changing MakeTransition
                  MakeTransition = true; //mark that we are taking a transition
                  // if transitioning to a state with history change kind of entry
                  EntryEventKind.EventType = ES_ENTRY;
                  // optionally, consume or re-map this event for the upper
                  // level state machine
                  ReturnEvent.EventType = ES_NO_EVENT;
                  break;
							 
							 case RETURN_QUERY : //If event is GET_STATUS
								 
									#ifdef DEBUG_SENDBYTES
									printf("\rSending\r\n");
									#endif
									for (uint8_t i = 0; i < 5; i++)
									{
										WriteFIFO(getQuery[i]);
										#ifdef DEBUG_SENDBYTES
									  printf("%x\r\n",getQuery[i]);
										#endif
									}
									//Enable the NVIC interrupt for the SSI when starting to transmit (or receive?)
									HWREG(NVIC_BASE+EN0) |= BIT7HI; //Interrupt 7 for SSI0
                  // Execute action function for state one : event one
                  NextState = WAITING4EOT;//Decide what the next state will be
                  // for internal transitions, skip changing MakeTransition
                  MakeTransition = true; //mark that we are taking a transition
                  // if transitioning to a state with history change kind of entry
                  EntryEventKind.EventType = ES_ENTRY;
                  // optionally, consume or re-map this event for the upper
                  // level state machine
                  ReturnEvent.EventType = ES_NO_EVENT;
                  break;
                            
                                case RETURN_PSEUDO_QUERY : //If event is GET_STATUS
								 
									#ifdef DEBUG_SENDBYTES
									printf("\rSending\r\n");
									#endif
									for (uint8_t i = 0; i < 5; i++)
									{
										WriteFIFO(getQuery[i]);
										#ifdef DEBUG_SENDBYTES
									  printf("%x\r\n",getQuery[i]);
										#endif
									}
									//Enable the NVIC interrupt for the SSI when starting to transmit (or receive?)
									HWREG(NVIC_BASE+EN0) |= BIT7HI; //Interrupt 7 for SSI0
                  // Execute action function for state one : event one
                  NextState = WAITING4EOT;//Decide what the next state will be
                  // for internal transitions, skip changing MakeTransition
                  MakeTransition = true; //mark that we are taking a transition
                  // if transitioning to a state with history change kind of entry
                  EntryEventKind.EventType = ES_ENTRY;
                  // optionally, consume or re-map this event for the upper
                  // level state machine
                  ReturnEvent.EventType = ES_NO_EVENT;
                  break;
                              
            }
         }
         break;
		 
	   case WAITING4EOT :       // If current state is WAITING4EOT
         // Execute During function for SENDING. ES_ENTRY & ES_EXIT are
         // processed here allow the lower level state machines to re-map
         // or consume the event
         CurrentEvent = DuringWaiting4EOT(CurrentEvent);
         //process any events
         if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active
         {
            switch (CurrentEvent.EventType)
            {
               case EOT_TIMEOUT : //If event is GET_STATUS
								#ifdef DEBUG_SENDBYTES
								if(!isStatus)
									{printf("\rReading result\r\n");
									}
									#endif
									for (uint8_t i = 0; i < 5; i++)
									{
										readdata[i] = ReadFIFO();
										#ifdef DEBUG_SENDBYTES
										if(!isStatus){printf("\r%x\r\n",readdata[i]);}
										#endif
									}
								
                  // Execute action function for state one : event one
                  NextState = WAITING4TIMEOUT;//Decide what the next state will be
                  // for internal transitions, skip changing MakeTransition
                  MakeTransition = true; //mark that we are taking a transition
                  // if transitioning to a state with history change kind of entry
                  EntryEventKind.EventType = ES_ENTRY;
                  // optionally, consume or re-map this event for the upper
                  // level state machine
                  ReturnEvent.EventType = ES_NO_EVENT;
									ES_Timer_InitTimer(EOTTimer,2);
                  break;
            }
         }
         break;
		 
	   case WAITING4TIMEOUT :       // If current state is WAITING4TIMEOUT
         // Execute During function for SENDING. ES_ENTRY & ES_EXIT are
         // processed here allow the lower level state machines to re-map
         // or consume the event
         CurrentEvent = DuringWaiting4Timeout(CurrentEvent);
         //process any events
         if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active
         {
            switch (CurrentEvent.EventType)
            {   
							case ES_TIMEOUT:
								// Execute action function for state one : event one
								NextState =  WAITING_SENDBYTES;//Decide what the next state will be
								// for internal transitions, skip changing MakeTransition
								MakeTransition = true; //mark that we are taking a transition
								// if transitioning to a state with history change kind of entry
								EntryEventKind.EventType = ES_ENTRY;
								// optionally, consume or re-map this event for the upper
								// level state machine
								ES_Event CompletedEvent;
							
								
								CompletedEvent.EventType = PAC_COMM_COMPLETE;
								
								for (uint8_t i= 0; i < 5; i++)
								{
									CompletedEvent.EventArray[i] = readdata[i];
								}
								
								//If this is a status request, send back to only Strategy
								//Otherwise, send to only campaigning
								if(isStatus)
								{

									PostStrategySM(CompletedEvent);
									
									isStatus = false;
								}
                                else if(isPseudo)
								{
									isPseudo = false;
								}
								else
								{
									//return or query request returned to 
									//campagin

									PostCampaigningSM(CompletedEvent);
									
								}
			
								break;
							}
         }
         break;
      // repeat state pattern as required for other states
    }
    //   If we are making a state transition
    if (MakeTransition == true)
    {
       //   Execute exit function for current state
       CurrentEvent.EventType = ES_EXIT;
       RunSendBytesStatus(CurrentEvent);

       CurrentState = NextState; //Modify state variable

       //   Execute entry function for new state
       // this defaults to ES_ENTRY
       RunSendBytesStatus(EntryEventKind);
     }
     return(ReturnEvent);
}