8 sights & sounds

28
Intro to Robots 8 Sights & Sounds

Upload: jasper

Post on 15-Jan-2016

62 views

Category:

Documents


0 download

DESCRIPTION

8 Sights & Sounds. Computing is more than Computation. So many devices – iPod, digital cameras, etc – use computers to do so much more than “compute”. Such devices manage and manipulate images and sounds in amazing ways. In this chapter we try to get some of that for ourselves. GraphWin():. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 8 Sights & Sounds

Intro to Robots

8 Sights & Sounds

Page 2: 8 Sights & Sounds

Intro to Robots

Computing is more than Computation

• So many devices – iPod, digital cameras, etc – use computers to do so much more than “compute”.

• Such devices manage and manipulate images and sounds in amazing ways.

• In this chapter we try to get some of that for ourselves.

Page 3: 8 Sights & Sounds

Intro to Robots

GraphWin():

• Myro comes with a graphics windows in which we can play.

• We create it by invoking it by name:

• This produces a gray 200 x 200 pixel window.

• The myCanvas window becomesour canvas where we exploringdrawing images.

From myro import *

myCanvas = GraphWin()

Exercise: Are pixels round, square orrectangular?

Page 4: 8 Sights & Sounds

Intro to Robots

GraphWin():

• Close it

• And create it again with a new name and size.

• Change its color

myCanvas.close()

myCanvas = GraphWin(‘Silly Scene’,200,300)

myCanvas.setBackground(“white”)

Page 5: 8 Sights & Sounds

Intro to Robots

Moment of Reflection:

• Our graphics window is an “object” with a name – myCanvas.

• You can create an object by calling a function whose name identifies the “type” of object (also called its class).

• We communicate with our window by “sending it messages”

myCanvas.setBackground(“white”)

message name

name of message “receiver”

message argument

myCanvas = GraphWin(‘Silly Scene’,200,300)

Page 6: 8 Sights & Sounds

Intro to Robots

Locating the Pixels

• Pixels, like points on the xy-plane, have coordinates.• The coordinate numbering system begins in the top, left-

hand corner.

(0,0)

(100,100)

Page 7: 8 Sights & Sounds

Intro to Robots

Creating More Objects

• Besides graphics windows objects we can create many others – points, lines, circles, etc.

• Up to this point, p only exists in the programming interface (IDLE). To see it, we need to place it on the graphics window at its specified location.

• Remember, we are sending a message to p to draw itself on the myCanvas graphics window.

p = Point(100,125)

p.draw(myCanvas)

Page 8: 8 Sights & Sounds

Intro to Robots

Point object messages:

• There are some more messages we can send to a Point.

getX() # returns the x-coordinategetY() # returns the y-coordinateundraw() # makes the object disappear

Page 9: 8 Sights & Sounds

Intro to Robots

What’s the Point?

• Points by themselves are not so interesting. • What is really the point is that with them we can draw

lines.• There is a lab exercise where we end up drawing a line

by ourselves.• Alternatively we can create a Line object.• A Line object is a straight line between two end points.

myLine = Line(Point(5,100), Point(50,200))

executing the Line function creates a new Line object

the arguments of the Line function are its starting andending Points. These pointsare created by calling the Point()function twice.myLine.draw()

Page 10: 8 Sights & Sounds

Intro to Robots

A real Fan of Lines.

for n in range(0, 200, 5): L=Line(Point(n,25),Point(100,100)) L.draw(myCanvas)

range(x,y,z) is a function thatreturns a list of numbers startingat x, x+z, x+2z, … until y

all the lines drawn have a commonend-point – Point(100,100)

all the lines drawn have starting pointswith a common y-coordinate (25)

Page 11: 8 Sights & Sounds

Intro to Robots

Other Line functions:

L = Line(Point(25,25), Point(100,100))L.draw()

start = L.getP1() # returns the starting point of lineend = L.getP2() # returns the end point of line

print start.getX(), start.getY(), end.getX(), end.getY()25 25 100 100

L.undraw()

Exercise: If L is a line, print out the starting and ending coordinates of L as (x,y)

print ‘(‘,L.getP1().getX(),’,’,L.getP1().getY(),’)’print ‘(‘,L.getP2().getX(),’,’,L.getP2().getY(),’)’

We send L a message to return its starting point and the startingpoint a message to return its x-coordinate. Obviously, the dot (.) operatorfunctions like +, left-to-right.

Page 12: 8 Sights & Sounds

Intro to Robots

Circles:

• The definition of a circle is “the set of points equidistant from a given point”.

