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

📄 homework.c

📁 这是上学期在Vxworks嵌入式系统上做的一个作业
💻 C
📖 第 1 页 / 共 2 页
字号:
}


/*********************************************************************************************************/
/*The function to serve the watch dog with semaphore*/
void sem_wDog_service_function(void)
{
    sem_flag=0;   //reset sem_flag
    sem_count--;  
    printf("\n");
}

/*The function to serve the watch dog with message*/
void msg_wDog_service_function(void)
{
    msg_flag=0;   //reset msg_flag
    msg_count--;
    printf("\n");
}

/*The function to serve the watch dog with event*/
void event_wDog_service_function(void)
{
    event_flag=0; //reset event_flag
    event_count--;
    printf("\n");
}

/*The function to serve the watch dog with overtime*/
void timeOut_wDog_service_function(void)
{
    timeOut_flag=0;  //reset timeOut_flag
    timeOut_count--;
    printf("\n");
}


/***********************************************************************************************************/
/*Task to control the shoulder*/
STATUS tShoulder(void)
{
  while(1)
  { 
     /*Detect the "working_select" flag,if flag==0,using semaphore to control the shoulder
     if flag==1,using message to control the shoulder
     if flag==2,using event to control the shouler
     if flag==3,using overtime to control the shoulder*/
     switch(working_select)  
	 {
         case 0:
                 semTake(shoulder_id,WAIT_FOREVER);  //Waite for semaphore
                 printf("%s\n",message_shoulder);    
                 if(sem_count==1)
                 working_select=1;                   //Reset the flag,so that next time using message to control the shoulder
                 break;
         case 1:      
                 msgQReceive(msg_q_knuckle_id,mes0,sizeof(mes0),WAIT_FOREVER);  //waite for message
                 printf("%s\n",mes0);
                 if(msg_count==1)
                 working_select=2;                   //Reset the flag,so that next time using event to control the shoulder
                 break;
          case 2:         
                 semEvStart(shoulder_id,VXEV01,EVENTS_OPTIONS_NONE);   //waite for event
                 eventReceive(VXEV01,EVENTS_WAIT_ANY,WAIT_FOREVER,&shoulder_buffer);
                 if((shoulder_buffer&VXEV01)!=0)
                 printf("%s\n",message_shoulder);
                 if(event_count==1)
                 working_select=3;                 //Reset the flag,so that next time  using overtime to control the shoulder
                 break;
          case 3:      
                 semTake(time_out_id,WAIT_FOREVER);    //waite for semaphore 
                 semTake(shoulder_id,10);              //waite for overtime
                 printf("%s\n",message_shoulder);
                 if(timeOut_count==1)
                 working_select=0;                  //Reset the flag,so that next time using semaphore to control the shoulder
                 break;
          default:break; 
   
	 }

  }

}



STATUS tElbow(void)
{
    /*Detect the "working_select" flag,if flag==0,using semaphore to control the elbow
     if flag==1,using message to control the elbow
     if flag==2,using event to control the elbow
     if flag==3,using overtime to control the elbow*/
    while(1)
	{
       switch(working_select)
	   {
         case 0:             
                semTake(elbow_id,WAIT_FOREVER);   //Waite for semaphore
                printf("%s\n",message_elbow);
                if(sem_count==1)
                working_select=1;                 //Reset the flag,so that next time using message to control the elbow
                break;
         case 1:
                 msgQReceive(msg_q_shoulder_id,mes1,sizeof(mes1),WAIT_FOREVER);   //waite for message
                 printf("%s\n",mes1);
                 if(msg_count==1)
                 working_select=2;               //Reset the flag,so that next time using event to control the elbow
                 break;
          case 2:      
                  semEvStart(elbow_id,VXEV01,EVENTS_OPTIONS_NONE);     //waite for event
                  eventReceive(VXEV01,EVENTS_WAIT_ANY,WAIT_FOREVER,&elbow_buffer);
                  if((elbow_buffer&VXEV01)!=0)
                  printf("%s\n",message_elbow);
                  if(event_count==1)
                  working_select=3;               //Reset the flag,so that next time  using overtime to control the elbow
                  break;
          case 3:    
                  semTake(time_out_id,WAIT_FOREVER);     //waite for semaphore 
                  semTake(elbow_id,15);                  //waite for overtime
                  printf("%s\n",message_elbow);
                  if(timeOut_count==1)
                  working_select=0;                      //Reset the flag,so that next time using semaphore to control the elbow
                  break;
           default: break;
	   }

	}
}

