Difference between revisions of "CS110:Lab10"

From IPRE Wiki
Jump to: navigation, search
(Assignment)
(Assignment)
Line 129: Line 129:
 
Buttons can work together to play two-tone frequencies. Or play sound files. Be Creative.
 
Buttons can work together to play two-tone frequencies. Or play sound files. Be Creative.
 
<br>
 
<br>
'''2.''' Create a graphical scene (you can use your collage or the seasonal scenes you created in labs 7 and 8).
+
'''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.
 
Use the buttons to do tasks such as moving an object, flipping an image, coloring an object etc.
  

Revision as of 09:29, 11 November 2009

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

Using Your Game Pad to Drive Your Robot Around

In lab 02, you saw how you could use your game pad to control your robot's movements. You should ensure that your game pad is plugged into a USB port before starting IDLE. The annotated image below shows a game pad similar to the one you will be using.

Gamepad1.JPG

To use the game pad, type the following at the prompt:

gamepad()

You should see the following options when you do this:

        Pad   Action
     ------   -------
 Left/Right   turnLeft() and turnRight()
    Up/Down   forward() and backward()

     Button   Action
     ------   -------
          1   stop()
          2   takePicture()
          3   beep(.25, 523)
          4   beep(.25, 587)
          5   beep(.25, 659)
          6   speak('Hello. My name is Dribbler.')
          7   speak('Ouch! I'm a sensitive robot.')
          8   speak('I'm hungry. Do you have any batteries?')

Gamepad is now running... Press button 1 to stop.

You can then use the buttons on your game pad to move your robot around as well as to explore additional features of your robot (e.g. taking a picture, beeping or speaking).

Useful Gamepad Details

In addition to the gamepad() command, 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

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