Note: This tutorial assumes that you have completed the previous tutorials: Creating Packages.

Using the Buttons

Description: This tutorial shows how to access the state of the buttons on Mini Maxwell

Tutorial Level: BEGINNER

Next Tutorial: Moving the Base

Overview

There are two buttons on Mini Maxwell, which are easily accessible through ROS.

Under the Hood

This section describes what is happening at a low-level, feel free to skip past it.

Both buttons are connected through the ArbotiX2, as digital inputs. Due to the way they are wired, the buttons are at 5V when unpressed, and pressing the button changes the level to 0V. The ArbotiX uses 8-bit values to encode the voltages, which means that 0V = 0, and 5V = 255 (the largest value which can be stored in an unsigned 8-bit data type).

The Topics

The buttons broadcast on two topics:

/arbotix/green_button
/arbotix/yellow_button

You can see the output using the rostopic tool:

$ rostopic echo /arbotix/green_button
header: 
  seq: 59
  stamp: 
    secs: 1307850081
    nsecs: 213567018
  frame_id: ''
value: 255
direction: 0
---
header: 
  seq: 60
  stamp: 
    secs: 1307850081
    nsecs: 296992063
  frame_id: ''
value: 0
direction: 0

Each topic is output at approximately 5 hz. A value of 0 means that the button is pressed, while 255 means it is not. The direction will always be 0 (an input).

Reading the Button State in Python

We will now create a simple program to read the state of the buttons. You should create a package called 'button_test' using roscreate-pkg, be sure to add a dependency on the 'arbotix_msgs' which contains the message definition we will need for reading the buttons. Alternatively, you can find this code in the mini_max_tutorials package.

Then open the file 'button_test.py' in the editor of your choice, and enter the following:

https://vanadium-ros-pkg.googlecode.com/svn/trunk/mini_max/mini_max_tutorials/nodes/button_test.py

   1 #!/usr/bin/env python
   2 
   3 """ Example code of how to access button state. """
   4 
   5 # We always import roslib, and load the manifest to handle dependencies
   6 import roslib; roslib.load_manifest('mini_max_tutorials')
   7 import rospy
   8 
   9 # arbotix_msgs defines the Digital message, which tells us the state
  10 #  of the button (a digital pin)
  11 from arbotix_msgs.msg import *
  12 
  13 class ButtonTest:
  14     
  15     def buttonCb(self, msg):
  16         # msg is arbotix_msgs.Digital, value == state of the button
  17         if msg.value == 0:
  18             print "Button pressed!"
  19 
  20     def __init__(self):
  21         # we have to initialize the node, with a name in the ROS graph
  22         rospy.init_node('button_test')
  23 
  24         # subscribe to the green_button value. 
  25         rospy.Subscriber('/arbotix/green_button',Digital,self.buttonCb)
  26 
  27         # everything will be handled in the callback, just spin
  28         rospy.spin()
  29 
  30 # this quick check means that the following code runs ONLY if this is 
  31 #  in the main file -- if we "import button_test" in another file, 
  32 #  this code will not execute
  33 if __name__ == "__main__":
  34     bt = ButtonTest()

Mark the file executable:

chmod +x button_test.py

And then run it:

rosrun button_test button_test.py

You should then see a message posted that the button is pressed whenever you press it!

Wiki: mini_max/Tutorials/Using the Buttons (last edited 2011-10-18 19:44:03 by MichaelFerguson)