CS110:Lab10

From IPRE Wiki
Revision as of 12:59, 11 November 2009 by Ttthomas (Talk | contribs) (Assignment)

Jump to: navigation, search

Lab 10: Music Demo & The Game Pad

Objective

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


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("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.

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("count")
2
>>> getGamepad("button") # waits till you press at least one button
[0, 0, 1, 0, 1, 0, 0, 0]
>>> getGamepad(1, "button") # waits till ID 1 presses at least one button
[1, 0, 0, 0, 0, 0, 0, 0]
>>> 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).


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()
  m.connect()
  m.setGain(0.3)
  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)
  m.noteOn(1)

buttonsHit = getGamepad("button")
if buttonsHit[4] == 1:
  playMandolin()

Here is another example of a program to control your robot:

done = False
while not done:
    results = getGamepad("button", "robot")
    move(*results["robot"])
    if results["button"][1]: beep(.5, 440)
    if results["button"][2]: beep(.5, 880)
    done = (results["button"][0] == 1)

Assignment

Note: Do either 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