查看: 4565|回复: 0

[经验] 【FirePrime】通过arduino学习ROS系列4用按键和ROS交流

[复制链接]
  • TA的每日心情
    奋斗
    2018-11-15 08:49
  • 签到天数: 1031 天

    连续签到: 2 天

    [LV.10]以坛为家III

    发表于 2015-10-16 18:59:56 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 liuyu-419812 于 2015-10-16 19:09 编辑

          在这里我们集成到ROS操作系统最简单和最常用的硬件:按键。一个按键或一个输入设备可以命令你的机器人做下一个任务,让一个传感器检测一个打开的盒子,或让一个电机在超过关节限制时停止,这是一个重要的安全特性。在这个教程中,我们会编写一个rosserial_arduino节点,监视正常的引脚7上的按钮的状态。每一次按键的状态改变时,它会发布一个布尔消息到题“pushed”。对于如何搭建按键电路这里就不做说明了。
          参考:http://wiki.ros.org/rosserial_arduino/Tutorials/Push%20Button
          可以通过在Arduino examples菜单选择ros_lib->button_exampled,打开下面的代码:
    1. /*
    2. * Button Example for Rosserial
    3. *
    4. /#include <ros.h>
    5. #include <std_msgs/Bool.h>
    6. ros::NodeHandle nh;
    7. std_msgs::Bool pushed_msg;
    8. ros::Publisher pub_button("pushed", &pushed_msg);
    9. const int button_pin = 7;
    10. const int led_pin = 13;
    11. bool last_reading;
    12. long last_debounce_time=0;
    13. long debounce_delay=50;
    14. bool published = true;
    15. void setup()
    16. {
    17.   nh.initNode();
    18.   nh.advertise(pub_button);
    19. //initialize an LED output pin
    20. //and a input pin for our push button         
    21. pinMode(led_pin, OUTPUT);  
    22. pinMode(button_pin, INPUT);
    23. //Enable the pullup resistor on the button  
    24. digitalWrite(button_pin, HIGH);
    25. //The button is a normally button
    26. last_reading = ! digitalRead(button_pin);
    27. }
    28. void loop()
    29. {
    30. bool reading = ! digitalRead(button_pin);
    31. if (last_reading!= reading)
    32. {
    33.       last_debounce_time = millis();   
    34.           published = false;  
    35. }
    36.    //if the button value has not changed during the debounce delay  
    37. // we know it is stable  
    38. if ( !published && (millis() - last_debounce_time)  > debounce_delay) {    digitalWrite(led_pin, reading);  
    39.   pushed_msg.data = reading;   
    40. pub_button.publish(&pushed_msg);
    41.     published = true;
    42. }
    43. last_reading = reading;   
    44. nh.spinOnce();
    45. }
    复制代码
          下面简要分析代码,一些代码的用法在之前已经说过,不再赘述。在代码中,使用了软件去抖。首先,定义全局变量存储按键最近读的一个值,读取的时间和去抖的延时时间,是否发布按键的值。
    1. bool last_reading;
    2. long last_debounce_time=0;
    3. long debounce_delay=50;
    4. bool published = true;
    复制代码
          下面,在void setup()函数里,我们初始化arduino引脚为输入引脚并配置上拉电阻。我们也初始化我们的状态变量。
    1. //initialize an LED output pin   
    2. //and a input pin for our push button  pinMode(led_pin, OUTPUT);  
    3. pinMode(button_pin, INPUT);   
    4. //Enable the pullup resistor on the button  
    5. digitalWrite(button_pin, HIGH);   
    6. //The button is a normally button  
    7. last_reading = ! digitalRead(button_pin);
    复制代码
          最后,在void loop()函数,在每一个循环,代码读取按键的值,检查按键的状态是否改变,然后检查按键的状态是否稳定,是否已经发布。
          编译上传。在新的终端窗口,启动roscore
    1. roscore
    复制代码
          接着,运行rosserial客户端应用程序,确保使用正确的串口:
    1. source ~/<ws>/install/setup.bash
    2. rosrun rosserial_python serial_node.py /dev/ttyACM0
    复制代码
          其中,<ws>为安装rosserial的工作空间
          通过pushed话题观察按键值
    1. rostopic echo pushed
    复制代码
    P51016-174205.jpg
    可以在终端看到,每次按键的状态改变时,都会向ROS发送信息。按下时发送False,松开时发送True。
    P51016-174213.jpg

    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-5-14 18:14 , Processed in 0.122944 second(s), 15 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.