From IPRE Wiki
Revision as of 14:32, 11 November 2009 by Doug Blank (Talk | contribs) (Advanced Examples)

Jump to: navigation, search

Lab 10: Music Demo & The Game Pad


  • Give me a demo of your music program (Assignment 9)
  • Explore the different functions of the game pad

NOTE: you must have a gamepad to do this lab. Also, you MUST plug it into the USB port before entering "from myro import *". If you forgot, you need to restart your shell, or restart Python.

Useful Gamepad Details

In lab 02, you saw how you could use your game pad to control your robot's movements. In addition to the gamepad() command you learned in lab 2(you should revisit this lab for reference), there is the getGamepad() function which allows you do a wider variety of tasks with our game pad. The parameters this function takes are listed below:

  1. getGamePad() - returns all of the possible values, when one is pressed
  2. getGamePad(ITEM) - returns one of the items available when pressed. ITEM is often "button" or "axis".
  3. getGamepadNow() - returns the state of all settings right now (maybe nothing is being pressed)
  4. getGamepad(ITEM) - returns the state of ITEM right now (maybe nothing is being pressed)

ITEM can be:

  1. "count" - returns (immediately) number of gamepads plugged in
  2. "robot" - returns axis values (floats) ready to be used by move() Myro command as [translate, rotate]
  3. empty - if nothing is passed in, it returns all of the following as a dictionary
  4. "init" - has this gamepad been initialized? as boolean
  5. "name" - name of gamepad, as string
  6. "axis" - returns values of axis controller in a list (as floats)
  7. "ball" - returns values of ball controller in a list
  8. "button" - returns values of buttons in a list (as integers)
  9. "hat" - returns values of hat in a list

Gamepad Examples

>>> getGamepad("button") # waits till you press at least one button
[0, 0, 1, 0, 1, 0, 0, 0]

You should explore the getGamepad("button") and write down the buttons that correspond to a '1' in the game pad list. For example, if you hit the button marked '3' in the game pad image above, after calling the getGamepad("button") command and this list is returned: [0, 0, 0, 0, 1, 0, 0, 0], then you will know that the button '3' corresponds to the 5th element in the game pad list. Using this knowledge, you can create an array of using functions. Here is an example:

def playMandolin():
  m = Mandolin()
  beat = 0.4
  wait(beat * 12) # wait for 4 measures / loop iterations
  for i in range(3):
     playOnce(m, beat, 1)
     playOnce(m, beat, 1)
     playOnce(m, beat/2, .8)
     playOnce(m, beat/2, .8)

# Get the gamepad button values as a list when a button is pressed:
buttonsHit = getGamepad("button")
# Get the 5th item in the list, and if it is pressed:
if buttonsHit[4] == 1:
  # then do something, like call a function:

Advanced Examples

Useful notes about the gamepad for advanced uses (eg, you final project):

  1. getGamepad("count") - returns (immediately) the number of gamepads connected to your computer.
  2. getGamepad(ID, ITEM, ...) - return the ITEMs for gamepad ID. ID can be left out and will default to 0, the first one. If you request more than one ITEM, then they come back in a dictionary. Just request one ITEM and you'll get the value (as a list, string, or number).
  3. getGamepad([ID1, ID2...], ITEM, ...) - return the ITEMs for gamepad IDs as a list of lists of ID, RESULTS. For example:
    >>> getGamepad([0, 1], "button", "axis")
    [[0, {'button': [0, 0, 0, 0, 0, 0, 0, 0], 'axis': [0.0, 1.0]}], [1, {'button': [1, 1, 0, 0, 0, 0, 0, 0], 'axis': [-1.0, -1.0]}]]
    >>> getGamepad([0, 1], "axis")
    [[0, [0.0, 1.0]], [1, [-1.0, -1.0]]]
  4. getGamepad() has one keyword argument, "wait" for setting a sleep value between gamepad polls. Default is 0.05 seconds. Setting to zero will have small latency, but may eat up your CPU.

Example using more than one gamepad:

>>> getGamepad("count")
>>> getGamepad(range(getGamepad("count")), "button")
# waits till someone presses a button
[[0, [1, 0, 0, 0, 0, 0, 0, 0]],
 [1, [0, 0, 0, 0, 0, 0, 0, 0]]]
>>> getGamepad("button", "axis", wait=.01)
{"button": [1, 0, 0, 0, 0, 0, 0, 0],
 "axis": [-0.999969482421875, 0.0]}
# (sometimes axis doesn't return exactly 1 or -1).

>>> getGamepad(1, "button") # waits till player #1 presses at least one button
[1, 0, 0, 0, 0, 0, 0, 0]


Note: Do either question 1 or 2 below:

1. Create a musical instrument. You can use the axes of the game pad to change the octave of the instruments. Buttons can work together to play two-tone frequencies. Or play sound files. Be Creative.
2. Create a graphical scene (you can use your collage or the seasonal scenes you created in labs 7 and 8 or add to them). Use the buttons to do tasks such as moving an object, flipping an image, coloring an object etc.

Links to Course-Related Pages