Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to Robots 8 Sights & Sounds. Intro to Robots Computing is more than Computation So many devices – iPod, digital cameras, etc – use computers to.

Similar presentations


Presentation on theme: "Intro to Robots 8 Sights & Sounds. Intro to Robots Computing is more than Computation So many devices – iPod, digital cameras, etc – use computers to."— Presentation transcript:

1 Intro to Robots 8 Sights & Sounds

2 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.

3 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 becomes our canvas where we exploring drawing images. From myro import * myCanvas = GraphWin() Exercise: Are pixels round, square or rectangular?

4 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”)

5 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)

6 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)

7 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)

8 Intro to Robots Point object messages: There are some more messages we can send to a Point. getX() # returns the x-coordinate getY() # returns the y-coordinate undraw() # makes the object disappear

9 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 and ending Points. These points are created by calling the Point() function twice. myLine.draw()

10 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 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)

11 Intro to Robots Other Line functions: L = Line(Point(25,25), Point(100,100)) L.draw() start = L.getP1() # returns the starting point of line end = L.getP2() # returns the end point of line print start.getX(), start.getY(), end.getX(), end.getY() 2525 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.

12 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

13 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 color c.setFill(“yellow”) # interior fill color c.setWidth(5) # line width in pixels color = color_rgb(100,125,150) c.setFill(color) Exercise: Try out these commands

14 Intro to Robots 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()

15 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)

16 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 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)

17 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

18 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.

19 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 second computer.beep(1,440) # computer plays the same tone Hertz is the unit for measuring frequencies 1 Hz = 1 cycle/second 1 cycle

20 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 = 10 9 cycles / second

21 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/second 52x 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)

22 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

23 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 * 2 0 Hz = 261.63 Hz C#4 frequency = 261.63 * 2 (1/12) Hz = 261.63 * 2 0.08333 Hz = 277.19 Hz D4 frequency = 261.63 * 2 (2/12) Hz = 261.63 * 2 0.16666 Hz = 293.67 Hz D#4 frequency = 261.63 * 2 (3/12) Hz = 261.63 * 2 0.25000 Hz = 311.13 Hz E4 frequency = 261.63 * 2 (4/12) Hz = 261.63 * 2 0.33333 Hz = 329.63 Hz F4 frequency = 261.63 * 2 (5/12) Hz = 261.63 * 2 0.41666 Hz = 349.23 Hz F#4 frequency = 261.63 * 2 (6/12) Hz = 261.63 * 2 0.50000 Hz = 370.00 Hz G4 frequency = 261.63 * 2 (7/12) Hz = 261.63 * 2 0.58333 Hz = 392.00 Hz G#4 frequency = 261.63 * 2 (8/12) Hz = 261.63 * 2 0.66666 Hz = 415.31 Hz A4 frequency = 261.63 * 2 (9/12) Hz = 261.63 * 2 0.75000 Hz = 440.01 Hz A#4 frequency = 261.63 * 2 (10/12) Hz = 261.63 * 2 0.83333 Hz = 466.17 Hz B4 frequency = 261.63 * 2 (11/12) Hz = 261.63 * 2 0.91666 Hz = 493.89 Hz C5 frequency = 261.63 * 2 (12/12) Hz = 261.63 * 2 1 Hz = 523.26 Hz

24 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. 4444

25 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;

26 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)

27 Intro to Robots Myro Reference: The end of chapter 8 holds a review of the myro graphics function calls. GraphWin() GraphWin(,, ).close().setBackground( ) = color_rgb(,, ) Point(, ).getX().getY() Line(, ) Circle(, ) Rectangle(, ) Oval(, ) Polygon(,,,…) Polygon([,, …]) Text(, ) Image(, ).draw( ).undraw().getCenter().setOutline( ).setFill( ).setWidth( ).move(, )

28 Intro to Robots Myro Reference: The end of chapter 8 holds a review of the myro graphics function calls. beep(, ) beep(,, ).beep(, ).beep(,, ) robot.playSong( ) readSong( ) song2text(song) makeSong( ) text2song( )


Download ppt "Intro to Robots 8 Sights & Sounds. Intro to Robots Computing is more than Computation So many devices – iPod, digital cameras, etc – use computers to."

Similar presentations


Ads by Google