📄 homework.c
字号:
/*A program to control the moving of a mechanical arm's four joins*/
#include<vxworks.h>
#include<stdio.h>
#include<semLib.h>
#include<taskLib.h>
#include<wdLib.h>
#include<msgQLib.h>
#include<eventLib.h>
#include<semEvLib.h>
/**********************************************************************************************/
/*Define the semaphore ID*/
SEM_ID shoulder_id=NULL;
SEM_ID elbow_id=NULL;
SEM_ID wrist_id=NULL;
SEM_ID knuckle_id=NULL;
/*Define the time out ID*/
SEM_ID time_out_id=NULL;
/*Define the watch dog ID*/
WDOG_ID watch_dog_id;
/*Define the message ID*/
MSG_Q_ID msg_q_shoulder_id;
MSG_Q_ID msg_q_elbow_id;
MSG_Q_ID msg_q_wrist_id;
MSG_Q_ID msg_q_knuckle_id;
/*Define the message to be printed out*/
#define message_shoulder "I'm shoulder "
#define message_elbow "I'm elbow "
#define message_wrist "I'm wrist "
#define message_knuckle "I'm knuckle "
/*Functions to control the working*/
void sem_function(void);
void msg_function(void);
void event_function(void);
void time_out_function(void);
/*Functions to serve the watchDog*/
void sem_wDog_service_function(void);
void msg_wDog_service_function(void);
void event_wDog_service_function(void);
void timeOut_wDog_service_function(void);
/*Function to delete the tasks*/
int delete_tasks(void);
/*Define five tasks*/
STATUS tShoulder(void);
STATUS tElbow(void);
STATUS tWrist(void);
STATUS tKnuckle(void);
STATUS main_control_task(void);
int t0;
int t1;
int t2;
int t3;
int t4;
/*Count the loops to be carried out*/
int loops;
/*"sem_count,msg_coun,tevent_count,timeOut_count" define what times carried out in a loop*/
/*"sem_flag, msg_flag,event_flag,timeOut_flag " are flags to control the working of control task*/
int sem_count;
int sem_flag;
int msg_count;
int msg_flag;
int event_count;
int event_flag;
int timeOut_count;
int timeOut_flag;
/*To control witch part work in a joint task*/
int working_select;
/*Define buffers for event*/
UINT32 shoulder_buffer,wrist_buffer,elbow_buffer,knuckle_buffer;
/*Define buffers for message*/
char mes0[15];
char mes1[15];
char mes2[15];
char mes3[15];
/**************************************************************************************************/
/*Initialize function to enter the program*/
int initialize_function(void)
{
shoulder_id=semBCreate(SEM_Q_PRIORITY,SEM_EMPTY);
elbow_id=semBCreate(SEM_Q_PRIORITY,SEM_EMPTY);
wrist_id=semBCreate(SEM_Q_PRIORITY,SEM_EMPTY);
knuckle_id=semBCreate(SEM_Q_PRIORITY,SEM_EMPTY);
time_out_id=semBCreate(SEM_Q_PRIORITY,SEM_EMPTY);
msg_q_shoulder_id=msgQCreate(5,30,MSG_Q_FIFO);
msg_q_elbow_id=msgQCreate(5,30,MSG_Q_FIFO);
msg_q_wrist_id=msgQCreate(5,30,MSG_Q_FIFO);
msg_q_knuckle_id=msgQCreate(5,30,MSG_Q_FIFO);
watch_dog_id=wdCreate();//Creat watch dog
sem_flag=0;
sem_count=30;//30 times to be carried out in a loop by using semaphore
msg_flag=0;
msg_count=30;//30 times to be carried out in a loop by using message
event_flag=0;
event_count=30;//30 times to be carried out in a loop by using event
timeOut_flag=0;
timeOut_count=30;//30 times to be carried out in a loop by using time out
working_select=0;//First using semaphore to control
loops=3;//loops to be carried out
t0=taskSpawn("watchDog",210,0,1024,(FUNCPTR)main_control_task,0,0,0,0,0,0,0,0,0,0);
t1=taskSpawn("shoulder",200,0,1024,(FUNCPTR)tShoulder,0,0,0,0,0,0,0,0,0,0);
t2=taskSpawn("elbow",200,0,1024,(FUNCPTR)tElbow,0,0,0,0,0,0,0,0,0,0);
t3=taskSpawn("wrist",200,0,1024,(FUNCPTR)tWrist,0,0,0,0,0,0,0,0,0,0);
t4=taskSpawn("knuckle",200,0,1024,(FUNCPTR)tKnuckle,0,0,0,0,0,0,0,0,0,0);
printf("Hello,the mechanical arm is working now!\n");
if(shoulder_id==NULL) // creat semaphore failed
{
return ERROR;
}
if(msg_q_shoulder_id==NULL) // creat message failed
{
return ERROR;
}
if(watch_dog_id==NULL) //creat watch dog failed
{
return ERROR;
}
return OK;
}
/*********************************************************************************************************/
/*A function in main task to control the arm's working by using semaphore*/
void sem_function()
{
semGive(shoulder_id);
semGive(elbow_id);
semGive(wrist_id);
semGive(knuckle_id);
}
/*A function in main task to control the arm's working by using message*/
void msg_function()
{
msgQSend(msg_q_shoulder_id,message_elbow,sizeof(message_elbow),WAIT_FOREVER,MSG_PRI_NORMAL);
msgQSend(msg_q_elbow_id,message_wrist,sizeof(message_wrist),WAIT_FOREVER,MSG_PRI_NORMAL);
msgQSend(msg_q_wrist_id,message_knuckle,sizeof(message_knuckle),WAIT_FOREVER,MSG_PRI_NORMAL);
msgQSend(msg_q_knuckle_id,message_shoulder,sizeof(message_shoulder),WAIT_FOREVER,MSG_PRI_NORMAL);
}
/*A function in main task to control the arm's working by using event*/
void event_function()
{
semGive(wrist_id);
semTake(wrist_id,NO_WAIT);
semGive(knuckle_id);
semTake(knuckle_id,NO_WAIT);
semGive(shoulder_id);
semTake(shoulder_id,NO_WAIT);
semGive(elbow_id);
semTake(elbow_id,NO_WAIT);
}
/*A function in main task to control the arm's working by using time out*/
void time_out_function()
{
semFlush(time_out_id);
}
/***********************************************************************************************************/
/*The main control task*/
STATUS main_control_task()
{
while(1)
{
printf("########################This is the: ");
printf("%d",4-loops);
printf(" loops######################\n");
printf("\n");
printf("******the first minute shoulder is working ******\n");
/* Using semaphore to control the mechanical arm */
while(sem_count>=1) // Count out times using semaphore
{
if(sem_flag==0) // Using a flag to control the watch dog
{
sem_flag=1;
wdStart(watch_dog_id,60,(FUNCPTR)sem_wDog_service_function,0); // start watch dog
sem_function();
if(sem_count==1) // Detect when to jump out of the loop
break;
}
}
printf("\n");
printf("******the second minute elbow is working ******\n");
/* Using message to control the mechanical arm */
while(msg_count>=1) // Count out times using message
{
if(msg_flag==0) // Using a flag to control the watch dog
{
msg_flag=1;
wdStart(watch_dog_id,60,(FUNCPTR)msg_wDog_service_function,0); // start watch dog
msg_function();
if(msg_count==1) // Detect when to jump out of the loop
break;
}
}
printf("\n");
printf("******the third minute wrist is working ******\n");
/* Using event to control the mechanical arm */
while(event_count>=1) // Count out times using event
{
if(event_flag==0) // Using a flag to control the watch dog
{
event_flag=1;
wdStart(watch_dog_id,60,(FUNCPTR)event_wDog_service_function,0); // start watch dog
event_function();
if(event_count==1) // Detect when to jump out of the loop
break;
}
}
printf("\n");
printf("******the forth minute knuckle is working ******\n");
/* Using timeout to control the mechanical arm */
while(timeOut_count>=1) // Count out times when overtime
{
if(timeOut_flag==0) // Using a flag to control the watch dog
{
timeOut_flag=1;
wdStart(watch_dog_id,60,(FUNCPTR)timeOut_wDog_service_function,0);// start watch dog
time_out_function();
taskDelay(30);
if(timeOut_count==1) // Detect when to jump out of the loop
break;
}
}
//Reset the counters
sem_count=30;
msg_count=30;
event_count=30;
timeOut_count=30;
//Reset the flags
sem_flag=0;
msg_flag=0;
event_flag=0;
timeOut_flag=0;
//If the the system finish,release resources
if(loops==0)
{
delete_tasks();
break;
}
loops--;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -