xenomai rt_sem 信号量

  
  
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/mman.h>
 
#include <native/task.h>
#include <native/timer.h>
#include <native/sem.h>
  
#include <rtdk.h>
#include "program.h"
RT_TASK task ;
RT_SEM readDone ;
RT_SEM executionDone ;
RT_SEM writeDone ;
short stopped = 0 ;
void * sensors ;
void * actuators ;
void sense ( void * args );
void execute ( void * args );
void act ( void * agrs );
void sense ( void * args ){
rt_task_set_periodic ( NULL , TM_NOW , PLCperiod );
while ( ! stopped ){
/*
* Read inputs status
*/
sensors = readInputs ();
rt_sem_v ( & readDone );
rt_task_wait_period ( NULL );
}
}
void act ( void * args ){
rt_task_set_periodic ( NULL , TM_NOW , PLCperiod );
while ( ! stopped ){
rt_sem_p ( & executionDone , 0 );
/*
* Write outputs status
*/
writeOutputs ( actuators );
rt_sem_v ( & writeDone );
rt_task_wait_period ( NULL );
}
}
void executor ( void * args ){
rt_task_set_periodic ( NULL , TM_NOW , PLCperiod );
short step = ( short ) * args ;
actuators = 0 ; // how to initialize the temporary value of actuators?
while ( ! stopped ){
rt_sem_p ( & readDone , 0 );
int c ;
for ( c = 0 ; c < nStep ; c ++ ){
if ( stepStatus [ c ])
step [ c ]( sensors ); // or equivalent
}
rt_sem_v ( & executionDone );
rt_sem_p ( & writeDone , 0 );
for ( c = 0 ; c < nStep ; c ++ ){
if ( stepStatus [ c ])
condition [ c ]( sensor , actuators );
}
rt_task_wait_period ( NULL );
}
return ;
}
void init_xenomai () {
// Perform auto-init of rt_print buffers if the task doesn't do so
rt_print_auto_init ( 1 );
  // Avoids memory swapping for this program
mlockall ( MCL_CURRENT | MCL_FUTURE );
}
void startup (){
stepInitializzator ();
rt_sem_create ( & readDone , "readSem" , 0 , S_PRIO );
rt_sem_create ( & executionDone , "executionSem" , 0 , S_PRIO );
rt_sem_create ( & writeDone , "writeSem" , 0 , S_PRIO );
rt_task_create ( & task , "readerTask" , 0 , 99 , 0 );
rt_task_start ( & task , & reader , NULL );
rt_task_create ( & task , "executorTask" , 0 , 98 , 0 );
rt_task_start ( & task , & executor , NULL );
rt_task_create ( & task , "writerTask" , 0 , 97 , 0 );
rt_task_start ( & task , & writer , NULL );
}
void catch_signal ( int sig ) {
stopped = 1 ;
rt_printf ( "--should I exit?-- \n " );
//exit(0);
}
void wait_for_ctrl_c () {
signal ( SIGTERM , catch_signal );
signal ( SIGINT , catch_signal );
// wait for SIGINT (CTRL-C) or SIGTERM signal
pause ();
}
void cleanup (){
rt_printf ( "cleaned \n " );
}
int main ( int argn , char ** argv ){
init_xenomai ();
// rt_printf("initialized.\n");
// rt_printf("starting up...\n");
startup ();
rt_printf ( "waiting Ctrl+C \n " );
wait_for_ctrl_c ();
rt_printf ( "closing... \n " );
cleanup ();
return 0 ;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值