最新消息:

Turbot与python编程-实现获取kobuki按键信息

Turtlebot2/二代机器人 少儿编程 1378浏览 0评论
Turbot与Python教程

Turbot与Python教程-实现获取kobuki按键信息

说明:

  • 介绍如何实现通过python控制turbot实现获取kobuki按键信息

代码:

  • 参考代码:github
  • 实现代码:
#!/usr/bin/env python 
# Monitor the kobuki's button status

import roslib
import rospy
from kobuki_msgs.msg import ButtonEvent

class kobuki_button():

    def __init__(self):
        rospy.init_node("kobuki_button")        

        #monitor kobuki's button events
        rospy.Subscriber("/mobile_base/events/button",ButtonEvent,self.ButtonEventCallback)

        #rospy.spin() tells the program to not exit until you press ctrl + c.  If this wasn't there... it'd subscribe and then immediatly exit (therefore stop "listening" to the thread).
        rospy.spin();


    
    def ButtonEventCallback(self,data):
        if ( data.state == ButtonEvent.RELEASED ) :
        state = "released"
        else:
        state = "pressed"  
        if ( data.button == ButtonEvent.Button0 ) :
        button = "B0"
        elif ( data.button == ButtonEvent.Button1 ) :
        button = "B1"
        else:
        button = "B2"
        rospy.loginfo("Button %s was %s."%(button, state))
    

if __name__ == '__main__':
    try:
        kobuki_button()
    except rospy.ROSInterruptException:
        rospy.loginfo("exception")

演示:

  • 主机,新终端,启动底盘
$ roslaunch turbot_bringup minimal.launch
  • 从机,新终端,启动脚本
$ rosrun turbot_code kobukiButtons.py

您必须 登录 才能发表评论!