• The Circle object has a similar definition.

myCircle = Circle(Point(100,100), 50)myCircle.draw()

center point radius

Page 13: 8 Sights & Sounds

Intro to Robots

Circle Functions:

• You can change and retrieve various Circle characteristics.

c = Circle(Point(100, 150), 30)c.draw(myCanvas)centerPoint = c.getCenter()

c.setOutline(“red”) # line colorc.setFill(“yellow”) # interior fill colorc.setWidth(5) # line width in pixels

color = color_rgb(100,125,150)c.setFill(color)

Exercise: Try out these commands

Page 14: 8 Sights & Sounds

Intro to Robots

Circle Example

# Program to draw a bunch of # random colored circlesfrom myro import *from random import *

width = 500height = 500

def makeCircle(x, y, r): # creates a Circle centered at point (x, y) of radius r # we plan to use randomly generated numbers x, y and r return Circle(Point(x, y), r)

def makeColor(): # creates a new color using random RGB values red = randrange(0, 256) # returns a random number between 0 and 255 green = randrange(0, 256) # returns a random number between 0 and 255 blue = randrange(0, 256) # returns a random number between 0 and 255 return color_rgb(red, green,blue)

main()

Page 15: 8 Sights & Sounds

Intro to Robots

Circle Example def main(): # Create and display a graphics window myCanvas = GraphWin("Cicrles", width, height) myCanvas.setBackground("white") # draw a bunch of random circles with random colors. N = 500 for i in range(N): # pick random center # point and radius # in the window x = randrange(0,width) y = randrange(0,height) r = randrange(5, 25) c = makeCircle(x, y, r) # select a random color c.setFill(makeColor()) c.draw(myCanvas)

Page 16: 8 Sights & Sounds

Intro to Robots

Function Definition Alternatives:

• Consider the following two examples:

• makeCircle() hides the details of makeCircle(x,y,r) exposes by requiring previously assigned values for x, y and r

x = randrange(0,width)y = randrange(0,height)r = randrange(5, 25)c = makeCircle(x, y, r)

def makeCircle():# creates a Circle centered at point (x, y) of radius rx = randrange(0,width) # only if width is “global”y = randrange(0,height) # only if height is “global”r = randrange(5, 25)return Circle(Point(x, y), r)

Page 17: 8 Sights & Sounds

Intro to Robots

Variable Scope:

• Variables defined outside any function definition have “global” scope. They can be used anywhere in the program.

• Variables defined inside a function have scope restricted to that function. If you want to use their values in a different functions you need to pass them as arguments.

def foo(): x = 4 # scope of x is inside foo() y = 5 # scope of y is inside foo() return bar()

def bar(): z = x + y # fails because x and y are # have scope within foo() only return z

def foo(): x = 4 y = 5 # now pass x and y as arguments return bar(x,y)

def bar(a,b): z = a + b return z

Page 18: 8 Sights & Sounds

Intro to Robots

Pros and Cons

• Having global variables is nice because you can use them whenever you need them.

• However it gives you added responsibility not to misuse the variable (change its value by mistake, for example)

• A variable with a function scope can only be modified inside that function.

Page 19: 8 Sights & Sounds

Intro to Robots

Sound:

• Both the computer and the robot can beep().

• What is Hertz?

beep(1,440) # plays a 440 Hz tone for 1 secondcomputer.beep(1,440) # computer plays the same tone

Hertz is the unit for measuring frequencies 1 Hz = 1 cycle/second

1 cycle

Page 20: 8 Sights & Sounds

Intro to Robots

Sound (cont):

• Computer CPUs measure their “speed” in Gigahertz.

• Computer CPUs have an internal clock that “ticks” at the Gigahertz rate of the CPU.

• All the various components of a computer synchronize their activities against this clock.

1 Gigahertz = 109 cycles / second

Page 21: 8 Sights & Sounds

Intro to Robots

Sound (cont):

• Other periodic motions:

• What is sound?

• What can the human ear hear?

earth rotational rate: 1/day = 1/86400 seconds = 0.00001157 cycles/second

cd rotational rate: 400 turns/second52x cd rotational rate: 52*400/second = 20800/second

hummingbird wing flap rate: 20-78 flaps/second

A periodic compression and decompression (refraction) of air. 1 cycle of sound = 1 compression + 1 decompression

440 cycles/second = 440 compressions and decompressions / second

in the range 20Hz to 20000Hz (20KHz)

Page 22: 8 Sights & Sounds

Intro to Robots

