player

How to get the Player Robot Device Interface running with Python and the Phidgets.

installation

Follow the instructions on the IAS@TUM - DevWiki to install Player and the Phidgets driver.

write python code

Player doesn't offer any documentation on the Python bindings, you only find an example file. However, looking through the code of ~/local/src/player/player/client_libs/libplayerc/bindings/python/playerc.py may help you to figure out some commands. They are mostly similar to the ones of libplayerc. And the interactive python shell is a good place for tests.

skeleton

Here's a short example that worked for us. It's nothing official and may contain errors but it might be better than nothing:

# Import python bindings
from playerc import *
 
# Set the constants
playerhost = 'localhost'
playerport = 6665
 
# Connect to Player
c = playerc_client(None, playerhost, playerport)
c.connect()
 
# Define the analog input (the Phidgets do not support analog output)
aio = playerc_aio(c, 0)
aio.subscribe(PLAYERC_OPEN_MODE)
 
# Define the analog input/output
dio = playerc_dio(c, 0)
 
# Define the speech device
s = playerc_speech(c,0)
 
##############################
#                            #
# Here's room for your code. #
#                            #
##############################
 
# Cleaning up
aio.unsubscribe()
dio.unsubscribe()
c.disconnect()

commands

The names are used as defined above.

datamode

Read Client Data Modes for a detailed description of the different modes.

# Set datamode to PLAYERC_DATAMODE_PULL
c.datamode(PLAYERC_DATAMODE_PULL)
 
# Delete old data from the stack
c.set_replace_rule(-1, -1, PLAYER_MSGTYPE_DATA, -1, 1)

analog input

# Get the number of aio ports. (Not identical with the number of available sensors.)
aio.voltages_count
 
# Get the voltage of aio port 0.
c.read()
aio.voltages[0]

digital input/output

# Operate output 0. (It works but I don't know why it has to be 1 rather than 0. Help is welcome ;-).
dio.set_output(1, 1)

speech

# Display "Hello World" on the phidget screen.
s.say('Hello World')

use system microphone as sound sensor

You can use the gstreamer framework to get the actual sound level measured by the microphone. (Thanks to laszlok from the Jokosher development team.)

initialize

We're using classes now. Call that function once:

def initMic(self):
    self.pipe = """alsasrc ! audioconvert ! level interval=1000 message=true ! fakesink"""
    self.loadingPipeline = gst.parse_launch(self.pipe)
    self.bus = self.loadingPipeline.get_bus()
    self.bus.add_signal_watch()
    self.bus.connect("message::element", self.bus_message)
    self.loadingPipeline.set_state(gst.STATE_PLAYING)
    self.micValue = 0.0

Your main program needs a mainloop (which you shoul already have if you're developing a GTK application):

gtk.main()

get the values

The initialization connects the standard input device to the function bus_message:

def bus_message(self, bus, message):
    self.micValue = message.structure['peak'][0] # We only use the first of the two channels.

You might need to adapt the value because it usually is much higher than those gathered from the phidget sensors.