8 Sights & Sounds

Download Report

Transcript 8 Sights & Sounds

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.
Intro to Robots
GraphWin():
• Myro comes with a graphics windows in which we can
play.
• We create it by invoking it by name:
From myro import *
myCanvas = GraphWin()
• This produces a gray 200 x 200
pixel window.
Exercise: Are pixels round, square or
rectangular?
• The myCanvas window becomes
our canvas where we exploring
drawing images.
Intro to Robots
GraphWin():
• Close it
myCanvas.close()
• And create it again with a
new name and size.
myCanvas = GraphWin(‘Silly Scene’,200,300)
• Change its color
myCanvas.setBackground(“white”)
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).
myCanvas = GraphWin(‘Silly Scene’,200,300)
• We communicate with our window by “sending it
messages”
myCanvas.setBackground(“white”)
message argument
message name
name of message “receiver”
Intro to Robots
Locating the Pixels
• Pixels, like points on the xy-plane, have coordinates.
• The coordinate numbering system begins in the top, lefthand corner.
(0,0)
(100,100)
Intro to Robots
Creating More Objects
• Besides graphics windows objects we can create many
others – points, lines, circles, etc.
p = Point(100,125)
• 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.
p.draw(myCanvas)
• Remember, we are sending a message to p to draw itself
on the myCanvas graphics window.
Intro to Robots
Point object messages:
• There are some more messages we can send to a Point.
getX()
getY()
undraw()
# returns the x-coordinate
# returns the y-coordinate
# makes the object disappear
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
myLine.draw()
Intro to Robots
the arguments of the Line
function are its starting and
ending Points. These points
are created by calling the Point()
function twice.
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 that
returns a list of numbers starting
at x, x+z, x+2z, … until y
all the lines drawn have a common
end-point – Point(100,100)
all the lines drawn have starting points
with a common y-coordinate (25)
Intro to Robots
Other Line functions:
L = Line(Point(25,25), Point(100,100))
L.draw()
start = L.getP1()
end = L.getP2()
# returns the starting point of line
# 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 starting
point a message to return its x-coordinate. Obviously, the dot (.) operator
functions like +, left-to-right.
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
Intro to Robots
radius
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”)
c.setFill(“yellow”)
c.setWidth(5)
color = color_rgb(100,125,150)
c.setFill(color)
Exercise: Try out these commands
Intro to Robots
# line color
# interior fill color
# line width in pixels
Circle Example
# Program to draw a bunch of # random colored circles
from myro import *
from random import *
width = 500
height = 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()
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)
Intro to Robots
Function Definition Alternatives:
• Consider the following two examples:
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 r
x = 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)
• makeCircle() hides the details of makeCircle(x,y,r)
exposes by requiring previously assigned values for x, y
and r
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
Intro to Robots
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
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.
Intro to Robots
Sound:
• Both the computer and the robot can beep().
beep(1,440)
computer.beep(1,440)
# plays a 440 Hz tone for 1 second
# computer plays the same tone
• What is Hertz?
Hertz is the unit for measuring frequencies
1 Hz = 1 cycle/second
1 cycle
Intro to Robots
Sound (cont):
• Computer CPUs measure their “speed” in Gigahertz.
1 Gigahertz = 109 cycles / second
• 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.
Intro to Robots
Sound (cont):
• Other periodic motions:
earth rotational rate: 1/day = 1/86400 seconds = 0.00001157 cycles/second
cd rotational rate: 400 turns/second
52x cd rotational rate: 52*400/second = 20800/second
hummingbird wing flap rate: 20-78 flaps/second
• What is sound?
A periodic compression and decompression (refraction) of air.
1 cycle of sound = 1 compression + 1 decompression
440 cycles/second = 440 compressions and decompressions / second
• What can the human ear hear?
in the range 20Hz to 20000Hz (20KHz)
Intro to Robots
What sounds can Scribbler make?
• A musical scale consists of 12 sounds
C C#/Db D D#/Eb E F F#/Gb G G#/Ab A A#/Bb B
• You can move up or down an octave by multiplying or
dividing by 2.
• The C-note in various octaves is identified as:
C0 C1 C2 C3 C4 C5 C6 C7 C8
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
C#4
D4
D#4
E4
F4
F#4
G4
G#4
A4
A#4
B4
C5
frequency
frequency
frequency
frequency
frequency
frequency
frequency
frequency
frequency
frequency
frequency
frequency
frequency
=
=
=
=
=
=
=
=
=
=
=
=
=
261.63 * 2(0/12) Hz
261.63 * 2(1/12) Hz
261.63 * 2(2/12) Hz
261.63 * 2(3/12) Hz
261.63 * 2(4/12) Hz
261.63 * 2(5/12) Hz
261.63 * 2(6/12) Hz
261.63 * 2(7/12) Hz
261.63 * 2(8/12) Hz
261.63 * 2(9/12) Hz
261.63 * 2(10/12) Hz
261.63 * 2(11/12) Hz
261.63 * 2(12/12) Hz
=
=
=
=
=
=
=
=
=
=
=
=
=
261.63 * 20 Hz
261.63 * 20.08333 Hz
261.63 * 20.16666 Hz
261.63 * 20.25000 Hz
261.63 * 20.33333 Hz
261.63 * 20.41666 Hz
261.63 * 20.50000 Hz
261.63 * 20.58333 Hz
261.63 * 20.66666 Hz
261.63 * 20.75000 Hz
261.63 * 20.83333 Hz
261.63 * 20.91666 Hz
261.63 * 21 Hz
Intro to Robots
=
=
=
=
=
=
=
=
=
=
=
=
=
261.63 Hz
277.19 Hz
293.67 Hz
311.13 Hz
329.63 Hz
349.23 Hz
370.00 Hz
392.00 Hz
415.31 Hz
440.01 Hz
466.17 Hz
493.89 Hz
523.26 Hz
Music Tempo:
• Music has tempo. In 44 tempo
– a quarter note is 0.68 seconds
– A half note is 1.36 seconds
– A whole note is 2.72 seconds.
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.
tune = “c 1; d 1; e 1; f 1; g 1; a 1; b 1; c7 1;“
song = makeSong(tune)
computer.playSong(song)
• Each note in the string is either
note time;
or
note1 note2 time;
where note = ‘C4’, etc and time = ¼, ½, 1, etc
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.
song = readSong(filename)
computer.playSong(song)
• Of course, if you want, the robot can play the song itself,
just drop the “computer” receiver tag.
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>)
Text(<anchor point>, <string>)
Image(<centerPoint>, <file name>)
<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>, …])
Intro to Robots
<object>.draw(<window>)
<object>.undraw()
<object>.getCenter()
<object>.setOutline(<color>)
<object>.setFill(<color>)
<object>.setWidth(<pixels>)
<object>.move(<dx>, <dy>)
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>)
Intro to Robots