예제 #1
0
void Tick_Fct(){
	distance = read_ultrasonic();
	
	if (USART_HasReceived(0))
	{
		direction = USART_Receive(0);
		PORTC = direction;
	}
	
	switch(state){//transitions
		case init:
			state = start;
			break;
		
		case start:
			if(direction == 0xFF){
				state = wait_impact;
				}else{
				state = controlled;
			}break;
			
		case controlled:
			if(direction == 0xFF){
				state = wait_impact;
			}else{
				count_1 = 0;	
				state = hold_direction;
			}break;
		
		case hold_direction:
			if(count_1 < 30){
				state = hold_direction;
			}else{
				state = reset_direction;
			}
			break;
		
		case reset_direction:
			state = controlled;
			break;
			
		case wait_impact:
			if (direction != 0xFF)
			{
				state = controlled;
			}
			else if(direction == 0xFF && distance > 30){
				state = wait_impact;
				}else {
				state = stop_car;
				count  = 0;
				PORTA = 0x00;
			}
			break;
		
		case stop_car:
			if (direction != 0xFF)
			{
				state = controlled;
			}
			else if(direction == 0xFF && count < 100){
				state = stop_car;
			}
			else if(direction == 0xFF && !(count < 100)){
				state = turn_left;
				count = 0;
			}
			break;
		
		case turn_left:
			if (direction != 0xFF)
			{
				state = controlled;
			}
			else if(direction == 0xFF && count < 100){
				state = turn_left;
			}
			else if(direction == 0xFF && !(count < 100)){
				state = wait_impact;
			}
			break;
		
		default:
			state = init;
			break;
	}
	
	switch(state){//actions
		case wait_impact:
			PORTA = 0x04;
			break;
		
		case stop_car:
			count++;
			PORTA = 0x42;
			break;
		
		case turn_left:
			count++;
			PORTA = 0x24;
			break;
		
		case start:
			PORTA = 0x00;
			break;
		
		case controlled:
			if(direction == 0x0A){
				PORTA = 0x00;
			}else if (direction == 0x01){
				PORTA = 0x44;//forward
			}else if (direction == 0x02){//reverse or backward
				PORTA = 0x42;
			}else if (direction == 0x04){//left
				PORTA = 0x24;
			}else if(direction == 0x08){//right
				PORTA = 0x44;
			}
			break;
			
		case hold_direction:
			count_1++;
			break;
		case reset_direction:
			PORTA = 0x00;
			break;
			
		default:
			break;
	}
}
예제 #2
0
//Main function to proccess the timers and events 
uint16 ParkWay_ProcessEvent( uint8 task_id, uint16 events )
{
  afIncomingMSGPacket_t *MSGpkt;
  //afDataConfirm_t *afDataConfirm;
  zAddrType_t dstAddr;

  // Data Confirmation message fields
  //byte sentEP;
  //ZStatus_t sentStatus;
  //byte sentTransID;       // This should match the value sent
  (void)task_id;  // Intentionally unreferenced parameter

  if ( events & SYS_EVENT_MSG )
  {
    MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( ParkWay_TaskID );
    while ( MSGpkt )
    {
      switch ( MSGpkt->hdr.event )
      {
        case ZDO_CB_MSG: //Event trigger by ZigBee Device Object like connect, bind...etc
          ParkWay_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
          break;

        case AF_DATA_CONFIRM_CMD: //Event trigger by a confirmation of sent message
          // This message is received as a confirmation of a data packet sent.
          // The status is of ZStatus_t type [defined in ZComDef.h]
          // The message fields are defined in AF.h
          // afDataConfirm = (afDataConfirm_t *)MSGpkt;
          // sentEP = afDataConfirm->endpoint;
          // sentStatus = afDataConfirm->hdr.status;
          // sentTransID = afDataConfirm->transID;
          // (void)sentEP;
          // (void)sentTransID;

          // // Action taken when confirmation is received.
          // if ( sentStatus != ZSuccess )
          // {
          //   // The data wasn't delivered -- Do something
          // }
          break;

        case AF_INCOMING_MSG_CMD: //Event trigger by a receive message
          ParkWay_MessageMSGCB( MSGpkt );
          break;

        case ZDO_STATE_CHANGE: //Event trigger by a connection to the coordinator or Router
          ParkWay_NwkState = (devStates_t)(MSGpkt->hdr.status);
          if ( (ParkWay_NwkState == DEV_ZB_COORD)
              || (ParkWay_NwkState == DEV_ROUTER)
              || (ParkWay_NwkState == DEV_END_DEVICE) )
          {
            // Start sending the status message in a regular interval.
            osal_start_timerEx( ParkWay_TaskID,
                                ParkWay_SEND_MSG_EVT,
                                ParkWay_SEND_MSG_TIMEOUT );
            // Start reading ultrasonic in a regular interval.
            osal_start_timerEx( ParkWay_TaskID,
                                POLLING_ULTRASONIC,
                                TIME_ULTRA);
          }
          break;

        default:
          break;
      }

      // Release the memory
      osal_msg_deallocate( (uint8 *)MSGpkt );

      // Next
      MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( ParkWay_TaskID );
    }

    // return unprocessed events
    return (events ^ SYS_EVENT_MSG);
  }

  // Send to the coordinator the status of ultrasound
  if ( events & ParkWay_SEND_MSG_EVT )
  {
    // Send "the" message
    ParkWay_SendTheMessage();
    
    // Setup to send message again
    osal_start_timerEx( ParkWay_TaskID,
                        ParkWay_SEND_MSG_EVT,
                        ParkWay_SEND_MSG_TIMEOUT );

    // return unprocessed events
    return (events ^ ParkWay_SEND_MSG_EVT);
  }
  
  if(events & POLLING_ULTRASONIC)
  {
    osal_start_timerEx( ParkWay_TaskID,
                        POLLING_ULTRASONIC,
                        TIME_ULTRA);
    read_ultrasonic();
  }
  
  if(events & POLLING_OF_DEV_EVT) //This force the auto bind to the coordinator or Router
  {
    //HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON);
    // Initiate a Match Description Request (Service Discovery)
    dstAddr.addrMode = AddrBroadcast;
    dstAddr.addr.shortAddr = NWK_BROADCAST_SHORTADDR;
    ZDP_MatchDescReq( &dstAddr, NWK_BROADCAST_SHORTADDR,
                      ParkWay_PROFID,
                      ParkWay_MAX_CLUSTERS, (cId_t *)ParkWay_ClusterList,
                      ParkWay_MAX_CLUSTERS, (cId_t *)ParkWay_ClusterList,
                      FALSE );
    osal_start_timerEx( ParkWay_TaskID,
                        POLLING_OF_DEV_EVT,
                        TIME_TO_CONNECT_COORD);
  } 


  HalLedSet( HAL_LED_ALL, HAL_LED_MODE_OFF ); //Shutdown all LEDS to reduce consumption of power
  
  // Discard unknown events
  return 0;
}