What sounds can Scribbler make?

• A musical scale consists of 12 sounds

• You can move up or down an octave by multiplying or dividing by 2.

• The C-note in various octaves is identified as:

C C#/Db D D#/Eb E F F#/Gb G G#/Ab A A#/Bb B

C0 C1 C2 C3 C4 C5 C6 C7 C8

Page 23: 8 Sights & Sounds

Intro to Robots

Music:

• C4 has frequency 261.63 Hz. Try it out with Scribbler.• The notes in a scale are exponentially equidistant. By

this we mean:

C4 frequency = 261.63 * 2(0/12) Hz = 261.63 * 20 Hz = 261.63 HzC#4 frequency = 261.63 * 2(1/12) Hz = 261.63 * 20.08333 Hz = 277.19 Hz D4 frequency = 261.63 * 2(2/12) Hz = 261.63 * 20.16666 Hz = 293.67 HzD#4 frequency = 261.63 * 2(3/12) Hz = 261.63 * 20.25000 Hz = 311.13 Hz E4 frequency = 261.63 * 2(4/12) Hz = 261.63 * 20.33333 Hz = 329.63 Hz F4 frequency = 261.63 * 2(5/12) Hz = 261.63 * 20.41666 Hz = 349.23 Hz F#4 frequency = 261.63 * 2(6/12) Hz = 261.63 * 20.50000 Hz = 370.00 Hz G4 frequency = 261.63 * 2(7/12) Hz = 261.63 * 20.58333 Hz = 392.00 Hz G#4 frequency = 261.63 * 2(8/12) Hz = 261.63 * 20.66666 Hz = 415.31 Hz A4 frequency = 261.63 * 2(9/12) Hz = 261.63 * 20.75000 Hz = 440.01 Hz A#4 frequency = 261.63 * 2(10/12) Hz = 261.63 * 20.83333 Hz = 466.17 Hz B4 frequency = 261.63 * 2(11/12) Hz = 261.63 * 20.91666 Hz = 493.89 HzC5 frequency = 261.63 * 2(12/12) Hz = 261.63 * 21 Hz = 523.26 Hz

Page 24: 8 Sights & Sounds

Intro to Robots

Music Tempo:

• Music has tempo. In tempo– a quarter note is 0.68 seconds– A half note is 1.36 seconds– A whole note is 2.72 seconds.

44

Page 25: 8 Sights & Sounds

Intro to Robots

Myro Music:

• Playing notes by frequency and time is a pain.• Myro lets you “write out a tune” as a string and then

converts the string into a song.

• Each note in the string is either

or

where note = ‘C4’, etc and time = ¼, ½, 1, etc

tune = “c 1; d 1; e 1; f 1; g 1; a 1; b 1; c7 1;“song = makeSong(tune)computer.playSong(song)

note time;

note1 note2 time;

Page 26: 8 Sights & Sounds

Intro to Robots

Myro Music 2:

• You can also put your string in a file and myro will read the file and play the song.

• Of course, if you want, the robot can play the song itself, just drop the “computer” receiver tag.

song = readSong(filename)computer.playSong(song)

Page 27: 8 Sights & Sounds

Intro to Robots

Myro Reference:

• The end of chapter 8 holds a review of the myro graphics function calls.

GraphWin()GraphWin(<title>, <width>, <height>)<window>.close()<window>.setBackground(<color>)

<new_color> = color_rgb(<red>, <green>, <blue>)

Point(<x>, <y>)<point>.getX()<point>.getY()

Line(<start point>, <end point>)

Circle(<center point>, <radius>)

Rectangle(<point1>, <point2>)

Oval(<point1>, <point2>)

Polygon(<point1>, <point2>, <point3>,…)Polygon([<point1>, <point2>, …])

Text(<anchor point>, <string>)

Image(<centerPoint>, <file name>)

<object>.draw(<window>)<object>.undraw()<object>.getCenter()<object>.setOutline(<color>)<object>.setFill(<color>)<object>.setWidth(<pixels>)<object>.move(<dx>, <dy>)

Page 28: 8 Sights & Sounds

Intro to Robots

Myro Reference:

• The end of chapter 8 holds a review of the myro graphics function calls.

beep(<seconds>, <frequency>)beep(<seconds>, <f1>, <f2>)

<robot/computer object>.beep(<seconds>, <frequency>)<robot/computer object>.beep(<seconds>, <f1>, <f2>)

robot.playSong(<song>)readSong(<filename>)song2text(song)makeSong(<text>)text2song(<text>)