Digital IO, LED & Bumper Interface

From KameRider
Jump to: navigation, search
  • Turtlebot base is named Kobuki and it has built-in LEDs, buttons and bumpers to provide feedback to user and Kobuki base itself.
  • Besides built-in functions, Kobuki base also has 4 digital output pins and 4 digital input pins which can be used by user.
  • The position of those external pins can be found here: [1], page 10.
  • To acquire or give information to the Kobuki base, user need to subscribe topics and publish information to certain topics.
  • Kobuki base itself will keep on publish information on certain topics such as:
    • /mobile_base/events/digital_input
    • /mobile_base/events/button
    • /mobile_base/events/bumper
  • These topics are similar to the inputs information of the Kobuki base.
  • Each topic contains information or status of an input.
  • For an example, topic /mobile_base/events/button contains the information of built-in buttons on the Kobuki base, the information shows whether the buttons are pressed or not.
  • However, there are also some topics which the Kobuki base will subscribe, such as:
    • /mobile_base/commands/led1
    • /mobile_base/commands/led2
    • /mobile_base/commands/digital_output
  • The user will publish information into these topics so that Kobuki base will shows the output that the user want.
  • For an example, if user publish information of turning on LED1 to topic /mobile_base/commands/led1, then led1 of Kobuki base will on.
  • But, before user can use extract information or put information to these topics, it is important to know the information/message type.
  • Different topics have different type of message, some of the topics have same type of message though.
  • Types of message based on different topics can be found here: [2].
  • User can access the topic published by typing:
    • $ rostopic list
  • User can also get the information in a topic by typing:
    • $ rostopic echo TOPIC_NAME
    • where TOPIC_NAME is the name of the topic where user wish to access.
  • An example of source code with explanation:

include section:

 1 # Include kobuki library
 2 import roslib; roslib.load_manifest('kobuki_testsuite');
 3 
 4 # Include topic message type
 5 from kobuki_msgs.msg import DigitalInputEvent, Led, ButtonEvent
 6 
 7 # Global variables used to store value and state
 8 digital_in = [True, True, True, True]
 9 button = 0
10 button_state = 0

initiation section:

 1 # Subscribe to /mobile_base/events/digital_input topic to receive digital input
 2 # First parameter is the topic name, second parameter is the message type, third parameter is the callback function whenever the topic has new changes.
 3 rospy.Subscriber('/mobile_base/events/digital_input', DigitalInputEvent, self.DigitalInputEventCallback)
 4 
 5 # Subscribe to /mobile_base/events/button topic to receive built-in button input
 6 rospy.Subscriber('/mobile_base/events/button', ButtonEvent, self.ButtonEventCallback)
 7 
 8 # Publish Led messages to /mobile_base/commands/led1 topic to give Led signal
 9 self.pub1 = rospy.Publisher('/mobile_base/commands/led1', Led)
10 
11 # This "self.led1" is an user generated variable which is same type as Led().
12 # If user wants to change state of led1, led1's state need to be store in "self.led1" then publish to self.pub1.
13 self.led1 = Led()

callback function section:

 1 # A local variable named "data" copy the type of "DigitalInputEvent".
 2 # The information of the digital input is located at data.values.
 3 # Before a global variable's value can be changed, user need to declare it again.
 4 def DigitalInputEventCallback(self,data):
 5     global digital_in
 6     digital_in = data.values
 7 
 8 # Global variable "button" stores the identity of state changes button.
 9 # Global variable "button_state" stores the state of a button. 
10 def ButtonEventCallback(self,data1):
11     global button
12     global button_state
13     button = data1.button
14     button_state = data1.state

main (loop) section:

 1     # Check identity of digital input and its states
 2     if digital_in[1]==True:
 3         # DO SOMETHING HERE
 4     elif digital_in[2]==True:
 5         # DO SOMETHING HERE TOO
 6     else:
 7         # DO ANYTHING USER LIKE
 8 
 9     # Check identity of button and its states
10     if button == 0:
11 	if button_state == 1:
12 	  # DO FAVOURITE A
13           # led1 green colour
14           self.led1.value = 1
15 	else:
16 	  # DO FAVOURITE B
17           # led1 orange colour
18           self.led1.value = 2
19 
20     if button == 1:
21 	if button_state == 1:
22 	  # DO FAVOURITE C
23           # led1 red colour
24           self.led1.value = 3
25 	else:
26 	  # DO FAVOURITE D
27           # switch off
28           self.led1.value = 0
29 
30     # Publish led1 state to change colour/switch off led 1.
31     self.pub1.publish(self.led1)
  • source code of the above example can be found here: [3]
  • source code of integration of digital IO with sound can be found here: [4]