安装xenomai实时补丁

参考网页: https://rtt-lwr.readthedocs.io/en/latest/rtpc/xenomai3.html

安装后的目录在 /usr/xenomai

在这里插入图片描述

编译用户程序

参考altency.c文件,编写一个周期任务程序:

demo_periodic_thread_posix.c

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/mman.h>
#include <pthread.h>
#include <alchemy/task.h>
#include <alchemy/timer.h>
#include <math.h>
#define CLOCK_RES 1e-9 //Clock resolution is 1 ns by default
#define LOOP_PERIOD 1e7 //Expressed in ticks
//RTIME period = 1000000000;
RT_TASK loop_task;
static pthread_t cyclic_thread;
static void *my_thread(void *arg)
{
	while(1) {
		sleep(10);
		printf("my_thread!!!!\n");
	}
	return 0;
}
static void loop_task_proc(void *arg)
{
	RT_TASK *curtask;
	RT_TASK_INFO curtaskinfo;
	RTIME tstart, now;
	curtask = rt_task_self();
	rt_task_inquire(curtask, &curtaskinfo);
	int ctr = 0;
	//Print the info
	printf("Starting task %s with period of 10 ms ....\n", curtaskinfo.name);
	//Make the task periodic with a specified loop period
	rt_task_set_periodic(NULL, TM_NOW, LOOP_PERIOD);
	tstart = rt_timer_read();
	now = rt_timer_read();
	//Start the task loop
	while(1){
		printf("Loop count: %d, Loop time: %.5f ms\n", ctr, (rt_timer_read() -
		tstart)/1000000.0);
		 
		printf("Loop count: %d, Loop time: %.5f ms\n", ctr, (rt_timer_read() -
		now)/1000000.0);
		ctr++;
		rt_task_wait_period(NULL);
	}
}
int main(int argc, char **argv)
{
	char str[20];
	int ret = 0;
	//Lock the memory to avoid memory swapping for this program
	mlockall(MCL_CURRENT | MCL_FUTURE);
   
	printf("Starting cyclic task...\n");
	/* Create cyclic RT-thread */
	/*
	struct sched_param param = { .sched_priority = 82 };
	pthread_attr_t thattr;
	pthread_attr_init(&thattr);
	pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE);
	pthread_attr_setinheritsched(&thattr, PTHREAD_EXPLICIT_SCHED);
	pthread_attr_setschedpolicy(&thattr, SCHED_FIFO);
	pthread_setschedparam(cyclic_thread, SCHED_FIFO, &param);
	*/
	ret = pthread_create(&cyclic_thread, NULL, &my_thread, NULL);
	if (ret) {
   
		fprintf(stderr, "%s: pthread_create cyclic task failed\n",
		strerror(-ret));
		return 1;
	}
	//Create the real time task
	sprintf(str, "cyclic_task");
	rt_task_create(&loop_task, str, 0, 50, 0);
	//Since task starts in suspended mode, start task
	rt_task_start(&loop_task, &loop_task_proc, NULL);
	//Wait for Ctrl-C
	pause();
	return 0;
}

Makefile

# MakeFile for demo of a simple periodic thread using Posix
# under 'standard' Linux
# and under Xenomai versions 2 and 3 using posix skin
# Marc Le Douarain, august 2015

#CROSS := arm-linux-gnueabi-


# For a 'standard' application...
MY_LDFLAGS = -lpthread
# Comment theses 2 lines for a 'standard' application...
MY_CFLAGS = `xeno-config --skin=alchemy --cflags`
MY_LDFLAGS = `xeno-config --skin=alchemy --ldflags`

CC := ${CROSS}gcc

all: demo_periodic_thread_posix

OBJS := demo_periodic_thread_posix.o

demo_periodic_thread_posix: ${OBJS}
	${CC} -o $@ ${OBJS} $(MY_LDFLAGS)

clean:
	-rm -f core ${OBJS} demo_periodic_thread_posix

demo_periodic_thread_posix.o: demo_periodic_thread_posix.c
	${CC} -c $< -o $@ $(MY_CFLAGS)

注意

MY_CFLAGS = `xeno-config --skin=alchemy --cflags`
MY_LDFLAGS = `xeno-config --skin=alchemy --ldflags`

在使用实时进程时,设置为 --skin=alchemy

编译,运行。运行的时候出现下面的问题:

在这里插入图片描述

检查程序的依赖库

origitech@Origitech:~/Documents/xenomai-build$ ldd demo_periodic_thread_posix
	linux-vdso.so.1 =>  (0x00007ffd775ec000)
	libalchemy.so.0 => /usr/xenomai/lib/libalchemy.so.0 (0x00007f5b512ca000)
	libcopperplate.so.0 => /usr/xenomai/lib/libcopperplate.so.0 (0x00007f5b510bc000)
	libcobalt.so.2 => /usr/xenomai/lib/libcobalt.so.2 (0x00007f5b50e9b000)
	libmodechk.so.0 => /usr/xenomai/lib/libmodechk.so.0 (0x00007f5b50c98000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f5b50a65000)
	librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f5b5085d000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f5b50492000)
	/lib64/ld-linux-x86-64.so.2 (0x00005610ba339000)

然后检查链接的环境路径:

origitech@Origitech:~/Documents/xenomai-build$ echo $LD_LIBRARY_PATH 
:/usr/xenomai/lib


发现libalchemy所在路径已经在环境变量中了,但是为什么还是链接不上呢?

后面就是Google,Google…终于看到一个方法,于是尝试:

在 /etc/ld.so.conf.d目录下新建 xenomai.conf,将xenomai的链接库路径添加进去:
在这里插入图片描述

/usr/xenomai/lib

再执行使更改生效:

/sbin/ldconfig -v




再次运行程序:

在这里插入图片描述

成功!