STATUS tWrist(void)
{
    while(1)
	{
          /*Detect the "working_select" flag,if flag==0,using semaphore to control the wrist
           if flag==1,using message to control the wrist
           if flag==2,using event to control the wrist
           if flag==3,using overtime to control the wrist*/
     switch(working_select)
	 {

         case 0:     
                semTake(wrist_id,WAIT_FOREVER);    //Waite for semaphore
                printf("%s\n",message_wrist);
                if(sem_count==1)
                working_select=1;                  //Reset the flag,so that next time using message to control the wrist
                break;
          case 1:     
                msgQReceive(msg_q_elbow_id,mes2,sizeof(mes2),WAIT_FOREVER);      //waite for message
                printf("%s\n",mes2);
                if(msg_count==1)
                working_select=2;                   //Reset the flag,so that next time using event to control the wrist
                break;
          case 2:    
                semEvStart(wrist_id,VXEV01,EVENTS_OPTIONS_NONE);    //waite for event
                eventReceive(VXEV01,EVENTS_WAIT_ANY,WAIT_FOREVER,&wrist_buffer);
                if((wrist_buffer&VXEV01)!=0)
                printf("%s\n",message_wrist);
                if(event_count==1)
                working_select=3;                   //Reset the flag,so that next time  using overtime to control the wrist
                break;
           case 3:       
                 semTake(time_out_id,WAIT_FOREVER);  //waite for semaphore 
                 semTake(wrist_id,20);               //waite for overtime
                 printf("%s\n",message_wrist);
                 if(timeOut_count==1)
                 working_select=0;                   //Reset the flag,so that next time using semaphore to control the wrist
                 break;
           default: break;
	 }
	}
}

STATUS tKnuckle(void)
{
   while(1)
   {
       /*Detect the "working_select" flag,if flag==0,using semaphore to control the knuckle
         if flag==1,using message to control the knuckle
         if flag==2,using event to control the knuckle
         if flag==3,using overtime to control the knuckle*/
     switch(working_select)
	 {
       case 0:           
              semTake(knuckle_id,WAIT_FOREVER);     //Waite for semaphore
              printf("%s\n",message_knuckle);
              if(sem_count==1)
              working_select=1;                     //Reset the flag,so that next time using message to control the wrist
              break;
        case 1:         
              msgQReceive(msg_q_wrist_id,mes3,sizeof(mes3),WAIT_FOREVER);       //waite for message
              printf("%s\n",mes3);
              if(msg_count==1)
              working_select=2;                       //Reset the flag,so that next time using event to control the wrist
              break;
        case 2:      
               semEvStart(knuckle_id,VXEV01,EVENTS_OPTIONS_NONE);     //waite for event
               eventReceive(VXEV01,EVENTS_WAIT_ANY,WAIT_FOREVER,&knuckle_buffer);
               if((knuckle_buffer&VXEV01)!=0)
               printf("%s\n",message_knuckle);
               if(event_count==1)
               working_select=3;                       //Reset the flag,so that next time  using overtime to control the wrist
               break;
         case 3:         
               semTake(time_out_id,WAIT_FOREVER);      //waite for semaphore 
               semTake(knuckle_id,5);                  //waite for overtime
               printf("%s\n",message_knuckle);
               if(timeOut_count==1)
               working_select=0;                      //Reset the flag,so that next time using semaphore to control the wrist
               break;
         default: break;
	 }
   }
}


/********************************************************************************************************/
/* Delete tasks,semaphores,messages and watch dog to release resorces*/
int delete_tasks(void)
{
  /*Delete tasks*/
  if(taskDelete(t1)==ERROR)   
  {
   return ERROR;
  }
  if(taskDelete(t2)==ERROR)
  {
   return ERROR;
  }
  if(taskDelete(t3)==ERROR)
  {
   return ERROR;
  }
  if(taskDelete(t4)==ERROR)
  {
   return ERROR;
  }
   /*Delete semaphores*/
  if(semDelete(shoulder_id)==ERROR)
  {
   return ERROR;
  }
  if(semDelete(elbow_id)==ERROR)
  {
   return ERROR;
  }
  if(semDelete(wrist_id)==ERROR)
  {
   return ERROR;
  }
  if(semDelete(knuckle_id)==ERROR)
  {
   return ERROR; 
  }
  if(semDelete(time_out_id)==ERROR)
  {
   return ERROR;
  }
  /*Delete messages*/
  if(msgQDelete(msg_q_shoulder_id)==ERROR)
  {
   return ERROR;
  }
  if(msgQDelete(msg_q_elbow_id)==ERROR)
  {
   return ERROR;
  }
  if(msgQDelete(msg_q_wrist_id)==ERROR)
  {
   return ERROR;
  }
  if(msgQDelete(msg_q_knuckle_id)==ERROR)
  {
   return ERROR;
  }
  /*Delete watch dog*/
  if(wdDelete(watch_dog_id)==ERROR)
  {
   return ERROR;
  }
  return OK;
}

⌨️ 快捷键说明

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