wait_queue.demo

等待队列demo

#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/wait.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/kthread.h>
#include <linux/uaccess.h>

int test_condition = 0;
module_param(test_condition, int, 0644);

static int wait_num = 0;
//定义等待队列头
//wait_queue_head_t my_wait;
//init_waitqueue_head(&my_wait);

/*使用宏定义*/
struct task_struct *wait_queue_task = NULL;
struct task_struct *wake_up_task = NULL;
static DECLARE_WAIT_QUEUE_HEAD(my_wait);
wait_queue_t *wq_entry=NULL;

static int wait_queue_thread(void *data)
{
    wait_queue_head_t *wait_tmp = (wait_queue_head_t *)data;
    DECLARE_WAITQUEUE(wait_task,current);
    wq_entry = &wait_task;

    add_wait_queue(wait_tmp, &wait_task);
    wait_num++;
    printk("TEST:wait_task will add to queue:%d\n", wait_num);
    wait_event(my_wait, test_condition);
    wait_num--;
    test_condition = 0;
    printk("TEST:wait_task after wait event:%d\n", wait_num);

    return 0;
}

static int wake_up_thread(void *data)
{
    wait_queue_head_t *wait_tmp = (wait_queue_head_t *)data;
    /* 线程中需要注意不能一直不退出,否则kthread_stop会阻塞死等 */
    while(!kthread_should_stop())
    {
        printk("TEST:wake_up wait_num:%d,condition:%d\n", wait_num, test_condition);
        if(test_condition > 0)
        {
            wake_up(wait_tmp);
        }
        msleep(5000);
    }
    return 0;
}

static int test_init(void)
{
    wait_queue_task = kthread_run(wait_queue_thread, (void *)(&my_wait), "wait_queue_thread");
	if (wait_queue_task == NULL) {
		pr_err("wait_queue_task_create failed\n");
		return -1;
	}

    wake_up_task = kthread_run(wake_up_thread, (void *)(&my_wait), "wake_up_thread");
	if (wake_up_task == NULL) {
		pr_err("wait_queue_task_create failed\n");
		return -1;
	}
    return 0;
}
module_init(test_init);

static void test_exit(void)
{
    int ret;
    /*kernel_stop更像一个等待子线程结束的函数,结束了再卸载模块,并没有强制停止的功能*/
    ret = kthread_stop(wake_up_task);
    printk("wake_up_task is stop...%d\n", ret);
    /* 已经唤醒后的无法再次移除 */
    remove_wait_queue(&my_wait, wq_entry);
    /* 已经wait的线程虽然已经被移除,但线程本身还在等待,需要手动唤醒,不然kthread_stop还是会阻塞 */
    test_condition = 1;
    printk("remove_wait_queue...%d\n",test_condition);
    ret = kthread_stop(wait_queue_task);
    printk("wait_queue_task is stop...%d\n", ret);
    return;
}
module_exit(test_exit);

MODULE_LICENSE("GPL V2");
该代码如何使小车判断交通灯颜色,判断后又如何使小车做出相应反应?class navigation_demo: def init(self): # self.set_pose_pub = rospy.Publisher('/initialpose', PoseWithCovarianceStamped, queue_size=5) # nav 创建发布器用于发送目标位置 self.pub_goal = rospy.Publisher('/move_base_simple/goal', PoseStamped, queue_size=10) # 创建客户端,用于发送导航目标 self.move_base = actionlib.SimpleActionClient("move_base", MoveBaseAction) self.move_base.wait_for_server(rospy.Duration(60)) self.sub_socket = rospy.Subscriber('/socket', Int16, self.socket_cb) # traffic light self.sub_traffic = rospy.Subscriber('/traffic_light', Bool, self.traffic_light) # line check车道线检测信息 self.pub_line = rospy.Publisher('/detector_line',Bool,queue_size=10) # 交通灯信息 self.pub_color = rospy.Publisher('/detector_trafficlight',Bool,queue_size=10) self.pub_reached = rospy.Publisher('/reached',Bool,queue_size=10) self.sub_done = rospy.Subscriber('/done',Bool,self.done_cb) #add self.tf_listener = tf.TransformListener() # 等待map到base_link坐标系变换的建立 try: self.tf_listener.waitForTransform('map', 'base_link', rospy.Time(0), rospy.Duration(1.0)) except (tf.Exception, tf.ConnectivityException, tf.LookupException): pass print("tf point successful") #add 初始化 self.count = 0 self.judge = 0 self.start = 0 self.end = 0 self.traffic = False self.control = 0 self.step = 0 self.flage = 1 # self.done = False #add 交通灯状态 def traffic_light(self, color): self.traffic = color.data # self.traffic = True if (self.traffic == False): print ("traffic red") self.judge = 0 if (self.traffic == True): print ("traffic green") self.judge = 1 def get_pos(self,x1,y1): try: (trans, rot) = self.tf_listener.lookupTransform('map', 'base_link', rospy.Time(0)) except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException): rospy.loginfo("tf Error") return None euler = transformations.euler_from_quaternion(rot) #print euler[2] / pi * 180 获取xy的坐标 x = trans[0] y = trans[1] # 计算当前位置与目标位置的距离 result = pow(abs(x-x1),2)+pow(abs(y-y1),2) result = sqrt(result) if (result <= 0.6):# 如果距离小于0.6,表示到达目标, return True #th = euler[2] / pi * 180 else: return False #return (x, y, th)
07-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值